Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,14 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Model setup
|
| 6 |
model_name = "IniNLP247/Kenko-mental-health-llama-3-model"
|
|
@@ -14,7 +22,7 @@ if tokenizer.pad_token is None:
|
|
| 14 |
|
| 15 |
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
model_name,
|
| 17 |
-
|
| 18 |
device_map="auto"
|
| 19 |
)
|
| 20 |
|
|
@@ -24,7 +32,7 @@ pipe = pipeline(
|
|
| 24 |
model=model,
|
| 25 |
tokenizer=tokenizer,
|
| 26 |
return_full_text=False,
|
| 27 |
-
max_new_tokens=
|
| 28 |
temperature=0.7,
|
| 29 |
top_p=0.9,
|
| 30 |
repetition_penalty=1.1,
|
|
@@ -33,16 +41,93 @@ pipe = pipeline(
|
|
| 33 |
|
| 34 |
print("β
Model loaded successfully!")
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
def chat_with_kenko(message, history):
|
| 37 |
-
"""Chat function for Gradio interface"""
|
|
|
|
| 38 |
# Build conversation context
|
| 39 |
conversation = ""
|
| 40 |
for user_msg, bot_msg in history:
|
| 41 |
conversation += f"User: {user_msg}\nKenko: {bot_msg}\n\n"
|
| 42 |
|
| 43 |
-
#
|
|
|
|
|
|
|
|
|
|
| 44 |
prompt = f"""### Instruction:
|
| 45 |
You are Kenko, a compassionate mental health therapist. Provide empathetic, helpful, and professional responses to support the user's mental wellbeing.
|
|
|
|
| 46 |
|
| 47 |
{conversation}User: {message}
|
| 48 |
|
|
@@ -56,11 +141,58 @@ You are Kenko, a compassionate mental health therapist. Provide empathetic, help
|
|
| 56 |
except Exception as e:
|
| 57 |
return f"I'm sorry, I'm having trouble processing your message right now. Error: {str(e)}"
|
| 58 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
# Custom CSS for a calming interface
|
| 60 |
css = """
|
| 61 |
.gradio-container {
|
| 62 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 63 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
"""
|
| 65 |
|
| 66 |
# Create Gradio interface
|
|
@@ -71,35 +203,63 @@ with gr.Blocks(
|
|
| 71 |
) as demo:
|
| 72 |
|
| 73 |
gr.Markdown("""
|
| 74 |
-
# π§ π Kenko - Your Mental Health Assistant
|
| 75 |
|
| 76 |
-
Welcome! I'm Kenko, an AI mental health therapist
|
| 77 |
-
|
| 78 |
|
| 79 |
*Please remember: I'm an AI assistant and cannot replace professional mental health care. In crisis situations, please contact emergency services or a mental health professional.*
|
| 80 |
""")
|
| 81 |
|
| 82 |
-
chatbot = gr.Chatbot(
|
| 83 |
-
height=500,
|
| 84 |
-
show_label=False,
|
| 85 |
-
container=True,
|
| 86 |
-
bubble_full_width=False,
|
| 87 |
-
avatar_images=("π€", "π§ ")
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
with gr.Row():
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
|
| 104 |
# Example prompts
|
| 105 |
with gr.Row(visible=False) as examples_row:
|
|
@@ -121,37 +281,85 @@ with gr.Blocks(
|
|
| 121 |
with gr.Accordion("βΉοΈ About Kenko", open=False):
|
| 122 |
gr.Markdown("""
|
| 123 |
**What I can help with:**
|
| 124 |
-
- Active listening and emotional support
|
| 125 |
- Coping strategies and stress management techniques
|
| 126 |
- Guidance on anxiety, depression, and mood concerns
|
| 127 |
- Relationship and communication advice
|
| 128 |
- Mindfulness and self-care suggestions
|
| 129 |
- Building healthy habits and routines
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
**Important Notes:**
|
| 132 |
- I'm an AI trained to provide mental health support
|
| 133 |
- For immediate crisis support, contact emergency services (911) or crisis hotlines
|
| 134 |
- Consider professional therapy for ongoing mental health needs
|
| 135 |
- I don't diagnose conditions or prescribe medications
|
| 136 |
|
| 137 |
-
**Privacy:** Your conversations are not stored or shared.
|
| 138 |
""")
|
| 139 |
|
| 140 |
def respond(message, chat_history):
|
| 141 |
if not message.strip():
|
| 142 |
-
return "", chat_history
|
|
|
|
|
|
|
| 143 |
|
|
|
|
| 144 |
bot_response = chat_with_kenko(message, chat_history)
|
|
|
|
|
|
|
| 145 |
chat_history.append((message, bot_response))
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
def toggle_examples():
|
| 149 |
return gr.Row(visible=True)
|
| 150 |
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 154 |
examples_btn.click(toggle_examples, outputs=examples_row)
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
if __name__ == "__main__":
|
| 157 |
-
demo.launch()
|
|
|
|
| 1 |
+
#INFERENCE NLP+EMOTION DETECTION CV+TTS
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 5 |
import torch
|
| 6 |
+
from deepface import DeepFace
|
| 7 |
+
import threading
|
| 8 |
+
import time
|
| 9 |
+
from parler_tts import ParlerTTSForConditionalGeneration
|
| 10 |
+
import soundfile as sf
|
| 11 |
+
import numpy as np
|
| 12 |
|
| 13 |
# Model setup
|
| 14 |
model_name = "IniNLP247/Kenko-mental-health-llama-3-model"
|
|
|
|
| 22 |
|
| 23 |
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
model_name,
|
| 25 |
+
load_in_8bit=True,
|
| 26 |
device_map="auto"
|
| 27 |
)
|
| 28 |
|
|
|
|
| 32 |
model=model,
|
| 33 |
tokenizer=tokenizer,
|
| 34 |
return_full_text=False,
|
| 35 |
+
max_new_tokens=1024,
|
| 36 |
temperature=0.7,
|
| 37 |
top_p=0.9,
|
| 38 |
repetition_penalty=1.1,
|
|
|
|
| 41 |
|
| 42 |
print("β
Model loaded successfully!")
|
| 43 |
|
| 44 |
+
#Loading of TTS
|
| 45 |
+
print("Loading Parler TTS Model...")
|
| 46 |
+
tts_device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 47 |
+
tts_model = ParlerTTSForConditionalGeneration.from_pretrained("parler-tts/parler-tts-mini-v1", torch_dtype=torch.float16).to(tts_device)
|
| 48 |
+
tts_tokenizer = AutoTokenizer.from_pretrained("parler-tts/parler-tts-mini-v1")
|
| 49 |
+
print("β
Parler TTS Model loaded successfully!")
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
# Global variable to store current emotion state
|
| 53 |
+
current_emotion_state = {
|
| 54 |
+
"dominant": "neutral",
|
| 55 |
+
"confidence": 0.0,
|
| 56 |
+
"all_emotions": {},
|
| 57 |
+
"last_update": None
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
def analyze_emotion(image):
|
| 61 |
+
"""Analyze emotion from webcam image"""
|
| 62 |
+
global current_emotion_state
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
if image is None:
|
| 66 |
+
return {}
|
| 67 |
+
|
| 68 |
+
result = DeepFace.analyze(
|
| 69 |
+
img_path=image,
|
| 70 |
+
actions=['emotion'],
|
| 71 |
+
enforce_detection=False,
|
| 72 |
+
detector_backend='opencv'
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
if isinstance(result, list):
|
| 76 |
+
emotions = result[0]['emotion']
|
| 77 |
+
dominant = result[0]['dominant_emotion']
|
| 78 |
+
else:
|
| 79 |
+
emotions = result['emotion']
|
| 80 |
+
dominant = result['dominant_emotion']
|
| 81 |
+
|
| 82 |
+
# Update global emotion state
|
| 83 |
+
current_emotion_state = {
|
| 84 |
+
"dominant": dominant,
|
| 85 |
+
"confidence": emotions[dominant],
|
| 86 |
+
"all_emotions": emotions,
|
| 87 |
+
"last_update": time.time()
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
# Format for display - REMOVE the % symbol and keep as numbers
|
| 91 |
+
output = {}
|
| 92 |
+
for emotion, score in sorted(emotions.items(), key=lambda x: x[1], reverse=True):
|
| 93 |
+
output[emotion.capitalize()] = score # Just the number, no formatting
|
| 94 |
+
|
| 95 |
+
return output
|
| 96 |
+
|
| 97 |
+
except Exception as e:
|
| 98 |
+
print(f"Emotion analysis error: {str(e)}")
|
| 99 |
+
return {}
|
| 100 |
+
|
| 101 |
+
def get_emotion_context():
|
| 102 |
+
"""Get current emotion as context string for the model"""
|
| 103 |
+
if current_emotion_state["last_update"] is None:
|
| 104 |
+
return ""
|
| 105 |
+
|
| 106 |
+
# Check if emotion data is recent (within last 60 seconds)
|
| 107 |
+
if time.time() - current_emotion_state["last_update"] > 60:
|
| 108 |
+
return ""
|
| 109 |
+
|
| 110 |
+
dominant = current_emotion_state["dominant"]
|
| 111 |
+
confidence = current_emotion_state["confidence"]
|
| 112 |
+
|
| 113 |
+
emotion_context = f"\n[User's Current Detected Emotion: {dominant} ({confidence:.1f}% confidence)]"
|
| 114 |
+
return emotion_context
|
| 115 |
+
|
| 116 |
def chat_with_kenko(message, history):
|
| 117 |
+
"""Chat function for Gradio interface with emotion awareness"""
|
| 118 |
+
|
| 119 |
# Build conversation context
|
| 120 |
conversation = ""
|
| 121 |
for user_msg, bot_msg in history:
|
| 122 |
conversation += f"User: {user_msg}\nKenko: {bot_msg}\n\n"
|
| 123 |
|
| 124 |
+
# Get emotion context
|
| 125 |
+
emotion_context = get_emotion_context()
|
| 126 |
+
|
| 127 |
+
# Create prompt in instruction format with emotion awareness
|
| 128 |
prompt = f"""### Instruction:
|
| 129 |
You are Kenko, a compassionate mental health therapist. Provide empathetic, helpful, and professional responses to support the user's mental wellbeing.
|
| 130 |
+
{emotion_context}
|
| 131 |
|
| 132 |
{conversation}User: {message}
|
| 133 |
|
|
|
|
| 141 |
except Exception as e:
|
| 142 |
return f"I'm sorry, I'm having trouble processing your message right now. Error: {str(e)}"
|
| 143 |
|
| 144 |
+
def generate_tts(text):
|
| 145 |
+
try:
|
| 146 |
+
# Limit text severely for testing
|
| 147 |
+
text = text[:200] # Even shorter for testing
|
| 148 |
+
|
| 149 |
+
print(f"[TTS] Starting generation for {len(text)} chars: '{text[:50]}...'")
|
| 150 |
+
|
| 151 |
+
description = "A calm, empathetic voice speaking at a moderate pace."
|
| 152 |
+
|
| 153 |
+
input_ids = tts_tokenizer(description, return_tensors="pt").input_ids.to(tts_device)
|
| 154 |
+
prompt_input_ids = tts_tokenizer(text, return_tensors="pt").input_ids.to(tts_device)
|
| 155 |
+
|
| 156 |
+
print(f"[TTS] Tokenization complete. Generating audio...")
|
| 157 |
+
|
| 158 |
+
# Use proper generation parameters for Parler TTS
|
| 159 |
+
generation = tts_model.generate(
|
| 160 |
+
input_ids=input_ids,
|
| 161 |
+
prompt_input_ids=prompt_input_ids,
|
| 162 |
+
do_sample=True,
|
| 163 |
+
temperature=1.0,
|
| 164 |
+
min_new_tokens=10,
|
| 165 |
+
max_new_tokens=500 # Use max_new_tokens instead of max_length
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
print(f"[TTS] Generation complete. Processing audio...")
|
| 169 |
+
|
| 170 |
+
audio_arr = generation.cpu().numpy().squeeze()
|
| 171 |
+
|
| 172 |
+
print(f"[TTS] Audio array shape: {audio_arr.shape}")
|
| 173 |
+
|
| 174 |
+
return (tts_model.config.sampling_rate, audio_arr)
|
| 175 |
+
|
| 176 |
+
except Exception as e:
|
| 177 |
+
print(f"β TTS generation error: {str(e)}")
|
| 178 |
+
import traceback
|
| 179 |
+
traceback.print_exc()
|
| 180 |
+
return None
|
| 181 |
+
|
| 182 |
+
print(f"TTS Model Device: {tts_model.device}")
|
| 183 |
+
print(f"TTS Device Variable: {tts_device}")
|
| 184 |
+
|
| 185 |
# Custom CSS for a calming interface
|
| 186 |
css = """
|
| 187 |
.gradio-container {
|
| 188 |
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 189 |
}
|
| 190 |
+
.emotion-box {
|
| 191 |
+
border: 2px solid #4CAF50;
|
| 192 |
+
border-radius: 10px;
|
| 193 |
+
padding: 10px;
|
| 194 |
+
margin: 10px 0;
|
| 195 |
+
}
|
| 196 |
"""
|
| 197 |
|
| 198 |
# Create Gradio interface
|
|
|
|
| 203 |
) as demo:
|
| 204 |
|
| 205 |
gr.Markdown("""
|
| 206 |
+
# π§ π Kenko - Your Emotion-Aware Mental Health Assistant
|
| 207 |
|
| 208 |
+
Welcome! I'm Kenko, an AI mental health therapist enhanced with real-time emotion detection.
|
| 209 |
+
Allow webcam access to enable emotion-aware responses that adapt to how you're feeling.
|
| 210 |
|
| 211 |
*Please remember: I'm an AI assistant and cannot replace professional mental health care. In crisis situations, please contact emergency services or a mental health professional.*
|
| 212 |
""")
|
| 213 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
with gr.Row():
|
| 215 |
+
# Left column: Chat interface
|
| 216 |
+
with gr.Column(scale=2):
|
| 217 |
+
chatbot = gr.Chatbot(
|
| 218 |
+
height=500,
|
| 219 |
+
show_label=False,
|
| 220 |
+
container=True,
|
| 221 |
+
bubble_full_width=False,
|
| 222 |
+
avatar_images=("π€", "π§ ")
|
| 223 |
+
)
|
| 224 |
|
| 225 |
+
audio_output = gr.Audio(
|
| 226 |
+
label="Kenko's Voice Response",
|
| 227 |
+
autoplay=True,
|
| 228 |
+
show_label=True
|
| 229 |
+
)
|
| 230 |
+
|
| 231 |
+
with gr.Row():
|
| 232 |
+
msg = gr.Textbox(
|
| 233 |
+
placeholder="Share what's on your mind... (press Enter to send)",
|
| 234 |
+
container=False,
|
| 235 |
+
scale=7,
|
| 236 |
+
lines=2,
|
| 237 |
+
max_lines=4
|
| 238 |
+
)
|
| 239 |
+
send_btn = gr.Button("Send π¬", scale=1, variant="primary")
|
| 240 |
+
|
| 241 |
+
with gr.Row():
|
| 242 |
+
clear_btn = gr.Button("ποΈ Clear Chat", scale=1, variant="secondary")
|
| 243 |
+
examples_btn = gr.Button("π‘ Example Topics", scale=1, variant="secondary")
|
| 244 |
+
|
| 245 |
+
# Right column: Emotion detection
|
| 246 |
+
with gr.Column(scale=1):
|
| 247 |
+
gr.Markdown("### πΈ Emotion Detection")
|
| 248 |
+
gr.Markdown("*Your emotional state helps me provide more personalized support*")
|
| 249 |
+
|
| 250 |
+
webcam_input = gr.Image(
|
| 251 |
+
sources=["webcam"],
|
| 252 |
+
type="numpy",
|
| 253 |
+
streaming=True,
|
| 254 |
+
label="Live Webcam Feed"
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
emotion_output = gr.Label(
|
| 258 |
+
num_top_classes=7,
|
| 259 |
+
label="Detected Emotions"
|
| 260 |
+
)
|
| 261 |
+
|
| 262 |
+
emotion_status = gr.Markdown("*Waiting for emotion data...*")
|
| 263 |
|
| 264 |
# Example prompts
|
| 265 |
with gr.Row(visible=False) as examples_row:
|
|
|
|
| 281 |
with gr.Accordion("βΉοΈ About Kenko", open=False):
|
| 282 |
gr.Markdown("""
|
| 283 |
**What I can help with:**
|
| 284 |
+
- Active listening and emotional support (now emotion-aware!)
|
| 285 |
- Coping strategies and stress management techniques
|
| 286 |
- Guidance on anxiety, depression, and mood concerns
|
| 287 |
- Relationship and communication advice
|
| 288 |
- Mindfulness and self-care suggestions
|
| 289 |
- Building healthy habits and routines
|
| 290 |
|
| 291 |
+
**Emotion Detection Feature:**
|
| 292 |
+
- Real-time facial emotion analysis
|
| 293 |
+
- Adapts responses based on your current emotional state
|
| 294 |
+
- Updates automatically every 30 seconds
|
| 295 |
+
- Completely optional - works without webcam too
|
| 296 |
+
|
| 297 |
**Important Notes:**
|
| 298 |
- I'm an AI trained to provide mental health support
|
| 299 |
- For immediate crisis support, contact emergency services (911) or crisis hotlines
|
| 300 |
- Consider professional therapy for ongoing mental health needs
|
| 301 |
- I don't diagnose conditions or prescribe medications
|
| 302 |
|
| 303 |
+
**Privacy:** Your conversations and emotion data are not stored or shared.
|
| 304 |
""")
|
| 305 |
|
| 306 |
def respond(message, chat_history):
|
| 307 |
if not message.strip():
|
| 308 |
+
return "", chat_history, None
|
| 309 |
+
|
| 310 |
+
import time
|
| 311 |
|
| 312 |
+
start = time.time()
|
| 313 |
bot_response = chat_with_kenko(message, chat_history)
|
| 314 |
+
text_time = time.time() - start
|
| 315 |
+
print(f"Text Generation Time: {text_time:.2f} seconds: {len(bot_response)} characters")
|
| 316 |
chat_history.append((message, bot_response))
|
| 317 |
+
|
| 318 |
+
tts_start = time.time()
|
| 319 |
+
print(f"Generating TTS for: '{bot_response[:100]}...'")
|
| 320 |
+
audio = generate_tts(bot_response)
|
| 321 |
+
tts_time = time.time() - tts_start
|
| 322 |
+
print(f"TTS Generation Time: {tts_time:.2f} seconds")
|
| 323 |
+
print(f"TOTAL TIME: {time.time() - start:.2f}s")
|
| 324 |
+
|
| 325 |
+
return "", chat_history, audio
|
| 326 |
|
| 327 |
def toggle_examples():
|
| 328 |
return gr.Row(visible=True)
|
| 329 |
|
| 330 |
+
def update_emotion_status():
|
| 331 |
+
"""Update emotion status text"""
|
| 332 |
+
if current_emotion_state["last_update"] is None:
|
| 333 |
+
return "*Waiting for emotion data...*"
|
| 334 |
+
|
| 335 |
+
elapsed = time.time() - current_emotion_state["last_update"]
|
| 336 |
+
if elapsed > 60:
|
| 337 |
+
return "*Emotion data outdated - please ensure webcam is active*"
|
| 338 |
+
|
| 339 |
+
dominant = current_emotion_state["dominant"]
|
| 340 |
+
confidence = current_emotion_state["confidence"]
|
| 341 |
+
return f"**Current Emotion:** {dominant.capitalize()} ({confidence:.1f}% confidence)\n*Last updated: {int(elapsed)}s ago*"
|
| 342 |
+
|
| 343 |
+
# Event handlers
|
| 344 |
+
submit = msg.submit(fn=respond, inputs=[msg, chatbot], outputs=[msg, chatbot, audio_output])
|
| 345 |
+
send = send_btn.click(fn=respond, inputs=[msg, chatbot], outputs=[msg, chatbot, audio_output])
|
| 346 |
+
clear_btn.click(lambda: [], None, outputs=[chatbot, audio_output])
|
| 347 |
examples_btn.click(toggle_examples, outputs=examples_row)
|
| 348 |
|
| 349 |
+
# Emotion detection with streaming (analyzes continuously)
|
| 350 |
+
webcam_input.stream(
|
| 351 |
+
analyze_emotion,
|
| 352 |
+
inputs=webcam_input,
|
| 353 |
+
outputs=emotion_output,
|
| 354 |
+
time_limit=30, # Analyze every 30 seconds
|
| 355 |
+
stream_every=30 # Update interval
|
| 356 |
+
)
|
| 357 |
+
|
| 358 |
+
timer = gr.Timer(value=5) # Update every 5 seconds
|
| 359 |
+
timer.tick(
|
| 360 |
+
fn=update_emotion_status,
|
| 361 |
+
outputs=emotion_status
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
if __name__ == "__main__":
|
| 365 |
+
demo.launch()
|