Spaces:
Sleeping
Sleeping
import requests | |
import gradio as gr | |
from datetime import datetime | |
# ํ ์คํธ๋ฅผ ์ํด Gradio ๊ณต์ ๊ณ์ ์ผ๋ก ์ค์ | |
USERNAME = "openfree" | |
def format_timestamp(timestamp): | |
if timestamp: | |
dt = datetime.fromisoformat(timestamp.replace('Z', '+00:00')) | |
return dt.strftime('%Y-%m-%d %H:%M') | |
return 'N/A' | |
def get_space_card(space): | |
"""Generate HTML card for a space""" | |
return f""" | |
<div style='border: 1px solid #ddd; padding: 15px; margin: 10px; border-radius: 8px; | |
background-color: white; box-shadow: 2px 2px 5px rgba(0,0,0,0.1); | |
transition: transform 0.2s ease-in-out;' | |
onmouseover='this.style.transform="scale(1.02)"' | |
onmouseout='this.style.transform="scale(1)"'> | |
<h3 style='color: #2d2d2d; margin: 0 0 10px 0;'> | |
<a href='{space["url"]}' target='_blank' | |
style='text-decoration: none; color: #2d2d2d;'> | |
{space["title"]} | |
</a> | |
</h3> | |
<p style='margin: 5px 0;'>{space.get("description", "No description available")}</p> | |
<div style='margin-top: 10px; display: flex; justify-content: space-between; align-items: center;'> | |
<a href='{space["url"]}' target='_blank' | |
style='background-color: #0084ff; color: white; padding: 5px 10px; | |
border-radius: 5px; text-decoration: none;'> | |
View Space | |
</a> | |
</div> | |
</div> | |
""" | |
def get_user_spaces(): | |
# Hugging Face API v2 ์ฌ์ฉ | |
url = f"https://huggingface.co/api/spaces?search={USERNAME}" | |
headers = { | |
"Accept": "application/json", | |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" | |
} | |
try: | |
response = requests.get(url, headers=headers) | |
print(f"Status Code: {response.status_code}") # ๋๋ฒ๊น ์ฉ | |
print(f"Response: {response.text[:500]}...") # ๋๋ฒ๊น ์ฉ | |
if response.status_code != 200: | |
return f"Error: Failed to fetch spaces (Status Code: {response.status_code})" | |
spaces_data = response.json() | |
# ์ฌ์ฉ์์ spaces๋ง ํํฐ๋ง | |
user_spaces = [] | |
for space in spaces_data: | |
if space.get('author', {}).get('name', '').lower() == USERNAME.lower(): | |
space_info = { | |
"title": space.get('id', '').split('/')[-1], | |
"description": space.get('description', ''), | |
"url": f"https://huggingface.co/spaces/{space.get('id', '')}", | |
} | |
user_spaces.append(space_info) | |
if not user_spaces: | |
return f""" | |
<div style='padding: 20px; text-align: center; color: #666;'> | |
<h2>No public Spaces found for user: {USERNAME}</h2> | |
<p>Try visiting: <a href='https://huggingface.co/{USERNAME}' target='_blank'> | |
https://huggingface.co/{USERNAME}</a></p> | |
</div> | |
""" | |
# Create HTML grid layout | |
html_content = f""" | |
<div style='padding: 20px; background-color: #f5f5f5;'> | |
<div style='margin-bottom: 20px;'> | |
<p style='color: #666; margin: 0;'>Found {len(user_spaces)} public spaces for {USERNAME}</p> | |
</div> | |
<div style=' | |
display: grid; | |
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); | |
gap: 20px; | |
'> | |
{"".join(get_space_card(space) for space in user_spaces)} | |
</div> | |
</div> | |
""" | |
return html_content | |
except Exception as e: | |
print(f"Error: {str(e)}") # ๋๋ฒ๊น ์ฉ | |
return f""" | |
<div style='padding: 20px; text-align: center; color: #666;'> | |
<h2>Error occurred while fetching spaces</h2> | |
<p>Error details: {str(e)}</p> | |
<p>Please try again later or check if the username is correct.</p> | |
</div> | |
""" | |
# Creating the Gradio interface | |
app = gr.Interface( | |
fn=get_user_spaces, | |
inputs=None, | |
outputs=gr.HTML(), | |
title=f"Hugging Face Public Spaces - {USERNAME}", | |
description=f"Displays public Spaces from {USERNAME}", | |
theme=gr.themes.Soft(), | |
css=""" | |
.gradio-container { | |
max-width: 100% !important; | |
} | |
""" | |
) | |
# Launch the Gradio app with sharing enabled | |
if __name__ == "__main__": | |
app.launch(share=True) # share=True๋ฅผ ์ถ๊ฐํ์ฌ ๊ณต๊ฐ ๋งํฌ ์์ฑ |