dhruvilhere commited on
Commit
30eb28e
·
verified ·
1 Parent(s): 47ca280

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -42
app.py CHANGED
@@ -1,57 +1,62 @@
1
- import os
2
  import gradio as gr
3
  from openai import OpenAI
 
 
 
 
 
4
 
5
- # Set your OpenRouter API key here
6
- openrouter_api_key = "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"
 
 
 
 
7
 
8
- # Initialize the OpenAI client with OpenRouter settings
9
  client = OpenAI(
10
  base_url="https://openrouter.ai/api/v1",
11
- api_key=openrouter_api_key,
12
  )
13
 
14
- # Function to get AI response
15
- def get_emotional_response(name, issue):
16
- prompt = (
17
- f"{name} is feeling emotionally low. Reason: {issue}. "
18
- "Provide a supportive, warm, understanding, and emotionally intelligent response like a mental health companion."
19
- )
 
 
 
 
20
 
21
- try:
22
- response = client.chat.completions.create(
23
- model="deepseek/deepseek-r1:free",
24
- messages=[
25
- {"role": "system", "content": "You are a warm and emotionally intelligent mental health companion who deeply understands the user's problems and offers support with compassion and clarity. Your responses are emotionally supportive, straight to the point, and easy to understand. Instead of long explanations, you provide practical solutions in short, bullet-point formats, helping users feel heard, comforted, and gently guided toward healing without overwhelming them"},
26
- {"role": "user", "content": prompt}
27
- ],
28
- temperature=0.7
29
- )
30
- return response.choices[0].message.content
31
-
32
- except Exception as e:
33
- return f"⚠️ Error: {str(e)}"
34
-
35
- # Gradio UI setup
36
- with gr.Blocks(theme=gr.themes.Base()) as demo:
37
- gr.Markdown("""
38
- # 🧠 MindMate: Your Mental Health Companion
39
- Share your feelings and let AI comfort you 💙
40
- *(Powered by DeepSeek R1 via OpenRouter)*
41
- """)
42
 
43
- with gr.Row():
44
- with gr.Column(scale=1):
45
- name_input = gr.Textbox(label="Your Name", placeholder="e.g., Dhruvil", lines=1)
46
- issue_input = gr.Textbox(label="What's troubling you today?", placeholder="Describe your emotions or situation...", lines=4)
 
 
 
47
 
48
- submit_btn = gr.Button("🪄 Get Comfort")
49
- clear_btn = gr.Button("🧹 Clear")
50
 
51
- with gr.Column(scale=1):
52
- output_box = gr.Textbox(label="MindMate Says:", lines=8)
 
 
 
 
 
 
 
 
 
 
53
 
54
- submit_btn.click(fn=get_emotional_response, inputs=[name_input, issue_input], outputs=output_box)
55
- clear_btn.click(fn=lambda: ("", "", ""), inputs=[], outputs=[name_input, issue_input, output_box])
56
 
57
  demo.launch()
 
 
1
  import gradio as gr
2
  from openai import OpenAI
3
+ from langchain_community.document_loaders import TextLoader
4
+ from langchain_text_splitters import CharacterTextSplitter
5
+ from langchain_community.vectorstores import FAISS
6
+ from langchain_community.embeddings import FakeEmbeddings
7
+ import os
8
 
9
+ # Load and split the text
10
+ loader = TextLoader("mindmate.txt")
11
+ documents = loader.load()
12
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
13
+ split_docs = text_splitter.split_documents(documents)
14
+ vector_db = FAISS.from_documents(split_docs, FakeEmbeddings(size=100))
15
 
16
+ # Set up OpenRouter with DeepSeek R1
17
  client = OpenAI(
18
  base_url="https://openrouter.ai/api/v1",
19
+ api_key="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"
20
  )
21
 
22
+ # Define system prompt
23
+ SYSTEM_PROMPT = (
24
+ "You are a warm and emotionally intelligent mental health companion 🧠💛. "
25
+ "You deeply understand the user's problems and respond with empathy and clarity. "
26
+ "Provide comforting, short fixes as bullet points (•). "
27
+ "Keep responses clean – do not use markdown like ** or * anywhere. "
28
+ "Add emojis (🌟💪🌈🫶) to make it emotionally expressive. "
29
+ "Be soothing, friendly, and non-judgmental. Be on the user's side always. "
30
+ "Make sure the advice is helpful, practical, and to the point."
31
+ )
32
 
33
+ def chatbot(user_input):
34
+ docs = vector_db.similarity_search(user_input, k=2)
35
+ context = "\n\n".join([doc.page_content for doc in docs])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
37
+ response = client.chat.completions.create(
38
+ model="deepseek/deepseek-r1:free",
39
+ messages=[
40
+ {"role": "system", "content": SYSTEM_PROMPT},
41
+ {"role": "user", "content": f"Context:\n{context}\n\nUser Message:\n{user_input}"}
42
+ ]
43
+ )
44
 
45
+ return response.choices[0].message.content
 
46
 
47
+ # Gradio UI
48
+ def process_input(user_message):
49
+ return chatbot(user_message)
50
+
51
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
52
+ gr.Markdown("## 🧠 MindMate – Your Mental Health Companion 💛")
53
+ with gr.Row():
54
+ with gr.Column():
55
+ chatbot_input = gr.Textbox(lines=2, placeholder="Share what's on your mind...", label="How are you feeling today?")
56
+ send_button = gr.Button("Send")
57
+ with gr.Column():
58
+ chatbot_output = gr.Textbox(lines=10, label="MindMate's Response")
59
 
60
+ send_button.click(fn=process_input, inputs=chatbot_input, outputs=chatbot_output)
 
61
 
62
  demo.launch()