Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1 +1,118 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# π§ π¬ Advanced Mental Health Companion AI Chatbot with Gradio UI
|
| 2 |
+
# Developed using Gemini 1.5 Pro API with RAG, JSON mode, embeddings, and vector search
|
| 3 |
+
|
| 4 |
+
# --- π Install Required Libraries ---
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
import gradio as gr
|
| 8 |
+
import google.generativeai as genai
|
| 9 |
+
from langchain.vectorstores import FAISS
|
| 10 |
+
from langchain.embeddings import OpenAIEmbeddings
|
| 11 |
+
from langchain.docstore.document import Document
|
| 12 |
+
from langchain.text_splitter import CharacterTextSplitter
|
| 13 |
+
|
| 14 |
+
# --- π Load Gemini API Key ---
|
| 15 |
+
gemini_key = os.getenv("GEMINI_API_KEY")
|
| 16 |
+
genai.configure(api_key=gemini_key)
|
| 17 |
+
|
| 18 |
+
# --- π Initialize Gemini Model ---
|
| 19 |
+
model = genai.GenerativeModel(model_name="gemini-1.5-pro")
|
| 20 |
+
chat = model.start_chat(history=[])
|
| 21 |
+
|
| 22 |
+
# --- π Define Core Functionalities ---
|
| 23 |
+
def detect_emotion(user_input):
|
| 24 |
+
prompt = f"""
|
| 25 |
+
Analyze the message and return a JSON with:
|
| 26 |
+
{{
|
| 27 |
+
"emotion": "<Emotion>",
|
| 28 |
+
"tone": "<Suggested_Tone>",
|
| 29 |
+
"affirmation": "<One_Line_Affirmation>",
|
| 30 |
+
"response": "<Comforting response>"
|
| 31 |
+
}}
|
| 32 |
+
|
| 33 |
+
Message: "{user_input}"
|
| 34 |
+
"""
|
| 35 |
+
response = model.generate_content(prompt, generation_config={"response_mime_type": "application/json"})
|
| 36 |
+
return json.loads(response.text)
|
| 37 |
+
|
| 38 |
+
def get_affirmation():
|
| 39 |
+
prompt = "Provide a unique calming affirmation for someone feeling overwhelmed."
|
| 40 |
+
return model.generate_content(prompt).text
|
| 41 |
+
|
| 42 |
+
def get_journaling_prompt():
|
| 43 |
+
prompt = "Give me a mental health journaling prompt."
|
| 44 |
+
return model.generate_content(prompt).text
|
| 45 |
+
|
| 46 |
+
def get_calming_technique():
|
| 47 |
+
prompt = "Suggest a calming breathing or grounding technique."
|
| 48 |
+
return model.generate_content(prompt).text
|
| 49 |
+
|
| 50 |
+
# --- π Memory & Vector Store (RAG Setup) ---
|
| 51 |
+
documents = [
|
| 52 |
+
Document(page_content="Take a deep breath. You are doing your best."),
|
| 53 |
+
Document(page_content="It's okay to not be okay. Give yourself grace."),
|
| 54 |
+
Document(page_content="You are not alone. Many have walked this path and found light again."),
|
| 55 |
+
Document(page_content="Small steps lead to big changes. Just begin.")
|
| 56 |
+
]
|
| 57 |
+
|
| 58 |
+
splitter = CharacterTextSplitter(chunk_size=100, chunk_overlap=0)
|
| 59 |
+
split_docs = splitter.split_documents(documents)
|
| 60 |
+
embedding = OpenAIEmbeddings(openai_api_key=gemini_key)
|
| 61 |
+
vector_db = FAISS.from_documents(split_docs, embedding)
|
| 62 |
+
|
| 63 |
+
def retrieve_supportive_text(user_input):
|
| 64 |
+
docs = vector_db.similarity_search(user_input, k=2)
|
| 65 |
+
return "\n".join([d.page_content for d in docs])
|
| 66 |
+
|
| 67 |
+
# --- π Smart Response Generator ---
|
| 68 |
+
def generate_companion_response(name, issue):
|
| 69 |
+
emotion_data = detect_emotion(issue)
|
| 70 |
+
rag_support = retrieve_supportive_text(issue)
|
| 71 |
+
|
| 72 |
+
system_prompt = f"""
|
| 73 |
+
You're MindMate, a kind mental health assistant.
|
| 74 |
+
Respond personally to {name}.
|
| 75 |
+
|
| 76 |
+
Emotion: {emotion_data['emotion']}
|
| 77 |
+
Tone: {emotion_data['tone']}
|
| 78 |
+
Affirmation: {emotion_data['affirmation']}
|
| 79 |
+
|
| 80 |
+
Based on the above, and the following supportive texts:
|
| 81 |
+
{rag_support}
|
| 82 |
+
|
| 83 |
+
Compose a comforting and warm message to the user:
|
| 84 |
+
Message: {issue}
|
| 85 |
+
"""
|
| 86 |
+
response = chat.send_message(system_prompt)
|
| 87 |
+
return response.text, emotion_data['emotion'], emotion_data['affirmation']
|
| 88 |
+
|
| 89 |
+
# --- π Gradio UI ---
|
| 90 |
+
def chatbot_interface(name, issue):
|
| 91 |
+
response, emotion, affirmation = generate_companion_response(name, issue)
|
| 92 |
+
journaling = get_journaling_prompt()
|
| 93 |
+
technique = get_calming_technique()
|
| 94 |
+
|
| 95 |
+
return {
|
| 96 |
+
"Emotion": emotion,
|
| 97 |
+
"Affirmation": affirmation,
|
| 98 |
+
"Companion Response": response,
|
| 99 |
+
"Journaling Prompt": journaling,
|
| 100 |
+
"Calming Tip": technique
|
| 101 |
+
}
|
| 102 |
+
|
| 103 |
+
inputs = [
|
| 104 |
+
gr.Textbox(label="Your Name"),
|
| 105 |
+
gr.Textbox(label="What's troubling you today?", lines=4)
|
| 106 |
+
]
|
| 107 |
+
|
| 108 |
+
outputs = gr.JSON(label="MindMate's Support")
|
| 109 |
+
|
| 110 |
+
demo = gr.Interface(
|
| 111 |
+
fn=chatbot_interface,
|
| 112 |
+
inputs=inputs,
|
| 113 |
+
outputs=outputs,
|
| 114 |
+
title="π§ MindMate: Your Mental Health Companion",
|
| 115 |
+
description="Talk to MindMate by sharing your name and what's on your mind. Get emotional insights, affirmations, and comfort."
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
demo.launch()
|