dhruvilhere commited on
Commit
d52f03e
·
verified ·
1 Parent(s): 01ccfcc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -44
app.py CHANGED
@@ -1,19 +1,18 @@
1
- import os
2
  import json
3
  import gradio as gr
4
- import google.generativeai as genai
5
-
6
  from langchain_community.vectorstores import FAISS
7
  from langchain_community.embeddings import FakeEmbeddings
8
  from langchain.docstore.document import Document
9
  from langchain.text_splitter import CharacterTextSplitter
10
 
11
- # --- Gemini API Key ---
12
- genai.configure(api_key="AIzaSyANP-IrxxK_mKwGBHEEOzvaOz-3ihTImjg")
13
- model = genai.GenerativeModel(model_name="gemini-1.5-pro")
14
- chat = model.start_chat(history=[])
 
15
 
16
- # --- RAG Docs ---
17
  documents = [
18
  Document(page_content="Take a deep breath. You are doing your best."),
19
  Document(page_content="It's okay to not be okay. Give yourself grace."),
@@ -22,9 +21,9 @@ documents = [
22
  ]
23
  splitter = CharacterTextSplitter(chunk_size=100, chunk_overlap=0)
24
  split_docs = splitter.split_documents(documents)
25
- vector_db = FAISS.from_documents(split_docs, FakeEmbeddings(size=768))
26
 
27
- # --- Emotion Detection ---
28
  def detect_emotion(user_input):
29
  prompt = f"""
30
  Analyze the message and return a JSON with:
@@ -36,50 +35,64 @@ def detect_emotion(user_input):
36
  }}
37
  Message: "{user_input}"
38
  """
39
- response = model.generate_content(prompt, generation_config={"response_mime_type": "application/json"})
40
- return json.loads(response.text)
 
 
 
 
41
 
42
  # --- Extra Tools ---
43
  def get_affirmation():
44
- return model.generate_content("Give a one-line calming affirmation.").text
 
 
 
 
45
 
46
  def get_journaling_prompt():
47
- return model.generate_content("Give a journaling prompt for mental clarity.").text
 
 
 
 
48
 
49
  def get_calming_technique():
50
- return model.generate_content("Suggest a calming breathing or grounding technique.").text
 
 
 
 
51
 
52
- # --- Chat Logic ---
53
  def chatbot_interface(name, issue):
54
- try:
55
- emotion_data = detect_emotion(issue)
56
- rag_support = "\n".join([d.page_content for d in vector_db.similarity_search(issue, k=2)])
57
 
58
- system_prompt = f"""
59
- You're MindMate, a kind mental health assistant.
60
- Talk directly to {name}.
61
- Emotion: {emotion_data['emotion']}
62
- Tone: {emotion_data['tone']}
63
- Affirmation: {emotion_data['affirmation']}
64
- Message: {issue}
65
- Based on this and the below RAG support:
66
- {rag_support}
67
- Write a warm, comforting response.
68
- """
69
- response = chat.send_message(system_prompt)
70
- journaling = get_journaling_prompt()
71
- technique = get_calming_technique()
72
 
73
- return {
74
- "Emotion": emotion_data['emotion'],
75
- "Affirmation": emotion_data['affirmation'],
76
- "Companion Response": response.text,
77
- "Journaling Prompt": journaling,
78
- "Calming Tip": technique
79
- }
80
 
81
- except Exception as e:
82
- return {"Error": str(e)}
 
 
 
 
 
83
 
84
  # --- Gradio UI ---
85
  inputs = [
@@ -94,7 +107,7 @@ demo = gr.Interface(
94
  inputs=inputs,
95
  outputs=outputs,
96
  title="🧠 MindMate: Your Mental Health Companion",
97
- description="Just share your name and how you're feeling. MindMate will support you emotionally with warmth, care, and science. No key needed 💖"
98
  )
99
 
100
- demo.launch(share=True)
 
 
1
  import json
2
  import gradio as gr
3
+ from openai import OpenAI
 
4
  from langchain_community.vectorstores import FAISS
5
  from langchain_community.embeddings import FakeEmbeddings
6
  from langchain.docstore.document import Document
7
  from langchain.text_splitter import CharacterTextSplitter
8
 
9
+ # --- DeepSeek R1 API Setup via OpenRouter ---
10
+ client = OpenAI(
11
+ base_url="https://openrouter.ai/api/v1",
12
+ api_key="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",
13
+ )
14
 
15
+ # --- Dummy Embeddings and RAG Docs ---
16
  documents = [
17
  Document(page_content="Take a deep breath. You are doing your best."),
18
  Document(page_content="It's okay to not be okay. Give yourself grace."),
 
21
  ]
22
  splitter = CharacterTextSplitter(chunk_size=100, chunk_overlap=0)
23
  split_docs = splitter.split_documents(documents)
24
+ vector_db = FAISS.from_documents(split_docs, FakeEmbeddings())
25
 
26
+ # --- Emotion Detection using DeepSeek R1 ---
27
  def detect_emotion(user_input):
28
  prompt = f"""
29
  Analyze the message and return a JSON with:
 
35
  }}
36
  Message: "{user_input}"
37
  """
38
+
39
+ completion = client.chat.completions.create(
40
+ model="deepseek/deepseek-r1:free",
41
+ messages=[{"role": "user", "content": prompt}]
42
+ )
43
+ return json.loads(completion.choices[0].message.content)
44
 
45
  # --- Extra Tools ---
46
  def get_affirmation():
47
+ completion = client.chat.completions.create(
48
+ model="deepseek/deepseek-r1:free",
49
+ messages=[{"role": "user", "content": "Give a one-line calming affirmation."}]
50
+ )
51
+ return completion.choices[0].message.content
52
 
53
  def get_journaling_prompt():
54
+ completion = client.chat.completions.create(
55
+ model="deepseek/deepseek-r1:free",
56
+ messages=[{"role": "user", "content": "Give a journaling prompt for mental clarity."}]
57
+ )
58
+ return completion.choices[0].message.content
59
 
60
  def get_calming_technique():
61
+ completion = client.chat.completions.create(
62
+ model="deepseek/deepseek-r1:free",
63
+ messages=[{"role": "user", "content": "Suggest a calming breathing or grounding technique."}]
64
+ )
65
+ return completion.choices[0].message.content
66
 
67
+ # --- Chatbot Main Logic ---
68
  def chatbot_interface(name, issue):
69
+ emotion_data = detect_emotion(issue)
70
+ rag_support = "\n".join([d.page_content for d in vector_db.similarity_search(issue, k=2)])
 
71
 
72
+ system_prompt = f"""
73
+ You're MindMate, a kind mental health assistant.
74
+ Talk directly to {name}.
75
+ Emotion: {emotion_data['emotion']}
76
+ Tone: {emotion_data['tone']}
77
+ Affirmation: {emotion_data['affirmation']}
78
+ Message: {issue}
79
+ Based on this and the below RAG support:
80
+ {rag_support}
81
+ Write a warm, comforting response.
82
+ """
 
 
 
83
 
84
+ final_response = client.chat.completions.create(
85
+ model="deepseek/deepseek-r1:free",
86
+ messages=[{"role": "user", "content": system_prompt}]
87
+ )
 
 
 
88
 
89
+ return {
90
+ "Emotion": emotion_data['emotion'],
91
+ "Affirmation": emotion_data['affirmation'],
92
+ "Companion Response": final_response.choices[0].message.content,
93
+ "Journaling Prompt": get_journaling_prompt(),
94
+ "Calming Tip": get_calming_technique()
95
+ }
96
 
97
  # --- Gradio UI ---
98
  inputs = [
 
107
  inputs=inputs,
108
  outputs=outputs,
109
  title="🧠 MindMate: Your Mental Health Companion",
110
+ description="Just share your name and how you're feeling. MindMate will support you emotionally with warmth, care, and science. Powered by DeepSeek R1 via OpenRouter 💖"
111
  )
112
 
113
+ demo.launch()