Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,57 @@
|
|
1 |
import streamlit as st
|
2 |
-
from transformers import
|
3 |
import torch
|
4 |
|
5 |
-
#
|
6 |
-
|
7 |
-
rag_retriever = RagRetriever.from_pretrained("facebook/rag-token-nq") # Pre-trained retriever
|
8 |
-
rag_model = RagSequenceForGeneration.from_pretrained("Izza-shahzad-13/fine-tuned-flan-t5") # Your fine-tuned model
|
9 |
|
10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
-
|
13 |
-
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
st.
|
42 |
-
|
43 |
-
#
|
44 |
user_input = st.text_input("How are you feeling today?")
|
45 |
|
|
|
46 |
if user_input:
|
47 |
-
|
48 |
-
response = generate_rag_response(user_input)
|
49 |
st.write("Model Response:", response)
|
|
|
1 |
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
3 |
import torch
|
4 |
|
5 |
+
# Hugging Face Token for Authentication
|
6 |
+
HUGGINGFACE_TOKEN = "your_hugging_face_token_here" # Replace with your token
|
|
|
|
|
7 |
|
8 |
+
# Function to load model and tokenizer (local or Hugging Face with token)
|
9 |
+
def load_model(model_path):
|
10 |
+
try:
|
11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, use_auth_token=HUGGINGFACE_TOKEN)
|
12 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_path, use_auth_token=HUGGINGFACE_TOKEN)
|
13 |
+
return tokenizer, model
|
14 |
+
except Exception as e:
|
15 |
+
st.error(f"Error loading model: {e}")
|
16 |
+
return None, None
|
17 |
+
|
18 |
+
# Set device (use GPU if available)
|
19 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
20 |
+
|
21 |
+
# Path to your model (either a local path or a Hugging Face model name)
|
22 |
+
model_path = "Izza-shahzad-13/fine-tuned-flan-t5" # Use your Hugging Face model identifier
|
23 |
+
|
24 |
+
# Load tokenizer and model
|
25 |
+
tokenizer, model = load_model(model_path)
|
26 |
+
if model:
|
27 |
+
model.to(device)
|
28 |
+
|
29 |
+
# Function to generate response from the model
|
30 |
+
def generate_response(input_text):
|
31 |
+
inputs = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True, max_length=512).to(device)
|
32 |
+
with torch.no_grad():
|
33 |
+
output = model.generate(
|
34 |
+
inputs['input_ids'],
|
35 |
+
max_length=500,
|
36 |
+
num_beams=4,
|
37 |
+
top_p=0.9,
|
38 |
+
top_k=50,
|
39 |
+
temperature=0.7,
|
40 |
+
do_sample=True,
|
41 |
+
no_repeat_ngram_size=3,
|
42 |
+
early_stopping=True
|
43 |
+
)
|
44 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
45 |
+
return response
|
46 |
+
|
47 |
+
# Streamlit app interface
|
48 |
+
st.title("FLAN-T5 Mental Health Counseling Assistant")
|
49 |
+
st.write("Type your thoughts or feelings, and let the model respond.")
|
50 |
+
|
51 |
+
# User input for interaction
|
52 |
user_input = st.text_input("How are you feeling today?")
|
53 |
|
54 |
+
# Generate and display model response when input is provided
|
55 |
if user_input:
|
56 |
+
response = generate_response(user_input)
|
|
|
57 |
st.write("Model Response:", response)
|