prasanth345 commited on
Commit
3bc3f66
·
verified ·
1 Parent(s): 15672e3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -46
app.py CHANGED
@@ -1,55 +1,35 @@
1
  import gradio as gr
2
- import pyttsx3
3
- import speech_recognition as sr
4
- from transformers import pipeline
5
 
6
- # Initialize Text-to-Speech engine
7
- engine = pyttsx3.init()
8
- engine.setProperty('rate', 150)
 
9
 
10
- # Load a conversational AI model
11
- chatbot = pipeline("conversational", model="microsoft/DialoGPT-medium")
 
12
 
13
- def tts_response(user_query):
14
- """Generate AI response, convert to speech, and return text."""
15
- try:
16
- response = chatbot(user_query)
17
- bot_reply = response[0]['generated_text']
18
- engine.say(bot_reply)
19
- engine.runAndWait()
20
- return bot_reply
21
- except Exception as e:
22
- return f"Error: {str(e)}"
23
 
24
- def stt_to_tts(audio):
25
- """Convert speech to text, process with AI, then convert response to speech."""
26
- recognizer = sr.Recognizer()
27
- try:
28
- with sr.AudioFile(audio) as source:
29
- audio_data = recognizer.record(source)
30
- user_query = recognizer.recognize_google(audio_data)
31
- return tts_response(user_query)
32
- except sr.UnknownValueError:
33
- return "Sorry, I could not understand the audio."
34
- except sr.RequestError as e:
35
- return f"Request error from Speech Recognition service; {e}"
36
 
37
  # Gradio interface
38
- def gradio_ui():
39
- with gr.Blocks() as app:
40
- gr.Markdown("## AI Voice Agent")
41
- with gr.Tab("Text-to-Speech"):
42
- user_query = gr.Textbox(label="Your Query")
43
- response = gr.Textbox(label="AI Response", interactive=False)
44
- submit_button = gr.Button("Submit")
45
- submit_button.click(tts_response, inputs=user_query, outputs=response)
46
- with gr.Tab("Speech-to-Speech"):
47
- audio_input = gr.Audio(source="microphone", type="filepath", label="Speak Now")
48
- audio_response = gr.Textbox(label="AI Response")
49
- audio_button = gr.Button("Submit")
50
- audio_button.click(stt_to_tts, inputs=audio_input, outputs=audio_response)
51
- return app
52
 
53
  if __name__ == "__main__":
54
- app = gradio_ui()
55
- app.launch()
 
1
  import gradio as gr
2
+ from utils.stt import speech_to_text
3
+ from utils.nlp import process_query
4
+ from utils.tts import text_to_speech
5
 
6
+ # Load intents and other configurations
7
+ import json
8
+ with open("intents.json", "r") as f:
9
+ intents = json.load(f)
10
 
11
+ def voice_agent(audio):
12
+ # Step 1: Convert speech to text
13
+ query = speech_to_text(audio)
14
 
15
+ # Step 2: Process the query and get a response
16
+ response = process_query(query, intents)
 
 
 
 
 
 
 
 
17
 
18
+ # Step 3: Convert the response to speech
19
+ audio_response = text_to_speech(response)
20
+
21
+ return response, audio_response
 
 
 
 
 
 
 
 
22
 
23
  # Gradio interface
24
+ interface = gr.Interface(
25
+ fn=voice_agent,
26
+ inputs=gr.Audio(source="microphone", type="filepath"),
27
+ outputs=[
28
+ gr.Textbox(label="Agent Response"),
29
+ gr.Audio(label="Voice Response")
30
+ ],
31
+ title="Hotel Booking Voice Agent"
32
+ )
 
 
 
 
 
33
 
34
  if __name__ == "__main__":
35
+ interface.launch()