dhruvilhere commited on
Commit
0458af3
·
verified ·
1 Parent(s): 175c13c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -66
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
- Based on the context and user's input, respond with a warm, personalized, emotionally intelligent response.
40
- Use the following context to help, but DO NOT mention it's from a document:
41
-
42
- Context:
43
- {context_text}
44
- """
45
-
46
- completion = client.chat.completions.create(
47
- model="deepseek/deepseek-r1:free",
48
- messages=[{"role": "user", "content": prompt}],
49
- extra_headers={
50
- "HTTP-Referer": "https://huggingface.co/spaces/dhruvilhere/mindmate-chatbot",
51
- "X-Title": "MindMate Chatbot",
52
- },
53
- )
54
-
55
- return completion.choices[0].message.content
56
-
57
-
58
- # ========== Gradio UI ==========
 
 
59
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
60
- gr.Markdown("🧠 **MindMate: Your Mental Health Companion**")
61
- gr.Markdown("Just share your name and how you're feeling. MindMate will support you emotionally with warmth, care, and science. No key needed ❤️")
 
 
 
 
 
 
62
 
63
  with gr.Row():
64
- with gr.Column():
65
- name = gr.Textbox(label="Your Name")
66
- feeling = gr.Textbox(label="What's troubling you today?", lines=4)
67
- submit_btn = gr.Button("Submit", scale=1)
 
68
  clear_btn = gr.Button("Clear")
69
 
70
- with gr.Column():
71
- output = gr.JSON(label="🧘 MindMate's Support")
72
 
73
- submit_btn.click(fn=mindmate, inputs=[name, feeling], outputs=output)
74
- clear_btn.click(fn=lambda: ("", "", {}), inputs=[], outputs=[name, feeling, output])
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()