Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlite as st
|
2 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
3 |
+
|
4 |
+
# Load chatbot model
|
5 |
+
chatbot_model = "microsoft/DialoGPT-medium"
|
6 |
+
tokenizer = AutoTokenizer.from_pretrained(chatbot_model)
|
7 |
+
model = AutoModelForCausalLM.from_pretrained(chatbot_model)
|
8 |
+
|
9 |
+
# Load emotion detection model
|
10 |
+
emotion_pipeline = pipeline("text-classification", model="j-hartmann/emotion-english-distilroberta-base")
|
11 |
+
|
12 |
+
st.title("🧠 Mental Health Chatbot")
|
13 |
+
|
14 |
+
# Chat history
|
15 |
+
if "chat_history" not in st.session_state:
|
16 |
+
st.session_state.chat_history = []
|
17 |
+
|
18 |
+
# User Input
|
19 |
+
user_input = st.text_input("You:", key="user_input")
|
20 |
+
|
21 |
+
if st.button("Send"):
|
22 |
+
if user_input:
|
23 |
+
# Generate chatbot response
|
24 |
+
input_ids = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
|
25 |
+
output = model.generate(input_ids, max_length=200, pad_token_id=tokenizer.eos_token_id)
|
26 |
+
response = tokenizer.decode(output[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
27 |
+
|
28 |
+
# Detect emotion
|
29 |
+
emotion_result = emotion_pipeline(user_input)
|
30 |
+
emotion = emotion_result[0]["label"]
|
31 |
+
|
32 |
+
# Store chat history
|
33 |
+
st.session_state.chat_history.append(("You", user_input))
|
34 |
+
st.session_state.chat_history.append(("Bot", response))
|
35 |
+
|
36 |
+
# Display chat
|
37 |
+
for sender, msg in st.session_state.chat_history:
|
38 |
+
st.write(f"**{sender}:** {msg}")
|
39 |
+
|
40 |
+
# Display emotion
|
41 |
+
st.write(f"🧠 **Emotion Detected:** {emotion}")
|