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