prasanth345 commited on
Commit
486cc49
·
verified ·
1 Parent(s): 8b5ae1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -79
app.py CHANGED
@@ -1,79 +1,37 @@
1
- import speech_recognition as sr
2
- from gtts import gTTS
3
- import playsound
4
- import os
5
- import pyttsx3
6
- import openai
7
-
8
- # Initialize OpenAI API
9
- openai.api_key = "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"
10
-
11
- # Initialize text-to-speech engine
12
- engine = pyttsx3.init()
13
-
14
- def speak(text):
15
- """Convert text to speech and play it."""
16
- try:
17
- tts = gTTS(text=text, lang='en')
18
- filename = "temp.mp3"
19
- tts.save(filename)
20
- playsound.playsound(filename)
21
- os.remove(filename)
22
- except Exception as e:
23
- print(f"Error in TTS (gTTS): {e}")
24
- engine.say(text)
25
- engine.runAndWait()
26
-
27
- def listen():
28
- """Capture voice input and convert it to text."""
29
- recognizer = sr.Recognizer()
30
- with sr.Microphone() as source:
31
- print("Listening...")
32
- try:
33
- audio = recognizer.listen(source, timeout=5, phrase_time_limit=10)
34
- text = recognizer.recognize_google(audio)
35
- print(f"User said: {text}")
36
- return text
37
- except sr.UnknownValueError:
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()