Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,22 @@ import os
|
|
| 3 |
from groq import Groq
|
| 4 |
import gradio as gr
|
| 5 |
from gtts import gTTS
|
|
|
|
| 6 |
|
| 7 |
-
client = Groq(api_key=os.getenv("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def get_chatbot_response(user_message, country, language, conversation_history):
|
| 10 |
system_message = (
|
|
@@ -154,6 +168,17 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
| 154 |
liability_btn = gr.Button("⚖️ Liability", elem_classes="insurance-button")
|
| 155 |
pet_btn = gr.Button("🐾 Pet", elem_classes="insurance-button")
|
| 156 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
with gr.Row(equal_height=True):
|
| 158 |
scenario_input = gr.Textbox(
|
| 159 |
label="💡 Type your message...",
|
|
@@ -176,24 +201,53 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
| 176 |
):
|
| 177 |
btn.click(lambda current, insurance=insurance: update_insurance_selection(current, insurance), inputs=scenario_input, outputs=scenario_input)
|
| 178 |
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
country_input.change(lambda c: gr.update(visible=c == "Other"), inputs=country_input, outputs=custom_country_input)
|
| 187 |
|
| 188 |
submit_btn.click(
|
| 189 |
submit,
|
| 190 |
-
inputs=[country_input, custom_country_input, language_input, scenario_input, conversation_state],
|
| 191 |
outputs=[conversation_state, chatbot, scenario_input]
|
| 192 |
)
|
| 193 |
|
| 194 |
scenario_input.submit(
|
| 195 |
fn=submit,
|
| 196 |
-
inputs=[country_input, custom_country_input, language_input, scenario_input, conversation_state],
|
| 197 |
outputs=[conversation_state, chatbot, scenario_input]
|
| 198 |
)
|
| 199 |
|
|
|
|
| 3 |
from groq import Groq
|
| 4 |
import gradio as gr
|
| 5 |
from gtts import gTTS
|
| 6 |
+
import speech_recognition as sr
|
| 7 |
|
| 8 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 9 |
+
|
| 10 |
+
# Speech-to-text function
|
| 11 |
+
def speech_to_text(audio_file):
|
| 12 |
+
recognizer = sr.Recognizer()
|
| 13 |
+
with sr.AudioFile(audio_file) as source:
|
| 14 |
+
audio = recognizer.record(source)
|
| 15 |
+
try:
|
| 16 |
+
text = recognizer.recognize_google(audio)
|
| 17 |
+
return text
|
| 18 |
+
except sr.UnknownValueError:
|
| 19 |
+
return "Could not understand audio"
|
| 20 |
+
except sr.RequestError:
|
| 21 |
+
return "Could not request results"
|
| 22 |
|
| 23 |
def get_chatbot_response(user_message, country, language, conversation_history):
|
| 24 |
system_message = (
|
|
|
|
| 168 |
liability_btn = gr.Button("⚖️ Liability", elem_classes="insurance-button")
|
| 169 |
pet_btn = gr.Button("🐾 Pet", elem_classes="insurance-button")
|
| 170 |
|
| 171 |
+
# Add a dropdown for input method (text or voice)
|
| 172 |
+
input_method = gr.Dropdown(
|
| 173 |
+
["Text", "Voice"],
|
| 174 |
+
label="🎤 Input Method",
|
| 175 |
+
value="Text",
|
| 176 |
+
interactive=True
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
# Add a microphone input for voice
|
| 180 |
+
voice_input = gr.Audio(source="microphone", type="filepath", label="🎤 Speak your query", visible=False)
|
| 181 |
+
|
| 182 |
with gr.Row(equal_height=True):
|
| 183 |
scenario_input = gr.Textbox(
|
| 184 |
label="💡 Type your message...",
|
|
|
|
| 201 |
):
|
| 202 |
btn.click(lambda current, insurance=insurance: update_insurance_selection(current, insurance), inputs=scenario_input, outputs=scenario_input)
|
| 203 |
|
| 204 |
+
# Function to handle input method selection
|
| 205 |
+
def handle_input_method(input_method, scenario_input, voice_input):
|
| 206 |
+
if input_method == "Voice":
|
| 207 |
+
return gr.update(visible=False), gr.update(visible=True)
|
| 208 |
+
else:
|
| 209 |
+
return gr.update(visible=True), gr.update(visible=False)
|
| 210 |
+
|
| 211 |
+
input_method.change(
|
| 212 |
+
handle_input_method,
|
| 213 |
+
inputs=[input_method, scenario_input, voice_input],
|
| 214 |
+
outputs=[scenario_input, voice_input]
|
| 215 |
+
)
|
| 216 |
+
|
| 217 |
+
# Function to process voice input
|
| 218 |
+
def process_voice_input(voice_file, country, custom_country, language, conversation_state):
|
| 219 |
+
if voice_file:
|
| 220 |
+
user_message = speech_to_text(voice_file)
|
| 221 |
+
selected_country = custom_country if country == "Other" else country
|
| 222 |
+
updated_history, chat_display = get_chatbot_response(
|
| 223 |
+
user_message, selected_country, language, conversation_state
|
| 224 |
+
)
|
| 225 |
+
return updated_history, chat_display, ""
|
| 226 |
+
else:
|
| 227 |
+
return conversation_state, [], "No audio input detected."
|
| 228 |
+
|
| 229 |
+
# Submit function for both text and voice input
|
| 230 |
+
def submit(country, custom_country, language, scenario, conversation_state, input_method, voice_input):
|
| 231 |
+
if input_method == "Text":
|
| 232 |
+
selected_country = custom_country if country == "Other" else country
|
| 233 |
+
updated_history, chat_display = get_chatbot_response(
|
| 234 |
+
scenario, selected_country, language, conversation_state
|
| 235 |
+
)
|
| 236 |
+
return updated_history, chat_display, ""
|
| 237 |
+
else:
|
| 238 |
+
return process_voice_input(voice_input, country, custom_country, language, conversation_state)
|
| 239 |
|
| 240 |
country_input.change(lambda c: gr.update(visible=c == "Other"), inputs=country_input, outputs=custom_country_input)
|
| 241 |
|
| 242 |
submit_btn.click(
|
| 243 |
submit,
|
| 244 |
+
inputs=[country_input, custom_country_input, language_input, scenario_input, conversation_state, input_method, voice_input],
|
| 245 |
outputs=[conversation_state, chatbot, scenario_input]
|
| 246 |
)
|
| 247 |
|
| 248 |
scenario_input.submit(
|
| 249 |
fn=submit,
|
| 250 |
+
inputs=[country_input, custom_country_input, language_input, scenario_input, conversation_state, input_method, voice_input],
|
| 251 |
outputs=[conversation_state, chatbot, scenario_input]
|
| 252 |
)
|
| 253 |
|