Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from scraper import scrape_website
|
| 4 |
+
from googletrans import Translator
|
| 5 |
+
from gtts import gTTS
|
| 6 |
+
import speech_recognition as sr
|
| 7 |
+
from langdetect import detect
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
from pydub import AudioSegment
|
| 10 |
+
import os
|
| 11 |
+
|
| 12 |
+
# Initialize components
|
| 13 |
+
translator = Translator()
|
| 14 |
+
qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased")
|
| 15 |
+
|
| 16 |
+
# Function to process text-based interaction
|
| 17 |
+
def process_text(user_input, context, lang):
|
| 18 |
+
try:
|
| 19 |
+
# Translate to English for the model
|
| 20 |
+
translated_input = translator.translate(user_input, src=lang, dest='en').text
|
| 21 |
+
answer = qa_pipeline({'question': translated_input, 'context': context})['answer']
|
| 22 |
+
# Translate answer back to user's language
|
| 23 |
+
translated_answer = translator.translate(answer, src='en', dest=lang).text
|
| 24 |
+
return translated_answer
|
| 25 |
+
except Exception as e:
|
| 26 |
+
return f"Error: {e}"
|
| 27 |
+
|
| 28 |
+
# Function to process voice input
|
| 29 |
+
def process_voice(file, context):
|
| 30 |
+
recognizer = sr.Recognizer()
|
| 31 |
+
audio = sr.AudioFile(file)
|
| 32 |
+
with audio as source:
|
| 33 |
+
audio_data = recognizer.record(source)
|
| 34 |
+
user_input = recognizer.recognize_google(audio_data)
|
| 35 |
+
lang = detect(user_input)
|
| 36 |
+
return process_text(user_input, context, lang)
|
| 37 |
+
|
| 38 |
+
# Streamlit UI
|
| 39 |
+
st.title("Multilingual Chatbot with Web Scraping")
|
| 40 |
+
st.sidebar.title("Options")
|
| 41 |
+
|
| 42 |
+
# Scraping options
|
| 43 |
+
url = st.sidebar.text_input("Enter a website URL to scrape:")
|
| 44 |
+
if st.sidebar.button("Scrape Website"):
|
| 45 |
+
context = scrape_website(url)
|
| 46 |
+
st.sidebar.success("Website content scraped successfully!")
|
| 47 |
+
else:
|
| 48 |
+
context = "No content available. Please scrape a website first."
|
| 49 |
+
|
| 50 |
+
# Interaction options
|
| 51 |
+
st.subheader("Chat with the Bot")
|
| 52 |
+
mode = st.radio("Choose interaction mode:", ["Text", "Voice"])
|
| 53 |
+
|
| 54 |
+
if mode == "Text":
|
| 55 |
+
user_input = st.text_input("Enter your query:")
|
| 56 |
+
if st.button("Ask"):
|
| 57 |
+
lang = detect(user_input)
|
| 58 |
+
response = process_text(user_input, context, lang)
|
| 59 |
+
st.write(f"Bot: {response}")
|
| 60 |
+
|
| 61 |
+
elif mode == "Voice":
|
| 62 |
+
uploaded_file = st.file_uploader("Upload a voice file (WAV format):", type="wav")
|
| 63 |
+
if uploaded_file and st.button("Ask"):
|
| 64 |
+
response = process_voice(uploaded_file, context)
|
| 65 |
+
st.write(f"Bot: {response}")
|
| 66 |
+
|
| 67 |
+
# Voice Output
|
| 68 |
+
if st.button("Play Response as Voice"):
|
| 69 |
+
if response:
|
| 70 |
+
tts = gTTS(text=response, lang=lang)
|
| 71 |
+
audio_file = BytesIO()
|
| 72 |
+
tts.write_to_fp(audio_file)
|
| 73 |
+
st.audio(audio_file.getvalue(), format="audio/mp3")
|
| 74 |
+
else:
|
| 75 |
+
st.warning("No response to play!")
|
| 76 |
+
|