Update app.py
Browse files
app.py
CHANGED
|
@@ -1,51 +1,53 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
import
|
| 4 |
-
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
| 11 |
base_path = os.path.dirname(os.path.abspath(__file__))
|
| 12 |
data_path = os.path.join(base_path, "data")
|
| 13 |
results_path = os.path.join(base_path, "results")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
os.makedirs(data_path, exist_ok=True)
|
| 17 |
os.makedirs(results_path, exist_ok=True)
|
| 18 |
|
| 19 |
-
#
|
| 20 |
csv_path = os.path.join(data_path, "competitors.csv")
|
| 21 |
|
| 22 |
try:
|
| 23 |
competitors_df = pd.read_csv(csv_path)
|
| 24 |
competitors = competitors_df['Company'].dropna().tolist()
|
| 25 |
except FileNotFoundError:
|
| 26 |
-
st.error("β File not found:
|
| 27 |
st.stop()
|
| 28 |
except pd.errors.EmptyDataError:
|
| 29 |
-
st.error("β The competitors.csv file is empty
|
| 30 |
st.stop()
|
| 31 |
|
| 32 |
-
#
|
| 33 |
if st.button("π Run Collaboration Search"):
|
| 34 |
with st.spinner("Searching... This may take a few moments..."):
|
| 35 |
result_df = get_collaboration_data(competitors)
|
| 36 |
if not result_df.empty:
|
| 37 |
-
|
| 38 |
-
result_df.to_csv(
|
| 39 |
st.success("β
Search Completed!")
|
| 40 |
st.dataframe(result_df)
|
| 41 |
else:
|
| 42 |
st.warning("β οΈ No collaboration data found. Nothing was saved.")
|
| 43 |
|
| 44 |
-
#
|
| 45 |
if st.button("π Show Saved Results"):
|
| 46 |
-
result_file_path = os.path.join(results_path, "virgin_collab_results.csv")
|
| 47 |
try:
|
| 48 |
-
saved_df = pd.read_csv(
|
| 49 |
if saved_df.empty:
|
| 50 |
st.warning("β οΈ The saved file is empty. Try running the search again.")
|
| 51 |
else:
|
|
|
|
| 1 |
+
import os
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
| 4 |
+
from utils.search import get_collaboration_data # Ensure the search.py is correctly placed in utils folder
|
| 5 |
+
|
| 6 |
+
# Set the page configuration
|
| 7 |
+
st.set_page_config(page_title="Virgin Media Collaboration Finder", layout="centered")
|
| 8 |
|
| 9 |
+
# Page title
|
| 10 |
+
st.title("π΅οΈ Virgin Media - Competitor Collaboration Finder")
|
| 11 |
+
st.markdown("Use this tool to find which competitors have worked with **Virgin Media**.")
|
| 12 |
|
| 13 |
+
# Ensure paths are handled properly, especially on Hugging Face or local environments
|
| 14 |
base_path = os.path.dirname(os.path.abspath(__file__))
|
| 15 |
data_path = os.path.join(base_path, "data")
|
| 16 |
results_path = os.path.join(base_path, "results")
|
| 17 |
|
| 18 |
+
# Ensure the required directories exist (on Hugging Face Spaces, directories like "data" or "results" may not exist)
|
| 19 |
os.makedirs(data_path, exist_ok=True)
|
| 20 |
os.makedirs(results_path, exist_ok=True)
|
| 21 |
|
| 22 |
+
# Load competitor list using absolute path
|
| 23 |
csv_path = os.path.join(data_path, "competitors.csv")
|
| 24 |
|
| 25 |
try:
|
| 26 |
competitors_df = pd.read_csv(csv_path)
|
| 27 |
competitors = competitors_df['Company'].dropna().tolist()
|
| 28 |
except FileNotFoundError:
|
| 29 |
+
st.error(f"β File not found: {csv_path}") # Show the correct path
|
| 30 |
st.stop()
|
| 31 |
except pd.errors.EmptyDataError:
|
| 32 |
+
st.error(f"β The competitors.csv file is empty: {csv_path}")
|
| 33 |
st.stop()
|
| 34 |
|
| 35 |
+
# Run search when button is clicked
|
| 36 |
if st.button("π Run Collaboration Search"):
|
| 37 |
with st.spinner("Searching... This may take a few moments..."):
|
| 38 |
result_df = get_collaboration_data(competitors)
|
| 39 |
if not result_df.empty:
|
| 40 |
+
result_csv_path = os.path.join(results_path, "virgin_collab_results.csv")
|
| 41 |
+
result_df.to_csv(result_csv_path, index=False)
|
| 42 |
st.success("β
Search Completed!")
|
| 43 |
st.dataframe(result_df)
|
| 44 |
else:
|
| 45 |
st.warning("β οΈ No collaboration data found. Nothing was saved.")
|
| 46 |
|
| 47 |
+
# Show saved results when button is clicked
|
| 48 |
if st.button("π Show Saved Results"):
|
|
|
|
| 49 |
try:
|
| 50 |
+
saved_df = pd.read_csv(os.path.join(results_path, "virgin_collab_results.csv"))
|
| 51 |
if saved_df.empty:
|
| 52 |
st.warning("β οΈ The saved file is empty. Try running the search again.")
|
| 53 |
else:
|