Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- .gitattributes +1 -0
- Chatbot.py +203 -0
- logo.png +3 -0
- requirements.txt +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
logo.png filter=lfs diff=lfs merge=lfs -text
|
Chatbot.py
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from google import genai
|
| 3 |
+
from google.genai import types
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
from collections import Counter
|
| 6 |
+
from huggingface_hub import login
|
| 7 |
+
from streamlit_elements import elements, nivo, mui
|
| 8 |
+
import os
|
| 9 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 10 |
+
login(token=hf_token)
|
| 11 |
+
|
| 12 |
+
# Handle full reset via query param at the very top
|
| 13 |
+
if "reset" in st.query_params:
|
| 14 |
+
st.query_params.clear() # Prevent infinite loop on rerun
|
| 15 |
+
logo_path = "D:\Alpha\logo Background Removed.png"
|
| 16 |
+
st.logo(logo_path,size="large")
|
| 17 |
+
|
| 18 |
+
# Initialize Gemini client
|
| 19 |
+
genai_token = os.getenv("G_TOKEN")
|
| 20 |
+
client = genai.Client(api_key=genai_token)
|
| 21 |
+
|
| 22 |
+
# Label mapping
|
| 23 |
+
label_map = {
|
| 24 |
+
"LABEL_0": 'anxiety',
|
| 25 |
+
"LABEL_1": 'depression',
|
| 26 |
+
"LABEL_2": 'bipolar',
|
| 27 |
+
"LABEL_3": 'normal',
|
| 28 |
+
"LABEL_4": 'personality disorder',
|
| 29 |
+
"LABEL_5": 'stress',
|
| 30 |
+
"LABEL_6": 'suicidal'
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
# Load your fine-tuned model
|
| 34 |
+
model_name = "Elite13/bert-finetuned-mental-health"
|
| 35 |
+
classifier = pipeline("text-classification", model=model_name, tokenizer=model_name)
|
| 36 |
+
|
| 37 |
+
# Initialize session state
|
| 38 |
+
if "messages" not in st.session_state:
|
| 39 |
+
st.session_state.messages = []
|
| 40 |
+
if "total_messages" not in st.session_state:
|
| 41 |
+
st.session_state.total_messages = 0
|
| 42 |
+
if "emotion_log" not in st.session_state:
|
| 43 |
+
st.session_state.emotion_log = []
|
| 44 |
+
if "accepted" not in st.session_state:
|
| 45 |
+
st.session_state.accepted = False
|
| 46 |
+
if "newchat" not in st.session_state:
|
| 47 |
+
st.session_state.newchat = False
|
| 48 |
+
|
| 49 |
+
st.markdown("<h1 style='text-align: center;'>Mental Health Chatbot</h1>", unsafe_allow_html=True)
|
| 50 |
+
st.markdown("---")
|
| 51 |
+
|
| 52 |
+
# Terms and Conditions Screen
|
| 53 |
+
if not st.session_state.accepted:
|
| 54 |
+
st.markdown("<h2 style ='text-align:center;'>Terms and Conditions</h2>", unsafe_allow_html=True)
|
| 55 |
+
st.markdown("""
|
| 56 |
+
**This is a University Project.**
|
| 57 |
+
|
| 58 |
+
- The application is part of academic coursework or research.
|
| 59 |
+
- Data entered here may be used for project evaluation or improvement.
|
| 60 |
+
- No personal data will be stored or shared beyond academic purposes.
|
| 61 |
+
|
| 62 |
+
By clicking **'I Agree'**, you acknowledge and accept the above.
|
| 63 |
+
""")
|
| 64 |
+
if st.button("✅ I Agree"):
|
| 65 |
+
st.session_state.accepted = True
|
| 66 |
+
st.rerun()
|
| 67 |
+
|
| 68 |
+
# Main App Interface
|
| 69 |
+
if st.session_state.accepted:
|
| 70 |
+
|
| 71 |
+
# Display chat history
|
| 72 |
+
for msg in st.session_state.messages:
|
| 73 |
+
with st.chat_message(msg["role"]):
|
| 74 |
+
st.markdown(msg["content"])
|
| 75 |
+
|
| 76 |
+
# Chat input
|
| 77 |
+
if prompt := st.chat_input("Say something..."):
|
| 78 |
+
# Display user input
|
| 79 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 80 |
+
with st.chat_message("user"):
|
| 81 |
+
st.markdown(prompt)
|
| 82 |
+
st.session_state.total_messages += 1
|
| 83 |
+
|
| 84 |
+
# Gemini response
|
| 85 |
+
response = client.models.generate_content(
|
| 86 |
+
model="gemini-2.0-flash",
|
| 87 |
+
config=types.GenerateContentConfig(system_instruction=(
|
| 88 |
+
"You are a kind and supportive friend. Speak casually like a real human, "
|
| 89 |
+
"offering empathy, emotional comfort, and helpful suggestions in a non-judgmental tone. "
|
| 90 |
+
"Always listen first, validate the user's feelings, and respond in a warm, understanding way. "
|
| 91 |
+
"Use short, friendly sentences like a close friend would. Avoid sounding robotic or overly formal. "
|
| 92 |
+
"If needed, gently encourage the user or suggest simple coping strategies — but never force advice. "
|
| 93 |
+
"Tone: supportive, chill, safe, and human-like. Don't diagnose, just be there like a good friend."
|
| 94 |
+
)),
|
| 95 |
+
contents=prompt
|
| 96 |
+
)
|
| 97 |
+
reply = response.text
|
| 98 |
+
|
| 99 |
+
# Display Gemini reply
|
| 100 |
+
st.session_state.messages.append({"role": "assistant", "content": reply})
|
| 101 |
+
with st.chat_message("assistant"):
|
| 102 |
+
st.markdown(reply)
|
| 103 |
+
|
| 104 |
+
# Run classifier
|
| 105 |
+
results = classifier(prompt, return_all_scores=True)
|
| 106 |
+
top_pred = max(results[0], key=lambda x: x['score'])
|
| 107 |
+
emotion = label_map[top_pred['label']]
|
| 108 |
+
confidence = top_pred['score']
|
| 109 |
+
st.session_state.emotion_log.append(emotion)
|
| 110 |
+
|
| 111 |
+
# Show detected emotion
|
| 112 |
+
st.markdown(f"🧠 **Detected Emotion:** `{emotion}` ({confidence:.2f} confidence)")
|
| 113 |
+
|
| 114 |
+
# Show emotional report
|
| 115 |
+
st.session_state.newchat = False
|
| 116 |
+
st.markdown("---")
|
| 117 |
+
if st.session_state.total_messages > 0:
|
| 118 |
+
if st.button("📊 View Emotional Report"):
|
| 119 |
+
emotion_counts = Counter(st.session_state.emotion_log)
|
| 120 |
+
total = st.session_state.total_messages
|
| 121 |
+
percentages = {e: (c / total) * 100 for e, c in emotion_counts.items()}
|
| 122 |
+
|
| 123 |
+
st.markdown("### Emotion Breakdown")
|
| 124 |
+
for e, p in percentages.items():
|
| 125 |
+
st.markdown(f"- **{e.capitalize()}**: {p:.2f}%")
|
| 126 |
+
|
| 127 |
+
# Prepare data for Nivo Pie chart
|
| 128 |
+
nivo_data = [
|
| 129 |
+
{"id": emotion, "label": emotion.capitalize(), "value": count}
|
| 130 |
+
for emotion, count in emotion_counts.items()
|
| 131 |
+
]
|
| 132 |
+
|
| 133 |
+
# Show interactive pie chart using Nivo
|
| 134 |
+
with elements("nivo_pie"):
|
| 135 |
+
with mui.Box(sx={"height": 500}):
|
| 136 |
+
nivo.Pie(
|
| 137 |
+
data=nivo_data,
|
| 138 |
+
margin={ "top": 40, "right": 80, "bottom": 80, "left": 80 },
|
| 139 |
+
innerRadius=0.5,
|
| 140 |
+
padAngle=0.7,
|
| 141 |
+
cornerRadius=3,
|
| 142 |
+
activeOuterRadiusOffset=8,
|
| 143 |
+
borderWidth=1,
|
| 144 |
+
borderColor={ "from": "color", "modifiers": [["darker", 0.2]] },
|
| 145 |
+
arcLinkLabelsSkipAngle=10,
|
| 146 |
+
arcLinkLabelsTextColor="#333333",
|
| 147 |
+
arcLinkLabelsThickness=2,
|
| 148 |
+
arcLinkLabelsColor={ "from": "color" },
|
| 149 |
+
arcLabelsSkipAngle=10,
|
| 150 |
+
arcLabelsTextColor={ "from": "color", "modifiers": [["darker", 2]] },
|
| 151 |
+
legends=[
|
| 152 |
+
{
|
| 153 |
+
"anchor": "bottom",
|
| 154 |
+
"direction": "row",
|
| 155 |
+
"translateY": 56,
|
| 156 |
+
"itemWidth": 100,
|
| 157 |
+
"itemHeight": 18,
|
| 158 |
+
"symbolSize": 18,
|
| 159 |
+
"symbolShape": "circle",
|
| 160 |
+
"itemTextColor": "#999",
|
| 161 |
+
"effects": [
|
| 162 |
+
{
|
| 163 |
+
"on": "hover",
|
| 164 |
+
"style": {
|
| 165 |
+
"itemTextColor": "#000"
|
| 166 |
+
}
|
| 167 |
+
}
|
| 168 |
+
]
|
| 169 |
+
}
|
| 170 |
+
],
|
| 171 |
+
theme={
|
| 172 |
+
"background": "#ffffff",
|
| 173 |
+
"textColor": "#333333",
|
| 174 |
+
"tooltip": {
|
| 175 |
+
"container": {
|
| 176 |
+
"background": "#ffffff",
|
| 177 |
+
"color": "#333333"
|
| 178 |
+
}
|
| 179 |
+
}
|
| 180 |
+
}
|
| 181 |
+
)
|
| 182 |
+
st.markdown("---")
|
| 183 |
+
st.markdown("### Summary Report")
|
| 184 |
+
response = client.models.generate_content(
|
| 185 |
+
model="gemini-2.0-flash",
|
| 186 |
+
config=types.GenerateContentConfig(system_instruction=(
|
| 187 |
+
"You are a mental health chatbot that provides a summary of the user's emotional state based on their chat history. "
|
| 188 |
+
"Use the detected emotions to create a friendly and supportive report. "
|
| 189 |
+
"Include the most common emotions, their percentages, and any patterns you notice. "
|
| 190 |
+
"Keep the tone warm, empathetic, and encouraging."
|
| 191 |
+
)),
|
| 192 |
+
contents=st.session_state.emotion_log )
|
| 193 |
+
reply = response.text
|
| 194 |
+
st.markdown(reply)
|
| 195 |
+
st.session_state.newchat = True
|
| 196 |
+
|
| 197 |
+
# Reset button after report
|
| 198 |
+
if st.session_state.newchat:
|
| 199 |
+
st.markdown("---")
|
| 200 |
+
if st.button("🗑️ Clear Chat"):
|
| 201 |
+
st.session_state.clear()
|
| 202 |
+
st.query_params["reset"] = "true"
|
| 203 |
+
st.rerun()
|
logo.png
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
Binary file (4.05 kB). View file
|
|
|