Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,24 @@ import streamlit as st
|
|
| 2 |
import pandas as pd
|
| 3 |
from datetime import datetime
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Initialize session state for temporary and historical storage
|
| 7 |
if "users" not in st.session_state:
|
|
@@ -9,29 +27,28 @@ if "users" not in st.session_state:
|
|
| 9 |
if "current_selections" not in st.session_state:
|
| 10 |
st.session_state.current_selections = []
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
history_dir = "history"
|
| 14 |
-
if not os.path.exists(history_dir):
|
| 15 |
-
os.makedirs(history_dir)
|
| 16 |
-
|
| 17 |
-
# Load history from existing text files
|
| 18 |
def load_history():
|
| 19 |
history = []
|
|
|
|
|
|
|
|
|
|
| 20 |
for filename in os.listdir(history_dir):
|
| 21 |
if filename.endswith(".txt"):
|
| 22 |
date = filename.split(".txt")[0]
|
| 23 |
filepath = os.path.join(history_dir, filename)
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
summary_df = pd.read_csv(filepath)
|
| 27 |
-
history.append({"Date": date, "Summary": summary_df})
|
| 28 |
return history
|
| 29 |
|
| 30 |
-
# Save current summary to a text file
|
| 31 |
def save_summary_to_file(summary_df):
|
| 32 |
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
|
|
|
|
|
|
| 33 |
filepath = os.path.join(history_dir, f"{timestamp}.txt")
|
| 34 |
summary_df.to_csv(filepath, index=False)
|
|
|
|
| 35 |
|
| 36 |
# Load history into session state
|
| 37 |
if "history" not in st.session_state:
|
|
@@ -93,7 +110,7 @@ if menu == "Poll":
|
|
| 93 |
st.table(df)
|
| 94 |
|
| 95 |
if st.button("Submit Summary", key="submit_summary"): # Unique key for the button
|
| 96 |
-
# Save the current summary to a text file in the
|
| 97 |
save_summary_to_file(df)
|
| 98 |
|
| 99 |
# Add to session state history
|
|
|
|
| 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 |
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:
|
|
|
|
| 110 |
st.table(df)
|
| 111 |
|
| 112 |
if st.button("Submit Summary", key="submit_summary"): # Unique key for the button
|
| 113 |
+
# Save the current summary to a text file in the repository
|
| 114 |
save_summary_to_file(df)
|
| 115 |
|
| 116 |
# Add to session state history
|