Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,79 +1,37 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
print("Sorry, I did not understand that.")
|
39 |
-
return None
|
40 |
-
except sr.RequestError as e:
|
41 |
-
print(f"Error with the speech recognition service: {e}")
|
42 |
-
return None
|
43 |
-
|
44 |
-
def generate_response(prompt):
|
45 |
-
"""Generate a response using OpenAI's GPT model."""
|
46 |
-
try:
|
47 |
-
response = openai.Completion.create(
|
48 |
-
engine="text-davinci-003",
|
49 |
-
prompt=prompt,
|
50 |
-
max_tokens=150
|
51 |
-
)
|
52 |
-
return response.choices[0].text.strip()
|
53 |
-
except Exception as e:
|
54 |
-
print(f"Error generating response: {e}")
|
55 |
-
return "I'm sorry, I cannot process that right now."
|
56 |
-
|
57 |
-
def main():
|
58 |
-
"""Main function to run the AI Voice Agent."""
|
59 |
-
print("AI Voice Agent is running. Say 'exit' to stop.")
|
60 |
-
speak("Hello, I am your AI Voice Agent. How can I help you today?")
|
61 |
-
|
62 |
-
while True:
|
63 |
-
# Listen for user input
|
64 |
-
user_input = listen()
|
65 |
-
if not user_input:
|
66 |
-
continue
|
67 |
-
|
68 |
-
# Exit condition
|
69 |
-
if user_input.lower() in ["exit", "quit", "stop"]:
|
70 |
-
speak("Goodbye! Have a nice day.")
|
71 |
-
break
|
72 |
-
|
73 |
-
# Generate and speak the response
|
74 |
-
response = generate_response(user_input)
|
75 |
-
print(f"AI: {response}")
|
76 |
-
speak(response)
|
77 |
-
|
78 |
-
if __name__ == "__main__":
|
79 |
-
main()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import speech_recognition as sr
|
4 |
+
|
5 |
+
# Set your OpenAI API key here
|
6 |
+
openai.api_key = "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"
|
7 |
+
|
8 |
+
def speech_to_text(audio):
|
9 |
+
recognizer = sr.Recognizer()
|
10 |
+
with sr.AudioFile(audio.name) as source:
|
11 |
+
audio_data = recognizer.record(source)
|
12 |
+
try:
|
13 |
+
text = recognizer.recognize_google(audio_data)
|
14 |
+
return text
|
15 |
+
except sr.UnknownValueError:
|
16 |
+
return "Sorry, I could not understand the audio."
|
17 |
+
except sr.RequestError:
|
18 |
+
return "Could not request results from Google Speech Recognition service."
|
19 |
+
|
20 |
+
def text_to_ai_response(text):
|
21 |
+
response = openai.Completion.create(
|
22 |
+
engine="text-davinci-003",
|
23 |
+
prompt=text,
|
24 |
+
max_tokens=200
|
25 |
+
)
|
26 |
+
return response.choices[0].text.strip()
|
27 |
+
|
28 |
+
# Interface for Gradio
|
29 |
+
interface = gr.Interface(
|
30 |
+
fn=lambda audio: text_to_ai_response(speech_to_text(audio)),
|
31 |
+
inputs="audio", # Correct input type
|
32 |
+
outputs="text", # Correct output type
|
33 |
+
title="Voice AI Agent",
|
34 |
+
description="An AI-powered voice assistant powered by OpenAI and Gradio."
|
35 |
+
)
|
36 |
+
|
37 |
+
interface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|