Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,76 +1,51 @@
|
|
| 1 |
-
from langchain_community.vectorstores import FAISS
|
| 2 |
-
from langchain_community.embeddings import FakeEmbeddings
|
| 3 |
-
from langchain.text_splitter import CharacterTextSplitter
|
| 4 |
-
from langchain_community.document_loaders import TextLoader
|
| 5 |
-
from openai import OpenAI
|
| 6 |
-
|
| 7 |
import os
|
|
|
|
| 8 |
import gradio as gr
|
| 9 |
-
import json
|
| 10 |
-
|
| 11 |
-
# ========== Load Documents and Create Vector DB ==========
|
| 12 |
-
loader = TextLoader("mindmate.txt")
|
| 13 |
-
documents = loader.load()
|
| 14 |
-
|
| 15 |
-
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 16 |
-
split_docs = text_splitter.split_documents(documents)
|
| 17 |
-
|
| 18 |
-
# Fix for FakeEmbeddings requiring size
|
| 19 |
-
embedding = FakeEmbeddings(size=1536)
|
| 20 |
-
vector_db = FAISS.from_documents(split_docs, embedding)
|
| 21 |
-
|
| 22 |
-
# ========== OpenRouter DeepSeek R1 Setup ==========
|
| 23 |
-
client = OpenAI(
|
| 24 |
-
base_url="https://openrouter.ai/api/v1",
|
| 25 |
-
api_key="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",
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
# ========== Gradio App ==========
|
| 29 |
-
def mindmate(name, feeling):
|
| 30 |
-
if not name or not feeling:
|
| 31 |
-
return "Please enter your name and what's troubling you."
|
| 32 |
-
|
| 33 |
-
context_docs = vector_db.similarity_search(feeling, k=2)
|
| 34 |
-
context_text = "\n".join([doc.page_content for doc in context_docs])
|
| 35 |
-
|
| 36 |
-
prompt = f"""You are a compassionate and empathetic mental health support assistant named MindMate.
|
| 37 |
-
The user's name is {name}. They said: "{feeling}"
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 60 |
-
gr.Markdown(
|
| 61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
with gr.Row():
|
| 64 |
-
with gr.Column():
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
| 68 |
clear_btn = gr.Button("Clear")
|
| 69 |
|
| 70 |
-
with gr.Column():
|
| 71 |
-
|
| 72 |
|
| 73 |
-
submit_btn.click(fn=
|
| 74 |
-
clear_btn.click(fn=lambda: ("", "",
|
| 75 |
|
| 76 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import openai
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
# Secure API key loading from Hugging Face Secrets or local env
|
| 6 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 7 |
+
openai.api_base = "https://openrouter.ai/api/v1"
|
| 8 |
+
|
| 9 |
+
# Define how the chatbot should respond
|
| 10 |
+
def get_emotional_response(name, issue):
|
| 11 |
+
prompt = f"{name} is feeling emotionally low. Reason: {issue}. Give a supportive, warm response like a mental health companion."
|
| 12 |
+
|
| 13 |
+
try:
|
| 14 |
+
response = openai.ChatCompletion.create(
|
| 15 |
+
model="deepseek-chat",
|
| 16 |
+
messages=[
|
| 17 |
+
{"role": "system", "content": "You are a warm and emotionally intelligent mental health companion."},
|
| 18 |
+
{"role": "user", "content": prompt}
|
| 19 |
+
],
|
| 20 |
+
temperature=0.7
|
| 21 |
+
)
|
| 22 |
+
return response.choices[0].message.content
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return f"⚠️ Error: {str(e)}"
|
| 25 |
+
|
| 26 |
+
# Gradio UI
|
| 27 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 28 |
+
gr.Markdown(
|
| 29 |
+
"""
|
| 30 |
+
🧠 **MindMate: Your Mental Health Companion**
|
| 31 |
+
Just share your name and how you're feeling.
|
| 32 |
+
MindMate will support you emotionally with warmth, care, and science.
|
| 33 |
+
_No key needed ❤️_
|
| 34 |
+
"""
|
| 35 |
+
)
|
| 36 |
|
| 37 |
with gr.Row():
|
| 38 |
+
with gr.Column(scale=1):
|
| 39 |
+
name_input = gr.Textbox(label="Your Name", placeholder="e.g. Rashi")
|
| 40 |
+
issue_input = gr.Textbox(label="What's troubling you today?", placeholder="Tell me anything...")
|
| 41 |
+
|
| 42 |
+
submit_btn = gr.Button("Submit")
|
| 43 |
clear_btn = gr.Button("Clear")
|
| 44 |
|
| 45 |
+
with gr.Column(scale=1):
|
| 46 |
+
output_box = gr.Textbox(label="MindMate Says:", lines=8)
|
| 47 |
|
| 48 |
+
submit_btn.click(fn=get_emotional_response, inputs=[name_input, issue_input], outputs=output_box)
|
| 49 |
+
clear_btn.click(fn=lambda: ("", "", ""), inputs=[], outputs=[name_input, issue_input, output_box])
|
| 50 |
|
| 51 |
+
demo.launch()
|