voicekkk / app.py
prasanth345's picture
Update app.py
47d5114 verified
raw
history blame
1.24 kB
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()