openfree's picture
Update app.py
cce8fbb verified
raw
history blame
13.5 kB
import gradio as gr
import requests
import pandas as pd
import plotly.graph_objects as go
from datetime import datetime
import os
HF_TOKEN = os.getenv("HF_TOKEN")
# 관심 슀페이슀 URL λ¦¬μŠ€νŠΈμ™€ 정보
target_spaces = {
"ginipick/FLUXllama": "https://huggingface.co/spaces/ginipick/FLUXllama",
"ginipick/SORA-3D": "https://huggingface.co/spaces/ginipick/SORA-3D",
"fantaxy/Sound-AI-SFX": "https://huggingface.co/spaces/fantaxy/Sound-AI-SFX",
"fantos/flx8lora": "https://huggingface.co/spaces/fantos/flx8lora",
"ginigen/Canvas": "https://huggingface.co/spaces/ginigen/Canvas",
"fantaxy/erotica": "https://huggingface.co/spaces/fantaxy/erotica",
"ginipick/time-machine": "https://huggingface.co/spaces/ginipick/time-machine",
"aiqcamp/FLUX-VisionReply": "https://huggingface.co/spaces/aiqcamp/FLUX-VisionReply",
"openfree/Tetris-Game": "https://huggingface.co/spaces/openfree/Tetris-Game",
"openfree/everychat": "https://huggingface.co/spaces/openfree/everychat",
"VIDraft/mouse1": "https://huggingface.co/spaces/VIDraft/mouse1",
"kolaslab/alpha-go": "https://huggingface.co/spaces/kolaslab/alpha-go",
"ginipick/text3d": "https://huggingface.co/spaces/ginipick/text3d",
"openfree/trending-board": "https://huggingface.co/spaces/openfree/trending-board",
"cutechicken/tankwar": "https://huggingface.co/spaces/cutechicken/tankwar",
"openfree/game-jewel": "https://huggingface.co/spaces/openfree/game-jewel",
"VIDraft/mouse-chat": "https://huggingface.co/spaces/VIDraft/mouse-chat",
"ginipick/AccDiffusion": "https://huggingface.co/spaces/ginipick/AccDiffusion",
"aiqtech/Particle-Accelerator-Simulation": "https://huggingface.co/spaces/aiqtech/Particle-Accelerator-Simulation",
"openfree/GiniGEN": "https://huggingface.co/spaces/openfree/GiniGEN",
"kolaslab/3DAudio-Spectrum-Analyzer": "https://huggingface.co/spaces/kolaslab/3DAudio-Spectrum-Analyzer",
"openfree/trending-news-24": "https://huggingface.co/spaces/openfree/trending-news-24",
"ginipick/Realtime-FLUX": "https://huggingface.co/spaces/ginipick/Realtime-FLUX",
"VIDraft/prime-number": "https://huggingface.co/spaces/VIDraft/prime-number",
"kolaslab/zombie-game": "https://huggingface.co/spaces/kolaslab/zombie-game",
"fantos/miro-game": "https://huggingface.co/spaces/fantos/miro-game",
"kolaslab/shooting": "https://huggingface.co/spaces/kolaslab/shooting",
"VIDraft/Mouse-Hackathon": "https://huggingface.co/spaces/VIDraft/Mouse-Hackathon",
"upstage/open-ko-llm-leaderboard": "https://huggingface.co/spaces/upstage/open-ko-llm-leaderboard",
"LGAI-EXAONE/EXAONE-3.5-Instruct-Demo": "https://huggingface.co/spaces/LGAI-EXAONE/EXAONE-3.5-Instruct-Demo",
"NCSOFT/VARCO_Arena": "https://huggingface.co/spaces/NCSOFT/VARCO_Arena"
}
def get_trending_spaces(search_query="", sort_by="rank", progress=gr.Progress()):
"""νŠΈλ Œλ”© 슀페이슀 κ°€μ Έμ˜€κΈ°"""
url = "https://huggingface.co/api/spaces"
try:
progress(0, desc="Fetching spaces data...")
params = {
'full': 'true',
'limit': 1000 # μΆ©λΆ„ν•œ 수의 슀페이슀λ₯Ό κ°€μ Έμ˜€κΈ° μœ„ν•΄ 증가
}
response = requests.get(url, params=params)
response.raise_for_status()
all_spaces = response.json()
# target_spaces에 μžˆλŠ” 슀페이슀만 필터링
spaces = [space for space in all_spaces if space.get('id', '') in target_spaces]
progress(0.1, desc="Creating visualization...")
# νŠΈλ Œλ“œ μ‹œκ°ν™” 생성
plot = create_trend_visualization(spaces)
progress(0.4, desc="Creating space cards...")
# 슀페이슀 μΉ΄λ“œ HTML 생성
html_content = """
<div style='padding: 20px; background: #f5f5f5;'>
<div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
"""
for idx, space in enumerate(spaces):
space_id = space.get('id', '')
likes = space.get('likes', 0)
title = space.get('title', 'No Title')
description = space.get('description', 'No Description')[:100]
html_content += f"""
<div style='
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
transition: transform 0.2s;
'>
<h3 style='color: #34495e;'>#{idx + 1} - {space_id}</h3>
<p style='color: #7f8c8d;'>πŸ‘ Likes: {likes}</p>
<p style='color: #2c3e50;'>{title}</p>
<p style='color: #7f8c8d; font-size: 0.9em;'>{description}...</p>
<a href='{target_spaces[space_id]}'
target='_blank'
style='
display: inline-block;
padding: 8px 16px;
background: #3498db;
color: white;
text-decoration: none;
border-radius: 5px;
transition: background 0.3s;
'>
Visit Space πŸ”—
</a>
</div>
"""
progress((0.4 + 0.5 * idx/len(spaces)), desc=f"Loading space {idx+1}/{len(spaces)}...")
html_content += "</div></div>"
# 데이터 ν…Œμ΄λΈ” 생성
df = create_data_table(spaces)
progress(1.0, desc="Complete!")
return plot, html_content, df
except Exception as e:
error_html = f'<div style="color: red; padding: 20px;">Error: {str(e)}</div>'
error_plot = create_error_plot()
return error_plot, error_html, pd.DataFrame()
def create_trend_visualization(spaces_data):
if not spaces_data:
return create_error_plot()
fig = go.Figure()
# μˆœμœ„ 데이터 μ€€λΉ„
ranks = []
for idx, space in enumerate(spaces_data, 1):
space_id = space.get('id', '')
if space_id in target_spaces:
ranks.append({
'id': space_id,
'rank': idx,
'likes': space.get('likes', 0),
'title': space.get('title', 'N/A'),
'views': space.get('views', 0)
})
if not ranks:
return create_error_plot()
# μˆœμœ„λ³„λ‘œ μ •λ ¬
ranks.sort(key=lambda x: x['rank'])
# ν”Œλ‘― 데이터 생성
ids = [r['id'] for r in ranks]
rank_values = [r['rank'] for r in ranks]
likes = [r['likes'] for r in ranks]
views = [r['views'] for r in ranks]
# λ§‰λŒ€ κ·Έλž˜ν”„ 생성
fig.add_trace(go.Bar(
x=ids,
y=rank_values,
text=[f"Rank: {r}<br>Likes: {l}<br>Views: {v}" for r, l, v in zip(rank_values, likes, views)],
textposition='auto',
marker_color='rgb(158,202,225)',
opacity=0.8
))
fig.update_layout(
title={
'text': 'Current Trending Ranks (All Target Spaces)',
'y':0.95,
'x':0.5,
'xanchor': 'center',
'yanchor': 'top'
},
xaxis_title='Space ID',
yaxis_title='Trending Rank',
yaxis_autorange='reversed',
height=800,
showlegend=False,
template='plotly_white',
xaxis_tickangle=-45
)
return fig
# 토큰이 μ—†λŠ” 경우λ₯Ό μœ„ν•œ λŒ€μ²΄ ν•¨μˆ˜
def get_trending_spaces_without_token():
try:
url = "https://huggingface.co/api/spaces"
params = {
'sort': 'likes',
'direction': -1,
'limit': 1000,
'full': 'true'
}
response = requests.get(url, params=params)
if response.status_code == 200:
return response.json()
else:
print(f"API μš”μ²­ μ‹€νŒ¨ (토큰 μ—†μŒ): {response.status_code}")
print(f"Response: {response.text}")
return None
except Exception as e:
print(f"API 호좜 쀑 μ—λŸ¬ λ°œμƒ (토큰 μ—†μŒ): {str(e)}")
return None
# API 토큰 μ„€μ • 및 ν•¨μˆ˜ 선택
if not HF_TOKEN:
get_trending_spaces = get_trending_spaces_without_token
def create_error_plot():
fig = go.Figure()
fig.add_annotation(
text="데이터λ₯Ό 뢈러올 수 μ—†μŠ΅λ‹ˆλ‹€.\n(API 인증이 ν•„μš”ν•©λ‹ˆλ‹€)",
xref="paper",
yref="paper",
x=0.5,
y=0.5,
showarrow=False,
font=dict(size=20)
)
fig.update_layout(
title="Error Loading Data",
height=400
)
return fig
def create_space_info_html(spaces_data):
if not spaces_data:
return "<div style='padding: 20px;'><h2>데이터λ₯Ό λΆˆλŸ¬μ˜€λŠ”λ° μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.</h2></div>"
html_content = """
<div style='padding: 20px;'>
<h2 style='color: #2c3e50;'>Current Trending Rankings</h2>
<div style='display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 20px;'>
"""
# λͺ¨λ“  target spacesλ₯Ό ν¬ν•¨ν•˜λ„λ‘ μˆ˜μ •
for space_id in target_spaces.keys():
space_info = next((s for s in spaces_data if s.get('id') == space_id), None)
if space_info:
rank = next((idx for idx, s in enumerate(spaces_data, 1) if s.get('id') == space_id), 'N/A')
html_content += f"""
<div style='
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
transition: transform 0.2s;
'>
<h3 style='color: #34495e;'>#{rank} - {space_id}</h3>
<p style='color: #7f8c8d;'>πŸ‘ Likes: {space_info.get('likes', 'N/A')}</p>
<p style='color: #7f8c8d;'>πŸ‘€ Views: {space_info.get('views', 'N/A')}</p>
<p style='color: #2c3e50;'>{space_info.get('title', 'N/A')}</p>
<p style='color: #7f8c8d; font-size: 0.9em;'>{space_info.get('description', 'N/A')[:100]}...</p>
<a href='{target_spaces[space_id]}'
target='_blank'
style='
display: inline-block;
padding: 8px 16px;
background: #3498db;
color: white;
text-decoration: none;
border-radius: 5px;
transition: background 0.3s;
'>
Visit Space πŸ”—
</a>
</div>
"""
else:
html_content += f"""
<div style='
background: #f8f9fa;
padding: 20px;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
'>
<h3 style='color: #34495e;'>{space_id}</h3>
<p style='color: #7f8c8d;'>Not in trending</p>
<a href='{target_spaces[space_id]}'
target='_blank'
style='
display: inline-block;
padding: 8px 16px;
background: #95a5a6;
color: white;
text-decoration: none;
border-radius: 5px;
'>
Visit Space πŸ”—
</a>
</div>
"""
html_content += "</div></div>"
return html_content
def create_data_table(spaces_data):
if not spaces_data:
return pd.DataFrame()
rows = []
for idx, space in enumerate(spaces_data, 1):
space_id = space.get('id', '')
if space_id in target_spaces:
rows.append({
'Rank': idx,
'Space ID': space_id,
'Likes': space.get('likes', 'N/A'),
'Title': space.get('title', 'N/A'),
'URL': target_spaces[space_id]
})
return pd.DataFrame(rows)
def refresh_data():
spaces_data = get_trending_spaces()
if spaces_data:
plot = create_trend_visualization(spaces_data)
info = create_space_info_html(spaces_data)
df = create_data_table(spaces_data)
return plot, info, df
else:
return create_error_plot(), "<div>API 인증이 ν•„μš”ν•©λ‹ˆλ‹€.</div>", pd.DataFrame()
# Gradio μΈν„°νŽ˜μ΄μŠ€ 생성
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("""
# πŸ€— HuggingFace Spaces Trending Analysis
μ‹€μ‹œκ°„μœΌλ‘œ Hugging Face Spaces의 νŠΈλ Œλ”© μˆœμœ„λ₯Ό λΆ„μ„ν•©λ‹ˆλ‹€.
""")
with gr.Tab("Trending Analysis"):
plot_output = gr.Plot()
info_output = gr.HTML()
with gr.Tab("Export Data"):
df_output = gr.DataFrame()
refresh_btn = gr.Button("πŸ”„ Refresh Data", variant="primary")
def refresh_data():
return get_trending_spaces()
refresh_btn.click(
refresh_data,
outputs=[plot_output, info_output, df_output]
)
# 초기 데이터 λ‘œλ“œ
initial_plot, initial_info, initial_df = get_trending_spaces()
plot_output.value = initial_plot
info_output.value = initial_info
df_output.value = initial_df
# Gradio μ•± μ‹€ν–‰
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
)