Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,9 @@ def load_and_process_data():
|
|
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 |
|
@@ -21,17 +24,17 @@ def load_and_process_data():
|
|
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(
|
30 |
-
if
|
31 |
-
return None
|
32 |
-
|
33 |
-
space_data = df[df['id'] == selected_id]
|
34 |
-
if space_data.empty:
|
35 |
return None
|
36 |
|
37 |
# ์ ํ๋ space์ ํธ๋ ๋ ์ฐจํธ ์์ฑ
|
@@ -39,7 +42,7 @@ def create_trend_chart(selected_id, df):
|
|
39 |
space_data,
|
40 |
x='created',
|
41 |
y='trendingScore',
|
42 |
-
title=f'Trending Score for {
|
43 |
labels={'created': 'Date', 'trendingScore': 'Trending Score'}
|
44 |
)
|
45 |
|
@@ -53,29 +56,40 @@ def create_trend_chart(selected_id, df):
|
|
53 |
|
54 |
return fig
|
55 |
|
56 |
-
def update_display(selected_id
|
57 |
-
|
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 |
-
|
|
|
71 |
|
72 |
return chart, info_text
|
|
|
73 |
except Exception as e:
|
74 |
-
print(f"Error
|
75 |
-
return None, f"Error: {str(e)}"
|
76 |
|
77 |
# ์ ์ญ ๋ณ์๋ก ๋ฐ์ดํฐํ๋ ์ ๋ก๋
|
|
|
78 |
df = load_and_process_data()
|
|
|
79 |
|
80 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
81 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
@@ -85,19 +99,22 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
85 |
# ์ผ์ชฝ ํจ๋ - ์คํ์ด์ค ๋ฆฌ์คํธ
|
86 |
with gr.Column(scale=1):
|
87 |
if not df.empty:
|
88 |
-
|
89 |
-
|
90 |
-
default_value =
|
91 |
-
else:
|
92 |
-
choices = []
|
93 |
-
default_value = None
|
94 |
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
101 |
|
102 |
# ์คํ์ด์ค ์ ๋ณด ํ์
|
103 |
info_box = gr.Textbox(
|
@@ -114,15 +131,12 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
114 |
)
|
115 |
|
116 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
117 |
-
def wrapped_update_display(selected_id):
|
118 |
-
return update_display(selected_id, df)
|
119 |
-
|
120 |
space_list.change(
|
121 |
-
fn=
|
122 |
inputs=[space_list],
|
123 |
outputs=[trend_plot, info_box]
|
124 |
)
|
125 |
|
126 |
# ๋์๋ณด๋ ์คํ
|
127 |
if __name__ == "__main__":
|
128 |
-
demo.launch(share=True)
|
|
|
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 |
|
|
|
24 |
filtered_df['created'] = filtered_df['createdAt'].dt.date
|
25 |
filtered_df = filtered_df.sort_values('trendingScore', ascending=False)
|
26 |
|
27 |
+
# ํํฐ๋ง๋ ๋ฐ์ดํฐ ํ์ธ
|
28 |
+
print("Filtered data shape:", filtered_df.shape)
|
29 |
+
print("Sample data:", filtered_df.head(1))
|
30 |
+
|
31 |
return filtered_df.head(100)
|
32 |
except Exception as e:
|
33 |
print(f"Error loading data: {e}")
|
34 |
return pd.DataFrame()
|
35 |
|
36 |
+
def create_trend_chart(space_data):
|
37 |
+
if space_data is None or space_data.empty:
|
|
|
|
|
|
|
|
|
38 |
return None
|
39 |
|
40 |
# ์ ํ๋ space์ ํธ๋ ๋ ์ฐจํธ ์์ฑ
|
|
|
42 |
space_data,
|
43 |
x='created',
|
44 |
y='trendingScore',
|
45 |
+
title=f'Trending Score for {space_data.iloc[0]["id"]}',
|
46 |
labels={'created': 'Date', 'trendingScore': 'Trending Score'}
|
47 |
)
|
48 |
|
|
|
56 |
|
57 |
return fig
|
58 |
|
59 |
+
def update_display(selected_id):
|
60 |
+
global df # ์ ์ญ ๋ฐ์ดํฐํ๋ ์ ์ฌ์ฉ
|
|
|
61 |
|
62 |
+
if selected_id is None:
|
63 |
+
return None, "Please select a space"
|
64 |
+
|
65 |
try:
|
66 |
+
# ์ ํ๋ ID๋ก ๋ฐ์ดํฐ ํํฐ๋ง
|
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 |
if space_data.empty:
|
72 |
+
return None, "Space not found in dataset"
|
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 |
+
|
85 |
except Exception as e:
|
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 |
df = load_and_process_data()
|
92 |
+
print("Data loaded. Shape:", df.shape)
|
93 |
|
94 |
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
95 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
|
99 |
# ์ผ์ชฝ ํจ๋ - ์คํ์ด์ค ๋ฆฌ์คํธ
|
100 |
with gr.Column(scale=1):
|
101 |
if not df.empty:
|
102 |
+
# ๋๋กญ๋ค์ด ์ ํ์ง ์์ฑ
|
103 |
+
space_choices = df['id'].tolist()
|
104 |
+
default_value = space_choices[0] if space_choices else None
|
|
|
|
|
|
|
105 |
|
106 |
+
space_list = gr.Dropdown(
|
107 |
+
choices=space_choices,
|
108 |
+
label="Select a Space",
|
109 |
+
value=default_value,
|
110 |
+
info="Select a space to view its trend"
|
111 |
+
)
|
112 |
+
else:
|
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(
|
|
|
131 |
)
|
132 |
|
133 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
|
|
|
|
|
|
134 |
space_list.change(
|
135 |
+
fn=update_display,
|
136 |
inputs=[space_list],
|
137 |
outputs=[trend_plot, info_box]
|
138 |
)
|
139 |
|
140 |
# ๋์๋ณด๋ ์คํ
|
141 |
if __name__ == "__main__":
|
142 |
+
demo.launch(share=True)
|