Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -7,48 +7,64 @@ from io import BytesIO
|
|
7 |
|
8 |
def load_and_process_data():
|
9 |
try:
|
10 |
-
# Hugging Face 데이터셋에서 parquet 파일 다운로드
|
11 |
url = "https://huggingface.co/datasets/cfahlgren1/hub-stats/resolve/main/spaces.parquet"
|
12 |
response = requests.get(url)
|
13 |
df = pd.read_parquet(BytesIO(response.content))
|
14 |
|
15 |
-
# 데이터
|
16 |
-
print("Initial data shape:", df.shape)
|
17 |
-
|
18 |
-
# 30일 전 날짜 계산
|
19 |
thirty_days_ago = datetime.now() - timedelta(days=30)
|
20 |
-
|
21 |
-
# SQL 쿼리와 동일한 처리
|
22 |
df['createdAt'] = pd.to_datetime(df['createdAt'])
|
23 |
-
filtered_df = df[df['createdAt'] >= thirty_days_ago].copy()
|
24 |
-
filtered_df['created'] = filtered_df['createdAt'].dt.date
|
25 |
-
filtered_df = filtered_df.sort_values('trendingScore', ascending=False)
|
26 |
|
27 |
-
#
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
except Exception as e:
|
33 |
print(f"Error loading data: {e}")
|
34 |
-
return pd.DataFrame()
|
35 |
|
36 |
-
def create_trend_chart(
|
37 |
-
if
|
38 |
return None
|
39 |
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
fig = px.line(
|
42 |
space_data,
|
43 |
-
x='
|
44 |
-
y='
|
45 |
-
title=f'
|
46 |
-
labels={'
|
|
|
47 |
)
|
48 |
|
49 |
fig.update_layout(
|
50 |
xaxis_title="Date",
|
51 |
-
yaxis_title="
|
|
|
52 |
hovermode='x unified',
|
53 |
plot_bgcolor='white',
|
54 |
paper_bgcolor='white'
|
@@ -57,28 +73,20 @@ def create_trend_chart(space_data):
|
|
57 |
return fig
|
58 |
|
59 |
def update_display(selected_id):
|
60 |
-
global
|
61 |
|
62 |
if selected_id is None:
|
63 |
return None, "Please select a space"
|
64 |
|
65 |
try:
|
66 |
-
|
67 |
-
space_data = df[df['id'] == selected_id]
|
68 |
-
print(f"Selected ID: {selected_id}")
|
69 |
-
print(f"Found data: {not space_data.empty}")
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
space_info = space_data.iloc[0]
|
76 |
-
info_text = f"""ID: {space_info['id']}
|
77 |
-
Created At: {space_info['createdAt'].strftime('%Y-%m-%d')}
|
78 |
-
Trending Score: {space_info['trendingScore']:.2f}"""
|
79 |
|
80 |
-
|
81 |
-
chart = create_trend_chart(space_data)
|
82 |
|
83 |
return chart, info_text
|
84 |
|
@@ -86,57 +94,54 @@ Trending Score: {space_info['trendingScore']:.2f}"""
|
|
86 |
print(f"Error in update_display: {e}")
|
87 |
return None, f"Error processing data: {str(e)}"
|
88 |
|
89 |
-
#
|
90 |
print("Loading initial data...")
|
91 |
-
|
92 |
-
print("Data loaded. Shape:",
|
93 |
|
94 |
# Gradio 인터페이스 생성
|
95 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
96 |
gr.Markdown("# Trending Spaces Dashboard")
|
97 |
|
98 |
with gr.Row():
|
99 |
-
# 왼쪽 패널 - 스페이스 리스트
|
100 |
with gr.Column(scale=1):
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
space_list = gr.Dropdown(
|
114 |
-
choices=[],
|
115 |
-
label="Select a Space",
|
116 |
-
info="No data available"
|
117 |
-
)
|
118 |
|
119 |
-
# 스페이스 정보 표시
|
120 |
info_box = gr.Textbox(
|
121 |
label="Space Details",
|
122 |
value="",
|
123 |
interactive=False,
|
124 |
-
lines=
|
125 |
)
|
126 |
|
127 |
-
# 오른쪽 패널 - 트렌드 차트
|
128 |
with gr.Column(scale=2):
|
129 |
trend_plot = gr.Plot(
|
130 |
-
label="
|
131 |
)
|
132 |
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
space_list.change(
|
135 |
-
fn=
|
136 |
inputs=[space_list],
|
137 |
outputs=[trend_plot, info_box]
|
138 |
)
|
139 |
|
140 |
-
# 대시보드 실행
|
141 |
if __name__ == "__main__":
|
142 |
demo.launch(share=True)
|
|
|
7 |
|
8 |
def load_and_process_data():
|
9 |
try:
|
|
|
10 |
url = "https://huggingface.co/datasets/cfahlgren1/hub-stats/resolve/main/spaces.parquet"
|
11 |
response = requests.get(url)
|
12 |
df = pd.read_parquet(BytesIO(response.content))
|
13 |
|
14 |
+
# 30일치 데이터 준비
|
|
|
|
|
|
|
15 |
thirty_days_ago = datetime.now() - timedelta(days=30)
|
|
|
|
|
16 |
df['createdAt'] = pd.to_datetime(df['createdAt'])
|
|
|
|
|
|
|
17 |
|
18 |
+
# 모든 날짜에 대한 데이터 생성
|
19 |
+
dates = pd.date_range(start=thirty_days_ago, end=datetime.now(), freq='D')
|
20 |
+
all_spaces = df['id'].unique()
|
21 |
+
|
22 |
+
# 일자별 순위 계산을 위한 데이터프레임 생성
|
23 |
+
daily_ranks = []
|
24 |
+
|
25 |
+
for date in dates:
|
26 |
+
date_data = df[df['createdAt'].dt.date <= date.date()]
|
27 |
+
date_ranks = date_data.sort_values('trendingScore', ascending=False)
|
28 |
+
date_ranks['rank'] = range(1, len(date_ranks) + 1)
|
29 |
+
date_ranks['date'] = date.date()
|
30 |
+
daily_ranks.append(date_ranks[['id', 'date', 'rank', 'trendingScore', 'createdAt']])
|
31 |
|
32 |
+
daily_ranks_df = pd.concat(daily_ranks)
|
33 |
+
|
34 |
+
# 최신 순위로 정렬된 상위 100개 공간
|
35 |
+
latest_date = daily_ranks_df['date'].max()
|
36 |
+
top_100_spaces = daily_ranks_df[daily_ranks_df['date'] == latest_date].head(100)
|
37 |
+
|
38 |
+
return daily_ranks_df, top_100_spaces
|
39 |
except Exception as e:
|
40 |
print(f"Error loading data: {e}")
|
41 |
+
return pd.DataFrame(), pd.DataFrame()
|
42 |
|
43 |
+
def create_trend_chart(space_id, daily_ranks_df):
|
44 |
+
if space_id is None or daily_ranks_df.empty:
|
45 |
return None
|
46 |
|
47 |
+
space_data = daily_ranks_df[daily_ranks_df['id'] == space_id].copy()
|
48 |
+
if space_data.empty:
|
49 |
+
return None
|
50 |
+
|
51 |
+
# 순위를 역순으로 변경 (차트에서 위로 갈수록 좋은 순위)
|
52 |
+
max_rank = daily_ranks_df.groupby('date')['rank'].transform('max')
|
53 |
+
space_data['rank_reversed'] = max_rank - space_data['rank'] + 1
|
54 |
+
|
55 |
fig = px.line(
|
56 |
space_data,
|
57 |
+
x='date',
|
58 |
+
y='rank_reversed',
|
59 |
+
title=f'Daily Rank Trend for {space_id}',
|
60 |
+
labels={'date': 'Date', 'rank_reversed': 'Rank'},
|
61 |
+
markers=True
|
62 |
)
|
63 |
|
64 |
fig.update_layout(
|
65 |
xaxis_title="Date",
|
66 |
+
yaxis_title="Rank",
|
67 |
+
yaxis_autorange="reversed", # 순위 1이 위쪽에 오도록
|
68 |
hovermode='x unified',
|
69 |
plot_bgcolor='white',
|
70 |
paper_bgcolor='white'
|
|
|
73 |
return fig
|
74 |
|
75 |
def update_display(selected_id):
|
76 |
+
global daily_ranks_df
|
77 |
|
78 |
if selected_id is None:
|
79 |
return None, "Please select a space"
|
80 |
|
81 |
try:
|
82 |
+
latest_data = daily_ranks_df[daily_ranks_df['id'] == selected_id].sort_values('date').iloc[-1]
|
|
|
|
|
|
|
83 |
|
84 |
+
info_text = f"""ID: {latest_data['id']}
|
85 |
+
Current Rank: {int(latest_data['rank'])}
|
86 |
+
Trending Score: {latest_data['trendingScore']:.2f}
|
87 |
+
Created At: {latest_data['createdAt'].strftime('%Y-%m-%d')}"""
|
|
|
|
|
|
|
|
|
88 |
|
89 |
+
chart = create_trend_chart(selected_id, daily_ranks_df)
|
|
|
90 |
|
91 |
return chart, info_text
|
92 |
|
|
|
94 |
print(f"Error in update_display: {e}")
|
95 |
return None, f"Error processing data: {str(e)}"
|
96 |
|
97 |
+
# 데이터 로드
|
98 |
print("Loading initial data...")
|
99 |
+
daily_ranks_df, top_100_spaces = load_and_process_data()
|
100 |
+
print("Data loaded. Shape:", daily_ranks_df.shape)
|
101 |
|
102 |
# Gradio 인터페이스 생성
|
103 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
104 |
gr.Markdown("# Trending Spaces Dashboard")
|
105 |
|
106 |
with gr.Row():
|
|
|
107 |
with gr.Column(scale=1):
|
108 |
+
# 순위가 포함된 리스트로 표시
|
109 |
+
space_choices = [
|
110 |
+
f"Rank {idx+1}: {row['id']} (Score: {row['trendingScore']:.2f})"
|
111 |
+
for idx, (_, row) in enumerate(top_100_spaces.iterrows())
|
112 |
+
]
|
113 |
+
|
114 |
+
space_list = gr.Radio(
|
115 |
+
choices=space_choices,
|
116 |
+
label="Top 100 Trending Spaces",
|
117 |
+
info="Select a space to view its rank trend",
|
118 |
+
value=space_choices[0] if space_choices else None
|
119 |
+
)
|
|
|
|
|
|
|
|
|
|
|
120 |
|
|
|
121 |
info_box = gr.Textbox(
|
122 |
label="Space Details",
|
123 |
value="",
|
124 |
interactive=False,
|
125 |
+
lines=4
|
126 |
)
|
127 |
|
|
|
128 |
with gr.Column(scale=2):
|
129 |
trend_plot = gr.Plot(
|
130 |
+
label="Daily Rank Trend"
|
131 |
)
|
132 |
|
133 |
+
def handle_selection(selection):
|
134 |
+
if selection:
|
135 |
+
# 선택된 항목에서 space ID 추출
|
136 |
+
space_id = selection.split(': ')[1].split(' (Score')[0]
|
137 |
+
return update_display(space_id)
|
138 |
+
return None, "Please select a space"
|
139 |
+
|
140 |
space_list.change(
|
141 |
+
fn=handle_selection,
|
142 |
inputs=[space_list],
|
143 |
outputs=[trend_plot, info_box]
|
144 |
)
|
145 |
|
|
|
146 |
if __name__ == "__main__":
|
147 |
demo.launch(share=True)
|