Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,23 +5,23 @@ 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 = "MarcosRodrigo/Breakfast-Poll"
|
9 |
-
HISTORY_DIR = "history"
|
10 |
-
TEMP_FILE = "current_selections.csv"
|
11 |
|
12 |
# Hugging Face API (requires a token with write access)
|
13 |
-
hf_token = st.secrets["HF_TOKEN"]
|
14 |
api = HfApi()
|
15 |
|
16 |
# Initialize all required session state variables
|
17 |
if "users" not in st.session_state:
|
18 |
-
st.session_state.users = []
|
19 |
if "current_selections" not in st.session_state:
|
20 |
-
st.session_state.current_selections = []
|
21 |
if "step" not in st.session_state:
|
22 |
-
st.session_state.step = 1
|
23 |
if "history" not in st.session_state:
|
24 |
-
st.session_state.history = []
|
25 |
|
26 |
# Load temporary selections from the shared file
|
27 |
def load_current_selections():
|
@@ -45,12 +45,11 @@ def upload_temp_file_to_repo():
|
|
45 |
repo_type="space"
|
46 |
)
|
47 |
|
48 |
-
# Download the shared file from the repository to ensure persistence
|
49 |
def download_temp_file_from_repo():
|
50 |
try:
|
51 |
-
hf_hub_download(repo_id=REPO_ID, filename=TEMP_FILE, repo_type="space", token=hf_token)
|
52 |
except Exception:
|
53 |
-
# If the file does not exist in the repo, create an empty file
|
54 |
pd.DataFrame(columns=["Name", "Drinks", "Food"]).to_csv(TEMP_FILE, index=False)
|
55 |
|
56 |
# Load history from the repository
|
@@ -70,22 +69,18 @@ def load_history():
|
|
70 |
def save_summary_to_file(summary_df):
|
71 |
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
72 |
filename = f"{HISTORY_DIR}/{timestamp}.txt"
|
73 |
-
|
74 |
-
# Save the DataFrame to a local CSV file
|
75 |
local_filepath = f"{timestamp}.txt"
|
76 |
summary_df.to_csv(local_filepath, index=False)
|
77 |
-
|
78 |
-
# Upload the file to the Space repository with `repo_type="space"`
|
79 |
upload_file(path_or_fileobj=local_filepath, path_in_repo=filename, repo_id=REPO_ID, token=hf_token, repo_type="space")
|
80 |
|
81 |
# Load persistent history and temporary selections on app start
|
82 |
if "history" not in st.session_state:
|
83 |
-
download_temp_file_from_repo()
|
84 |
st.session_state.history = load_history()
|
85 |
st.session_state.current_selections = load_current_selections().to_dict(orient="records")
|
86 |
|
87 |
# Sidebar for navigating through different views
|
88 |
-
menu = st.sidebar.selectbox("Select View", ["Poll", "History"])
|
89 |
|
90 |
# Function to reset the current selections after submission
|
91 |
def reset_selections():
|
@@ -96,10 +91,6 @@ def reset_selections():
|
|
96 |
if menu == "Poll":
|
97 |
st.title("Breakfast Poll Application")
|
98 |
|
99 |
-
# Step 1: User's Name
|
100 |
-
if "step" not in st.session_state:
|
101 |
-
st.session_state.step = 1
|
102 |
-
|
103 |
if st.session_state.step == 1:
|
104 |
st.header("Step 1: Enter your name")
|
105 |
name = st.text_input("Name:")
|
@@ -140,22 +131,22 @@ if menu == "Poll":
|
|
140 |
st.table(df)
|
141 |
|
142 |
if st.button("Submit Summary", key="submit_summary"):
|
143 |
-
# Save the current summary to a text file in the repository
|
144 |
save_summary_to_file(df)
|
145 |
-
|
146 |
-
# Save and upload current selections for real-time updates
|
147 |
-
save_current_selection_to_file(df)
|
148 |
upload_temp_file_to_repo()
|
149 |
-
|
150 |
-
# Add to session state history
|
151 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
152 |
st.session_state.history.append({"Date": timestamp, "Summary": df})
|
153 |
-
|
154 |
st.success(f"Summary submitted at {timestamp}")
|
155 |
-
reset_selections()
|
156 |
st.session_state.step = 1
|
157 |
st.experimental_rerun()
|
158 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
# History view to check past summaries
|
160 |
elif menu == "History":
|
161 |
st.title("Breakfast Poll History")
|
|
|
5 |
from huggingface_hub import HfApi, upload_file, list_repo_files, hf_hub_download
|
6 |
|
7 |
# Configuration for Hugging Face Repository
|
8 |
+
REPO_ID = "MarcosRodrigo/Breakfast-Poll"
|
9 |
+
HISTORY_DIR = "history"
|
10 |
+
TEMP_FILE = "current_selections.csv"
|
11 |
|
12 |
# Hugging Face API (requires a token with write access)
|
13 |
+
hf_token = st.secrets["HF_TOKEN"]
|
14 |
api = HfApi()
|
15 |
|
16 |
# Initialize all required session state variables
|
17 |
if "users" not in st.session_state:
|
18 |
+
st.session_state.users = []
|
19 |
if "current_selections" not in st.session_state:
|
20 |
+
st.session_state.current_selections = []
|
21 |
if "step" not in st.session_state:
|
22 |
+
st.session_state.step = 1
|
23 |
if "history" not in st.session_state:
|
24 |
+
st.session_state.history = []
|
25 |
|
26 |
# Load temporary selections from the shared file
|
27 |
def load_current_selections():
|
|
|
45 |
repo_type="space"
|
46 |
)
|
47 |
|
48 |
+
# Download the shared file from the repository to ensure persistence and real-time updates
|
49 |
def download_temp_file_from_repo():
|
50 |
try:
|
51 |
+
hf_hub_download(repo_id=REPO_ID, filename=TEMP_FILE, repo_type="space", token=hf_token, local_dir=".")
|
52 |
except Exception:
|
|
|
53 |
pd.DataFrame(columns=["Name", "Drinks", "Food"]).to_csv(TEMP_FILE, index=False)
|
54 |
|
55 |
# Load history from the repository
|
|
|
69 |
def save_summary_to_file(summary_df):
|
70 |
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
71 |
filename = f"{HISTORY_DIR}/{timestamp}.txt"
|
|
|
|
|
72 |
local_filepath = f"{timestamp}.txt"
|
73 |
summary_df.to_csv(local_filepath, index=False)
|
|
|
|
|
74 |
upload_file(path_or_fileobj=local_filepath, path_in_repo=filename, repo_id=REPO_ID, token=hf_token, repo_type="space")
|
75 |
|
76 |
# Load persistent history and temporary selections on app start
|
77 |
if "history" not in st.session_state:
|
78 |
+
download_temp_file_from_repo()
|
79 |
st.session_state.history = load_history()
|
80 |
st.session_state.current_selections = load_current_selections().to_dict(orient="records")
|
81 |
|
82 |
# Sidebar for navigating through different views
|
83 |
+
menu = st.sidebar.selectbox("Select View", ["Poll", "Current", "History"])
|
84 |
|
85 |
# Function to reset the current selections after submission
|
86 |
def reset_selections():
|
|
|
91 |
if menu == "Poll":
|
92 |
st.title("Breakfast Poll Application")
|
93 |
|
|
|
|
|
|
|
|
|
94 |
if st.session_state.step == 1:
|
95 |
st.header("Step 1: Enter your name")
|
96 |
name = st.text_input("Name:")
|
|
|
131 |
st.table(df)
|
132 |
|
133 |
if st.button("Submit Summary", key="submit_summary"):
|
|
|
134 |
save_summary_to_file(df)
|
135 |
+
save_current_selection_to_file(pd.DataFrame(st.session_state.current_selections))
|
|
|
|
|
136 |
upload_temp_file_to_repo()
|
|
|
|
|
137 |
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
138 |
st.session_state.history.append({"Date": timestamp, "Summary": df})
|
|
|
139 |
st.success(f"Summary submitted at {timestamp}")
|
|
|
140 |
st.session_state.step = 1
|
141 |
st.experimental_rerun()
|
142 |
|
143 |
+
# "Current" view to display the current summary of all users' selections
|
144 |
+
elif menu == "Current":
|
145 |
+
st.title("Current Selections of All Users")
|
146 |
+
download_temp_file_from_repo() # Ensure the latest file is downloaded
|
147 |
+
current_df = load_current_selections()
|
148 |
+
st.table(current_df)
|
149 |
+
|
150 |
# History view to check past summaries
|
151 |
elif menu == "History":
|
152 |
st.title("Breakfast Poll History")
|