Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -55,36 +55,45 @@ def generate_response(user_input, chat_history: list):
|
|
55 |
print(f"Error occurred: {e}") # Print error to console for debugging
|
56 |
return "An error occurred while generating the response. Please try again.", chat_history
|
57 |
|
58 |
-
# Define Gradio interface
|
59 |
def gradio_interface():
|
60 |
with gr.Blocks() as demo:
|
61 |
# Initialize chat history
|
62 |
chat_history = []
|
63 |
|
64 |
-
# Create input
|
65 |
gr.Markdown("## LoserHero - A Mental Health Chatbot")
|
66 |
chatbot = gr.Chatbot(type="messages")
|
67 |
-
audio_input = gr.Audio(source="microphone", type="filepath", label="Speak your message")
|
68 |
msg = gr.Textbox(placeholder="Type your message here...")
|
69 |
-
|
70 |
-
audio_output = gr.Audio(label="Response (Audio)")
|
71 |
clear = gr.Button("Clear")
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
|
|
|
|
|
|
79 |
history = updated_history
|
80 |
-
history.append({"role": "assistant", "content":
|
81 |
-
|
82 |
-
|
83 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
-
|
86 |
-
|
87 |
-
|
|
|
88 |
|
89 |
demo.launch()
|
90 |
|
|
|
55 |
print(f"Error occurred: {e}") # Print error to console for debugging
|
56 |
return "An error occurred while generating the response. Please try again.", chat_history
|
57 |
|
|
|
58 |
def gradio_interface():
|
59 |
with gr.Blocks() as demo:
|
60 |
# Initialize chat history
|
61 |
chat_history = []
|
62 |
|
63 |
+
# Create input components
|
64 |
gr.Markdown("## LoserHero - A Mental Health Chatbot")
|
65 |
chatbot = gr.Chatbot(type="messages")
|
|
|
66 |
msg = gr.Textbox(placeholder="Type your message here...")
|
67 |
+
audio_input = gr.Audio(type="filepath", label="Speak your message")
|
|
|
68 |
clear = gr.Button("Clear")
|
69 |
|
70 |
+
# User message submission function
|
71 |
+
def user(user_message, history: list):
|
72 |
+
history.append({"role": "user", "content": user_message})
|
73 |
+
return "", history
|
74 |
+
|
75 |
+
def bot(history: list):
|
76 |
+
if len(history) > 0:
|
77 |
+
user_input = history[-1]["content"]
|
78 |
+
response, updated_history = generate_response(user_input, history)
|
79 |
history = updated_history
|
80 |
+
history.append({"role": "assistant", "content": ""})
|
81 |
+
for character in response:
|
82 |
+
history[-1]['content'] += character
|
83 |
+
time.sleep(0.02)
|
84 |
+
yield history
|
85 |
+
|
86 |
+
# Speech-to-text processing
|
87 |
+
def process_audio(audio_file, history):
|
88 |
+
if audio_file:
|
89 |
+
transcription = audio_to_text(audio_file) # Convert audio to text
|
90 |
+
history.append({"role": "user", "content": transcription})
|
91 |
+
return history
|
92 |
|
93 |
+
# Set up interaction flow:
|
94 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(bot, chatbot, chatbot)
|
95 |
+
audio_input.change(process_audio, [audio_input, chatbot], chatbot)
|
96 |
+
clear.click(lambda: [], None, chatbot, queue=False)
|
97 |
|
98 |
demo.launch()
|
99 |
|