Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|