Spaces:
Build error
Build error
Download app.py from Student3331/voicebot: direct link, hf CLI and curl.
- Browser
- Download file 4.24 kB
-
https://huggingface.co/spaces/Student3331/voicebot/resolve/main/app.py
- Command line
-
hf download hf://spaces/Student3331/voicebot/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Student3331/voicebot/resolve/main/app.py
4.24 kB
| # 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() | |