Spaces:
Running
Running
Update app-backup.py
Browse files- app-backup.py +168 -78
app-backup.py
CHANGED
@@ -10,12 +10,10 @@ def create_trend_chart(space_id, daily_ranks_df):
|
|
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(
|
@@ -24,29 +22,29 @@ def create_trend_chart(space_id, daily_ranks_df):
|
|
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],
|
35 |
-
tickmode='linear',
|
36 |
-
tick0=1,
|
37 |
-
dtick=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,
|
@@ -58,119 +56,211 @@ def create_trend_chart(space_id, daily_ranks_df):
|
|
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 |
-
|
|
|
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("
|
|
|
142 |
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
150 |
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
value=space_choices[0] if space_choices else None
|
156 |
-
)
|
157 |
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
164 |
|
165 |
-
with gr.
|
166 |
-
|
167 |
-
|
168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
-
|
|
|
171 |
fn=update_display,
|
172 |
-
inputs=[
|
173 |
-
outputs=[trend_plot, info_box]
|
|
|
174 |
)
|
175 |
|
176 |
if __name__ == "__main__":
|
|
|
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(
|
|
|
22 |
y='rank',
|
23 |
title=f'Daily Rank Trend for {space_id}',
|
24 |
labels={'date': 'Date', 'rank': 'Rank'},
|
25 |
+
markers=True,
|
26 |
+
height=400 # ์ฐจํธ ๋์ด ์ค์
|
27 |
)
|
28 |
|
29 |
fig.update_layout(
|
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,
|
|
|
56 |
print(f"Error creating chart: {e}")
|
57 |
return None
|
58 |
|
59 |
+
def update_display(selection):
|
60 |
+
global daily_ranks_df
|
61 |
+
|
62 |
+
if not selection:
|
63 |
+
return None, gr.HTML(value="<div style='text-align: center; padding: 20px; color: #666;'>Select a space to view details</div>")
|
64 |
+
|
65 |
+
try:
|
66 |
+
space_id = selection
|
67 |
+
|
68 |
+
latest_data = daily_ranks_df[
|
69 |
+
daily_ranks_df['id'] == space_id
|
70 |
+
].sort_values('date').iloc[-1]
|
71 |
+
|
72 |
+
info_text = f"""
|
73 |
+
<div style="padding: 16px; background-color: white; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.1);">
|
74 |
+
<h3 style="margin: 0 0 12px 0;">Space Details</h3>
|
75 |
+
<p style="margin: 4px 0;"><strong>ID:</strong> {space_id}</p>
|
76 |
+
<p style="margin: 4px 0;"><strong>Current Rank:</strong> {int(latest_data['rank'])}</p>
|
77 |
+
<p style="margin: 4px 0;"><strong>Trending Score:</strong> {latest_data['trendingScore']:.2f}</p>
|
78 |
+
<p style="margin: 4px 0;"><strong>Created At:</strong> {latest_data['createdAt'].strftime('%Y-%m-%d')}</p>
|
79 |
+
<p style="margin: 12px 0 0 0;">
|
80 |
+
<a href="https://huggingface.co/spaces/{space_id}"
|
81 |
+
target="_blank"
|
82 |
+
style="color: #2563eb; text-decoration: none;">
|
83 |
+
View Space โ
|
84 |
+
</a>
|
85 |
+
</p>
|
86 |
+
</div>
|
87 |
+
"""
|
88 |
+
|
89 |
+
chart = create_trend_chart(space_id, daily_ranks_df)
|
90 |
+
|
91 |
+
return chart, gr.HTML(value=info_text)
|
92 |
+
|
93 |
+
except Exception as e:
|
94 |
+
print(f"Error in update_display: {e}")
|
95 |
+
return None, gr.HTML(value=f"<div style='color: red;'>Error processing data: {str(e)}</div>")
|
96 |
+
|
97 |
def load_and_process_data():
|
98 |
try:
|
99 |
url = "https://huggingface.co/datasets/cfahlgren1/hub-stats/resolve/main/spaces.parquet"
|
100 |
response = requests.get(url)
|
101 |
df = pd.read_parquet(BytesIO(response.content))
|
102 |
|
|
|
103 |
thirty_days_ago = datetime.now() - timedelta(days=30)
|
104 |
df['createdAt'] = pd.to_datetime(df['createdAt'])
|
105 |
df = df[df['createdAt'] >= thirty_days_ago].copy()
|
106 |
|
|
|
107 |
dates = pd.date_range(start=thirty_days_ago, end=datetime.now(), freq='D')
|
108 |
daily_ranks = []
|
109 |
|
110 |
for date in dates:
|
|
|
111 |
date_data = df[df['createdAt'].dt.date <= date.date()].copy()
|
|
|
|
|
112 |
date_data = date_data.sort_values(['trendingScore', 'id'], ascending=[False, True])
|
|
|
|
|
113 |
date_data['rank'] = range(1, len(date_data) + 1)
|
114 |
date_data['date'] = date.date()
|
|
|
|
|
115 |
daily_ranks.append(
|
116 |
date_data[['id', 'date', 'rank', 'trendingScore', 'createdAt']]
|
117 |
)
|
118 |
|
|
|
119 |
daily_ranks_df = pd.concat(daily_ranks, ignore_index=True)
|
120 |
|
|
|
121 |
latest_date = daily_ranks_df['date'].max()
|
122 |
top_100_spaces = daily_ranks_df[
|
123 |
+
(daily_ranks_df['date'] == latest_date) &
|
124 |
+
(daily_ranks_df['rank'] <= 100)
|
125 |
+
].sort_values('rank').copy()
|
126 |
|
127 |
return daily_ranks_df, top_100_spaces
|
128 |
except Exception as e:
|
129 |
print(f"Error loading data: {e}")
|
130 |
return pd.DataFrame(), pd.DataFrame()
|
131 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
# ๋ฐ์ดํฐ ๋ก๋
|
133 |
print("Loading initial data...")
|
134 |
daily_ranks_df, top_100_spaces = load_and_process_data()
|
135 |
+
print("Data loaded successfully!")
|
136 |
|
137 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
138 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
139 |
+
gr.Markdown("""
|
140 |
+
# HF Space Ranking Tracker
|
141 |
|
142 |
+
Track, analyze, and discover trending AI applications in the Hugging Face ecosystem. Our service continuously monitors and ranks all Spaces over a 30-day period, providing detailed analytics and daily ranking changes for the top 100 performers.
|
143 |
+
""")
|
144 |
+
|
145 |
+
with gr.Tabs():
|
146 |
+
with gr.Tab("Dashboard"):
|
147 |
+
with gr.Row(variant="panel"):
|
148 |
+
trend_plot = gr.Plot(
|
149 |
+
label="Daily Rank Trend",
|
150 |
+
container=True,
|
151 |
+
)
|
152 |
|
153 |
+
with gr.Row():
|
154 |
+
info_box = gr.HTML(
|
155 |
+
value="<div style='text-align: center; padding: 20px; color: #666;'>Select a space to view details</div>"
|
156 |
+
)
|
|
|
|
|
157 |
|
158 |
+
# ๋ผ๋์ค ๋ฒํผ์ ๋จผ์ ์ ์
|
159 |
+
space_selection = gr.Radio(
|
160 |
+
choices=[row['id'] for _, row in top_100_spaces.iterrows()],
|
161 |
+
value=None,
|
162 |
+
visible=False
|
163 |
)
|
164 |
+
|
165 |
+
# HTML์์ JavaScript ์ด๋ฒคํธ๋ฅผ ์ง์ ์ฒ๋ฆฌ
|
166 |
+
html_content = """
|
167 |
+
<div style='display: flex; flex-wrap: wrap; gap: 16px; justify-content: center;'>
|
168 |
+
""" + "".join([
|
169 |
+
f"""
|
170 |
+
<div class="space-card"
|
171 |
+
data-space-id="{row['id']}"
|
172 |
+
style="
|
173 |
+
border: 1px solid #e5e7eb;
|
174 |
+
border-radius: 8px;
|
175 |
+
padding: 16px;
|
176 |
+
margin: 8px;
|
177 |
+
background-color: hsl(210, {max(30, 90 - (row['rank'] / 100 * 60))}%, {min(97, 85 + (row['rank'] / 100 * 10))}%);
|
178 |
+
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
|
179 |
+
display: inline-block;
|
180 |
+
width: 250px;
|
181 |
+
vertical-align: top;
|
182 |
+
cursor: pointer;
|
183 |
+
transition: all 0.2s;
|
184 |
+
"
|
185 |
+
onmouseover="this.style.transform='translateY(-2px)';this.style.boxShadow='0 4px 6px rgba(0,0,0,0.1)';"
|
186 |
+
onmouseout="this.style.transform='none';this.style.boxShadow='0 1px 3px rgba(0,0,0,0.1)';"
|
187 |
+
>
|
188 |
+
<div style="font-size: 1.2em; font-weight: bold; margin-bottom: 8px;">
|
189 |
+
#{int(row['rank'])}
|
190 |
+
</div>
|
191 |
+
<div style="margin-bottom: 8px;">
|
192 |
+
{row['id']}
|
193 |
+
</div>
|
194 |
+
<div style="color: #666; margin-bottom: 12px;">
|
195 |
+
Score: {row['trendingScore']:.2f}
|
196 |
+
</div>
|
197 |
+
<div style="display: flex; gap: 8px;">
|
198 |
+
<a href="https://huggingface.co/spaces/{row['id']}"
|
199 |
+
target="_blank"
|
200 |
+
style="padding: 6px 12px; background-color: white; color: #2563eb; text-decoration: none; border-radius: 4px; font-size: 0.9em; border: 1px solid #2563eb;"
|
201 |
+
onclick="event.stopPropagation();">
|
202 |
+
View Space โ
|
203 |
+
</a>
|
204 |
+
<button onclick="event.preventDefault(); gradioEvent('{row['id']}');"
|
205 |
+
style="padding: 6px 12px; background-color: #2563eb; color: white; border: none; border-radius: 4px; cursor: pointer; font-size: 0.9em;">
|
206 |
+
View Trend
|
207 |
+
</button>
|
208 |
+
</div>
|
209 |
+
</div>
|
210 |
+
"""
|
211 |
+
for _, row in top_100_spaces.iterrows()
|
212 |
+
]) + """
|
213 |
+
</div>
|
214 |
+
<script>
|
215 |
+
function gradioEvent(spaceId) {
|
216 |
+
const radio = document.querySelector(`input[type="radio"][value="${spaceId}"]`);
|
217 |
+
if (radio) {
|
218 |
+
radio.checked = true;
|
219 |
+
const event = new Event('change');
|
220 |
+
radio.dispatchEvent(event);
|
221 |
+
}
|
222 |
+
}
|
223 |
+
</script>
|
224 |
+
"""
|
225 |
+
|
226 |
+
with gr.Row():
|
227 |
+
space_grid = gr.HTML(value=html_content)
|
228 |
|
229 |
+
with gr.Tab("About"):
|
230 |
+
gr.Markdown("""
|
231 |
+
### Our Tracking System
|
232 |
+
|
233 |
+
#### What We Track
|
234 |
+
- Daily ranking changes for all Hugging Face Spaces
|
235 |
+
- Comprehensive trending scores based on 30-day activity
|
236 |
+
- Detailed performance metrics for top 100 Spaces
|
237 |
+
- Historical ranking data with daily granularity
|
238 |
+
|
239 |
+
#### Key Features
|
240 |
+
- **Real-time Rankings**: Stay updated with daily rank changes
|
241 |
+
- **Interactive Visualizations**: Track ranking trajectories over time
|
242 |
+
- **Trend Analysis**: Identify emerging popular AI applications
|
243 |
+
- **Direct Access**: Quick links to explore trending Spaces
|
244 |
+
- **Performance Metrics**: Detailed trending scores and statistics
|
245 |
+
|
246 |
+
### Why Use HF Space Ranking Tracker?
|
247 |
+
- Discover trending AI demos and applications
|
248 |
+
- Monitor your Space's performance and popularity
|
249 |
+
- Identify emerging trends in the AI community
|
250 |
+
- Make data-driven decisions about your AI projects
|
251 |
+
- Stay ahead of the curve in AI application development
|
252 |
+
|
253 |
+
Our dashboard provides a comprehensive view of the Hugging Face Spaces ecosystem, helping developers, researchers, and enthusiasts track and understand the dynamics of popular AI applications. Whether you're monitoring your own Space's performance or discovering new trending applications, HF Space Ranking Tracker offers the insights you need.
|
254 |
+
|
255 |
+
Experience the pulse of the AI community through our daily updated rankings and discover what's making waves in the world of practical AI applications.
|
256 |
+
""")
|
257 |
|
258 |
+
# ๋ผ๋์ค ๋ฒํผ ๋ณ๊ฒฝ ์ด๋ฒคํธ ์ฐ๊ฒฐ
|
259 |
+
space_selection.change(
|
260 |
fn=update_display,
|
261 |
+
inputs=[space_selection],
|
262 |
+
outputs=[trend_plot, info_box],
|
263 |
+
api_name="update_display"
|
264 |
)
|
265 |
|
266 |
if __name__ == "__main__":
|