-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
222 lines (191 loc) Β· 7.1 KB
/
app.py
File metadata and controls
222 lines (191 loc) Β· 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# app.py
import streamlit as st
import requests
from deep_translator import GoogleTranslator
from gtts import gTTS
import os
import uuid
import base64
from streamlit_mic_recorder import mic_recorder
# ------------------- CONFIG -------------------
st.set_page_config(page_title="πΈ EmoAid: Chatbot for Sensitive Souls", layout="centered")
# ------------------- HELPERS -------------------
def get_groq_chat(user_input, personality, api_key):
"""Call Groq Chat API"""
prompt = f"""
You are a {personality} for someone who's emotionally sensitive.
Follow this structure in your response:
1. Start with a gentle and supportive reply (about 7 lines, empathetic and understanding).
2. Then provide exactly 3 short practical tips in bullet points.
3. End with a heartfelt encouragement based on the user's feeling and also give a quote according to feelings of user.
Keep language simple, caring, and maximum 15-25 lines in total.
User feeling/message: {user_input}
"""
#Respond to: {user_input}"
headers = {"Authorization": f"Bearer {api_key}", "Content-Type": "application/json"}
payload = {
"model": "llama-3.3-70b-versatile",
"messages": [{"role": "user", "content": prompt}]
}
r = requests.post("https://api.groq.com/openai/v1/chat/completions", headers=headers, json=payload)
if r.status_code == 200:
data = r.json()
return data["choices"][0].get("message", {}).get("content", "β No response")
else:
return f"β Chat error: {r.text}"
def get_groq_transcription(audio_path, api_key):
"""Call Groq Whisper API"""
headers = {"Authorization": f"Bearer {api_key}"}
with open(audio_path, "rb") as f:
files = {"file": f}
data = {"model": "whisper-large-v3"}
r = requests.post("https://api.groq.com/openai/v1/audio/transcriptions", headers=headers, files=files, data=data)
if r.status_code == 200:
return r.json()["text"]
else:
return ""
def translate_text(text, target_lang):
try:
return GoogleTranslator(source="auto", target=target_lang).translate(text)
except Exception as e:
return f"Translation error: {e}"
def detect_mood(text):
keywords = {
"loneliness": ["alone", "lonely", "isolated"],
"breakup": ["breakup", "heartbroken", "relationship"],
"anxiety": ["anxious", "nervous", "worried"],
"sadness": ["sad", "depressed", "cry"],
"anger": ["angry", "frustrated"]
}
text = text.lower()
for mood, words in keywords.items():
if any(word in text for word in words):
return mood
return "neutral"
def speak_text(text, lang="en"):
filename = f"speech_{uuid.uuid4().hex}.mp3"
tts = gTTS(text=text, lang=lang)
tts.save(filename)
with open(filename, "rb") as audio_file:
audio_bytes = audio_file.read()
b64 = base64.b64encode(audio_bytes).decode()
audio_html = f'<audio autoplay controls><source src="data:audio/mp3;base64,{b64}" type="audio/mp3"></audio>'
st.markdown(audio_html, unsafe_allow_html=True)
os.remove(filename)
# ------------------- SIDEBAR -------------------
st.sidebar.title("π§ EmoAid Settings")
api_key = st.secrets.get("GROQ_API_KEY", None) or os.getenv("GROQ_API_KEY")
if not api_key:
st.error("β Groq API key not found. Please set it in secrets.toml or as an environment variable.")
language_codes = {
"Afrikaans": "af",
"Arabic": "ar",
"Bengali": "bn",
"Bosnian": "bs",
"Catalan": "ca",
"Czech": "cs",
"Welsh": "cy",
"Danish": "da",
"German": "de",
"Greek": "el",
"English": "en",
"Esperanto": "eo",
"Spanish": "es",
"Estonian": "et",
"Finnish": "fi",
"French": "fr",
"Gujarati": "gu",
"Hindi": "hi",
"Croatian": "hr",
"Hungarian": "hu",
"Indonesian": "id",
"Icelandic": "is",
"Italian": "it",
"Japanese": "ja",
"Javanese": "jw",
"Khmer": "km",
"Kannada": "kn",
"Korean": "ko",
"Latin": "la",
"Latvian": "lv",
"Macedonian": "mk",
"Malayalam": "ml",
"Marathi": "mr",
"Myanmar (Burmese)": "my",
"Nepali": "ne",
"Dutch": "nl",
"Norwegian": "no",
"Punjabi": "pa",
"Polish": "pl",
"Portuguese": "pt",
"Romanian": "ro",
"Russian": "ru",
"Sinhala": "si",
"Slovak": "sk",
"Albanian": "sq",
"Serbian": "sr",
"Sundanese": "su",
"Swedish": "sv",
"Swahili": "sw",
"Tamil": "ta",
"Telugu": "te",
"Thai": "th",
"Filipino": "tl",
"Turkish": "tr",
"Ukrainian": "uk",
"Urdu": "ur",
"Vietnamese": "vi",
"Chinese (Mandarin)": "zh-CN"
}
language = st.sidebar.selectbox("π Language", list(language_codes.keys()))
personality = st.sidebar.radio("π Personality Mode",
["Therapist", "Motivator", "Funny Friend","Wise Elder","Gentle Listener",
"Romantic Poet", "Spiritual Guide","Empathetic Sister", "Tough Love Coach",
"Stoic Philosopher", "Inner Child"])
speak = st.sidebar.checkbox("π Enable Voice Response")
# ------------------- MAIN APP -------------------
st.title("πΈ EmoAid: Chatbot for Sensitive Souls")
st.markdown("Helping you feel heard, healed, and hopeful β¨")
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
# ------------------- VOICE INPUT -------------------
st.subheader("ποΈ Voice Input")
voice = mic_recorder(start_prompt="π€ Record", stop_prompt="βΉοΈ Stop", just_once=True)
voice_text = ""
if voice:
st.audio(voice['bytes']) # playback recorded audio
with open("temp.wav", "wb") as f:
f.write(voice['bytes'])
voice_text = get_groq_transcription("temp.wav", api_key)
if voice_text:
st.success(f"π€ Voice captured: {voice_text}")
else:
st.warning("β οΈ Could not transcribe voice.")
# ------------------- USER INPUT -------------------
if "user_input" not in st.session_state:
st.session_state.user_input = ""
if voice_text:
st.session_state.user_input = voice_text
user_input = st.text_area("π¬ Type your message here OR use your voice above",
value=st.session_state.user_input, height=100)
# ------------------- PROCESS -------------------
if st.button("Send") and user_input:
st.session_state.chat_history.append(("You", user_input))
if not api_key:
st.warning("Please add your GROQ API key.")
else:
response = get_groq_chat(user_input, personality, api_key)
target_lang_code = language_codes.get(language, "en")
response_translated = response if language == "English" else translate_text(response, target_lang_code)
mood_tag = detect_mood(user_input)
st.session_state.chat_history.append(("EmoAid", f"{response_translated} \n\n㪠*Mood: {mood_tag}*"))
if speak:
speak_text(response_translated, lang=target_lang_code)
st.session_state.user_input = ""
# ------------------- CHAT DISPLAY -------------------
st.markdown("---")
st.subheader("ποΈ Chat History")
for sender, message in st.session_state.chat_history[::-1]:
role = "user" if "You" in sender else "assistant"
with st.chat_message(role):
st.markdown(f"**{sender}:** {message}")