File size: 1,244 Bytes
486cc49
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47d5114
1
2
3
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
import gradio as gr
import openai
import speech_recognition as sr

# Set your OpenAI API key here
openai.api_key = "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"

def speech_to_text(audio):
    recognizer = sr.Recognizer()
    with sr.AudioFile(audio.name) as source:
        audio_data = recognizer.record(source)
    try:
        text = recognizer.recognize_google(audio_data)
        return text
    except sr.UnknownValueError:
        return "Sorry, I could not understand the audio."
    except sr.RequestError:
        return "Could not request results from Google Speech Recognition service."

def text_to_ai_response(text):
    response = openai.Completion.create(
      engine="text-davinci-003",
      prompt=text,
      max_tokens=200
    )
    return response.choices[0].text.strip()

# Interface for Gradio
interface = gr.Interface(
    fn=lambda audio: text_to_ai_response(speech_to_text(audio)),
    inputs="audio",  # Correct input type
    outputs="text",  # Correct output type
    title="Voice AI Agent",
    description="An AI-powered voice assistant powered by OpenAI and Gradio."
)

interface.launch()