Spaces:
Sleeping
Sleeping
| # π§ π¬ Advanced Mental Health Companion AI Chatbot with Gradio UI | |
| # Developed using Gemini 1.5 Pro API with RAG, JSON mode, embeddings, and vector search | |
| # --- π Install Required Libraries --- | |
| import os | |
| import json | |
| import gradio as gr | |
| import google.generativeai as genai | |
| from langchain.vectorstores import FAISS | |
| from langchain.embeddings import OpenAIEmbeddings | |
| from langchain.docstore.document import Document | |
| from langchain.text_splitter import CharacterTextSplitter | |
| # --- π Load Gemini API Key --- | |
| gemini_key = os.getenv("GEMINI_API_KEY") | |
| genai.configure(api_key=gemini_key) | |
| # --- π Initialize Gemini Model --- | |
| model = genai.GenerativeModel(model_name="gemini-1.5-pro") | |
| chat = model.start_chat(history=[]) | |
| # --- π Define Core Functionalities --- | |
| def detect_emotion(user_input): | |
| prompt = f""" | |
| Analyze the message and return a JSON with: | |
| {{ | |
| "emotion": "<Emotion>", | |
| "tone": "<Suggested_Tone>", | |
| "affirmation": "<One_Line_Affirmation>", | |
| "response": "<Comforting response>" | |
| }} | |
| Message: "{user_input}" | |
| """ | |
| response = model.generate_content(prompt, generation_config={"response_mime_type": "application/json"}) | |
| return json.loads(response.text) | |
| def get_affirmation(): | |
| prompt = "Provide a unique calming affirmation for someone feeling overwhelmed." | |
| return model.generate_content(prompt).text | |
| def get_journaling_prompt(): | |
| prompt = "Give me a mental health journaling prompt." | |
| return model.generate_content(prompt).text | |
| def get_calming_technique(): | |
| prompt = "Suggest a calming breathing or grounding technique." | |
| return model.generate_content(prompt).text | |
| # --- π Memory & Vector Store (RAG Setup) --- | |
| documents = [ | |
| Document(page_content="Take a deep breath. You are doing your best."), | |
| Document(page_content="It's okay to not be okay. Give yourself grace."), | |
| Document(page_content="You are not alone. Many have walked this path and found light again."), | |
| Document(page_content="Small steps lead to big changes. Just begin.") | |
| ] | |
| splitter = CharacterTextSplitter(chunk_size=100, chunk_overlap=0) | |
| split_docs = splitter.split_documents(documents) | |
| embedding = OpenAIEmbeddings(openai_api_key=gemini_key) | |
| vector_db = FAISS.from_documents(split_docs, embedding) | |
| def retrieve_supportive_text(user_input): | |
| docs = vector_db.similarity_search(user_input, k=2) | |
| return "\n".join([d.page_content for d in docs]) | |
| # --- π Smart Response Generator --- | |
| def generate_companion_response(name, issue): | |
| emotion_data = detect_emotion(issue) | |
| rag_support = retrieve_supportive_text(issue) | |
| system_prompt = f""" | |
| You're MindMate, a kind mental health assistant. | |
| Respond personally to {name}. | |
| Emotion: {emotion_data['emotion']} | |
| Tone: {emotion_data['tone']} | |
| Affirmation: {emotion_data['affirmation']} | |
| Based on the above, and the following supportive texts: | |
| {rag_support} | |
| Compose a comforting and warm message to the user: | |
| Message: {issue} | |
| """ | |
| response = chat.send_message(system_prompt) | |
| return response.text, emotion_data['emotion'], emotion_data['affirmation'] | |
| # --- π Gradio UI --- | |
| def chatbot_interface(name, issue): | |
| response, emotion, affirmation = generate_companion_response(name, issue) | |
| journaling = get_journaling_prompt() | |
| technique = get_calming_technique() | |
| return { | |
| "Emotion": emotion, | |
| "Affirmation": affirmation, | |
| "Companion Response": response, | |
| "Journaling Prompt": journaling, | |
| "Calming Tip": technique | |
| } | |
| inputs = [ | |
| gr.Textbox(label="Your Name"), | |
| gr.Textbox(label="What's troubling you today?", lines=4) | |
| ] | |
| outputs = gr.JSON(label="MindMate's Support") | |
| demo = gr.Interface( | |
| fn=chatbot_interface, | |
| inputs=inputs, | |
| outputs=outputs, | |
| title="π§ MindMate: Your Mental Health Companion", | |
| description="Talk to MindMate by sharing your name and what's on your mind. Get emotional insights, affirmations, and comfort." | |
| ) | |
| demo.launch() | |