Satyam0077 commited on
Commit
ddafd74
Β·
verified Β·
1 Parent(s): 8e022d8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -1,51 +1,53 @@
 
1
  import streamlit as st
2
  import pandas as pd
3
- import os
4
- import sys
 
 
5
 
6
- # βœ… Fix Python path for Hugging Face + VS Code (handling imports correctly)
7
- sys.path.append(os.path.join(os.path.dirname(__file__), 'utils')) # Ensure the utils directory is found
8
- from search import get_collaboration_data # Adjusted import for search.py from utils
9
 
10
- # βœ… Hugging Face File Paths
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
- # βœ… Ensure the required directories exist (on Hugging Face Spaces, directories like "data" or "results" may not exist)
16
  os.makedirs(data_path, exist_ok=True)
17
  os.makedirs(results_path, exist_ok=True)
18
 
19
- # βœ… Load competitor list using absolute path
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: data/competitors.csv")
27
  st.stop()
28
  except pd.errors.EmptyDataError:
29
- st.error("❌ The competitors.csv file is empty.")
30
  st.stop()
31
 
32
- # βœ… Run search
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
- result_file_path = os.path.join(results_path, "virgin_collab_results.csv")
38
- result_df.to_csv(result_file_path, index=False)
39
  st.success("βœ… Search Completed!")
40
  st.dataframe(result_df)
41
  else:
42
  st.warning("⚠️ No collaboration data found. Nothing was saved.")
43
 
44
- # βœ… Show saved results
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(result_file_path)
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: