File size: 2,585 Bytes
40660a1
11810ad
30eb28e
 
 
 
 
25e2ea6
30eb28e
 
 
 
 
 
0458af3
30eb28e
11810ad
 
30eb28e
11810ad
 
30eb28e
 
 
 
 
 
 
 
 
 
0458af3
a25237e
 
 
30eb28e
40660a1
30eb28e
 
 
 
a25237e
30eb28e
 
0458af3
30eb28e
40660a1
30eb28e
 
a25237e
30eb28e
 
a25237e
 
 
30eb28e
a25237e
40660a1
a25237e
25e2ea6
a25237e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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()