Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,183 @@ from datetime import datetime, timedelta
|
|
5 |
import requests
|
6 |
from io import BytesIO
|
7 |
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Gradio μΈν°νμ΄μ€ μμ±
|
11 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
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_data = daily_ranks_df[daily_ranks_df['id'] == space_id].copy()
|
14 |
+
if space_data.empty:
|
15 |
+
return None
|
16 |
+
|
17 |
+
space_data = space_data.sort_values('date')
|
18 |
+
|
19 |
+
fig = px.line(
|
20 |
+
space_data,
|
21 |
+
x='date',
|
22 |
+
y='rank',
|
23 |
+
title=f'Daily Rank Trend for {space_id}',
|
24 |
+
labels={'date': 'Date', 'rank': 'Rank'},
|
25 |
+
markers=True
|
26 |
+
)
|
27 |
+
|
28 |
+
fig.update_layout(
|
29 |
+
height=500,
|
30 |
+
xaxis_title="Date",
|
31 |
+
yaxis_title="Rank",
|
32 |
+
yaxis=dict(
|
33 |
+
range=[100, 1],
|
34 |
+
tickmode='linear',
|
35 |
+
tick0=1,
|
36 |
+
dtick=10
|
37 |
+
),
|
38 |
+
hovermode='x unified',
|
39 |
+
plot_bgcolor='white',
|
40 |
+
paper_bgcolor='white',
|
41 |
+
showlegend=False,
|
42 |
+
margin=dict(t=50, r=20, b=40, l=40)
|
43 |
+
)
|
44 |
+
|
45 |
+
fig.update_xaxes(showgrid=True, gridwidth=1, gridcolor='lightgray')
|
46 |
+
fig.update_yaxes(showgrid=True, gridwidth=1, gridcolor='lightgray')
|
47 |
+
|
48 |
+
fig.update_traces(
|
49 |
+
line_color='#2563eb',
|
50 |
+
line_width=2,
|
51 |
+
marker=dict(size=8, color='#2563eb')
|
52 |
+
)
|
53 |
+
|
54 |
+
return fig
|
55 |
+
except Exception as e:
|
56 |
+
print(f"Error creating chart: {e}")
|
57 |
+
return None
|
58 |
+
|
59 |
+
def create_space_card(space_info):
|
60 |
+
# μμμ λ°λ₯Έ νμ€ν
μμ κ³μ°
|
61 |
+
hue = 210 # νλμ κ³μ΄
|
62 |
+
saturation = max(30, 90 - (space_info['rank'] / 100 * 60)) # μμκ° λμμλ‘ λ μ λͺ
ν μμ
|
63 |
+
lightness = min(97, 85 + (space_info['rank'] / 100 * 10)) # μμκ° λμμλ‘ λ λ°μ μμ
|
64 |
+
|
65 |
+
return f"""
|
66 |
+
<div class="space-card"
|
67 |
+
data-space-id="{space_info['id']}"
|
68 |
+
style="
|
69 |
+
border: 1px solid #e5e7eb;
|
70 |
+
border-radius: 8px;
|
71 |
+
padding: 16px;
|
72 |
+
margin: 8px;
|
73 |
+
background-color: hsl({hue}, {saturation}%, {lightness}%);
|
74 |
+
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
75 |
+
display: inline-block;
|
76 |
+
width: 250px;
|
77 |
+
vertical-align: top;
|
78 |
+
cursor: pointer;
|
79 |
+
transition: all 0.2s;
|
80 |
+
"
|
81 |
+
onmouseover="this.style.transform='translateY(-2px)';this.style.boxShadow='0 4px 6px rgba(0,0,0,0.1)';"
|
82 |
+
onmouseout="this.style.transform='none';this.style.boxShadow='0 1px 3px rgba(0,0,0,0.1)';"
|
83 |
+
>
|
84 |
+
<div style="font-size: 1.2em; font-weight: bold; margin-bottom: 8px;">
|
85 |
+
#{int(space_info['rank'])}
|
86 |
+
</div>
|
87 |
+
<div style="margin-bottom: 8px;">
|
88 |
+
{space_info['id']}
|
89 |
+
</div>
|
90 |
+
<div style="color: #666; margin-bottom: 12px;">
|
91 |
+
Score: {space_info['trendingScore']:.2f}
|
92 |
+
</div>
|
93 |
+
<div style="display: flex; gap: 8px;">
|
94 |
+
<a href="https://huggingface.co/spaces/{space_info['id']}"
|
95 |
+
target="_blank"
|
96 |
+
style="padding: 6px 12px; background-color: white; color: #2563eb; text-decoration: none; border-radius: 4px; font-size: 0.9em; border: 1px solid #2563eb;"
|
97 |
+
onclick="event.stopPropagation();">
|
98 |
+
View Space β
|
99 |
+
</a>
|
100 |
+
<button onclick="event.stopPropagation(); document.querySelector('input[type=\'radio\'][value=\'{space_info['id']}\']').click();"
|
101 |
+
style="padding: 6px 12px; background-color: #2563eb; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 0.9em;">
|
102 |
+
View Trend
|
103 |
+
</button>
|
104 |
+
</div>
|
105 |
+
</div>
|
106 |
+
"""
|
107 |
+
|
108 |
+
def update_display(selection):
|
109 |
+
global daily_ranks_df
|
110 |
+
|
111 |
+
if not selection:
|
112 |
+
return None, gr.HTML(value="<div style='text-align: center; padding: 20px; color: #666;'>Select a space to view details</div>")
|
113 |
+
|
114 |
+
try:
|
115 |
+
space_id = selection
|
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"""
|
122 |
+
<div style="padding: 16px; background-color: white; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);">
|
123 |
+
<h3 style="margin: 0 0 12px 0;">Space Details</h3>
|
124 |
+
<p style="margin: 4px 0;"><strong>ID:</strong> {space_id}</p>
|
125 |
+
<p style="margin: 4px 0;"><strong>Current Rank:</strong> {int(latest_data['rank'])}</p>
|
126 |
+
<p style="margin: 4px 0;"><strong>Trending Score:</strong> {latest_data['trendingScore']:.2f}</p>
|
127 |
+
<p style="margin: 4px 0;"><strong>Created At:</strong> {latest_data['createdAt'].strftime('%Y-%m-%d')}</p>
|
128 |
+
<p style="margin: 12px 0 0 0;">
|
129 |
+
<a href="https://huggingface.co/spaces/{space_id}"
|
130 |
+
target="_blank"
|
131 |
+
style="color: #2563eb; text-decoration: none;">
|
132 |
+
View Space β
|
133 |
+
</a>
|
134 |
+
</p>
|
135 |
+
</div>
|
136 |
+
"""
|
137 |
+
|
138 |
+
chart = create_trend_chart(space_id, daily_ranks_df)
|
139 |
+
|
140 |
+
return chart, gr.HTML(value=info_text)
|
141 |
+
|
142 |
+
except Exception as e:
|
143 |
+
print(f"Error in update_display: {e}")
|
144 |
+
return None, gr.HTML(value=f"<div style='color: red;'>Error processing data: {str(e)}</div>")
|
145 |
+
|
146 |
+
def load_and_process_data():
|
147 |
+
try:
|
148 |
+
url = "https://huggingface.co/datasets/cfahlgren1/hub-stats/resolve/main/spaces.parquet"
|
149 |
+
response = requests.get(url)
|
150 |
+
df = pd.read_parquet(BytesIO(response.content))
|
151 |
+
|
152 |
+
thirty_days_ago = datetime.now() - timedelta(days=30)
|
153 |
+
df['createdAt'] = pd.to_datetime(df['createdAt'])
|
154 |
+
df = df[df['createdAt'] >= thirty_days_ago].copy()
|
155 |
+
|
156 |
+
dates = pd.date_range(start=thirty_days_ago, end=datetime.now(), freq='D')
|
157 |
+
daily_ranks = []
|
158 |
+
|
159 |
+
for date in dates:
|
160 |
+
date_data = df[df['createdAt'].dt.date <= date.date()].copy()
|
161 |
+
date_data = date_data.sort_values(['trendingScore', 'id'], ascending=[False, True])
|
162 |
+
date_data['rank'] = range(1, len(date_data) + 1)
|
163 |
+
date_data['date'] = date.date()
|
164 |
+
daily_ranks.append(
|
165 |
+
date_data[['id', 'date', 'rank', 'trendingScore', 'createdAt']]
|
166 |
+
)
|
167 |
+
|
168 |
+
daily_ranks_df = pd.concat(daily_ranks, ignore_index=True)
|
169 |
+
|
170 |
+
latest_date = daily_ranks_df['date'].max()
|
171 |
+
top_100_spaces = daily_ranks_df[
|
172 |
+
(daily_ranks_df['date'] == latest_date) &
|
173 |
+
(daily_ranks_df['rank'] <= 100)
|
174 |
+
].sort_values('rank').copy()
|
175 |
+
|
176 |
+
return daily_ranks_df, top_100_spaces
|
177 |
+
except Exception as e:
|
178 |
+
print(f"Error loading data: {e}")
|
179 |
+
return pd.DataFrame(), pd.DataFrame()
|
180 |
+
|
181 |
+
# λ°μ΄ν° λ‘λ
|
182 |
+
print("Loading initial data...")
|
183 |
+
daily_ranks_df, top_100_spaces = load_and_process_data()
|
184 |
+
print("Data loaded successfully!")
|
185 |
|
186 |
# Gradio μΈν°νμ΄μ€ μμ±
|
187 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|