|
|
import os |
|
|
import streamlit as st |
|
|
import pandas as pd |
|
|
import requests |
|
|
import time |
|
|
|
|
|
|
|
|
API_KEY = os.getenv("SERPAPI_API_KEY", "") |
|
|
|
|
|
|
|
|
if not API_KEY: |
|
|
raise ValueError("API key is missing. Please set the SERPAPI_API_KEY environment variable.") |
|
|
|
|
|
|
|
|
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", |
|
|
"num": 5 |
|
|
} |
|
|
|
|
|
print(f"Searching: {query}") |
|
|
try: |
|
|
|
|
|
response = requests.get("https://serpapi.com/search", params=params) |
|
|
response.raise_for_status() |
|
|
|
|
|
|
|
|
data = response.json() |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
return pd.DataFrame(results) |
|
|
|
|
|
|
|
|
st.set_page_config(page_title="Virgin Media Collaboration Finder", layout="centered") |
|
|
|
|
|
|
|
|
st.title("π΅οΈ Virgin Media - Competitor Collaboration Finder") |
|
|
st.markdown("Use this tool to find which competitors have worked with **Virgin Media**.") |
|
|
|
|
|
|
|
|
csv_url = "https://huggingface.co/spaces/Satyam0077/anapan_ai_Competitor_Task/resolve/main/competitors.csv" |
|
|
|
|
|
|
|
|
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() |
|
|
|
|
|
|
|
|
if st.button("π Run Collaboration Search"): |
|
|
with st.spinner("Searching... This may take a few moments..."): |
|
|
result_df = get_collaboration_data(competitors) |
|
|
if not result_df.empty: |
|
|
|
|
|
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.") |
|
|
|
|
|
|
|
|
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.") |
|
|
|