MarcosRodrigo commited on
Commit
cb21807
1 Parent(s): ac33dda

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -30
app.py CHANGED
@@ -2,24 +2,15 @@ import streamlit as st
2
  import pandas as pd
3
  from datetime import datetime
4
  import os
5
- from huggingface_hub import HfApi, Repository
6
 
7
  # Configuration for Hugging Face Repository
8
- REPO_ID = "your-huggingface-username/your-repo-name" # Replace with your repository ID
9
- LOCAL_DIR = "repo" # Local directory to sync with the repo
10
- history_dir = os.path.join(LOCAL_DIR, "history")
11
 
12
- # Hugging Face Authentication (requires a token with write access)
13
  hf_token = st.secrets["HF_TOKEN"] # Store your token in Streamlit secrets
14
-
15
- # Initialize the Hugging Face repository
16
- repo = Repository(local_dir=LOCAL_DIR, clone_from=REPO_ID, use_auth_token=hf_token)
17
-
18
- # Function to commit changes to the Hugging Face repository
19
- def commit_changes(message="Updating history"):
20
- repo.git_add(pattern="history/*")
21
- repo.git_commit(message=message)
22
- repo.git_push()
23
 
24
  # Initialize session state for temporary and historical storage
25
  if "users" not in st.session_state:
@@ -27,28 +18,30 @@ if "users" not in st.session_state:
27
  if "current_selections" not in st.session_state:
28
  st.session_state.current_selections = []
29
 
30
- # Load history from existing text files in the repository
31
  def load_history():
32
  history = []
33
- repo.git_pull() # Pull the latest changes from the repo
34
- if not os.path.exists(history_dir):
35
- os.makedirs(history_dir)
36
- for filename in os.listdir(history_dir):
37
- if filename.endswith(".txt"):
38
- date = filename.split(".txt")[0]
39
- filepath = os.path.join(history_dir, filename)
40
- summary_df = pd.read_csv(filepath)
41
- history.append({"Date": date, "Summary": summary_df})
42
  return history
43
 
44
- # Save current summary to a text file in the repository
45
  def save_summary_to_file(summary_df):
46
  timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
47
- if not os.path.exists(history_dir):
48
- os.makedirs(history_dir)
49
- filepath = os.path.join(history_dir, f"{timestamp}.txt")
50
- summary_df.to_csv(filepath, index=False)
51
- commit_changes(message=f"Added summary for {timestamp}")
 
 
 
52
 
53
  # Load history into session state
54
  if "history" not in st.session_state:
 
2
  import pandas as pd
3
  from datetime import datetime
4
  import os
5
+ from huggingface_hub import HfApi, upload_file, list_repo_files, hf_hub_download
6
 
7
  # Configuration for Hugging Face Repository
8
+ REPO_ID = "https://huggingface.co/spaces/MarcosRodrigo/Breakfast-Poll" # Replace with your repository ID
9
+ HISTORY_DIR = "history" # Directory within the repository to store history
 
10
 
11
+ # Hugging Face API (requires a token with write access)
12
  hf_token = st.secrets["HF_TOKEN"] # Store your token in Streamlit secrets
13
+ api = HfApi()
 
 
 
 
 
 
 
 
14
 
15
  # Initialize session state for temporary and historical storage
16
  if "users" not in st.session_state:
 
18
  if "current_selections" not in st.session_state:
19
  st.session_state.current_selections = []
20
 
21
+ # Load history from the repository
22
  def load_history():
23
  history = []
24
+ files_in_repo = list_repo_files(REPO_ID, token=hf_token)
25
+ history_files = [f for f in files_in_repo if f.startswith(f"{HISTORY_DIR}/") and f.endswith(".txt")]
26
+
27
+ for file in history_files:
28
+ local_filepath = hf_hub_download(repo_id=REPO_ID, filename=file, token=hf_token)
29
+ summary_df = pd.read_csv(local_filepath)
30
+ date = file.split("/")[-1].split(".txt")[0]
31
+ history.append({"Date": date, "Summary": summary_df})
 
32
  return history
33
 
34
+ # Save the current summary to a text file and upload to the repository
35
  def save_summary_to_file(summary_df):
36
  timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
37
+ filename = f"{HISTORY_DIR}/{timestamp}.txt"
38
+
39
+ # Save the DataFrame to a local CSV file
40
+ local_filepath = f"{timestamp}.txt"
41
+ summary_df.to_csv(local_filepath, index=False)
42
+
43
+ # Upload the file to the repository
44
+ upload_file(path_or_fileobj=local_filepath, path_in_repo=filename, repo_id=REPO_ID, token=hf_token)
45
 
46
  # Load history into session state
47
  if "history" not in st.session_state: