import streamlit as st
import os
from utils.api_client import HuggingFaceClient
def check_authentication():
"""Handle user authentication with Hugging Face API"""
st.markdown(
"""
🤗 Hugging Face Model Manager
Manage your machine learning models and publish to the Hugging Face Model Hub
""",
unsafe_allow_html=True,
)
st.markdown(
"""
Welcome to Hugging Face Model Manager
This application allows you to:
- Create and manage model repositories
- Upload and publish models to Hugging Face Hub
- Update model metadata and documentation
- Organize your models with tags and descriptions
""",
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()