Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,89 @@
|
|
| 1 |
-
import asyncio
|
| 2 |
import streamlit as st
|
| 3 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
def
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def main():
|
| 17 |
st.title("Custom Multilingual Chatbot")
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
if __name__ == "__main__":
|
| 29 |
main()
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
import speech_recognition as sr
|
| 5 |
+
from gtts import gTTS
|
| 6 |
+
import os
|
| 7 |
+
from sentence_transformers import SentenceTransformer
|
| 8 |
+
import faiss
|
| 9 |
+
import numpy as np
|
| 10 |
+
from transformers import pipeline
|
| 11 |
|
| 12 |
+
# Scrape website data
|
| 13 |
+
def scrape_website(url):
|
| 14 |
+
response = requests.get(url)
|
| 15 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 16 |
+
text = soup.get_text()
|
| 17 |
+
return text
|
| 18 |
|
| 19 |
+
# Function to create embeddings
|
| 20 |
+
def create_embeddings(texts):
|
| 21 |
+
model = SentenceTransformer('paraphrase-MiniLM-L6-v2')
|
| 22 |
+
embeddings = model.encode(texts)
|
| 23 |
+
return embeddings
|
| 24 |
|
| 25 |
+
# Use Faiss for similarity search
|
| 26 |
+
def search(query, documents, k=1):
|
| 27 |
+
embeddings = create_embeddings([query] + documents)
|
| 28 |
+
query_embedding = embeddings[0]
|
| 29 |
+
doc_embeddings = np.stack(embeddings[1:])
|
| 30 |
+
|
| 31 |
+
index = faiss.IndexFlatL2(doc_embeddings.shape[1]) # L2 distance for similarity
|
| 32 |
+
index.add(doc_embeddings)
|
| 33 |
+
|
| 34 |
+
# Search for the top-k most similar documents
|
| 35 |
+
D, I = index.search(np.array([query_embedding]), k)
|
| 36 |
+
return [documents[i] for i in I[0]]
|
| 37 |
+
|
| 38 |
+
# Function for Text-to-Speech
|
| 39 |
+
def text_to_speech(text):
|
| 40 |
+
tts = gTTS(text)
|
| 41 |
+
tts.save("response.mp3")
|
| 42 |
+
os.system("start response.mp3") # For Windows, use "start", on Linux or macOS use "open"
|
| 43 |
+
|
| 44 |
+
# Function for Speech-to-Text
|
| 45 |
+
def speech_to_text():
|
| 46 |
+
recognizer = sr.Recognizer()
|
| 47 |
+
with sr.Microphone() as source:
|
| 48 |
+
print("Listening...")
|
| 49 |
+
audio = recognizer.listen(source)
|
| 50 |
+
query = recognizer.recognize_google(audio)
|
| 51 |
+
print(f"User: {query}")
|
| 52 |
+
return query
|
| 53 |
+
|
| 54 |
+
# Function to generate responses using Hugging Face GPT model
|
| 55 |
+
def generate_response(query):
|
| 56 |
+
generator = pipeline("text-generation", model="gpt2")
|
| 57 |
+
response = generator(query, max_length=50, num_return_sequences=1)
|
| 58 |
+
return response[0]['generated_text']
|
| 59 |
+
|
| 60 |
+
# Main Streamlit function
|
| 61 |
def main():
|
| 62 |
st.title("Custom Multilingual Chatbot")
|
| 63 |
+
|
| 64 |
+
mode = st.selectbox("Choose Mode", ["Text", "Voice"])
|
| 65 |
|
| 66 |
+
if mode == "Text":
|
| 67 |
+
user_input = st.text_input("Ask me anything:")
|
| 68 |
+
if user_input:
|
| 69 |
+
url = "https://www.sbbusba.edu.pk/" # Example URL, can be dynamically set by the user
|
| 70 |
+
web_content = scrape_website(url)
|
| 71 |
+
relevant_data = search(user_input, [web_content])
|
| 72 |
+
|
| 73 |
+
response = generate_response(f"Based on the content of the website: {relevant_data[0]}")
|
| 74 |
+
st.write("Bot: " + response)
|
| 75 |
+
text_to_speech(response) # Convert the text response to speech
|
| 76 |
+
|
| 77 |
+
elif mode == "Voice":
|
| 78 |
+
if st.button("Start Listening"):
|
| 79 |
+
query = speech_to_text() # Listen and convert to text
|
| 80 |
+
url = "https://www.sbbusba.edu.pk/" # Example URL
|
| 81 |
+
web_content = scrape_website(url)
|
| 82 |
+
relevant_data = search(query, [web_content])
|
| 83 |
+
|
| 84 |
+
response = generate_response(f"Based on the content of the website: {relevant_data[0]}")
|
| 85 |
+
st.write("Bot: " + response)
|
| 86 |
+
text_to_speech(response) # Convert the text response to speech
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
main()
|