Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,14 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
#from llama_cpp import Llama
|
| 4 |
from datasets import load_dataset
|
| 5 |
import os
|
|
@@ -47,24 +56,32 @@ st.markdown('<div class="blurred-background"></div>', unsafe_allow_html=True)
|
|
| 47 |
|
| 48 |
#""""""""""""""""""""""""" Application Code Starts here """""""""""""""""""""""""""""""""""""""""""""
|
| 49 |
|
| 50 |
-
#
|
| 51 |
@st.cache_resource
|
| 52 |
def load_counseling_dataset():
|
| 53 |
-
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
dataset
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# Fine-tune the model and save it
|
| 58 |
@st.cache_resource
|
| 59 |
def fine_tune_model():
|
| 60 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, DataCollatorForLanguageModeling
|
| 61 |
-
|
| 62 |
# Load base model and tokenizer
|
| 63 |
model_name = "prabureddy/Mental-Health-FineTuned-Mistral-7B-Instruct-v0.2"
|
| 64 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 65 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 66 |
|
|
|
|
|
|
|
|
|
|
| 67 |
# Prepare dataset for training
|
|
|
|
|
|
|
| 68 |
def preprocess_function(examples):
|
| 69 |
return tokenizer(examples["context"] + "\n" + examples["response"], truncation=True)
|
| 70 |
|
|
@@ -74,13 +91,16 @@ def fine_tune_model():
|
|
| 74 |
# Training arguments
|
| 75 |
training_args = TrainingArguments(
|
| 76 |
output_dir="./fine_tuned_model",
|
| 77 |
-
evaluation_strategy="
|
| 78 |
learning_rate=2e-5,
|
| 79 |
-
per_device_train_batch_size=
|
|
|
|
| 80 |
num_train_epochs=3,
|
| 81 |
weight_decay=0.01,
|
|
|
|
| 82 |
save_total_limit=2,
|
| 83 |
-
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
# Trainer
|
|
@@ -124,10 +144,8 @@ if st.button("Get Supportive Response"):
|
|
| 124 |
if user_input.strip():
|
| 125 |
with st.spinner("Analyzing your input and generating a response..."):
|
| 126 |
try:
|
| 127 |
-
# Construct the messages for the pipeline
|
| 128 |
-
messages = [{"role": "user", "content": user_input}]
|
| 129 |
# Generate a response
|
| 130 |
-
response = pipe(
|
| 131 |
st.subheader("Supportive Suggestion:")
|
| 132 |
st.markdown(f"**{response}**")
|
| 133 |
except Exception as e:
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from transformers import (
|
| 4 |
+
AutoTokenizer,
|
| 5 |
+
AutoModelForCausalLM,
|
| 6 |
+
TrainingArguments,
|
| 7 |
+
Trainer,
|
| 8 |
+
DataCollatorForLanguageModeling,
|
| 9 |
+
pipeline,
|
| 10 |
+
)
|
| 11 |
+
#from transformers import AutoModelForCausalLM, AutoTokenizer, TrainingArguments, Trainer, pipeline
|
| 12 |
#from llama_cpp import Llama
|
| 13 |
from datasets import load_dataset
|
| 14 |
import os
|
|
|
|
| 56 |
|
| 57 |
#""""""""""""""""""""""""" Application Code Starts here """""""""""""""""""""""""""""""""""""""""""""
|
| 58 |
|
| 59 |
+
# Cache resource for dataset loading
|
| 60 |
@st.cache_resource
|
| 61 |
def load_counseling_dataset():
|
| 62 |
+
# Load a smaller subset of the dataset for memory efficiency
|
| 63 |
+
dataset = load_dataset("Amod/mental_health_counseling_conversations", split="train")
|
| 64 |
+
return dataset
|
| 65 |
|
| 66 |
+
# Process the dataset in batches to avoid memory overuse
|
| 67 |
+
def process_dataset_in_batches(dataset, batch_size=1000):
|
| 68 |
+
for example in dataset.shuffle().select(range(batch_size)):
|
| 69 |
+
yield example
|
| 70 |
|
| 71 |
# Fine-tune the model and save it
|
| 72 |
@st.cache_resource
|
| 73 |
def fine_tune_model():
|
|
|
|
|
|
|
| 74 |
# Load base model and tokenizer
|
| 75 |
model_name = "prabureddy/Mental-Health-FineTuned-Mistral-7B-Instruct-v0.2"
|
| 76 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 77 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 78 |
|
| 79 |
+
# Enable gradient checkpointing for memory optimization
|
| 80 |
+
model.gradient_checkpointing_enable()
|
| 81 |
+
|
| 82 |
# Prepare dataset for training
|
| 83 |
+
dataset = load_counseling_dataset()
|
| 84 |
+
|
| 85 |
def preprocess_function(examples):
|
| 86 |
return tokenizer(examples["context"] + "\n" + examples["response"], truncation=True)
|
| 87 |
|
|
|
|
| 91 |
# Training arguments
|
| 92 |
training_args = TrainingArguments(
|
| 93 |
output_dir="./fine_tuned_model",
|
| 94 |
+
evaluation_strategy="steps",
|
| 95 |
learning_rate=2e-5,
|
| 96 |
+
per_device_train_batch_size=8,
|
| 97 |
+
per_device_eval_batch_size=8,
|
| 98 |
num_train_epochs=3,
|
| 99 |
weight_decay=0.01,
|
| 100 |
+
fp16=True, # Enable FP16 for lower memory usage
|
| 101 |
save_total_limit=2,
|
| 102 |
+
save_steps=500,
|
| 103 |
+
logging_steps=100,
|
| 104 |
)
|
| 105 |
|
| 106 |
# Trainer
|
|
|
|
| 144 |
if user_input.strip():
|
| 145 |
with st.spinner("Analyzing your input and generating a response..."):
|
| 146 |
try:
|
|
|
|
|
|
|
| 147 |
# Generate a response
|
| 148 |
+
response = pipe(user_input, max_length=150, num_return_sequences=1)[0]["generated_text"]
|
| 149 |
st.subheader("Supportive Suggestion:")
|
| 150 |
st.markdown(f"**{response}**")
|
| 151 |
except Exception as e:
|