ginipick commited on
Commit
4c178d3
ยท
verified ยท
1 Parent(s): e23690a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -31
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(selected_id, df):
30
- if selected_id is None or df.empty:
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 {selected_id}',
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, 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 ์ธํ„ฐํŽ˜์ด์Šค ์ƒ์„ฑ
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
- 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
  # ์ŠคํŽ˜์ด์Šค ์ •๋ณด ํ‘œ์‹œ
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=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 ์ถ”๊ฐ€
 
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)