Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import os | |
| from utils.api_client import HuggingFaceClient | |
| def check_authentication(): | |
| """Handle user authentication with Hugging Face API""" | |
| st.markdown( | |
| """ | |
| <div style="text-align: center; margin-bottom: 30px;"> | |
| <h1>π€ Hugging Face Model Manager</h1> | |
| <p>Manage your machine learning models and publish to the Hugging Face Model Hub</p> | |
| </div> | |
| """, | |
| unsafe_allow_html=True, | |
| ) | |
| st.markdown( | |
| """ | |
| <div style="background-color: #F9FAFB; padding: 20px; border-radius: 10px; border: 1px solid #E5E7EB;"> | |
| <h3 style="margin-top: 0;">Welcome to Hugging Face Model Manager</h3> | |
| <p>This application allows you to:</p> | |
| <ul> | |
| <li>Create and manage model repositories</li> | |
| <li>Upload and publish models to Hugging Face Hub</li> | |
| <li>Update model metadata and documentation</li> | |
| <li>Organize your models with tags and descriptions</li> | |
| </ul> | |
| </div> | |
| """, | |
| unsafe_allow_html=True, | |
| ) | |
| with st.form("auth_form"): | |
| st.subheader("Login with Hugging Face API Token") | |
| # Info alert about creating a token | |
| st.info( | |
| """ | |
| To use this application, you need a Hugging Face API token. | |
| You can create one at: [https://huggingface.co/settings/tokens](https://huggingface.co/settings/tokens) | |
| Make sure to grant **write** access if you want to upload models. | |
| """ | |
| ) | |
| # Token input | |
| token = st.text_input("Enter your Hugging Face API token", type="password") | |
| # Get token from environment or Secrets if available and not provided | |
| if not token: | |
| if os.environ.get("HF_TOKEN"): | |
| token = os.environ.get("HF_TOKEN") | |
| st.success("Using API token from Secrets.") | |
| elif os.environ.get("HF_API_TOKEN"): | |
| token = os.environ.get("HF_API_TOKEN") | |
| st.success("Using API token from environment variables.") | |
| submitted = st.form_submit_button("Login", use_container_width=True) | |
| if submitted and token: | |
| # Authenticate with Hugging Face | |
| with st.spinner("Authenticating..."): | |
| client = HuggingFaceClient() | |
| success, user_info = client.authenticate(token) | |
| if success: | |
| st.session_state.authenticated = True | |
| st.session_state.api_token = token | |
| st.session_state.username = user_info.get("name", "User") | |
| st.session_state.client = client | |
| st.success( | |
| f"Successfully authenticated as {st.session_state.username}" | |
| ) | |
| st.rerun() | |
| else: | |
| st.error(f"Authentication failed: {user_info}") | |
| elif submitted: | |
| st.error("Please enter your Hugging Face API token") | |
| def logout(): | |
| """Log out the current user""" | |
| for key in list(st.session_state.keys()): | |
| del st.session_state[key] | |
| st.session_state.authenticated = False | |
| st.session_state.page = "home" | |
| st.rerun() |