trending-board / app.py
openfree's picture
Update app.py
dffb689 verified
raw
history blame
1.03 kB
import os
import requests
import gradio as gr
# Hugging Face API URL
HF_API_URL = "https://huggingface.co/api/spaces"
# Hugging Face Token
HF_TOKEN = os.getenv("HF_TOKEN")
# Function to get all Spaces
def get_all_spaces():
if not HF_TOKEN:
return "Error: Hugging Face token not found."
headers = {"Authorization": f"Bearer {HF_TOKEN}"}
response = requests.get(HF_API_URL, headers=headers)
if response.status_code != 200:
return f"Error: Failed to fetch spaces (Status Code: {response.status_code})"
spaces = response.json()
if not spaces:
return "No Spaces found."
# Formatting Spaces into a grid, handling missing keys with .get()
space_info = [
f"Name: {space.get('name', 'N/A')}\nSDK: {space.get('sdk', 'N/A')}\nStatus: {space.get('status', 'N/A')}"
for space in spaces
]
return "\n\n".join(space_info)
# Creating the Gradio interface
app = gr.Interface(fn=get_all_spaces, inputs=None, outputs="text")
# Launch the Gradio app
app.launch()