Satyam0077's picture
Update app.py
ba6e74f verified
import os
import streamlit as st
import pandas as pd
import requests
import time
# βœ… Safely get the API key from the environment or fallback to empty string
API_KEY = os.getenv("SERPAPI_API_KEY", "")
# βœ… Ensure API_KEY is available
if not API_KEY:
raise ValueError("API key is missing. Please set the SERPAPI_API_KEY environment variable.")
# Function to get collaboration data using SERPAPI
def get_collaboration_data(competitors, target="Virgin Media"):
results = []
for comp in competitors:
query = f"{comp} {target} partnership OR collaboration"
params = {
"q": query,
"api_key": API_KEY,
"engine": "google", # You can switch to other engines if needed
"num": 5 # Number of results to return per query
}
print(f"Searching: {query}") # Log the query for tracking
try:
# Make a request to the SERP API
response = requests.get("https://serpapi.com/search", params=params)
response.raise_for_status() # Check for request errors (e.g., 4xx, 5xx)
# Parse the JSON response
data = response.json()
# Extract the organic results
for item in data.get("organic_results", []):
results.append({
"Competitor": comp,
"Title": item.get("title", "No title available"),
"Link": item.get("link", "No link available"),
"Snippet": item.get("snippet", "No snippet available")
})
except requests.exceptions.RequestException as e:
print(f"Error fetching data for {comp}: {e}")
except ValueError as e:
print(f"Error parsing JSON response for {comp}: {e}")
time.sleep(1) # βœ… Respect API rate limits
# Return results as a DataFrame
return pd.DataFrame(results)
# Set the page configuration
st.set_page_config(page_title="Virgin Media Collaboration Finder", layout="centered")
# Page title
st.title("πŸ•΅οΈ Virgin Media - Competitor Collaboration Finder")
st.markdown("Use this tool to find which competitors have worked with **Virgin Media**.")
# URL to the competitors CSV on Hugging Face
csv_url = "https://huggingface.co/spaces/Satyam0077/anapan_ai_Competitor_Task/resolve/main/competitors.csv"
# Load competitor list from Hugging Face URL
try:
competitors_df = pd.read_csv(csv_url)
competitors = competitors_df['Company'].dropna().tolist()
except Exception as e:
st.error(f"❌ Failed to load the CSV: {e}")
st.stop()
# Run search when button is clicked
if st.button("πŸ” Run Collaboration Search"):
with st.spinner("Searching... This may take a few moments..."):
result_df = get_collaboration_data(competitors) # This will now work as the function is defined
if not result_df.empty:
# Save results to the "results" folder
os.makedirs("results", exist_ok=True)
result_csv_path = os.path.join("results", "virgin_collab_results.csv")
result_df.to_csv(result_csv_path, index=False)
st.success("βœ… Search Completed!")
st.dataframe(result_df)
else:
st.warning("⚠️ No collaboration data found. Nothing was saved.")
# Show saved results when button is clicked
if st.button("πŸ“‚ Show Saved Results"):
try:
saved_df = pd.read_csv("results/virgin_collab_results.csv")
if saved_df.empty:
st.warning("⚠️ The saved file is empty. Try running the search again.")
else:
st.success("βœ… Loaded saved results.")
st.dataframe(saved_df)
except FileNotFoundError:
st.warning("⚠️ No saved results found. Please run the search first.")
except pd.errors.EmptyDataError:
st.warning("⚠️ The saved file exists but is empty or invalid.")