Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from groq import Groq
|
|
| 10 |
client = Groq(api_key=os.environ.get('GroqApi'))
|
| 11 |
|
| 12 |
# Global variable to store scraped data
|
| 13 |
-
SCRAPED_DATA =
|
| 14 |
|
| 15 |
def scrape_web_data(url, scrape_option):
|
| 16 |
"""Scrape data from the given URL based on the scrape option."""
|
|
@@ -41,6 +41,9 @@ def scrape_web_data(url, scrape_option):
|
|
| 41 |
|
| 42 |
def process_query_with_groq(query, data):
|
| 43 |
"""Process the user's query with Groq based on the scraped data."""
|
|
|
|
|
|
|
|
|
|
| 44 |
try:
|
| 45 |
# Combine the scraped data into a single text block
|
| 46 |
combined_text = "\n".join([str(item) for sublist in data for item in sublist.values()])
|
|
@@ -63,13 +66,14 @@ def process_query_with_groq(query, data):
|
|
| 63 |
st.title("Web Scraping and Query Tool")
|
| 64 |
|
| 65 |
# Step 1: Scraping
|
|
|
|
| 66 |
website_url = st.text_input("Enter the URL to scrape:")
|
| 67 |
scrape_option = st.selectbox("Select what to scrape:", ['data', 'links'])
|
| 68 |
|
| 69 |
if st.button("Scrape Data"):
|
| 70 |
SCRAPED_DATA = scrape_web_data(website_url, scrape_option)
|
| 71 |
if SCRAPED_DATA:
|
| 72 |
-
st.
|
| 73 |
|
| 74 |
# Save data to a temporary CSV file
|
| 75 |
df = pd.DataFrame(SCRAPED_DATA)
|
|
@@ -84,12 +88,15 @@ if st.button("Scrape Data"):
|
|
| 84 |
mime="text/csv",
|
| 85 |
)
|
| 86 |
else:
|
| 87 |
-
st.
|
| 88 |
|
| 89 |
# Step 2: Querying
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
|
|
|
|
|
|
|
|
|
| 93 |
answer = process_query_with_groq(user_query, SCRAPED_DATA)
|
| 94 |
st.write("**Answer:**")
|
| 95 |
st.write(answer)
|
|
|
|
| 10 |
client = Groq(api_key=os.environ.get('GroqApi'))
|
| 11 |
|
| 12 |
# Global variable to store scraped data
|
| 13 |
+
SCRAPED_DATA = []
|
| 14 |
|
| 15 |
def scrape_web_data(url, scrape_option):
|
| 16 |
"""Scrape data from the given URL based on the scrape option."""
|
|
|
|
| 41 |
|
| 42 |
def process_query_with_groq(query, data):
|
| 43 |
"""Process the user's query with Groq based on the scraped data."""
|
| 44 |
+
if not data:
|
| 45 |
+
return "No data available to process. Please scrape data first."
|
| 46 |
+
|
| 47 |
try:
|
| 48 |
# Combine the scraped data into a single text block
|
| 49 |
combined_text = "\n".join([str(item) for sublist in data for item in sublist.values()])
|
|
|
|
| 66 |
st.title("Web Scraping and Query Tool")
|
| 67 |
|
| 68 |
# Step 1: Scraping
|
| 69 |
+
st.subheader("Step 1: Scrape Data")
|
| 70 |
website_url = st.text_input("Enter the URL to scrape:")
|
| 71 |
scrape_option = st.selectbox("Select what to scrape:", ['data', 'links'])
|
| 72 |
|
| 73 |
if st.button("Scrape Data"):
|
| 74 |
SCRAPED_DATA = scrape_web_data(website_url, scrape_option)
|
| 75 |
if SCRAPED_DATA:
|
| 76 |
+
st.success(f"Scraping completed. {len(SCRAPED_DATA)} items found.")
|
| 77 |
|
| 78 |
# Save data to a temporary CSV file
|
| 79 |
df = pd.DataFrame(SCRAPED_DATA)
|
|
|
|
| 88 |
mime="text/csv",
|
| 89 |
)
|
| 90 |
else:
|
| 91 |
+
st.warning("No data found. Please check the URL or scrape option.")
|
| 92 |
|
| 93 |
# Step 2: Querying
|
| 94 |
+
st.subheader("Step 2: Ask a Query")
|
| 95 |
+
user_query = st.text_input("Enter your query:")
|
| 96 |
+
if st.button("Get Answer"):
|
| 97 |
+
if user_query.strip() == "":
|
| 98 |
+
st.warning("Please enter a valid query.")
|
| 99 |
+
else:
|
| 100 |
answer = process_query_with_groq(user_query, SCRAPED_DATA)
|
| 101 |
st.write("**Answer:**")
|
| 102 |
st.write(answer)
|