Priyacodes310 commited on
Commit
f977c0e
Β·
verified Β·
1 Parent(s): 306466b

Upload 4 files

Browse files
Files changed (4) hide show
  1. emotion_selector.py +35 -0
  2. journal.txt +0 -0
  3. requirements.txt +0 -0
  4. soul_sync.py +92 -0
emotion_selector.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ class EmotionSelector(gr.Component):
4
+ def __init__(self, label="Select your emotion", **kwargs):
5
+ super().__init__(**kwargs)
6
+ self.label = label
7
+ self.emotions = [
8
+ ("joy", "😊"),
9
+ ("anger", "😠"),
10
+ ("sadness", "😒"),
11
+ ("fear", "😨"),
12
+ ("surprise", "😲"),
13
+ ("love", "❀️"),
14
+ ("neutral", "😐"),
15
+ ("disgust", "🀒")
16
+ ]
17
+
18
+ def get_block(self, root):
19
+ with gr.Row():
20
+ gr.Markdown(f"### {self.label}")
21
+ btns = []
22
+ for emotion, emoji in self.emotions:
23
+ btn = gr.Button(f"{emoji} {emotion.capitalize()}", variant="secondary", scale=1)
24
+ btns.append((btn, emotion))
25
+
26
+ selected_emotion = gr.State(value="neutral")
27
+
28
+ for btn, emotion in btns:
29
+ btn.click(lambda e=emotion: e, None, selected_emotion)
30
+
31
+ return selected_emotion
32
+
33
+ def _get_props(self):
34
+ return {"label": self.label}
35
+
journal.txt ADDED
File without changes
requirements.txt ADDED
Binary file (132 Bytes). View file
 
soul_sync.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from emotion_selector import EmotionSelector # custom component file
3
+ import requests
4
+ import modal
5
+
6
+ # --- Modal GPT-2 Inference Function ---
7
+ generate_remote = modal.Function.lookup("soulsync-inference", "generate_gpt2_response")
8
+
9
+ # --- Nebius Emotion Detection ---
10
+ def detect_emotion_nebius(text):
11
+ API_URL = "https://models.api.nebius.ai/v1/models/huggingface/j-hartmann--emotion-english-distilroberta-base:predict"
12
+ headers = {
13
+ "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsImtpZCI6IlV6SXJWd1h0dnprLVRvdzlLZWstc0M1akptWXBvX1VaVkxUZlpnMDRlOFUiLCJ0eXAiOiJKV1QifQ.eyJzdWIiOiJnb29nbGUtb2F1dGgyfDEwMDE2MjA4NDQ4MDA2OTkyMTUzNSIsInNjb3BlIjoib3BlbmlkIG9mZmxpbmVfYWNjZXNzIiwiaXNzIjoiYXBpX2tleV9pc3N1ZXIiLCJhdWQiOlsiaHR0cHM6Ly9uZWJpdXMtaW5mZXJlbmNlLmV1LmF1dGgwLmNvbS9hcGkvdjIvIl0sImV4cCI6MTkwNzAzNTc1NSwidXVpZCI6ImQ2MjA0NjgyLTdiNjAtNGRmOS04Zjk2LWM0NzE1YzE1NDg5MSIsIm5hbWUiOiJzb3Vsc3luYzMxIiwiZXhwaXJlc19hdCI6IjIwMzAtMDYtMDdUMDQ6MDk6MTUrMDAwMCJ9.ywEtajHSL6Gq-gQTe7vHmFsfwFauhQdIKj-vu5v9VCU"
14
+ "Content-Type": "application/json"
15
+ }
16
+ payload = {"inputs": text}
17
+
18
+ response = requests.post(API_URL, headers=headers, json=payload)
19
+ response.raise_for_status()
20
+ predictions = response.json()
21
+
22
+ if isinstance(predictions, list):
23
+ return predictions[0]['label']
24
+ else:
25
+ return "neutral"
26
+
27
+ # --- Response Generator ---
28
+ def generate_response(dummy_text, emotion):
29
+ affirmations = {
30
+ "joy": "Keep smiling, you radiate positive energy! 😊",
31
+ "anger": "Take a deep breath. You are in control. 🌿",
32
+ "sadness": "You’re not alone. Brighter days are coming. πŸ’™",
33
+ "fear": "You are safe. Everything is going to be okay. πŸ›‘οΈ",
34
+ "surprise": "Let the unexpected bring growth. 🌱",
35
+ "love": "You are deeply valued and loved. πŸ’–",
36
+ "neutral": "Stay grounded. Peace comes from within. 🧘",
37
+ "disgust": "This moment will pass. Choose calm. 🌊"
38
+ }
39
+ affirmation = affirmations.get(emotion, "Stay strong. You’ve got this! πŸ’ͺ")
40
+
41
+ # Call Modal GPT-2 Inference
42
+ gpt_output = generate_remote.call(dummy_text)
43
+
44
+ # Log to journal
45
+ with open("journal.txt", "a", encoding="utf-8") as file:
46
+ file.write(f"User Emotion: {emotion}\nAI Response: {gpt_output}\nAffirmation: {affirmation}\n\n")
47
+
48
+ return f"🧠 **Detected Emotion:** {emotion}\n\nπŸ’¬ **SoulSync Reply:** {gpt_output}\n\n🌈 **Affirmation:** {affirmation}"
49
+
50
+ # --- View journal ---
51
+ def view_journal():
52
+ try:
53
+ with open("journal.txt", "r", encoding="utf-8") as file:
54
+ return file.read()
55
+ except FileNotFoundError:
56
+ return "πŸ“­ No journal entries yet."
57
+
58
+ # --- Reset session/journal ---
59
+ def reset_session():
60
+ with open("journal.txt", "w", encoding="utf-8") as f:
61
+ f.write("")
62
+ return gr.update(value="")
63
+
64
+ # --- Main function combining emotion selector & response generation ---
65
+ def soul_sync(emotion):
66
+ dummy_input = "Let's talk"
67
+ return generate_response(dummy_input, emotion)
68
+
69
+ # --- Gradio UI ---
70
+ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {background-color: #fff8f0;}") as demo:
71
+ gr.Markdown("## 🌟 SoulSync – Your AI Mental Health Companion 🌈")
72
+ gr.Markdown("Select your emotion and receive support, encouragement, and AI wisdom.")
73
+
74
+ with gr.Tab("🧠 Talk to SoulSync"):
75
+ emotion_input = EmotionSelector(label="Select your emotion")
76
+ send_btn = gr.Button("Send πŸ’¬")
77
+ response_output = gr.Textbox(label="SoulSync Response", lines=5, interactive=False)
78
+
79
+ send_btn.click(fn=soul_sync, inputs=emotion_input, outputs=response_output)
80
+
81
+ with gr.Tab("πŸ““ Journal"):
82
+ journal_btn = gr.Button("πŸ“– View Journal")
83
+ journal_output = gr.Textbox(lines=15, interactive=False)
84
+ clear_btn = gr.Button("Reset Conversation πŸ”")
85
+
86
+ journal_btn.click(fn=view_journal, outputs=journal_output)
87
+ clear_btn.click(fn=reset_session, outputs=journal_output)
88
+
89
+ if __name__ == "__main__":
90
+ demo.launch()
91
+
92
+