import streamlit as st import requests import torch from transformers import AutoModelForCausalLM, AutoTokenizer from gtts import gTTS import os import tempfile import speech_recognition as sr # Set your Hugging Face API key HUGGING_FACE_API_KEY = "voicebot" # Load the model and tokenizer tokenizer = AutoTokenizer.from_pretrained("declare-lab/tango-full") model = AutoModelForCausalLM.from_pretrained("declare-lab/tango-full") # Function to get a response from the chatbot def get_response(input_text): inputs = tokenizer.encode(input_text, return_tensors='pt') response_ids = model.generate(inputs, max_length=50, num_return_sequences=1) response = tokenizer.decode(response_ids[0], skip_special_tokens=True) return response # Function to convert text to speech def text_to_speech(text): tts = gTTS(text=text, lang='en') with tempfile.NamedTemporaryFile(delete=True) as fp: tts.save(f"{fp.name}.mp3") os.system(f"start {fp.name}.mp3") # For Windows, use 'open' for macOS # Speech Recognition Function def recognize_speech(): r = sr.Recognizer() with sr.Microphone() as source: st.write("Listening...") audio = r.listen(source) st.write("Recognizing...") try: text = r.recognize_google(audio) st.success(f"You said: {text}") return text except sr.UnknownValueError: st.error("Sorry, I could not understand the audio.") return None except sr.RequestError: st.error("Could not request results from Google Speech Recognition service.") return None # Streamlit Interface st.title("Voice-to-Text Chatbot") # Recognize speech if st.button("Speak"): user_input = recognize_speech() else: user_input = st.text_input("Type your message here:") # Display response and convert to speech if user_input: st.write("You: ", user_input) chatbot_response = get_response(user_input) st.write("Chatbot: ", chatbot_response) text_to_speech(chatbot_response)