Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- emotion_selector.py +35 -0
- journal.txt +0 -0
- requirements.txt +0 -0
- 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 |
+
|