Upload app.py.py
Browse files
app.py.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"},"kaggle":{"accelerator":"none","dataSources":[],"isInternetEnabled":true,"language":"python","sourceType":"script","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# π§ π¬ Advanced Mental Health Companion AI Chatbot with Gradio UI\n# Developed using Gemini 1.5 Pro API with RAG, JSON mode, embeddings, and vector search\n\n# --- π Install Required Libraries ---\nimport os\nimport json\nimport gradio as gr\nimport google.generativeai as genai\nfrom langchain.vectorstores import FAISS\nfrom langchain.embeddings import OpenAIEmbeddings\nfrom langchain.docstore.document import Document\nfrom langchain.text_splitter import CharacterTextSplitter\n\n# --- π Load Gemini API Key ---\ngemini_key = os.getenv(\"GEMINI_API_KEY\")\ngenai.configure(api_key=gemini_key)\n\n# --- π Initialize Gemini Model ---\nmodel = genai.GenerativeModel(model_name=\"gemini-1.5-pro\")\nchat = model.start_chat(history=[])\n\n# --- π Define Core Functionalities ---\ndef detect_emotion(user_input):\n prompt = f\"\"\"\n Analyze the message and return a JSON with:\n {{\n \"emotion\": \"<Emotion>\",\n \"tone\": \"<Suggested_Tone>\",\n \"affirmation\": \"<One_Line_Affirmation>\",\n \"response\": \"<Comforting response>\"\n }}\n\n Message: \"{user_input}\"\n \"\"\"\n response = model.generate_content(prompt, generation_config={\"response_mime_type\": \"application/json\"})\n return json.loads(response.text)\n\ndef get_affirmation():\n prompt = \"Provide a unique calming affirmation for someone feeling overwhelmed.\"\n return model.generate_content(prompt).text\n\ndef get_journaling_prompt():\n prompt = \"Give me a mental health journaling prompt.\"\n return model.generate_content(prompt).text\n\ndef get_calming_technique():\n prompt = \"Suggest a calming breathing or grounding technique.\"\n return model.generate_content(prompt).text\n\n# --- π Memory & Vector Store (RAG Setup) ---\ndocuments = [\n Document(page_content=\"Take a deep breath. You are doing your best.\"),\n Document(page_content=\"It's okay to not be okay. Give yourself grace.\"),\n Document(page_content=\"You are not alone. Many have walked this path and found light again.\"),\n Document(page_content=\"Small steps lead to big changes. Just begin.\")\n]\n\nsplitter = CharacterTextSplitter(chunk_size=100, chunk_overlap=0)\nsplit_docs = splitter.split_documents(documents)\nembedding = OpenAIEmbeddings(openai_api_key=gemini_key)\nvector_db = FAISS.from_documents(split_docs, embedding)\n\ndef retrieve_supportive_text(user_input):\n docs = vector_db.similarity_search(user_input, k=2)\n return \"\\n\".join([d.page_content for d in docs])\n\n# --- π Smart Response Generator ---\ndef generate_companion_response(name, issue):\n emotion_data = detect_emotion(issue)\n rag_support = retrieve_supportive_text(issue)\n\n system_prompt = f\"\"\"\n You're MindMate, a kind mental health assistant.\n Respond personally to {name}.\n\n Emotion: {emotion_data['emotion']}\n Tone: {emotion_data['tone']}\n Affirmation: {emotion_data['affirmation']}\n\n Based on the above, and the following supportive texts:\n {rag_support}\n\n Compose a comforting and warm message to the user:\n Message: {issue}\n \"\"\"\n response = chat.send_message(system_prompt)\n return response.text, emotion_data['emotion'], emotion_data['affirmation']\n\n# --- π Gradio UI ---\ndef chatbot_interface(name, issue):\n response, emotion, affirmation = generate_companion_response(name, issue)\n journaling = get_journaling_prompt()\n technique = get_calming_technique()\n\n return {\n \"Emotion\": emotion,\n \"Affirmation\": affirmation,\n \"Companion Response\": response,\n \"Journaling Prompt\": journaling,\n \"Calming Tip\": technique\n }\n\ninputs = [\n gr.Textbox(label=\"Your Name\"),\n gr.Textbox(label=\"What's troubling you today?\", lines=4)\n]\n\noutputs = gr.JSON(label=\"MindMate's Support\")\n\ndemo = gr.Interface(\n fn=chatbot_interface,\n inputs=inputs,\n outputs=outputs,\n title=\"π§ MindMate: Your Mental Health Companion\",\n description=\"Talk to MindMate by sharing your name and what's on your mind. Get emotional insights, affirmations, and comfort.\"\n)\n\ndemo.launch()\n\n# --- π Notes for Deployment ---\n# Save this file as app.py\n# Create requirements.txt with:\n# gradio\n# google-generativeai\n# langchain\n# faiss-cpu\n# openai\n# tiktoken\n# Push both to Hugging Face Spaces (Gradio app type)\n# Add your GEMINI_API_KEY under Settings > Secrets\n","metadata":{"_uuid":"bdce8786-d7ca-4786-97c2-1b7a55ba21fb","_cell_guid":"162a3627-87ff-431e-96e1-3fd5cfefce13","trusted":true,"collapsed":false,"jupyter":{"outputs_hidden":false}},"outputs":[],"execution_count":null}]}
|