Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import speech_recognition as sr
|
| 3 |
+
import os
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
from gtts import gTTS
|
| 7 |
+
|
| 8 |
+
# Load your OpenAI API key from the Hugging Face secrets
|
| 9 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
+
|
| 11 |
+
# The prompt will be retrieved from Hugging Face secrets
|
| 12 |
+
def get_prompt():
|
| 13 |
+
return os.getenv("AI_PROMPT_SECRET")
|
| 14 |
+
|
| 15 |
+
# Initialize speech recognition
|
| 16 |
+
recognizer = sr.Recognizer()
|
| 17 |
+
|
| 18 |
+
# Function to convert speech to text
|
| 19 |
+
def speech_to_text(audio):
|
| 20 |
+
if not audio: # Check if audio is None or empty
|
| 21 |
+
return "No audio input detected. Please provide a valid audio file."
|
| 22 |
+
try:
|
| 23 |
+
with sr.AudioFile(audio) as source:
|
| 24 |
+
recognizer.adjust_for_ambient_noise(source)
|
| 25 |
+
audio_data = recognizer.record(source)
|
| 26 |
+
text = recognizer.recognize_google(audio_data)
|
| 27 |
+
return text
|
| 28 |
+
except sr.UnknownValueError:
|
| 29 |
+
return "Sorry, I could not understand the audio."
|
| 30 |
+
except sr.RequestError as e:
|
| 31 |
+
return f"Could not request results; {e}"
|
| 32 |
+
|
| 33 |
+
# Function to get GPT-3.5-turbo response
|
| 34 |
+
def get_gpt_response(prompt):
|
| 35 |
+
try:
|
| 36 |
+
response = openai.ChatCompletion.create(
|
| 37 |
+
model="gpt-3.5-turbo",
|
| 38 |
+
messages=[
|
| 39 |
+
{"role": "system", "content": get_prompt()}, # Using the secret prompt
|
| 40 |
+
{"role": "user", "content": prompt}
|
| 41 |
+
]
|
| 42 |
+
)
|
| 43 |
+
return response['choices'][0]['message']['content']
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Error: {e}"
|
| 46 |
+
|
| 47 |
+
# Function to convert text to speech using gTTS
|
| 48 |
+
def text_to_speech(response):
|
| 49 |
+
tts = gTTS(response)
|
| 50 |
+
tts.save("response.mp3")
|
| 51 |
+
return "response.mp3"
|
| 52 |
+
|
| 53 |
+
# Gradio function that integrates all components
|
| 54 |
+
def chatbot(audio):
|
| 55 |
+
user_input = speech_to_text(audio)
|
| 56 |
+
if "Sorry" in user_input or "No audio input" in user_input:
|
| 57 |
+
return user_input, None
|
| 58 |
+
response = get_gpt_response(user_input)
|
| 59 |
+
if response:
|
| 60 |
+
audio_response = text_to_speech(response)
|
| 61 |
+
return response, audio_response
|
| 62 |
+
return "Error generating response.", None
|
| 63 |
+
|
| 64 |
+
# Gradio interface
|
| 65 |
+
iface = gr.Interface(
|
| 66 |
+
fn=chatbot,
|
| 67 |
+
inputs=gr.Audio(type="filepath"),
|
| 68 |
+
outputs=[gr.Textbox(label="Transcription"), gr.Audio(label="AI Response")],
|
| 69 |
+
live=True,
|
| 70 |
+
title="Conversational Assistant",
|
| 71 |
+
description="Upload an audio file or record your voice to get a response from the AI."
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Launch the Gradio interface
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
iface.launch()
|