Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -32,18 +32,15 @@ def load_current_selections():
|
|
| 32 |
|
| 33 |
# Save current user selections to the shared CSV file without overwriting previous data
|
| 34 |
def save_current_selection_to_file(current_selections):
|
| 35 |
-
# Convert list columns to strings to avoid unhashable type errors
|
| 36 |
current_selections["Drinks"] = current_selections["Drinks"].apply(lambda x: ", ".join(x) if isinstance(x, list) else x)
|
| 37 |
current_selections["Food"] = current_selections["Food"].apply(lambda x: ", ".join(x) if isinstance(x, list) else x)
|
| 38 |
|
| 39 |
-
# Read the existing file to avoid overwriting
|
| 40 |
if os.path.exists(TEMP_FILE):
|
| 41 |
existing_selections = pd.read_csv(TEMP_FILE)
|
| 42 |
combined_selections = pd.concat([existing_selections, current_selections]).drop_duplicates()
|
| 43 |
else:
|
| 44 |
combined_selections = current_selections
|
| 45 |
|
| 46 |
-
# Save the updated DataFrame to the CSV file
|
| 47 |
combined_selections.to_csv(TEMP_FILE, index=False)
|
| 48 |
|
| 49 |
# Upload the shared file to Hugging Face repository for persistence
|
|
@@ -57,6 +54,15 @@ def upload_temp_file_to_repo():
|
|
| 57 |
repo_type="space"
|
| 58 |
)
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
# Download the shared file from the repository to ensure persistence and real-time updates
|
| 61 |
def download_temp_file_from_repo():
|
| 62 |
try:
|
|
@@ -82,16 +88,13 @@ def save_summary_to_history():
|
|
| 82 |
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 83 |
history_filename = f"{HISTORY_DIR}/{timestamp}.txt"
|
| 84 |
|
| 85 |
-
# Ensure the history directory exists
|
| 86 |
if not os.path.exists(HISTORY_DIR):
|
| 87 |
os.makedirs(HISTORY_DIR)
|
| 88 |
|
| 89 |
-
# Read the current selections file
|
| 90 |
if os.path.exists(TEMP_FILE):
|
| 91 |
summary_df = pd.read_csv(TEMP_FILE)
|
| 92 |
summary_df.to_csv(history_filename, index=False)
|
| 93 |
|
| 94 |
-
# Upload the file to the repository
|
| 95 |
upload_file(path_or_fileobj=history_filename, path_in_repo=history_filename, repo_id=REPO_ID, token=hf_token, repo_type="space")
|
| 96 |
return timestamp
|
| 97 |
|
|
@@ -120,7 +123,6 @@ if menu == "Poll":
|
|
| 120 |
st.session_state.users.append(name)
|
| 121 |
st.session_state.step = 2
|
| 122 |
|
| 123 |
-
# Step 2: Select Drinks
|
| 124 |
if st.session_state.step == 2:
|
| 125 |
st.header("Step 2: Select your drink(s)")
|
| 126 |
drinks_options = [
|
|
@@ -133,7 +135,6 @@ if menu == "Poll":
|
|
| 133 |
st.session_state.current_selections.append({"Name": st.session_state.users[-1], "Drinks": selected_drinks})
|
| 134 |
st.session_state.step = 3
|
| 135 |
|
| 136 |
-
# Step 3: Select Food and Save Selections
|
| 137 |
if st.session_state.step == 3:
|
| 138 |
st.header("Step 3: Select your food(s)")
|
| 139 |
food_options = [
|
|
@@ -158,33 +159,26 @@ elif menu == "Current":
|
|
| 158 |
st.table(current_df)
|
| 159 |
|
| 160 |
if st.button("Submit Summary to History"):
|
| 161 |
-
# Save the current selections to history
|
| 162 |
timestamp = save_summary_to_history()
|
| 163 |
st.success(f"Summary saved to history at {timestamp}")
|
| 164 |
-
|
| 165 |
-
# Reload history after saving to reflect the new entry
|
| 166 |
st.session_state.history = load_history()
|
| 167 |
-
|
| 168 |
-
#
|
| 169 |
if os.path.exists(TEMP_FILE):
|
| 170 |
-
os.remove(TEMP_FILE)
|
| 171 |
-
#
|
| 172 |
-
st.session_state.current_selections = []
|
| 173 |
|
| 174 |
# Create an empty CSV to replace the deleted one
|
| 175 |
pd.DataFrame(columns=["Name", "Drinks", "Food"]).to_csv(TEMP_FILE, index=False)
|
| 176 |
-
# Upload the empty file to the repository to ensure shared state is reset
|
| 177 |
upload_temp_file_to_repo()
|
| 178 |
-
|
| 179 |
-
# Refresh the view to show the empty table
|
| 180 |
-
st.experimental_set_query_params(step="reset")
|
| 181 |
|
|
|
|
| 182 |
|
| 183 |
# History view to check past summaries
|
| 184 |
elif menu == "History":
|
| 185 |
st.title("Breakfast Poll History")
|
| 186 |
if not st.session_state.history:
|
| 187 |
-
st.session_state.history = load_history()
|
| 188 |
if st.session_state.history:
|
| 189 |
for record in st.session_state.history:
|
| 190 |
st.subheader(f"Date: {record['Date']}")
|
|
|
|
| 32 |
|
| 33 |
# Save current user selections to the shared CSV file without overwriting previous data
|
| 34 |
def save_current_selection_to_file(current_selections):
|
|
|
|
| 35 |
current_selections["Drinks"] = current_selections["Drinks"].apply(lambda x: ", ".join(x) if isinstance(x, list) else x)
|
| 36 |
current_selections["Food"] = current_selections["Food"].apply(lambda x: ", ".join(x) if isinstance(x, list) else x)
|
| 37 |
|
|
|
|
| 38 |
if os.path.exists(TEMP_FILE):
|
| 39 |
existing_selections = pd.read_csv(TEMP_FILE)
|
| 40 |
combined_selections = pd.concat([existing_selections, current_selections]).drop_duplicates()
|
| 41 |
else:
|
| 42 |
combined_selections = current_selections
|
| 43 |
|
|
|
|
| 44 |
combined_selections.to_csv(TEMP_FILE, index=False)
|
| 45 |
|
| 46 |
# Upload the shared file to Hugging Face repository for persistence
|
|
|
|
| 54 |
repo_type="space"
|
| 55 |
)
|
| 56 |
|
| 57 |
+
# Delete a file from the repository (e.g., `current_selections.csv`)
|
| 58 |
+
def delete_file_from_repo(filename):
|
| 59 |
+
api.delete_file(
|
| 60 |
+
path_in_repo=filename,
|
| 61 |
+
repo_id=REPO_ID,
|
| 62 |
+
token=hf_token,
|
| 63 |
+
repo_type="space"
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
# Download the shared file from the repository to ensure persistence and real-time updates
|
| 67 |
def download_temp_file_from_repo():
|
| 68 |
try:
|
|
|
|
| 88 |
timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
|
| 89 |
history_filename = f"{HISTORY_DIR}/{timestamp}.txt"
|
| 90 |
|
|
|
|
| 91 |
if not os.path.exists(HISTORY_DIR):
|
| 92 |
os.makedirs(HISTORY_DIR)
|
| 93 |
|
|
|
|
| 94 |
if os.path.exists(TEMP_FILE):
|
| 95 |
summary_df = pd.read_csv(TEMP_FILE)
|
| 96 |
summary_df.to_csv(history_filename, index=False)
|
| 97 |
|
|
|
|
| 98 |
upload_file(path_or_fileobj=history_filename, path_in_repo=history_filename, repo_id=REPO_ID, token=hf_token, repo_type="space")
|
| 99 |
return timestamp
|
| 100 |
|
|
|
|
| 123 |
st.session_state.users.append(name)
|
| 124 |
st.session_state.step = 2
|
| 125 |
|
|
|
|
| 126 |
if st.session_state.step == 2:
|
| 127 |
st.header("Step 2: Select your drink(s)")
|
| 128 |
drinks_options = [
|
|
|
|
| 135 |
st.session_state.current_selections.append({"Name": st.session_state.users[-1], "Drinks": selected_drinks})
|
| 136 |
st.session_state.step = 3
|
| 137 |
|
|
|
|
| 138 |
if st.session_state.step == 3:
|
| 139 |
st.header("Step 3: Select your food(s)")
|
| 140 |
food_options = [
|
|
|
|
| 159 |
st.table(current_df)
|
| 160 |
|
| 161 |
if st.button("Submit Summary to History"):
|
|
|
|
| 162 |
timestamp = save_summary_to_history()
|
| 163 |
st.success(f"Summary saved to history at {timestamp}")
|
|
|
|
|
|
|
| 164 |
st.session_state.history = load_history()
|
| 165 |
+
|
| 166 |
+
# Clear local and remote current selections
|
| 167 |
if os.path.exists(TEMP_FILE):
|
| 168 |
+
os.remove(TEMP_FILE)
|
| 169 |
+
delete_file_from_repo(TEMP_FILE) # Delete the file from the remote repo
|
|
|
|
| 170 |
|
| 171 |
# Create an empty CSV to replace the deleted one
|
| 172 |
pd.DataFrame(columns=["Name", "Drinks", "Food"]).to_csv(TEMP_FILE, index=False)
|
|
|
|
| 173 |
upload_temp_file_to_repo()
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
+
st.experimental_set_query_params(step="reset")
|
| 176 |
|
| 177 |
# History view to check past summaries
|
| 178 |
elif menu == "History":
|
| 179 |
st.title("Breakfast Poll History")
|
| 180 |
if not st.session_state.history:
|
| 181 |
+
st.session_state.history = load_history()
|
| 182 |
if st.session_state.history:
|
| 183 |
for record in st.session_state.history:
|
| 184 |
st.subheader(f"Date: {record['Date']}")
|