dhruvilhere's picture
Update app.py
a25237e verified
import gradio as gr
from openai import OpenAI
from langchain_community.document_loaders import TextLoader
from langchain_text_splitters import CharacterTextSplitter
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings import FakeEmbeddings
import os
# Load and split the text
loader = TextLoader("mindmate.txt")
documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
split_docs = text_splitter.split_documents(documents)
vector_db = FAISS.from_documents(split_docs, FakeEmbeddings(size=100))
# Set up OpenRouter with DeepSeek R1
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="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"
)
# Define system prompt
SYSTEM_PROMPT = (
"You are a warm and emotionally intelligent mental health companion πŸ§ πŸ’›. "
"You deeply understand the user's problems and respond with empathy and clarity. "
"Provide comforting, short fixes as bullet points (β€’). "
"Keep responses clean – do not use markdown like ** or * anywhere. "
"Add emojis (🌟πŸ’ͺ🌈🫢) to make it emotionally expressive. "
"Be soothing, friendly, and non-judgmental. Be on the user's side always. "
"Make sure the advice is helpful, practical, and to the point."
)
def chatbot(name, issue):
full_prompt = f"{name} is feeling emotionally low. Reason: {issue}. Please help."
docs = vector_db.similarity_search(issue, k=2)
context = "\n\n".join([doc.page_content for doc in docs])
response = client.chat.completions.create(
model="deepseek/deepseek-r1:free",
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": f"Context:\n{context}\n\nUser: {full_prompt}"}
]
)
return response.choices[0].message.content
# Gradio UI
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("## 🧠 MindMate – Your Mental Health Companion πŸ’›\nShare your feelings and get comforting support πŸ’™")
with gr.Row():
with gr.Column():
name_input = gr.Textbox(label="Your Name", placeholder="e.g., Dhruvil")
issue_input = gr.Textbox(lines=3, placeholder="What's troubling you today?", label="What’s bothering you?")
send_button = gr.Button("πŸͺ„ Get Support")
with gr.Column():
chatbot_output = gr.Textbox(lines=12, label="MindMate's Response")
send_button.click(fn=chatbot, inputs=[name_input, issue_input], outputs=chatbot_output)
demo.launch()