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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import plotly.express as px
|
| 4 |
+
from datetime import datetime, timedelta
|
| 5 |
+
import requests
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
|
| 8 |
+
def create_trend_chart(space_id, daily_ranks_df):
|
| 9 |
+
if space_id is None or daily_ranks_df.empty:
|
| 10 |
+
return None
|
| 11 |
+
|
| 12 |
+
try:
|
| 13 |
+
# ํน์ space์ ๋ฐ์ดํฐ๋ง ํํฐ๋ง
|
| 14 |
+
space_data = daily_ranks_df[daily_ranks_df['id'] == space_id].copy()
|
| 15 |
+
if space_data.empty:
|
| 16 |
+
return None
|
| 17 |
+
|
| 18 |
+
# ๋ฐ์ดํฐ ์ ๋ ฌ
|
| 19 |
+
space_data = space_data.sort_values('date')
|
| 20 |
+
|
| 21 |
+
fig = px.line(
|
| 22 |
+
space_data,
|
| 23 |
+
x='date',
|
| 24 |
+
y='rank',
|
| 25 |
+
title=f'Daily Rank Trend for {space_id}',
|
| 26 |
+
labels={'date': 'Date', 'rank': 'Rank'},
|
| 27 |
+
markers=True
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
fig.update_layout(
|
| 31 |
+
xaxis_title="Date",
|
| 32 |
+
yaxis_title="Rank",
|
| 33 |
+
yaxis=dict(
|
| 34 |
+
range=[100, 1], # 100์๋ถํฐ 1์๊น์ง (์ญ์์ผ๋ก ์ค์ )
|
| 35 |
+
tickmode='linear', # ์ ํ ๊ฐ๊ฒฉ์ผ๋ก ๋๊ธ ํ์
|
| 36 |
+
tick0=1, # ์ฒซ ๋๊ธ
|
| 37 |
+
dtick=10 # ๋๊ธ ๊ฐ๊ฒฉ (10๋จ์๋ก ํ์)
|
| 38 |
+
),
|
| 39 |
+
hovermode='x unified',
|
| 40 |
+
plot_bgcolor='white',
|
| 41 |
+
paper_bgcolor='white',
|
| 42 |
+
showlegend=False
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
# ๊ฒฉ์ ์ถ๊ฐ
|
| 46 |
+
fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='lightgray')
|
| 47 |
+
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='lightgray')
|
| 48 |
+
|
| 49 |
+
# ๋ผ์ธ ์คํ์ผ ์์
|
| 50 |
+
fig.update_traces(
|
| 51 |
+
line_color='#2563eb',
|
| 52 |
+
line_width=2,
|
| 53 |
+
marker=dict(size=8, color='#2563eb')
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
return fig
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"Error creating chart: {e}")
|
| 59 |
+
return None
|
| 60 |
+
|
| 61 |
+
def load_and_process_data():
|
| 62 |
+
try:
|
| 63 |
+
url = "https://huggingface.co/datasets/cfahlgren1/hub-stats/resolve/main/spaces.parquet"
|
| 64 |
+
response = requests.get(url)
|
| 65 |
+
df = pd.read_parquet(BytesIO(response.content))
|
| 66 |
+
|
| 67 |
+
# 30์ผ์น ๋ฐ์ดํฐ ์ค๋น
|
| 68 |
+
thirty_days_ago = datetime.now() - timedelta(days=30)
|
| 69 |
+
df['createdAt'] = pd.to_datetime(df['createdAt'])
|
| 70 |
+
df = df[df['createdAt'] >= thirty_days_ago].copy()
|
| 71 |
+
|
| 72 |
+
# ๋ ์ง๋ณ ๋ฐ์ดํฐ ์ฒ๋ฆฌ
|
| 73 |
+
dates = pd.date_range(start=thirty_days_ago, end=datetime.now(), freq='D')
|
| 74 |
+
daily_ranks = []
|
| 75 |
+
|
| 76 |
+
for date in dates:
|
| 77 |
+
# ํด๋น ๋ ์ง์ ๋ฐ์ดํฐ ์ถ์ถ
|
| 78 |
+
date_data = df[df['createdAt'].dt.date <= date.date()].copy()
|
| 79 |
+
|
| 80 |
+
# trendingScore๊ฐ ๊ฐ์ ๊ฒฝ์ฐ id๋ก ์ ๋ ฌํ์ฌ ์ ๋ํฌํ ์์ ๋ณด์ฅ
|
| 81 |
+
date_data = date_data.sort_values(['trendingScore', 'id'], ascending=[False, True])
|
| 82 |
+
|
| 83 |
+
# ์์ ๊ณ์ฐ
|
| 84 |
+
date_data['rank'] = range(1, len(date_data) + 1)
|
| 85 |
+
date_data['date'] = date.date()
|
| 86 |
+
|
| 87 |
+
# ํ์ํ ์ปฌ๋ผ๋ง ์ ํ
|
| 88 |
+
daily_ranks.append(
|
| 89 |
+
date_data[['id', 'date', 'rank', 'trendingScore', 'createdAt']]
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
# ์ ์ฒด ๋ฐ์ดํฐ ๋ณํฉ
|
| 93 |
+
daily_ranks_df = pd.concat(daily_ranks, ignore_index=True)
|
| 94 |
+
|
| 95 |
+
# ์ต์ ๋ ์ง์ top 100 ์ถ์ถ
|
| 96 |
+
latest_date = daily_ranks_df['date'].max()
|
| 97 |
+
top_100_spaces = daily_ranks_df[
|
| 98 |
+
daily_ranks_df['date'] == latest_date
|
| 99 |
+
].sort_values('rank').head(100).copy()
|
| 100 |
+
|
| 101 |
+
return daily_ranks_df, top_100_spaces
|
| 102 |
+
except Exception as e:
|
| 103 |
+
print(f"Error loading data: {e}")
|
| 104 |
+
return pd.DataFrame(), pd.DataFrame()
|
| 105 |
+
|
| 106 |
+
def update_display(selection):
|
| 107 |
+
global daily_ranks_df
|
| 108 |
+
|
| 109 |
+
if not selection:
|
| 110 |
+
return None, "Please select a space"
|
| 111 |
+
|
| 112 |
+
try:
|
| 113 |
+
# ์ ํ๋ ํญ๋ชฉ์์ space ID ์ถ์ถ
|
| 114 |
+
space_id = selection.split(': ')[1].split(' (Score')[0]
|
| 115 |
+
|
| 116 |
+
# ์ต์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
|
| 117 |
+
latest_data = daily_ranks_df[
|
| 118 |
+
daily_ranks_df['id'] == space_id
|
| 119 |
+
].sort_values('date').iloc[-1]
|
| 120 |
+
|
| 121 |
+
info_text = f"""ID: {space_id}
|
| 122 |
+
Current Rank: {int(latest_data['rank'])}
|
| 123 |
+
Trending Score: {latest_data['trendingScore']:.2f}
|
| 124 |
+
Created At: {latest_data['createdAt'].strftime('%Y-%m-%d')}"""
|
| 125 |
+
|
| 126 |
+
chart = create_trend_chart(space_id, daily_ranks_df)
|
| 127 |
+
|
| 128 |
+
return chart, info_text
|
| 129 |
+
|
| 130 |
+
except Exception as e:
|
| 131 |
+
print(f"Error in update_display: {e}")
|
| 132 |
+
return None, f"Error processing data: {str(e)}"
|
| 133 |
+
|
| 134 |
+
# ๋ฐ์ดํฐ ๋ก๋
|
| 135 |
+
print("Loading initial data...")
|
| 136 |
+
daily_ranks_df, top_100_spaces = load_and_process_data()
|
| 137 |
+
print("Data loaded.")
|
| 138 |
+
|
| 139 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
| 140 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 141 |
+
gr.Markdown("# Trending Spaces Dashboard")
|
| 142 |
+
|
| 143 |
+
with gr.Row():
|
| 144 |
+
with gr.Column(scale=1):
|
| 145 |
+
# ์์๊ฐ ํฌํจ๋ ๋ฆฌ์คํธ๋ก ํ์
|
| 146 |
+
space_choices = [
|
| 147 |
+
f"Rank {row['rank']}: {row['id']} (Score: {row['trendingScore']:.2f})"
|
| 148 |
+
for _, row in top_100_spaces.iterrows()
|
| 149 |
+
]
|
| 150 |
+
|
| 151 |
+
space_list = gr.Radio(
|
| 152 |
+
choices=space_choices,
|
| 153 |
+
label="Top 100 Trending Spaces",
|
| 154 |
+
info="Select a space to view its rank trend",
|
| 155 |
+
value=space_choices[0] if space_choices else None
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
info_box = gr.Textbox(
|
| 159 |
+
label="Space Details",
|
| 160 |
+
value="",
|
| 161 |
+
interactive=False,
|
| 162 |
+
lines=4
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
with gr.Column(scale=2):
|
| 166 |
+
trend_plot = gr.Plot(
|
| 167 |
+
label="Daily Rank Trend"
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
space_list.change(
|
| 171 |
+
fn=update_display,
|
| 172 |
+
inputs=[space_list],
|
| 173 |
+
outputs=[trend_plot, info_box]
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
if __name__ == "__main__":
|
| 177 |
+
demo.launch(share=True)
|