Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,109 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import torch
|
3 |
+
from audio_handler import AudioHandler
|
4 |
+
from chat_handler import ChatHandler
|
5 |
+
from transformers import pipeline
|
6 |
+
import base64
|
7 |
+
import time
|
8 |
+
|
9 |
+
# Page configuration
|
10 |
+
st.set_page_config(
|
11 |
+
page_title="Darija Mental Health Assistant",
|
12 |
+
page_icon="🤗",
|
13 |
+
layout="centered"
|
14 |
+
)
|
15 |
+
|
16 |
+
# Custom CSS
|
17 |
+
st.markdown("""
|
18 |
+
<style>
|
19 |
+
.stButton > button {
|
20 |
+
background-color: #4CAF50;
|
21 |
+
color: white;
|
22 |
+
padding: 10px 20px;
|
23 |
+
border-radius: 5px;
|
24 |
+
}
|
25 |
+
.main-header {
|
26 |
+
text-align: center;
|
27 |
+
color: #2e7d32;
|
28 |
+
}
|
29 |
+
.chat-container {
|
30 |
+
padding: 20px;
|
31 |
+
border-radius: 10px;
|
32 |
+
background-color: #f5f5f5;
|
33 |
+
margin: 10px 0;
|
34 |
+
}
|
35 |
+
</style>
|
36 |
+
""", unsafe_allow_html=True)
|
37 |
+
|
38 |
+
# Initialize session state
|
39 |
+
if 'messages' not in st.session_state:
|
40 |
+
st.session_state.messages = []
|
41 |
+
if 'recording' not in st.session_state:
|
42 |
+
st.session_state.recording = False
|
43 |
+
if 'audio_handler' not in st.session_state:
|
44 |
+
st.session_state.audio_handler = AudioHandler()
|
45 |
+
if 'chat_handler' not in st.session_state:
|
46 |
+
st.session_state.chat_handler = ChatHandler()
|
47 |
+
|
48 |
+
def main():
|
49 |
+
# Header
|
50 |
+
st.markdown("<h1 class='main-header'>Darija Mental Health Assistant 🤗</h1>", unsafe_allow_html=True)
|
51 |
+
st.markdown("<h3 style='text-align: center;'>تحدث معي بالدارجة عن مشاعرك</h3>", unsafe_allow_html=True)
|
52 |
+
|
53 |
+
# Sidebar for settings
|
54 |
+
with st.sidebar:
|
55 |
+
st.header("Settings ⚙️")
|
56 |
+
voice_enabled = st.toggle("Enable Voice Input 🎤", True)
|
57 |
+
st.divider()
|
58 |
+
st.markdown("### About")
|
59 |
+
st.info("This assistant provides mental health support in Moroccan Arabic (Darija). Feel free to speak or type your thoughts.")
|
60 |
+
|
61 |
+
# Main chat interface
|
62 |
+
st.markdown("<div class='chat-container'>", unsafe_allow_html=True)
|
63 |
+
|
64 |
+
# Voice input section
|
65 |
+
if voice_enabled:
|
66 |
+
cols = st.columns([1, 1])
|
67 |
+
with cols[0]:
|
68 |
+
if st.button("🎤 Start Recording", disabled=st.session_state.recording):
|
69 |
+
st.session_state.recording = True
|
70 |
+
st.session_state.audio_handler.start_recording()
|
71 |
+
st.experimental_rerun()
|
72 |
+
|
73 |
+
with cols[1]:
|
74 |
+
if st.button("⏹️ Stop Recording", disabled=not st.session_state.recording):
|
75 |
+
if st.session_state.recording:
|
76 |
+
audio_file = st.session_state.audio_handler.stop_recording()
|
77 |
+
with st.spinner("Processing your message..."):
|
78 |
+
transcription = st.session_state.audio_handler.transcribe_audio(audio_file)
|
79 |
+
process_input(transcription)
|
80 |
+
st.session_state.recording = False
|
81 |
+
st.experimental_rerun()
|
82 |
+
|
83 |
+
# Text input
|
84 |
+
user_input = st.text_input("اكتب رسالتك هنا:", key="text_input")
|
85 |
+
if user_input:
|
86 |
+
process_input(user_input)
|
87 |
+
|
88 |
+
# Display chat history
|
89 |
+
display_chat_history()
|
90 |
+
|
91 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
92 |
+
|
93 |
+
def process_input(user_input):
|
94 |
+
if user_input:
|
95 |
+
# Add user message
|
96 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
97 |
+
|
98 |
+
# Get bot response
|
99 |
+
with st.spinner("جاري التفكير..."):
|
100 |
+
response = st.session_state.chat_handler.get_response(user_input)
|
101 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
102 |
+
|
103 |
+
def display_chat_history():
|
104 |
+
for message in st.session_state.messages:
|
105 |
+
with st.chat_message(message["role"]):
|
106 |
+
st.write(message["content"])
|
107 |
+
|
108 |
+
if __name__ == "__main__":
|
109 |
+
main()
|