Munwar92 commited on
Commit
9970661
·
verified ·
1 Parent(s): cf18915

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +79 -19
app.py CHANGED
@@ -1,29 +1,89 @@
1
- import asyncio
2
  import streamlit as st
3
- import httpx
 
 
 
 
 
 
 
 
4
 
5
- # Define an async function to fetch data
6
- async def fetch_data(url):
7
- async with httpx.AsyncClient() as client:
8
- response = await client.get(url)
9
- return response.text # Return the response text
 
10
 
11
- # Define a wrapper function for asyncio to run inside Streamlit
12
- def get_data(url):
13
- return asyncio.run(fetch_data(url)) # Run async function inside sync context
 
 
14
 
15
- # Streamlit app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  def main():
17
  st.title("Custom Multilingual Chatbot")
 
 
18
 
19
- # User input for chatbot
20
- user_input = st.text_input("Ask me anything:")
21
-
22
- if user_input:
23
- # Example: Use the get_data function to fetch data from a website
24
- url = "https://www.sbbusba.edu.pk/"
25
- response_text = get_data(url) # Get the data using the sync wrapper
26
- st.write(response_text) # Show the fetched data in the Streamlit app
 
 
 
 
 
 
 
 
 
 
 
 
 
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()