Spaces:
Build error
Build error
File size: 4,237 Bytes
058b6a6 2f3b7bf 058b6a6 | 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | # Import necessary libraries
import streamlit as st
from pydub import AudioSegment
import speech_recognition as sr
import wikipedia
import openai
from gtts import gTTS
import tempfile
import os
# Set OpenAI API key (replace with your actual API key)
openai.api_key = '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'
# Function to convert the uploaded audio file to WAV format if it's not already in WAV format
def convert_to_wav(audio_file):
file_extension = audio_file.name.split('.')[-1].lower()
if file_extension == "mp3":
audio = AudioSegment.from_mp3(audio_file)
wav_file = tempfile.NamedTemporaryFile(delete=False, suffix=".wav")
audio.export(wav_file.name, format="wav")
return wav_file.name
elif file_extension in ["wav", "flac", "aiff"]:
return audio_file
else:
raise ValueError("Unsupported audio format. Please upload MP3, WAV, or FLAC.")
# Function to recognize speech from the audio file
def recognize_speech_from_file(file_path):
recognizer = sr.Recognizer()
# Open the audio file and recognize the speech
with sr.AudioFile(file_path) as source:
audio = recognizer.record(source) # Read the entire audio file
try:
print("Recognizing...")
text = recognizer.recognize_google(audio) # Use Google's speech recognition
return text
except sr.UnknownValueError:
return "Sorry, I could not understand the audio."
except sr.RequestError:
return "Sorry, my speech service is down."
# Function to get the Wikipedia summary
def get_wikipedia_answer(query):
try:
result = wikipedia.summary(query, sentences=2) # Get a short summary from Wikipedia
return result
except wikipedia.exceptions.DisambiguationError as e:
return f"Your query is ambiguous, did you mean: {e.options}"
except wikipedia.exceptions.HTTPTimeoutError:
return "I am unable to access Wikipedia right now."
except wikipedia.exceptions.RedirectError:
return "Redirect error while fetching the information."
except wikipedia.exceptions.PageError:
return "Sorry, I couldn't find any information on that topic."
# Function to get the answer from GPT-3/4 (OpenAI)
def get_gpt_answer(query):
try:
response = openai.Completion.create(
engine="text-davinci-003", # Use the appropriate engine based on your OpenAI account
prompt=query,
max_tokens=150
)
return response.choices[0].text.strip()
except openai.error.OpenAIError as e:
return f"Error from GPT: {str(e)}"
# Function to convert text to speech using gTTS
def speak(text):
tts = gTTS(text=text, lang='en')
tts.save("response.mp3") # Save the speech to a file
return "response.mp3"
# Streamlit UI
def main():
st.title("Voice-to-Text Chatbot")
st.write("Upload an audio file to ask a question, and the bot will respond with text and speech.")
# Upload audio file
uploaded_file = st.file_uploader("Upload an Audio File", type=["mp3", "wav", "flac"])
if uploaded_file is not None:
# Convert the uploaded file to WAV if necessary
audio_file_path = convert_to_wav(uploaded_file)
st.audio(uploaded_file, format='audio/wav') # Play the uploaded audio for the user
# Recognize speech from the audio file
user_input = recognize_speech_from_file(audio_file_path)
st.write(f"You asked: {user_input}")
# Get the answer from Wikipedia or GPT-3
wikipedia_answer = get_wikipedia_answer(user_input)
if wikipedia_answer:
st.write(f"Wikipedia says: {wikipedia_answer}")
response_text = wikipedia_answer
else:
gpt_answer = get_gpt_answer(user_input)
st.write(f"GPT says: {gpt_answer}")
response_text = gpt_answer
# Convert the response to speech and play it
response_audio_path = speak(response_text)
st.audio(response_audio_path, format='audio/mp3') # Play the response audio
if __name__ == "__main__":
main()
|