Skip to content

Commit

Permalink
few fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
itsOwen committed Aug 18, 2024
1 parent bc8669b commit a8d4530
Show file tree
Hide file tree
Showing 18 changed files with 69 additions and 27 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ Check out our [YouTube video](https://www.youtube.com/watch?v=iATSd5Ijl4M) for a
pip install -r requirements.txt
```

4. Install the playwright:
```bash
playwright install
```

## 🚀 Usage

1. Fire up the Streamlit app:
Expand Down
Binary file modified app/__pycache__/streamlit_web_scraper_chat.cpython-312.pyc
Binary file not shown.
Binary file modified app/__pycache__/ui_components.cpython-312.pyc
Binary file not shown.
Binary file modified app/__pycache__/utils.cpython-312.pyc
Binary file not shown.
26 changes: 25 additions & 1 deletion app/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import streamlit as st
import time
import random

def get_loading_message():
Expand All @@ -13,4 +15,26 @@ def get_loading_message():
"Scraping the net, one byte at a time...",
"Crashing through the data barriers, Johnny-style..."
]
return random.choice(messages)
return random.choice(messages)

def loading_animation(process_func, *args, **kwargs):
loading_placeholder = st.empty()
result = None
start_time = time.time()
while result is None:
elapsed_time = time.time() - start_time
if elapsed_time > 30: # Timeout after 30 seconds
loading_placeholder.error("Request timed out. Please try again.")
return None

loading_message = get_loading_message()
loading_placeholder.text(f"{loading_message}\n\nIn progress...")

try:
result = process_func(*args, **kwargs)
except Exception as e:
loading_placeholder.error(f"An error occurred: {str(e)}. Retrying...")
time.sleep(1)

loading_placeholder.empty()
return result
61 changes: 36 additions & 25 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import streamlit as st
import time
from app.streamlit_web_scraper_chat import StreamlitWebScraperChat
from app.ui_components import display_info_icons, display_message
from app.utils import get_loading_message
from app.utils import loading_animation, get_loading_message

def safe_process_message(web_scraper_chat, message):
if message is None or message.strip() == "":
return "I'm sorry, but I didn't receive any input. Could you please try again?"
try:
return web_scraper_chat.process_message(message)
except AttributeError as e:
if "'NoneType' object has no attribute 'lower'" in str(e):
return "I encountered an issue while processing your request. It seems like I received an unexpected empty value. Could you please try rephrasing your input?"
else:
raise e
except Exception as e:
return f"An unexpected error occurred: {str(e)}. Please try again or contact support if the issue persists."

def main():
st.set_page_config(page_title="CyberScraper 2077", page_icon="🌐", layout="wide")
Expand Down Expand Up @@ -32,38 +44,37 @@ def main():

if "messages" not in st.session_state:
st.session_state.messages = []


if "processing" not in st.session_state:
st.session_state.processing = False

chat_container = st.container()

with chat_container:
for message in st.session_state.messages:
with st.chat_message(message["role"]):
display_message(message)

if prompt := st.chat_input("Enter the URL to scrape or ask a question regarding the data"):
if prompt := st.chat_input("Enter the URL to scrape or ask a question regarding the data", key="user_input"):
st.session_state.messages.append({"role": "user", "content": prompt})
with chat_container:
with st.chat_message("user"):
st.markdown(prompt)
st.session_state.processing = True
st.rerun()

with st.chat_message("assistant"):
message_placeholder = st.empty()
loading_placeholder = st.empty()

while True:
loading_placeholder.text(get_loading_message())
try:
full_response = st.session_state.web_scraper_chat.process_message(prompt)
break
except Exception as e:
st.error(f"An error occurred: {str(e)}. Retrying...")
time.sleep(1)

loading_placeholder.empty()

display_message({"role": "assistant", "content": full_response})

st.session_state.messages.append({"role": "assistant", "content": full_response})
if st.session_state.processing:
with st.chat_message("assistant"):
try:
full_response = loading_animation(
safe_process_message,
st.session_state.web_scraper_chat,
st.session_state.messages[-1]["content"]
)
if full_response is not None:
st.session_state.messages.append({"role": "assistant", "content": full_response})
except Exception as e:
st.error(f"An unexpected error occurred: {str(e)}")
finally:
st.session_state.processing = False
st.rerun()

st.markdown(
"""
Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ pandas
tiktoken
langchain
langchain-community
langchain_openai
openai
playwright
openpyxl
xlsxwriter
beautifulsoup4
markdown
aiohttp
python-dotenv
python-dotenv
watchdog
Binary file modified src/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file modified src/__pycache__/models.cpython-312.pyc
Binary file not shown.
Binary file modified src/__pycache__/web_extractor.cpython-312.pyc
Binary file not shown.
Binary file modified src/scrapers/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file modified src/scrapers/__pycache__/base_scraper.cpython-312.pyc
Binary file not shown.
Binary file modified src/scrapers/__pycache__/html_scraper.cpython-312.pyc
Binary file not shown.
Binary file modified src/scrapers/__pycache__/json_scraper.cpython-312.pyc
Binary file not shown.
Binary file modified src/scrapers/__pycache__/playwright_scraper.cpython-312.pyc
Binary file not shown.
Binary file modified src/utils/__pycache__/__init__.cpython-312.pyc
Binary file not shown.
Binary file modified src/utils/__pycache__/markdown_formatter.cpython-312.pyc
Binary file not shown.
Binary file modified src/utils/__pycache__/proxy_manager.cpython-312.pyc
Binary file not shown.

0 comments on commit a8d4530

Please sign in to comment.