Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +109 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
import numpy as np
|
4 |
+
import os
|
5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
6 |
+
from tensorflow.keras.models import load_model
|
7 |
+
from tensorflow.keras.preprocessing.text import tokenizer_from_json
|
8 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
9 |
+
import json
|
10 |
+
|
11 |
+
# Load tokenizer from file
|
12 |
+
with open("tokenizer.json") as f:
|
13 |
+
data = json.load(f)
|
14 |
+
from tensorflow.keras.preprocessing.text import tokenizer_from_json
|
15 |
+
tokenizer = tokenizer_from_json(data)
|
16 |
+
|
17 |
+
# Preprocessing function
|
18 |
+
def preprocess(text):
|
19 |
+
sequence = tokenizer.texts_to_sequences([text])
|
20 |
+
return pad_sequences(sequence, maxlen=100)
|
21 |
+
|
22 |
+
# Load Keras models
|
23 |
+
model1 = load_model("model1.h5")
|
24 |
+
model2 = load_model("model2.h5")
|
25 |
+
|
26 |
+
def model1_predict(text):
|
27 |
+
pred = model1.predict(preprocess(text))[0][0]
|
28 |
+
return int(pred > 0.5)
|
29 |
+
|
30 |
+
def model2_predict(text):
|
31 |
+
pred = model2.predict(preprocess(text))[0]
|
32 |
+
return int(np.argmax(pred))
|
33 |
+
|
34 |
+
diagnosis_labels = {
|
35 |
+
1: "Anxiety",
|
36 |
+
2: "Depression",
|
37 |
+
3: "Bipolar disorder",
|
38 |
+
4: "PTSD",
|
39 |
+
5: "OCD",
|
40 |
+
6: "ADHD",
|
41 |
+
7: "General emotional distress"
|
42 |
+
}
|
43 |
+
|
44 |
+
@st.cache_resource
|
45 |
+
def load_llm():
|
46 |
+
model_id = "tiiuae/falcon-7b-instruct"
|
47 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
48 |
+
model = AutoModelForCausalLM.from_pretrained(
|
49 |
+
model_id,
|
50 |
+
device_map="auto",
|
51 |
+
trust_remote_code=True,
|
52 |
+
torch_dtype="auto"
|
53 |
+
)
|
54 |
+
return pipeline("text-generation", model=model, tokenizer=tokenizer, device_map="auto")
|
55 |
+
|
56 |
+
generator = load_llm()
|
57 |
+
|
58 |
+
if "history" not in st.session_state:
|
59 |
+
st.session_state.history = []
|
60 |
+
|
61 |
+
def therapist_pipeline(user_input):
|
62 |
+
st.session_state.history.append(f"User: {user_input}")
|
63 |
+
risk = model1_predict(user_input)
|
64 |
+
|
65 |
+
if risk == 1:
|
66 |
+
response = (
|
67 |
+
"I'm really sorry you're feeling this way. You're not alone — please talk to someone you trust "
|
68 |
+
"or a professional. I'm here to listen, but it's important to get real support too. 💙"
|
69 |
+
)
|
70 |
+
else:
|
71 |
+
diagnosis_code = model2_predict(user_input)
|
72 |
+
diagnosis = diagnosis_labels.get(diagnosis_code, "General emotional distress")
|
73 |
+
|
74 |
+
prompt = f"""You are an empathetic AI therapist. The user has been diagnosed with {diagnosis}. Respond supportively.
|
75 |
+
|
76 |
+
User: {user_input}
|
77 |
+
AI:"""
|
78 |
+
|
79 |
+
response = generator(prompt, max_new_tokens=150, temperature=0.7)[0]["generated_text"]
|
80 |
+
response = response.split("AI:")[-1].strip()
|
81 |
+
|
82 |
+
st.session_state.history.append(f"AI: {response}")
|
83 |
+
return response
|
84 |
+
|
85 |
+
def summarize_session():
|
86 |
+
session_text = "\n".join(st.session_state.history)
|
87 |
+
prompt = f"""Summarize the emotional state of the user based on the following conversation. Include emotional cues and possible diagnoses. Write it like a therapist note.
|
88 |
+
|
89 |
+
Conversation:
|
90 |
+
{session_text}
|
91 |
+
|
92 |
+
Summary:"""
|
93 |
+
summary = generator(prompt, max_new_tokens=250, temperature=0.5)[0]["generated_text"]
|
94 |
+
return summary.split("Summary:")[-1].strip()
|
95 |
+
|
96 |
+
st.title("🧠 TARS.help - AI Therapist")
|
97 |
+
user_input = st.text_input("How are you feeling today?")
|
98 |
+
|
99 |
+
if user_input:
|
100 |
+
response = therapist_pipeline(user_input)
|
101 |
+
st.markdown(f"**AI Therapist:** {response}")
|
102 |
+
|
103 |
+
if st.button("🧾 Generate Therapist Summary"):
|
104 |
+
st.markdown("### 🧠 Session Summary")
|
105 |
+
st.markdown(summarize_session())
|
106 |
+
|
107 |
+
for i in range(0, len(st.session_state.history), 2):
|
108 |
+
st.markdown(f"**You:** {st.session_state.history[i][6:]}")
|
109 |
+
st.markdown(f"**AI:** {st.session_state.history[i+1][4:]}")
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
transformers
|
3 |
+
torch
|
4 |
+
tensorflow-cpu==2.11.0
|