adeelshuaib commited on
Commit
bfdd8ad
·
verified ·
1 Parent(s): e9a8554

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -18
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 textbox and button for clearing chat
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
- text_output = gr.Textbox(label="Response (Text)")
70
- audio_output = gr.Audio(label="Response (Audio)")
71
  clear = gr.Button("Clear")
72
 
73
- def process_input(user_message, audio_file, history: list):
74
- if audio_file:
75
- user_message = audio_to_text(audio_file)
76
- if user_message:
77
- history.append({"role": "user", "content": user_message})
78
- response, updated_history = generate_response(user_message, history)
 
 
 
79
  history = updated_history
80
- history.append({"role": "assistant", "content": response})
81
- response_audio = text_to_audio(response)
82
- return "", None, response, response_audio, history
83
- return "", None, "Please provide a valid input.", None, history
 
 
 
 
 
 
 
 
84
 
85
- msg.submit(process_input, [msg, audio_input, chatbot], [msg, audio_input, text_output, audio_output, chatbot])
86
- audio_input.change(process_input, [msg, audio_input, chatbot], [msg, audio_input, text_output, audio_output, chatbot])
87
- clear.click(lambda: ("", None, "", None, []), None, [msg, audio_input, text_output, audio_output, chatbot])
 
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