Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,11 +2,9 @@ import os
|
|
| 2 |
import json
|
| 3 |
import random
|
| 4 |
import datetime
|
| 5 |
-
from flask import Flask, request, jsonify
|
| 6 |
from transformers import pipeline
|
| 7 |
from geopy.geocoders import Nominatim
|
| 8 |
-
|
| 9 |
-
app = Flask(__name__)
|
| 10 |
|
| 11 |
# ------------------------------
|
| 12 |
# 💬 Emotion Analyzer Pipeline
|
|
@@ -56,7 +54,6 @@ def detect_crisis(text):
|
|
| 56 |
]
|
| 57 |
return any(signal in text.lower() for signal in crisis_signals)
|
| 58 |
|
| 59 |
-
# Geo-based crisis helpline lookup
|
| 60 |
def get_geo_helpline(location_query="world"):
|
| 61 |
helplines = {
|
| 62 |
"india": "AASRA Helpline: 91-9820466726 or 91-22-27546669",
|
|
@@ -102,16 +99,19 @@ def enrich_response(reply_text):
|
|
| 102 |
return reply_text
|
| 103 |
|
| 104 |
# ------------------------------
|
| 105 |
-
# 🧠 Generate AI Response
|
| 106 |
# ------------------------------
|
| 107 |
-
def generate_supportive_reply(user_input, user_name, user_age):
|
| 108 |
emotions = emotion_analyzer(user_input)[0]
|
| 109 |
dominant_emotion = max(emotions, key=lambda x: x["score"])["label"].lower()
|
| 110 |
|
| 111 |
# Crisis response
|
| 112 |
if detect_crisis(user_input):
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
| 115 |
helpline = get_geo_helpline(location)
|
| 116 |
return (
|
| 117 |
f"{user_name}, I’m really sorry that you’re feeling this way. "
|
|
@@ -134,47 +134,41 @@ def generate_supportive_reply(user_input, user_name, user_age):
|
|
| 134 |
return enrich_response(base_reply)
|
| 135 |
|
| 136 |
# ------------------------------
|
| 137 |
-
# 🌐
|
| 138 |
# ------------------------------
|
| 139 |
-
|
| 140 |
-
def home():
|
| 141 |
-
return jsonify({"message": "💛 Emotional Support Chatbot API is running!"})
|
| 142 |
-
|
| 143 |
-
@app.route('/chat', methods=['POST'])
|
| 144 |
-
def chat():
|
| 145 |
global memory
|
| 146 |
-
data = request.get_json()
|
| 147 |
-
user_input = data.get('message', '').strip()
|
| 148 |
-
user_name = data.get('name', 'Friend')
|
| 149 |
-
user_age = data.get('age', '20')
|
| 150 |
|
| 151 |
-
if not
|
| 152 |
-
return
|
| 153 |
|
| 154 |
-
#
|
| 155 |
if user_name not in memory:
|
| 156 |
memory[user_name] = {"last_chat": datetime.datetime.now().isoformat(), "mood": "neutral"}
|
| 157 |
else:
|
| 158 |
memory[user_name]["last_chat"] = datetime.datetime.now().isoformat()
|
| 159 |
save_memory(memory)
|
| 160 |
|
| 161 |
-
# Detect if the user was sad before
|
| 162 |
last_mood = memory[user_name].get("mood", "neutral")
|
| 163 |
-
if last_mood == "sadness"
|
| 164 |
-
pretext = f"{user_name}, last time you were feeling a bit down. How are you feeling today? 💛\n"
|
| 165 |
-
else:
|
| 166 |
-
pretext = ""
|
| 167 |
|
| 168 |
-
|
| 169 |
-
reply = generate_supportive_reply(user_input, user_name, user_age)
|
| 170 |
-
reply = pretext + reply
|
| 171 |
|
| 172 |
# Update mood
|
| 173 |
-
mood = emotion_analyzer(
|
| 174 |
memory[user_name]["mood"] = max(mood, key=lambda x: x["score"])["label"].lower()
|
| 175 |
save_memory(memory)
|
| 176 |
|
| 177 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
-
|
| 180 |
-
app.run(host="0.0.0.0", port=7860)
|
|
|
|
| 2 |
import json
|
| 3 |
import random
|
| 4 |
import datetime
|
|
|
|
| 5 |
from transformers import pipeline
|
| 6 |
from geopy.geocoders import Nominatim
|
| 7 |
+
import gradio as gr
|
|
|
|
| 8 |
|
| 9 |
# ------------------------------
|
| 10 |
# 💬 Emotion Analyzer Pipeline
|
|
|
|
| 54 |
]
|
| 55 |
return any(signal in text.lower() for signal in crisis_signals)
|
| 56 |
|
|
|
|
| 57 |
def get_geo_helpline(location_query="world"):
|
| 58 |
helplines = {
|
| 59 |
"india": "AASRA Helpline: 91-9820466726 or 91-22-27546669",
|
|
|
|
| 99 |
return reply_text
|
| 100 |
|
| 101 |
# ------------------------------
|
| 102 |
+
# 🧠 Generate AI Response
|
| 103 |
# ------------------------------
|
| 104 |
+
def generate_supportive_reply(user_input, user_name="Friend", user_age="20"):
|
| 105 |
emotions = emotion_analyzer(user_input)[0]
|
| 106 |
dominant_emotion = max(emotions, key=lambda x: x["score"])["label"].lower()
|
| 107 |
|
| 108 |
# Crisis response
|
| 109 |
if detect_crisis(user_input):
|
| 110 |
+
try:
|
| 111 |
+
geo = Nominatim(user_agent="support_bot").geocode("your location") or None
|
| 112 |
+
location = geo.address if geo else "world"
|
| 113 |
+
except:
|
| 114 |
+
location = "world"
|
| 115 |
helpline = get_geo_helpline(location)
|
| 116 |
return (
|
| 117 |
f"{user_name}, I’m really sorry that you’re feeling this way. "
|
|
|
|
| 134 |
return enrich_response(base_reply)
|
| 135 |
|
| 136 |
# ------------------------------
|
| 137 |
+
# 🌐 Gradio Interface
|
| 138 |
# ------------------------------
|
| 139 |
+
def chatbot_interface(user_message, user_name="Friend", user_age="20"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
global memory
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
+
if not user_message.strip():
|
| 143 |
+
return "I didn’t quite catch that — could you say that again?"
|
| 144 |
|
| 145 |
+
# Memory system
|
| 146 |
if user_name not in memory:
|
| 147 |
memory[user_name] = {"last_chat": datetime.datetime.now().isoformat(), "mood": "neutral"}
|
| 148 |
else:
|
| 149 |
memory[user_name]["last_chat"] = datetime.datetime.now().isoformat()
|
| 150 |
save_memory(memory)
|
| 151 |
|
|
|
|
| 152 |
last_mood = memory[user_name].get("mood", "neutral")
|
| 153 |
+
pretext = f"{user_name}, last time you were feeling a bit down. How are you feeling today? 💛\n" if last_mood == "sadness" else ""
|
|
|
|
|
|
|
|
|
|
| 154 |
|
| 155 |
+
reply = generate_supportive_reply(user_message, user_name, user_age)
|
|
|
|
|
|
|
| 156 |
|
| 157 |
# Update mood
|
| 158 |
+
mood = emotion_analyzer(user_message)[0]
|
| 159 |
memory[user_name]["mood"] = max(mood, key=lambda x: x["score"])["label"].lower()
|
| 160 |
save_memory(memory)
|
| 161 |
|
| 162 |
+
return pretext + reply
|
| 163 |
+
|
| 164 |
+
# Gradio UI
|
| 165 |
+
with gr.Blocks() as demo:
|
| 166 |
+
gr.Markdown("## 💛 Emotional Support Chatbot")
|
| 167 |
+
name_input = gr.Textbox(label="Your Name", placeholder="Enter your name...")
|
| 168 |
+
age_input = gr.Textbox(label="Your Age", placeholder="Enter your age...")
|
| 169 |
+
message_input = gr.Textbox(label="Your Message", placeholder="Type your message here...", lines=3)
|
| 170 |
+
output_chat = gr.Textbox(label="Chatbot Response", interactive=False)
|
| 171 |
+
|
| 172 |
+
message_input.submit(chatbot_interface, [message_input, name_input, age_input], output_chat)
|
| 173 |
|
| 174 |
+
demo.launch()
|
|
|