import gradio as gr import requests import pandas as pd import matplotlib.pyplot as plt import seaborn as sns from datetime import datetime, timedelta import plotly.graph_objects as go import numpy as np import json # 관심 스페이스 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(date): url = f"https://huggingface.co/api/spaces/trending?date={date}&limit=300" response = requests.get(url) if response.status_code == 200: return response.json() return None def get_space_rank(spaces, space_id): for idx, space in enumerate(spaces, 1): if space.get('id', '') == space_id: return idx return None def fetch_and_analyze_data(): start_date = datetime(2023, 12, 1) end_date = datetime(2023, 12, 31) dates = [(start_date + timedelta(days=x)).strftime('%Y-%m-%d') for x in range((end_date - start_date).days + 1)] trending_data = {} target_space_ranks = {space: [] for space in target_spaces.keys()} for date in dates: spaces = get_trending_spaces(date) if spaces: trending_data[date] = spaces for space_id in target_spaces.keys(): rank = get_space_rank(spaces, space_id) target_space_ranks[space_id].append(rank) return trending_data, target_space_ranks, dates def create_trend_plot(trending_data, target_space_ranks, dates): fig = go.Figure() for space_id, ranks in target_space_ranks.items(): fig.add_trace(go.Scatter( x=dates, y=ranks, name=space_id, mode='lines+markers' )) fig.update_layout( title='Trending Ranks Over Time', xaxis_title='Date', yaxis_title='Rank', yaxis_autorange='reversed', height=800 ) return fig def create_space_info_html(trending_data): latest_date = max(trending_data.keys()) latest_spaces = trending_data[latest_date] html_content = "
" html_content += f"

Latest Rankings ({latest_date})

" for space_id, url in target_spaces.items(): rank = get_space_rank(latest_spaces, space_id) if rank: space_info = next((s for s in latest_spaces if s['id'] == space_id), None) if space_info: html_content += f"""

#{rank} - {space_id}

👍 Likes: {space_info.get('likes', 'N/A')}

📝 {space_info.get('title', 'N/A')}

{space_info.get('description', 'N/A')[:100]}...

Visit Space 🔗
""" html_content += "
" return html_content def export_data(trending_data, dates): df_data = [] for date in dates: spaces = trending_data.get(date, []) for space_id in target_spaces.keys(): rank = get_space_rank(spaces, space_id) if rank: space_info = next((s for s in spaces if s['id'] == space_id), None) if space_info: df_data.append({ 'Date': date, 'Space ID': space_id, 'Rank': rank, 'Likes': space_info.get('likes', 'N/A'), 'Title': space_info.get('title', 'N/A'), 'URL': target_spaces[space_id] }) df = pd.DataFrame(df_data) return df def main_interface(): trending_data, target_space_ranks, dates = fetch_and_analyze_data() # 트렌드 플롯 생성 plot = create_trend_plot(trending_data, target_space_ranks, dates) # 스페이스 정보 HTML 생성 space_info = create_space_info_html(trending_data) # 데이터 익스포트 df = export_data(trending_data, dates) return plot, space_info, df # Gradio 인터페이스 생성 with gr.Blocks(theme=gr.themes.Soft()) as demo: gr.Markdown("# 🤗 HuggingFace Spaces Trending Analysis") 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") refresh_btn.click( main_interface, outputs=[plot_output, info_output, df_output] ) # 초기 데이터 로드 plot, info, df = main_interface() plot_output.update(value=plot) info_output.update(value=info) df_output.update(value=df) # Gradio 앱 실행 demo.launch()