ginipick commited on
Commit
360fc46
ยท
verified ยท
1 Parent(s): 76e34c2

Create app-backup.py

Browse files
Files changed (1) hide show
  1. app-backup.py +177 -0
app-backup.py ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)