Arhashmi commited on
Commit
fc2385b
1 Parent(s): efc3d79

Upload 4 files

Browse files
Files changed (4) hide show
  1. app.py +30 -0
  2. requirements.txt +16 -0
  3. response.py +11 -0
  4. scrape.py +15 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from scrape import scrape_data_from_url
3
+ import response
4
+
5
+ data = "No data"
6
+ url = ""
7
+
8
+ def main():
9
+ global data
10
+ global url
11
+
12
+ st.title("Web Scraping and Chat App")
13
+
14
+ url = st.text_input("Enter URL:")
15
+
16
+ if st.button("Scrape Data"):
17
+ data = scrape_data_from_url(url)
18
+ st.success("Data scraped successfully!")
19
+
20
+ st.subheader("Scraped Data:")
21
+ st.write(data)
22
+
23
+ user_input = st.text_input("Enter your message:")
24
+ if st.button("Send"):
25
+ bot_response = response.get_response(user_input, data)
26
+ st.success("Bot Response:")
27
+ st.write(bot_response)
28
+
29
+ if __name__ == '__main__':
30
+ main()
requirements.txt ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ beautifulsoup4==4.12.2
2
+ streamlit
3
+ blinker==1.6.2
4
+ certifi==2023.7.22
5
+ charset-normalizer==3.2.0
6
+ click==8.1.6
7
+ colorama==0.4.6
8
+ Flask==2.3.2
9
+ idna==3.4
10
+ itsdangerous==2.1.2
11
+ Jinja2==3.1.2
12
+ MarkupSafe==2.1.3
13
+ requests==2.31.0
14
+ soupsieve==2.4.1
15
+ urllib3==2.0.4
16
+ Werkzeug==2.3.6
response.py ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def get_response(user_input: str, scraped_data: str) -> str:
2
+ user_input_lower = user_input.lower()
3
+ scraped_data_lower = scraped_data.lower()
4
+
5
+ print("User Input:", user_input_lower)
6
+ print("Scraped Data:", scraped_data_lower)
7
+
8
+ if "birthplace" in user_input_lower and "imran khan" in scraped_data_lower:
9
+ return "Imran Khan was born in Lahore, Pakistan."
10
+ else:
11
+ return "I'm sorry, I couldn't find an answer based on the provided data."
scrape.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ from bs4 import BeautifulSoup
3
+
4
+ def scrape_data_from_url(url):
5
+ try:
6
+ response = requests.get(url)
7
+ response.raise_for_status() # Check if the request was successful
8
+ soup = BeautifulSoup(response.text, 'html.parser')
9
+ # Find all <p> tags and extract their text content
10
+ paragraphs = soup.find_all('p')
11
+ # Combine the text content from all paragraphs into a single string
12
+ data = "\n".join(paragraph.get_text() for paragraph in paragraphs)
13
+ return data
14
+ except requests.exceptions.RequestException as e:
15
+ return f"Error fetching data: {str(e)}"