Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,24 +6,28 @@ import requests
|
|
6 |
from io import BytesIO
|
7 |
|
8 |
def load_and_process_data():
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
24 |
|
25 |
def create_trend_chart(selected_id, df):
|
26 |
-
if
|
27 |
return None
|
28 |
|
29 |
space_data = df[df['id'] == selected_id]
|
@@ -50,19 +54,27 @@ def create_trend_chart(selected_id, df):
|
|
50 |
return fig
|
51 |
|
52 |
def update_display(selected_id, df):
|
53 |
-
if selected_id is None:
|
54 |
-
return None, ""
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
#
|
66 |
df = load_and_process_data()
|
67 |
|
68 |
# Gradio 인터페이스 생성
|
@@ -72,12 +84,19 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
72 |
with gr.Row():
|
73 |
# 왼쪽 패널 - 스페이스 리스트
|
74 |
with gr.Column(scale=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
space_list = gr.Dropdown(
|
76 |
-
choices=
|
77 |
-
for _, row in df.iterrows()],
|
78 |
label="Select a Space",
|
79 |
info="Click to select a space and view its trend",
|
80 |
-
value=
|
81 |
)
|
82 |
|
83 |
# 스페이스 정보 표시
|
@@ -95,12 +114,15 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
95 |
)
|
96 |
|
97 |
# 이벤트 핸들러
|
|
|
|
|
|
|
98 |
space_list.change(
|
99 |
-
fn=
|
100 |
inputs=[space_list],
|
101 |
outputs=[trend_plot, info_box]
|
102 |
)
|
103 |
|
104 |
# 대시보드 실행
|
105 |
if __name__ == "__main__":
|
106 |
-
demo.launch()
|
|
|
6 |
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 |
+
# 30일 전 날짜 계산
|
16 |
+
thirty_days_ago = datetime.now() - timedelta(days=30)
|
17 |
+
|
18 |
+
# SQL 쿼리와 동일한 처리
|
19 |
+
df['createdAt'] = pd.to_datetime(df['createdAt'])
|
20 |
+
filtered_df = df[df['createdAt'] >= thirty_days_ago].copy()
|
21 |
+
filtered_df['created'] = filtered_df['createdAt'].dt.date
|
22 |
+
filtered_df = filtered_df.sort_values('trendingScore', ascending=False)
|
23 |
+
|
24 |
+
return filtered_df.head(100)
|
25 |
+
except Exception as e:
|
26 |
+
print(f"Error loading data: {e}")
|
27 |
+
return pd.DataFrame()
|
28 |
|
29 |
def create_trend_chart(selected_id, df):
|
30 |
+
if selected_id is None or df.empty:
|
31 |
return None
|
32 |
|
33 |
space_data = df[df['id'] == selected_id]
|
|
|
54 |
return fig
|
55 |
|
56 |
def update_display(selected_id, df):
|
57 |
+
if selected_id is None or df.empty:
|
58 |
+
return None, "No data selected"
|
59 |
|
60 |
+
try:
|
61 |
+
space_data = df[df['id'] == selected_id]
|
62 |
+
if space_data.empty:
|
63 |
+
return None, "Space not found"
|
64 |
+
|
65 |
+
space_info = space_data.iloc[0]
|
66 |
+
info_text = f"""ID: {space_info['id']}
|
67 |
+
Created At: {space_info['createdAt'].strftime('%Y-%m-%d')}
|
68 |
+
Trending Score: {space_info['trendingScore']:.2f}"""
|
69 |
+
|
70 |
+
chart = create_trend_chart(selected_id, df)
|
71 |
+
|
72 |
+
return chart, info_text
|
73 |
+
except Exception as e:
|
74 |
+
print(f"Error updating display: {e}")
|
75 |
+
return None, f"Error: {str(e)}"
|
76 |
|
77 |
+
# 전역 변수로 데이터프레임 로드
|
78 |
df = load_and_process_data()
|
79 |
|
80 |
# Gradio 인터페이스 생성
|
|
|
84 |
with gr.Row():
|
85 |
# 왼쪽 패널 - 스페이스 리스트
|
86 |
with gr.Column(scale=1):
|
87 |
+
if not df.empty:
|
88 |
+
choices = [(row['id'], f"{row['id']} (Score: {row['trendingScore']:.2f})")
|
89 |
+
for _, row in df.iterrows()]
|
90 |
+
default_value = df['id'].iloc[0]
|
91 |
+
else:
|
92 |
+
choices = []
|
93 |
+
default_value = None
|
94 |
+
|
95 |
space_list = gr.Dropdown(
|
96 |
+
choices=choices,
|
|
|
97 |
label="Select a Space",
|
98 |
info="Click to select a space and view its trend",
|
99 |
+
value=default_value
|
100 |
)
|
101 |
|
102 |
# 스페이스 정보 표시
|
|
|
114 |
)
|
115 |
|
116 |
# 이벤트 핸들러
|
117 |
+
def wrapped_update_display(selected_id):
|
118 |
+
return update_display(selected_id, df)
|
119 |
+
|
120 |
space_list.change(
|
121 |
+
fn=wrapped_update_display,
|
122 |
inputs=[space_list],
|
123 |
outputs=[trend_plot, info_box]
|
124 |
)
|
125 |
|
126 |
# 대시보드 실행
|
127 |
if __name__ == "__main__":
|
128 |
+
demo.launch(share=True) # share=True 추가
|