File size: 22,666 Bytes
d0b211e c253a21 93c8832 00e496b 84d76a9 6c92b85 93c8832 298147b 80c9092 93c8832 d0b211e 00e496b 6c92b85 00e496b d0b211e 00e496b 6c92b85 00e496b 6c92b85 6efba62 6d97d23 6c92b85 00e496b 6c92b85 4206b8e 6c92b85 00e496b d0b211e 00e496b 2342683 298147b 2342683 66954e0 d0b211e 93c8832 d0b211e 93c8832 d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e 1bb45dc d0b211e b5f0e9f d0b211e b5f0e9f d0b211e 71ea6eb 20b342e 71ea6eb 20b342e 71ea6eb 20b342e 71ea6eb 93c8832 7bfb3bc 93c8832 6c92b85 93c8832 6c92b85 00e496b 93c8832 66954e0 6c92b85 7bfb3bc 6c92b85 00e496b 6c92b85 93c8832 298147b 93c8832 298147b 66954e0 298147b 66954e0 298147b 93c8832 298147b 93c8832 298147b 93c8832 298147b 93c8832 6c92b85 93c8832 00e496b 6c92b85 d0b211e 93c8832 6c92b85 93c8832 6c92b85 93c8832 903aacb 93c8832 6c92b85 7bfb3bc 6c92b85 93c8832 6c92b85 93c8832 6c92b85 93c8832 6c92b85 6d97d23 6c92b85 93c8832 6c92b85 93c8832 6c92b85 93c8832 6c92b85 93c8832 6c92b85 00e496b 66954e0 d0b211e 93c8832 6c92b85 00e496b d0b211e 93c8832 66954e0 7bfb3bc 66954e0 93c8832 d0b211e 66954e0 340fd24 d0b211e 66954e0 00e496b b5f0e9f 66954e0 7bfb3bc 139466f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 |
#INFERENCE NLP+EMOTION DETECTION CV+TTS+Memory Management
import spaces
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import torch
from deepface import DeepFace
import time
from kokoro import KPipeline
from IPython.display import display, Audio
import soundfile as sf
from sentence_transformers import SentenceTransformer
import numpy as np
import chromadb
from langchain_community.vectorstores import Chroma
from collections import defaultdict
from sklearn.cluster import DBSCAN
model_name = "IniNLP247/Kenko-mental-health-llama-3-model"
print("Loading Kenko Mental Health Model...")
tokenizer = AutoTokenizer.from_pretrained(model_name)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype=torch.float16,
device_map="auto"
)
pipe = pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
return_full_text=False,
max_new_tokens=512,
temperature=0.7,
top_p=0.9,
repetition_penalty=1.1,
pad_token_id=tokenizer.pad_token_id
)
print("Model loaded successfully!")
print("Loading Kokoro TTS Model...")
tts_pipeline = KPipeline(lang_code='b')
print("Kokoro TTS Model loaded successfully!")
print("Initializing Memory Components...")
chroma_client = chromadb.Client()
embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
def embed_function(texts):
if isinstance(texts, str):
texts = [texts]
return embedding_model.encode(texts).tolist()
global_vector_store = Chroma(
client=chroma_client,
embedding_function=embed_function
)
print("Memory components initialized!")
current_emotion_state = {
"dominant": "neutral",
"confidence": 0.0,
"all_emotions": {},
"last_update": None
}
class AdvancedMemorySystem:
"""
Multi-tier memory system inspired by human memory:
- Working Memory: Current conversation (high priority)
- Short-term Memory: Recent session with decay
- Long-term Memory: Semantic clusters of important themes
- Emotional Memory: Affective associations and patterns
"""
def __init__(self, embedding_model, vector_store):
self.embedding_model = embedding_model
self.vector_store = vector_store
self.working_memory = []
self.short_term_memory = []
self.semantic_clusters = defaultdict(list)
self.emotional_memory = {
"emotion_transitions": [],
"trigger_patterns": defaultdict(list),
"coping_effectiveness": {}
}
self.conversation_themes = []
self.user_model = {
"communication_style": None,
"recurring_concerns": [],
"progress_indicators": [],
"relational_patterns": []
}
def calculate_importance(self, text, emotion, user_engagement):
"""Calculate memory importance using multiple factors"""
importance = 0.5
high_intensity_emotions = ["fear", "angry", "sad", "surprise"]
if emotion in high_intensity_emotions:
importance += 0.3
if len(text.split()) > 30:
importance += 0.2
therapeutic_keywords = [
"trauma", "suicide", "self-harm", "abuse", "panic",
"breakthrough", "progress", "better", "worse", "relationship"
]
if any(kw in text.lower() for kw in therapeutic_keywords):
importance += 0.3
return min(importance, 1.0)
def add_to_working_memory(self, user_msg, bot_msg, emotion, timestamp):
"""Add to immediate working memory (sliding window)"""
self.working_memory.append({
"user": user_msg,
"bot": bot_msg,
"emotion": emotion,
"timestamp": timestamp
})
if len(self.working_memory) > 5:
oldest = self.working_memory.pop(0)
self._consolidate_to_short_term(oldest)
def _consolidate_to_short_term(self, memory_item):
"""Move from working to short-term memory with importance scoring"""
text = f"User: {memory_item['user']}\nKenko: {memory_item['bot']}"
embedding = self.embedding_model.encode(text)
importance = self.calculate_importance(
memory_item['user'],
memory_item['emotion'],
len(memory_item['user'].split())
)
self.short_term_memory.append({
"text": text,
"embedding": embedding,
"importance": importance,
"timestamp": memory_item['timestamp'],
"emotion": memory_item['emotion']
})
try:
self.vector_store.add_texts(
texts=[text],
metadatas=[{"importance": importance, "timestamp": memory_item['timestamp']}]
)
except Exception as e:
print(f"Vector store error: {e}")
def apply_temporal_decay(self, current_time):
"""Apply decay to short-term memories over time"""
decay_rate = 0.01
for memory in self.short_term_memory:
time_elapsed = (current_time - memory['timestamp']) / 60
decay_factor = np.exp(-decay_rate * time_elapsed)
memory['importance'] *= decay_factor
if memory['importance'] < 0.15:
self._consolidate_to_long_term(memory)
def _consolidate_to_long_term(self, memory):
"""Cluster similar memories into semantic long-term memory"""
if not self.semantic_clusters:
self.semantic_clusters[0] = [memory]
self.short_term_memory.remove(memory)
return
best_cluster = 0
best_similarity = -1
for cluster_id, cluster_memories in self.semantic_clusters.items():
cluster_embeddings = [m['embedding'] for m in cluster_memories]
centroid = np.mean(cluster_embeddings, axis=0)
similarity = np.dot(memory['embedding'], centroid) / (
np.linalg.norm(memory['embedding']) * np.linalg.norm(centroid)
)
if similarity > best_similarity:
best_similarity = similarity
best_cluster = cluster_id
if best_similarity > 0.7:
self.semantic_clusters[best_cluster].append(memory)
else:
new_cluster_id = max(self.semantic_clusters.keys()) + 1
self.semantic_clusters[new_cluster_id] = [memory]
if memory in self.short_term_memory:
self.short_term_memory.remove(memory)
def track_emotional_transition(self, prev_emotion, current_emotion, context):
"""Track emotional state transitions for pattern recognition"""
self.emotional_memory["emotion_transitions"].append({
"from": prev_emotion,
"to": current_emotion,
"context": context,
"timestamp": time.time()
})
if prev_emotion != current_emotion:
self.emotional_memory["trigger_patterns"][current_emotion].append(context)
def analyze_conversation_themes(self):
"""Use topic modeling on conversation to identify recurring themes"""
if len(self.short_term_memory) < 3:
return []
all_text = " ".join([m['text'] for m in self.short_term_memory])
words = all_text.lower().split()
word_freq = defaultdict(int)
stopwords = {"the", "a", "is", "in", "and", "to", "of", "i", "my", "me", "you", "that", "it"}
for word in words:
if word not in stopwords and len(word) > 4:
word_freq[word] += 1
themes = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)[:5]
self.conversation_themes = [theme[0] for theme in themes]
return self.conversation_themes
def retrieve_contextual_memory(self, query, current_emotion):
"""Advanced retrieval using multiple memory tiers"""
context = {
"working": [],
"short_term": [],
"long_term": [],
"emotional": [],
"themes": []
}
context["working"] = self.working_memory[-3:]
if self.short_term_memory:
query_embedding = self.embedding_model.encode(query)
scored_memories = []
for memory in self.short_term_memory:
similarity = np.dot(query_embedding, memory['embedding']) / (
np.linalg.norm(query_embedding) * np.linalg.norm(memory['embedding'])
)
final_score = similarity * memory['importance']
if memory['emotion'] == current_emotion:
final_score *= 1.2
scored_memories.append((final_score, memory))
scored_memories.sort(reverse=True, key=lambda x: x[0])
context["short_term"] = [m[1] for m in scored_memories[:3]]
if self.semantic_clusters:
query_embedding = self.embedding_model.encode(query)
best_cluster_id = None
best_cluster_score = -1
for cluster_id, cluster_memories in self.semantic_clusters.items():
cluster_embeddings = [m['embedding'] for m in cluster_memories]
centroid = np.mean(cluster_embeddings, axis=0)
similarity = np.dot(query_embedding, centroid) / (
np.linalg.norm(query_embedding) * np.linalg.norm(centroid)
)
if similarity > best_cluster_score:
best_cluster_score = similarity
best_cluster_id = cluster_id
if best_cluster_id is not None and best_cluster_score > 0.6:
cluster = self.semantic_clusters[best_cluster_id]
context["long_term"] = cluster[:2]
if current_emotion in self.emotional_memory["trigger_patterns"]:
triggers = self.emotional_memory["trigger_patterns"][current_emotion]
context["emotional"] = triggers[-2:]
context["themes"] = self.analyze_conversation_themes()
return context
def update_user_model(self, message, emotion):
"""Build a psychological profile of the user over time"""
if len(message.split()) > 50:
style = "detailed"
elif len(message.split()) < 10:
style = "concise"
else:
style = "moderate"
self.user_model["communication_style"] = style
concern_keywords = {
"anxiety": ["anxious", "worried", "panic", "nervous", "anxiety"],
"depression": ["sad", "depressed", "hopeless", "empty", "depression"],
"relationships": ["partner", "relationship", "friend", "family"],
"work_stress": ["work", "job", "career", "boss", "stress"]
}
for concern, keywords in concern_keywords.items():
if any(kw in message.lower() for kw in keywords):
if concern not in self.user_model["recurring_concerns"]:
self.user_model["recurring_concerns"].append(concern)
def generate_memory_context_string(self, contextual_memory):
"""Format retrieved memories into prompt context"""
context_parts = []
if contextual_memory["working"]:
recent = "\n".join([
f"User: {m['user']}\nKenko: {m['bot']}"
for m in contextual_memory["working"]
])
context_parts.append(f"### Recent Conversation:\n{recent}")
if contextual_memory["short_term"]:
important = "\n".join([m['text'] for m in contextual_memory["short_term"]])
context_parts.append(f"### Important Recent Context:\n{important}")
if contextual_memory["long_term"]:
longterm = "\n".join([m['text'] for m in contextual_memory["long_term"]])
context_parts.append(f"### Related Past Discussions:\n{longterm}")
if contextual_memory["emotional"]:
emotional = ", ".join(contextual_memory["emotional"][:3])
context_parts.append(f"### Emotional Pattern: Previously triggered by: {emotional}")
if contextual_memory["themes"]:
themes = ", ".join(contextual_memory["themes"])
context_parts.append(f"### Session Themes: {themes}")
if self.user_model["recurring_concerns"]:
concerns = ", ".join(self.user_model["recurring_concerns"])
context_parts.append(f"### Recurring Concerns: {concerns}")
return "\n\n".join(context_parts)
def reset(self):
"""Reset all memory tiers"""
self.working_memory = []
self.short_term_memory = []
self.semantic_clusters = defaultdict(list)
self.emotional_memory = {
"emotion_transitions": [],
"trigger_patterns": defaultdict(list),
"coping_effectiveness": {}
}
self.conversation_themes = []
self.user_model = {
"communication_style": None,
"recurring_concerns": [],
"progress_indicators": [],
"relational_patterns": []
}
print("Initializing Advanced Memory System...")
advanced_memory = AdvancedMemorySystem(embedding_model, global_vector_store)
print("Advanced Memory System initialized!")
previous_emotion = "neutral"
def update_emotion_status():
if current_emotion_state["last_update"] is None:
return "*Waiting for emotion data...*"
elapsed = time.time() - current_emotion_state["last_update"]
if elapsed > 60:
return "*Emotion data outdated - please ensure webcam is active*"
dominant = current_emotion_state["dominant"]
confidence = current_emotion_state["confidence"]
return f"**Current Emotion:** {dominant.capitalize()} ({confidence:.1f}% confidence)\n*Last updated: {int(elapsed)}s ago*"
def analyze_emotion(image):
global current_emotion_state
try:
if image is None:
return {}
result = DeepFace.analyze(
img_path=image,
actions=['emotion'],
enforce_detection=False,
detector_backend='opencv'
)
if isinstance(result, list):
emotions = result[0]['emotion']
dominant = result[0]['dominant_emotion']
else:
emotions = result['emotion']
dominant = result['dominant_emotion']
current_emotion_state = {
"dominant": dominant,
"confidence": emotions[dominant],
"all_emotions": emotions,
"last_update": time.time()
}
output = {}
for emotion, score in sorted(emotions.items(), key=lambda x: x[1], reverse=True):
output[emotion.capitalize()] = score
return output
except Exception as e:
print(f"Emotion analysis error: {str(e)}")
return {}
def get_emotion_context():
"""Get current emotion as context string for the model"""
if current_emotion_state["last_update"] is None:
return ""
if time.time() - current_emotion_state["last_update"] > 60:
return ""
dominant = current_emotion_state["dominant"]
confidence = current_emotion_state["confidence"]
emotion_context = f"\n[User's Current Detected Emotion: {dominant} ({confidence:.1f}% confidence)]"
return emotion_context
def chat_with_kenko(message, history):
"""Chat function for Gradio interface with emotion awareness"""
conversation = ""
for user_msg, bot_msg in history:
conversation += f"User: {user_msg}\nKenko: {bot_msg}\n\n"
emotion_context = get_emotion_context()
prompt = f"""### Instruction:
You are Kenko, a compassionate mental health therapist. Provide empathetic, helpful, and professional responses to support the user's mental wellbeing.
{emotion_context}
{conversation}User: {message}
### Response:
"""
try:
response = pipe(prompt)[0]['generated_text']
return response.strip()
except Exception as e:
return f"I'm sorry, I'm having trouble processing your message right now. Error: {str(e)}"
def generate_tts(text):
try:
text = text[:600]
generator = tts_pipeline(
text, voice='af_heart',
speed=1, split_pattern=r'\n+'
)
audio_chunks = []
for gs, ps, audio in generator:
audio_chunks.append(audio)
if not audio_chunks:
print("TTS generation failed")
return None
audio_array = np.concatenate(audio_chunks, axis=0)
audio_array = audio_array.astype(np.float32)
sample_rate = 24000
return (sample_rate, audio_array)
except Exception as e:
print(f"TTS generation error: {str(e)}")
return None
css = """
.gradio-container {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.emotion-box {
border: 2px solid #4CAF50;
border-radius: 10px;
padding: 10px;
margin: 10px 0;
}
"""
with gr.Blocks(
title="Kenko - Mental Health Assistant",
theme=gr.themes.Soft(),
css=css
) as demo:
gr.Markdown("""
# π Kenko - Your Emotion-Aware Mental Health Assistant
Welcome! I'm Kenko, an AI mental health therapist enhanced with real-time emotion detection.
Allow webcam access to enable emotion-aware responses that adapt to how you're feeling.
*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.*
""")
with gr.Row():
with gr.Column(scale=2):
chatbot = gr.Chatbot(
height=500,
show_label=False,
container=True,
bubble_full_width=False,
avatar_images=("π€", "π§ ")
)
audio_output = gr.Audio(
label="Kenko's Voice Response",
autoplay=True,
show_label=True
)
with gr.Row():
msg = gr.Textbox(
placeholder="Share what's on your mind... (press Enter to send)",
container=False,
scale=7,
lines=2,
max_lines=4
)
send_btn = gr.Button("Send π¬", scale=1, variant="primary")
with gr.Row():
clear_btn = gr.Button("ποΈ Clear Chat", scale=1, variant="secondary")
examples_btn = gr.Button("π‘ Example Topics", scale=1, variant="secondary")
with gr.Column(scale=1):
gr.Markdown("### πΈ Emotion Detection")
gr.Markdown("*Your emotional state helps me provide more personalized support*")
webcam_input = gr.Image(
sources=["webcam"],
type="numpy",
streaming=True,
label="Live Webcam Feed"
)
emotion_output = gr.Label(
num_top_classes=7,
label="Detected Emotions"
)
emotion_status = gr.Markdown("*Waiting for emotion data...*")
with gr.Row(visible=False) as examples_row:
gr.Examples(
examples=[
"I've been feeling really anxious lately and I don't know why.",
"I'm having trouble sleeping and my mind won't stop racing.",
"I feel overwhelmed with work and personal responsibilities.",
"I'm struggling with low self-esteem and negative thoughts.",
"I'm having difficulty in my relationships.",
"I feel lonely and isolated.",
"I'm dealing with grief and loss.",
"I want to build better coping strategies."
],
inputs=msg,
label="Try these conversation starters:"
)
with gr.Accordion("βΉοΈ About Kenko", open=False):
gr.Markdown("""
**What I can help with:**
- Active listening and emotional support (now emotion-aware!)
- Coping strategies and stress management techniques
- Guidance on anxiety, depression, and mood concerns
- Relationship and communication advice
- Mindfulness and self-care suggestions
- Building healthy habits and routines
**Emotion Detection Feature:**
- Real-time facial emotion analysis
- Adapts responses based on your current emotional state
- Updates automatically every 30 seconds
- Completely optional - works without webcam too
**Important Notes:**
- I'm an AI trained to provide mental health support
- For immediate crisis support, contact emergency services (911) or crisis hotlines
- Consider professional therapy for ongoing mental health needs
- I don't diagnose conditions or prescribe medications
**Privacy:** Your conversations and emotion data are not stored or shared.
""")
@spaces.GPU
def respond(message, chat_history):
if not message.strip():
return "", chat_history, None
import time
start = time.time()
bot_response = chat_with_kenko(message, chat_history)
text_time = time.time() - start
print(f"Text Generation Time: {text_time:.2f} seconds: {len(bot_response)} characters")
chat_history.append((message, bot_response))
tts_start = time.time()
print(f"Generating TTS for: '{bot_response[:100]}...'")
audio = generate_tts(bot_response)
tts_time = time.time() - tts_start
print(f"TTS Generation Time: {tts_time:.2f} seconds")
print(f"TOTAL TIME: {time.time() - start:.2f}s")
return "", chat_history, audio
def toggle_examples():
return gr.Row(visible=True)
submit = msg.submit(fn=respond, inputs=[msg, chatbot], outputs=[msg, chatbot, audio_output])
send = send_btn.click(fn=respond, inputs=[msg, chatbot], outputs=[msg, chatbot, audio_output])
clear_btn.click(lambda: [], None, outputs=[chatbot, audio_output])
examples_btn.click(toggle_examples, outputs=examples_row)
webcam_input.stream(
analyze_emotion,
inputs=webcam_input,
outputs=emotion_output,
stream_every=1,
time_limit=60
)
timer = gr.Timer(value=5)
timer.tick(
fn=lambda: (update_emotion_status()),
outputs=[emotion_status]
)
if __name__ == "__main__":
print("Starting Kenko Mental Health Assistant with Emotion Detection...")
demo.launch(
share=True,
show_error=True
) |