import streamlit as st import pandas as pd from datetime import datetime import os from huggingface_hub import HfApi, upload_file, list_repo_files, hf_hub_download # Configuration for Hugging Face Repository REPO_ID = "https://huggingface.co/spaces/MarcosRodrigo/Breakfast-Poll" # Replace with your repository ID HISTORY_DIR = "history" # Directory within the repository to store history # Hugging Face API (requires a token with write access) hf_token = st.secrets["HF_TOKEN"] # Store your token in Streamlit secrets api = HfApi() # Initialize session state for temporary and historical storage if "users" not in st.session_state: st.session_state.users = [] if "current_selections" not in st.session_state: st.session_state.current_selections = [] # Load history from the repository def load_history(): history = [] files_in_repo = list_repo_files(REPO_ID, token=hf_token) history_files = [f for f in files_in_repo if f.startswith(f"{HISTORY_DIR}/") and f.endswith(".txt")] for file in history_files: local_filepath = hf_hub_download(repo_id=REPO_ID, filename=file, token=hf_token) summary_df = pd.read_csv(local_filepath) date = file.split("/")[-1].split(".txt")[0] history.append({"Date": date, "Summary": summary_df}) return history # Save the current summary to a text file and upload to the repository def save_summary_to_file(summary_df): timestamp = datetime.now().strftime("%Y-%m-%d_%H-%M-%S") filename = f"{HISTORY_DIR}/{timestamp}.txt" # Save the DataFrame to a local CSV file local_filepath = f"{timestamp}.txt" summary_df.to_csv(local_filepath, index=False) # Upload the file to the repository upload_file(path_or_fileobj=local_filepath, path_in_repo=filename, repo_id=REPO_ID, token=hf_token) # Load history into session state if "history" not in st.session_state: st.session_state.history = load_history() # Sidebar for navigating through different views menu = st.sidebar.selectbox("Select View", ["Poll", "History"]) # Function to reset the current selections after submission def reset_selections(): st.session_state.users = [] st.session_state.current_selections = [] # Poll view with four consecutive steps if menu == "Poll": st.title("Breakfast Poll Application") # Step 1: User's Name if "step" not in st.session_state: st.session_state.step = 1 if st.session_state.step == 1: st.header("Step 1: Enter your name") name = st.text_input("Name:") if st.button("Next", key="step1_next") and name: # Unique key for the button st.session_state.users.append(name) st.session_state.step = 2 # Step 2: Select Drinks if st.session_state.step == 2: st.header("Step 2: Select your drink(s)") drinks_options = [ "Café con leche", "Colacao", "Descafeinado con leche", "Cortado", "Aguasusia", "Aguasusia susia", "Café descafeinado con leche desnatada", "Italiano", "Café con soja", "Té", "Manzanilla", "Nada" ] selected_drinks = st.multiselect("Choose your drinks:", drinks_options) if st.button("Next", key="step2_next") and selected_drinks: # Unique key for the button st.session_state.current_selections.append({"Name": st.session_state.users[-1], "Drinks": selected_drinks}) st.session_state.step = 3 # Step 3: Select Food if st.session_state.step == 3: st.header("Step 3: Select your food(s)") food_options = [ "Barrita con aceite", "Barrita con tomate", "Palmera de chocolate", "Palmera de chocolate blanco", "Yogurt", "Pincho de tortilla", "Nada" ] selected_food = st.multiselect("Choose your food:", food_options) if st.button("Next", key="step3_next") and selected_food: # Unique key for the button st.session_state.current_selections[-1]["Food"] = selected_food st.session_state.step = 4 # Step 4: Display Summary if st.session_state.step == 4: st.header("Step 4: Summary of Selections") if st.session_state.current_selections: df = pd.DataFrame(st.session_state.current_selections) st.table(df) if st.button("Submit Summary", key="submit_summary"): # Unique key for the button # Save the current summary to a text file in the repository save_summary_to_file(df) # Add to session state history timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S") st.session_state.history.append({"Date": timestamp, "Summary": df}) st.success(f"Summary submitted at {timestamp}") reset_selections() st.session_state.step = 1 # History view to check past summaries elif menu == "History": st.title("Breakfast Poll History") if st.session_state.history: for record in st.session_state.history: st.subheader(f"Date: {record['Date']}") st.table(record["Summary"]) else: st.write("No history records found.")