Spaces:
Runtime error
Runtime error
Rename .gitattributes to app.py
Browse files- .gitattributes +0 -35
- app.py +162 -0
.gitattributes
DELETED
|
@@ -1,35 +0,0 @@
|
|
| 1 |
-
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
-
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
-
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 4 |
-
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 5 |
-
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 6 |
-
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 7 |
-
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
-
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 9 |
-
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 10 |
-
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 11 |
-
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 12 |
-
*.model filter=lfs diff=lfs merge=lfs -text
|
| 13 |
-
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 14 |
-
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 15 |
-
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 16 |
-
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 17 |
-
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 18 |
-
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 19 |
-
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 20 |
-
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 21 |
-
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 22 |
-
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 23 |
-
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 24 |
-
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 25 |
-
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 26 |
-
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 27 |
-
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 28 |
-
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 29 |
-
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 30 |
-
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 31 |
-
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 32 |
-
*.xz 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
import random
|
| 6 |
+
import requests
|
| 7 |
+
from transformers import pipeline
|
| 8 |
+
from datetime import datetime
|
| 9 |
+
|
| 10 |
+
# --- Setup ---
|
| 11 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 12 |
+
|
| 13 |
+
emotion_classifier = pipeline("text-classification",
|
| 14 |
+
model="j-hartmann/emotion-english-distilroberta-base")
|
| 15 |
+
|
| 16 |
+
USER_FILE = "user_data.json"
|
| 17 |
+
QUOTE_API = "https://api.quotable.io/random"
|
| 18 |
+
|
| 19 |
+
LOCAL_QUOTES = [
|
| 20 |
+
"Every sunrise brings new hope. Keep shining.",
|
| 21 |
+
"You are capable of more than you realize.",
|
| 22 |
+
"Each step, no matter how small, is progress.",
|
| 23 |
+
"Difficult roads often lead to beautiful destinations.",
|
| 24 |
+
"The best time for new beginnings is now.",
|
| 25 |
+
"Your inner light never fades—let it guide you."
|
| 26 |
+
]
|
| 27 |
+
|
| 28 |
+
# --- Helper functions ---
|
| 29 |
+
def load_user_data():
|
| 30 |
+
if os.path.exists(USER_FILE):
|
| 31 |
+
with open(USER_FILE, "r") as f:
|
| 32 |
+
return json.load(f)
|
| 33 |
+
return {}
|
| 34 |
+
|
| 35 |
+
def save_user_data(data):
|
| 36 |
+
with open(USER_FILE, "w") as f:
|
| 37 |
+
json.dump(data, f, indent=2)
|
| 38 |
+
|
| 39 |
+
def fetch_quote():
|
| 40 |
+
try:
|
| 41 |
+
r = requests.get(QUOTE_API, timeout=5)
|
| 42 |
+
if r.status_code == 200:
|
| 43 |
+
q = r.json()
|
| 44 |
+
return f"“{q['content']}” — {q['author']}"
|
| 45 |
+
except Exception:
|
| 46 |
+
pass
|
| 47 |
+
return random.choice(LOCAL_QUOTES)
|
| 48 |
+
|
| 49 |
+
def classify_emotion(text):
|
| 50 |
+
try:
|
| 51 |
+
result = emotion_classifier(text)
|
| 52 |
+
return result[0]["label"].lower()
|
| 53 |
+
except Exception:
|
| 54 |
+
return "neutral"
|
| 55 |
+
|
| 56 |
+
def get_color_for_emotion(e):
|
| 57 |
+
palette = {
|
| 58 |
+
"happy": "#fff59d",
|
| 59 |
+
"joy": "#fff59d",
|
| 60 |
+
"sad": "#90caf9",
|
| 61 |
+
"angry": "#ff8a65",
|
| 62 |
+
"calm": "#a5d6a7",
|
| 63 |
+
"motivated": "#ffcc80",
|
| 64 |
+
"neutral": "#eeeeee",
|
| 65 |
+
}
|
| 66 |
+
return palette.get(e.lower(), "#f5f5f5")
|
| 67 |
+
|
| 68 |
+
# --- Core logic ---
|
| 69 |
+
def chat_with_bot(message, name, age, audio=None):
|
| 70 |
+
if not message and audio:
|
| 71 |
+
# if user spoke instead of typed
|
| 72 |
+
import speech_recognition as sr
|
| 73 |
+
recognizer = sr.Recognizer()
|
| 74 |
+
with sr.AudioFile(audio) as src:
|
| 75 |
+
audio_data = recognizer.record(src)
|
| 76 |
+
try:
|
| 77 |
+
message = recognizer.recognize_google(audio_data)
|
| 78 |
+
except Exception:
|
| 79 |
+
message = "..."
|
| 80 |
+
|
| 81 |
+
if not message:
|
| 82 |
+
return "Please say or type something.", None, None
|
| 83 |
+
|
| 84 |
+
user_id = "default"
|
| 85 |
+
users = load_user_data()
|
| 86 |
+
user = users.get(user_id, {"name": name, "age": age, "recent_mood": "neutral"})
|
| 87 |
+
emotion = classify_emotion(message)
|
| 88 |
+
|
| 89 |
+
# Motivational triggers
|
| 90 |
+
if "motivate" in message.lower() or "guidance" in message.lower():
|
| 91 |
+
quote = fetch_quote()
|
| 92 |
+
user["recent_mood"] = "motivated"
|
| 93 |
+
users[user_id] = user
|
| 94 |
+
save_user_data(users)
|
| 95 |
+
return quote, None, get_color_for_emotion("motivated")
|
| 96 |
+
|
| 97 |
+
# Normal empathetic reply
|
| 98 |
+
prompt = f"""
|
| 99 |
+
You are a warm, empathetic emotional support companion.
|
| 100 |
+
The user's name is {name}, age {age}, currently feeling {emotion}.
|
| 101 |
+
Reply with kindness, encouragement, and positivity.
|
| 102 |
+
Avoid therapy or diagnosis.
|
| 103 |
+
|
| 104 |
+
User: {message}
|
| 105 |
+
Assistant:
|
| 106 |
+
"""
|
| 107 |
+
try:
|
| 108 |
+
response = openai.ChatCompletion.create(
|
| 109 |
+
model="gpt-4o-mini",
|
| 110 |
+
messages=[{"role": "user", "content": prompt}],
|
| 111 |
+
temperature=0.8
|
| 112 |
+
)
|
| 113 |
+
reply = response.choices[0].message["content"].strip()
|
| 114 |
+
except Exception:
|
| 115 |
+
reply = "I'm here for you. Tell me how you’re feeling today."
|
| 116 |
+
|
| 117 |
+
# Save user state
|
| 118 |
+
user["recent_mood"] = emotion
|
| 119 |
+
user["last_active"] = datetime.utcnow().strftime("%Y-%m-%d")
|
| 120 |
+
users[user_id] = user
|
| 121 |
+
save_user_data(users)
|
| 122 |
+
|
| 123 |
+
return reply, reply, get_color_for_emotion(emotion)
|
| 124 |
+
|
| 125 |
+
# --- Gradio UI ---
|
| 126 |
+
with gr.Blocks(title="Empathetic Voice Chatbot 🌼") as app:
|
| 127 |
+
gr.Markdown(
|
| 128 |
+
"## 🌼 Empathetic Voice Chatbot\n"
|
| 129 |
+
"Speak or type your feelings — your friendly listener will respond with warmth and understanding."
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
with gr.Row():
|
| 133 |
+
name = gr.Textbox(label="Your Name", value="Alex")
|
| 134 |
+
age = gr.Number(label="Your Age", value=25)
|
| 135 |
+
|
| 136 |
+
chatbox = gr.Textbox(label="Type your message", placeholder="Say something or click record below...")
|
| 137 |
+
mic = gr.Audio(sources=["microphone"], type="filepath", label="🎤 Speak here")
|
| 138 |
+
output_text = gr.Textbox(label="Assistant's Response")
|
| 139 |
+
audio_reply = gr.Audio(label="🔊 Spoken Reply")
|
| 140 |
+
|
| 141 |
+
send_btn = gr.Button("Send / Talk 💬")
|
| 142 |
+
|
| 143 |
+
def respond(msg, n, a, aud):
|
| 144 |
+
text_reply, tts_text, color = chat_with_bot(msg, n, a, aud)
|
| 145 |
+
if tts_text:
|
| 146 |
+
speech = openai.audio.speech.with_streaming_response.create(
|
| 147 |
+
model="gpt-4o-mini-tts",
|
| 148 |
+
voice="alloy",
|
| 149 |
+
input=tts_text
|
| 150 |
+
)
|
| 151 |
+
out_path = "reply.mp3"
|
| 152 |
+
with open(out_path, "wb") as f:
|
| 153 |
+
f.write(speech.read())
|
| 154 |
+
else:
|
| 155 |
+
out_path = None
|
| 156 |
+
# Dynamically change background color
|
| 157 |
+
app.theme = None
|
| 158 |
+
return gr.update(value=text_reply), out_path
|
| 159 |
+
|
| 160 |
+
send_btn.click(respond, [chatbox, name, age, mic], [output_text, audio_reply])
|
| 161 |
+
|
| 162 |
+
app.launch()
|