Spaces:
Build error
Build error
| import streamlit as st | |
| from transformers import pipeline | |
| from scraper import scrape_website | |
| from googletrans import Translator | |
| from gtts import gTTS | |
| import speech_recognition as sr | |
| from langdetect import detect | |
| from io import BytesIO | |
| from pydub import AudioSegment | |
| import os | |
| # Initialize components | |
| translator = Translator() | |
| qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased") | |
| # Function to process text-based interaction | |
| def process_text(user_input, context, lang): | |
| try: | |
| # Translate to English for the model | |
| translated_input = translator.translate(user_input, src=lang, dest='en').text | |
| answer = qa_pipeline({'question': translated_input, 'context': context})['answer'] | |
| # Translate answer back to user's language | |
| translated_answer = translator.translate(answer, src='en', dest=lang).text | |
| return translated_answer | |
| except Exception as e: | |
| return f"Error: {e}" | |
| # Function to process voice input | |
| def process_voice(file, context): | |
| recognizer = sr.Recognizer() | |
| audio = sr.AudioFile(file) | |
| with audio as source: | |
| audio_data = recognizer.record(source) | |
| user_input = recognizer.recognize_google(audio_data) | |
| lang = detect(user_input) | |
| return process_text(user_input, context, lang) | |
| # Streamlit UI | |
| st.title("Multilingual Chatbot with Web Scraping") | |
| st.sidebar.title("Options") | |
| # Scraping options | |
| url = st.sidebar.text_input("Enter a website URL to scrape:") | |
| if st.sidebar.button("Scrape Website"): | |
| context = scrape_website(url) | |
| st.sidebar.success("Website content scraped successfully!") | |
| else: | |
| context = "No content available. Please scrape a website first." | |
| # Interaction options | |
| st.subheader("Chat with the Bot") | |
| mode = st.radio("Choose interaction mode:", ["Text", "Voice"]) | |
| if mode == "Text": | |
| user_input = st.text_input("Enter your query:") | |
| if st.button("Ask"): | |
| lang = detect(user_input) | |
| response = process_text(user_input, context, lang) | |
| st.write(f"Bot: {response}") | |
| elif mode == "Voice": | |
| uploaded_file = st.file_uploader("Upload a voice file (WAV format):", type="wav") | |
| if uploaded_file and st.button("Ask"): | |
| response = process_voice(uploaded_file, context) | |
| st.write(f"Bot: {response}") | |
| # Voice Output | |
| if st.button("Play Response as Voice"): | |
| if response: | |
| tts = gTTS(text=response, lang=lang) | |
| audio_file = BytesIO() | |
| tts.write_to_fp(audio_file) | |
| st.audio(audio_file.getvalue(), format="audio/mp3") | |
| else: | |
| st.warning("No response to play!") | |