Munwar92 commited on
Commit
7dd7b7f
·
verified ·
1 Parent(s): 5830366

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -85
app.py CHANGED
@@ -1,16 +1,5 @@
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
- ##########
13
  import asyncio
 
14
  import httpx
15
 
16
  # Define an async function to fetch data
@@ -19,80 +8,22 @@ async def fetch_data(url):
19
  response = await client.get(url)
20
  return response.text # Return the response text
21
 
22
- # Create another async function for the chatbot to run
23
- async def main():
24
- url = "https://www.sbbusba.edu.pk/"
25
- response_text = await fetch_data(url) # Use await inside an async function
26
- print(response_text) # Or process the response as needed
27
-
28
- # Run the async function using asyncio.run()
29
- if __name__ == "__main__":
30
- asyncio.run(main()) # This is where the async function is actually executed
31
-
32
- ###########
33
-
34
- # Initialize components
35
- translator = Translator()
36
- qa_pipeline = pipeline("question-answering", model="distilbert-base-uncased")
37
 
38
- # Function to process text-based interaction
39
- def process_text(user_input, context, lang):
40
- try:
41
- # Translate to English for the model
42
- translated_input = translator.translate(user_input, src=lang, dest='en').text
43
- answer = qa_pipeline({'question': translated_input, 'context': context})['answer']
44
- # Translate answer back to user's language
45
- translated_answer = translator.translate(answer, src='en', dest=lang).text
46
- return translated_answer
47
- except Exception as e:
48
- return f"Error: {e}"
49
 
50
- # Function to process voice input
51
- def process_voice(file, context):
52
- recognizer = sr.Recognizer()
53
- audio = sr.AudioFile(file)
54
- with audio as source:
55
- audio_data = recognizer.record(source)
56
- user_input = recognizer.recognize_google(audio_data)
57
- lang = detect(user_input)
58
- return process_text(user_input, context, lang)
59
 
60
- # Streamlit UI
61
- st.title("Multilingual Chatbot with Web Scraping")
62
- st.sidebar.title("Options")
63
-
64
- # Scraping options
65
- url = st.sidebar.text_input("https://www.sbbusba.edu.pk/")
66
- if st.sidebar.button("Scrape Website"):
67
- context = scrape_website(url)
68
- st.sidebar.success("Website content scraped successfully!")
69
- else:
70
- context = "No content available. Please scrape a website first."
71
-
72
- # Interaction options
73
- st.subheader("Chat with the Bot")
74
- mode = st.radio("Choose interaction mode:", ["Text", "Voice"])
75
-
76
- if mode == "Text":
77
- user_input = st.text_input("Enter your query:")
78
- if st.button("Ask"):
79
- lang = detect(user_input)
80
- response = process_text(user_input, context, lang)
81
- st.write(f"Bot: {response}")
82
-
83
- elif mode == "Voice":
84
- uploaded_file = st.file_uploader("Upload a voice file (WAV format):", type="wav")
85
- if uploaded_file and st.button("Ask"):
86
- response = process_voice(uploaded_file, context)
87
- st.write(f"Bot: {response}")
88
-
89
- # Voice Output
90
- if st.button("Play Response as Voice"):
91
- if response:
92
- tts = gTTS(text=response, lang=lang)
93
- audio_file = BytesIO()
94
- tts.write_to_fp(audio_file)
95
- st.audio(audio_file.getvalue(), format="audio/mp3")
96
- else:
97
- st.warning("No response to play!")
98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import asyncio
2
+ import streamlit as st
3
  import httpx
4
 
5
  # Define an async function to fetch data
 
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()