openfree commited on
Commit
d37fe64
ยท
verified ยท
1 Parent(s): 6ab4377

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -72
app.py CHANGED
@@ -995,8 +995,53 @@ def refresh_data():
995
 
996
 
997
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
998
  def create_pie_chart(data, total_count, type_name="Spaces"):
999
- korean_count = len(data)
 
 
 
 
 
 
1000
  other_count = total_count - korean_count
1001
 
1002
  fig = go.Figure(data=[go.Pie(
@@ -1019,36 +1064,25 @@ def create_pie_chart(data, total_count, type_name="Spaces"):
1019
 
1020
  return fig
1021
 
1022
- def create_registration_bar_chart(data, type_name="Spaces"):
1023
- # ๋“ฑ๋ก์ž๋ณ„ ๊ฑด์ˆ˜ ์ง‘๊ณ„
1024
- registrations = {}
1025
- for item in data:
1026
- creator = item.get('id', '').split('/')[0] # ID์˜ ์ฒซ ๋ถ€๋ถ„์„ ๋“ฑ๋ก์ž๋กœ ๊ฐ„์ฃผ
1027
- registrations[creator] = registrations.get(creator, 0) + 1
1028
 
1029
- # ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ ์ค€๋น„
1030
- sorted_data = sorted(registrations.items(), key=lambda x: x[1], reverse=True)
1031
- creators = [x[0] for x in sorted_data]
1032
- counts = [x[1] for x in sorted_data]
1033
-
1034
- fig = go.Figure(data=[go.Bar(
1035
- x=creators,
1036
- y=counts,
1037
- text=counts,
1038
- textposition='auto',
1039
- marker_color='#FF6B6B'
1040
- )])
1041
 
1042
- fig.update_layout(
1043
- title=f"Korean {type_name} Registrations by Creator",
1044
- xaxis_title="Creator ID",
1045
- yaxis_title="Number of Registrations",
1046
- showlegend=False,
1047
- height=400,
1048
- width=700
1049
- )
1050
 
1051
- return fig
 
 
 
 
 
 
1052
 
1053
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
1054
  gr.Markdown("""
@@ -1063,45 +1097,71 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
1063
  trending_plot = gr.Plot()
1064
  with gr.Row():
1065
  # ์›ํ˜• ๊ทธ๋ž˜ํ”„์™€ ๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„๋ฅผ ์œ„ํ•œ ์ปจํ…Œ์ด๋„ˆ ์ถ”๊ฐ€
1066
- with gr.Column():
1067
- spaces_pie_chart = gr.Plot(label="Korean Spaces Distribution")
1068
- with gr.Column():
1069
- spaces_bar_chart = gr.Plot(label="Registrations by Creator")
 
 
 
 
 
 
1070
  trending_info = gr.HTML()
1071
- trending_df = gr.DataFrame()
 
 
 
 
1072
 
1073
  with gr.Tab("Models Trending"):
1074
  models_plot = gr.Plot()
1075
  with gr.Row():
1076
  # ์›ํ˜• ๊ทธ๋ž˜ํ”„์™€ ๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„๋ฅผ ์œ„ํ•œ ์ปจํ…Œ์ด๋„ˆ ์ถ”๊ฐ€
1077
- with gr.Column():
1078
- models_pie_chart = gr.Plot(label="Korean Models Distribution")
1079
- with gr.Column():
1080
- models_bar_chart = gr.Plot(label="Registrations by Creator")
 
 
 
 
 
 
1081
  models_info = gr.HTML()
1082
- models_df = gr.DataFrame()
1083
-
 
 
 
 
1084
  def refresh_all_data():
1085
- spaces_results = get_spaces_data("trending")
1086
- models_results = get_models_data()
1087
-
1088
- # Spaces ์ฐจํŠธ ์ƒ์„ฑ
1089
- spaces_pie = create_pie_chart(spaces_results[2], 500, "Spaces") # DataFrame์—์„œ ๋ฐ์ดํ„ฐ ์ถ”์ถœ
1090
- spaces_bar = create_registration_bar_chart(spaces_results[2], "Spaces")
1091
-
1092
- # Models ์ฐจํŠธ ์ƒ์„ฑ
1093
- models_pie = create_pie_chart(models_results[2], 3000, "Models") # DataFrame์—์„œ ๋ฐ์ดํ„ฐ ์ถ”์ถœ
1094
- models_bar = create_registration_bar_chart(models_results[2], "Models")
1095
-
1096
- return [
1097
- spaces_results[0], spaces_results[1], spaces_results[2],
1098
- spaces_pie, spaces_bar,
1099
- models_results[0], models_results[1], models_results[2],
1100
- models_pie, models_bar
1101
- ]
1102
-
 
 
 
 
 
 
1103
  refresh_btn.click(
1104
- refresh_all_data,
1105
  outputs=[
1106
  trending_plot, trending_info, trending_df,
1107
  spaces_pie_chart, spaces_bar_chart,
@@ -1111,23 +1171,44 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
1111
  )
1112
 
1113
  # ์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ ๋กœ๋“œ
1114
- initial_data = refresh_all_data()
1115
-
1116
- # ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •
1117
- trending_plot.value = initial_data[0]
1118
- trending_info.value = initial_data[1]
1119
- trending_df.value = initial_data[2]
1120
- spaces_pie_chart.value = initial_data[3]
1121
- spaces_bar_chart.value = initial_data[4]
1122
- models_plot.value = initial_data[5]
1123
- models_info.value = initial_data[6]
1124
- models_df.value = initial_data[7]
1125
- models_pie_chart.value = initial_data[8]
1126
- models_bar_chart.value = initial_data[9]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1127
 
1128
  # Gradio ์•ฑ ์‹คํ–‰
1129
  demo.launch(
1130
  server_name="0.0.0.0",
1131
  server_port=7860,
1132
- share=False
 
1133
  )
 
995
 
996
 
997
 
998
+ def create_registration_bar_chart(data, type_name="Spaces"):
999
+ # DataFrame์ธ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
1000
+ if isinstance(data, pd.DataFrame):
1001
+ registrations = data['Space ID' if type_name == "Spaces" else 'Model ID'].apply(
1002
+ lambda x: x.split('/')[0]
1003
+ ).value_counts()
1004
+ else:
1005
+ # ๋ฆฌ์ŠคํŠธ๋‚˜ ๋‹ค๋ฅธ ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ์ธ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
1006
+ registrations = {}
1007
+ for item in data:
1008
+ if isinstance(item, dict):
1009
+ creator = item.get('id', '').split('/')[0]
1010
+ else:
1011
+ creator = str(item).split('/')[0]
1012
+ registrations[creator] = registrations.get(creator, 0) + 1
1013
+ registrations = pd.Series(registrations)
1014
+
1015
+ # ์ •๋ ฌ๋œ ๋ฐ์ดํ„ฐ ์ค€๋น„
1016
+ registrations = registrations.sort_values(ascending=False)
1017
+
1018
+ fig = go.Figure(data=[go.Bar(
1019
+ x=registrations.index,
1020
+ y=registrations.values,
1021
+ text=registrations.values,
1022
+ textposition='auto',
1023
+ marker_color='#FF6B6B'
1024
+ )])
1025
+
1026
+ fig.update_layout(
1027
+ title=f"Korean {type_name} Registrations by Creator",
1028
+ xaxis_title="Creator ID",
1029
+ yaxis_title="Number of Registrations",
1030
+ showlegend=False,
1031
+ height=400,
1032
+ width=700
1033
+ )
1034
+
1035
+ return fig
1036
+
1037
  def create_pie_chart(data, total_count, type_name="Spaces"):
1038
+ # DataFrame์ธ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
1039
+ if isinstance(data, pd.DataFrame):
1040
+ korean_count = len(data)
1041
+ else:
1042
+ # ๋ฆฌ์ŠคํŠธ๋‚˜ ๋‹ค๋ฅธ ํ˜•ํƒœ์˜ ๋ฐ์ดํ„ฐ์ธ ๊ฒฝ์šฐ ์ฒ˜๋ฆฌ
1043
+ korean_count = len(data)
1044
+
1045
  other_count = total_count - korean_count
1046
 
1047
  fig = go.Figure(data=[go.Pie(
 
1064
 
1065
  return fig
1066
 
1067
+ def refresh_all_data():
1068
+ spaces_results = get_spaces_data("trending")
1069
+ models_results = get_models_data()
 
 
 
1070
 
1071
+ # Spaces ์ฐจํŠธ ์ƒ์„ฑ
1072
+ spaces_pie = create_pie_chart(spaces_results[2], 500, "Spaces")
1073
+ spaces_bar = create_registration_bar_chart(spaces_results[2], "Spaces")
 
 
 
 
 
 
 
 
 
1074
 
1075
+ # Models ์ฐจํŠธ ์ƒ์„ฑ
1076
+ models_pie = create_pie_chart(models_results[2], 3000, "Models")
1077
+ models_bar = create_registration_bar_chart(models_results[2], "Models")
 
 
 
 
 
1078
 
1079
+ return [
1080
+ spaces_results[0], spaces_results[1], spaces_results[2],
1081
+ spaces_pie, spaces_bar,
1082
+ models_results[0], models_results[1], models_results[2],
1083
+ models_pie, models_bar
1084
+ ]
1085
+
1086
 
1087
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
1088
  gr.Markdown("""
 
1097
  trending_plot = gr.Plot()
1098
  with gr.Row():
1099
  # ์›ํ˜• ๊ทธ๋ž˜ํ”„์™€ ๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„๋ฅผ ์œ„ํ•œ ์ปจํ…Œ์ด๋„ˆ ์ถ”๊ฐ€
1100
+ with gr.Column(scale=1):
1101
+ spaces_pie_chart = gr.Plot(
1102
+ label="Korean Spaces Distribution",
1103
+ elem_id="spaces_pie"
1104
+ )
1105
+ with gr.Column(scale=2):
1106
+ spaces_bar_chart = gr.Plot(
1107
+ label="Registrations by Creator",
1108
+ elem_id="spaces_bar"
1109
+ )
1110
  trending_info = gr.HTML()
1111
+ trending_df = gr.DataFrame(
1112
+ headers=["Rank", "Space ID", "Title", "Likes", "URL"],
1113
+ datatype=["number", "str", "str", "number", "str"],
1114
+ row_count=(10, "dynamic")
1115
+ )
1116
 
1117
  with gr.Tab("Models Trending"):
1118
  models_plot = gr.Plot()
1119
  with gr.Row():
1120
  # ์›ํ˜• ๊ทธ๋ž˜ํ”„์™€ ๋ง‰๋Œ€ ๊ทธ๋ž˜ํ”„๋ฅผ ์œ„ํ•œ ์ปจํ…Œ์ด๋„ˆ ์ถ”๊ฐ€
1121
+ with gr.Column(scale=1):
1122
+ models_pie_chart = gr.Plot(
1123
+ label="Korean Models Distribution",
1124
+ elem_id="models_pie"
1125
+ )
1126
+ with gr.Column(scale=2):
1127
+ models_bar_chart = gr.Plot(
1128
+ label="Registrations by Creator",
1129
+ elem_id="models_bar"
1130
+ )
1131
  models_info = gr.HTML()
1132
+ models_df = gr.DataFrame(
1133
+ headers=["Global Rank", "Model ID", "Title", "Downloads", "Likes", "Korea Search", "URL"],
1134
+ datatype=["str", "str", "str", "str", "str", "str", "str"],
1135
+ row_count=(10, "dynamic")
1136
+ )
1137
+
1138
  def refresh_all_data():
1139
+ try:
1140
+ spaces_results = get_spaces_data("trending")
1141
+ models_results = get_models_data()
1142
+
1143
+ # Spaces ์ฐจํŠธ ์ƒ์„ฑ
1144
+ spaces_pie = create_pie_chart(spaces_results[2], 500, "Spaces")
1145
+ spaces_bar = create_registration_bar_chart(spaces_results[2], "Spaces")
1146
+
1147
+ # Models ์ฐจํŠธ ์ƒ์„ฑ
1148
+ models_pie = create_pie_chart(models_results[2], 3000, "Models")
1149
+ models_bar = create_registration_bar_chart(models_results[2], "Models")
1150
+
1151
+ return [
1152
+ spaces_results[0], spaces_results[1], spaces_results[2],
1153
+ spaces_pie, spaces_bar,
1154
+ models_results[0], models_results[1], models_results[2],
1155
+ models_pie, models_bar
1156
+ ]
1157
+ except Exception as e:
1158
+ print(f"Error in refresh_all_data: {str(e)}")
1159
+ # ์—๋Ÿฌ ๋ฐœ์ƒ ์‹œ ๊ธฐ๋ณธ๊ฐ’ ๋ฐ˜ํ™˜
1160
+ return [None] * 10
1161
+
1162
+ # ์ƒˆ๋กœ๊ณ ์นจ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ
1163
  refresh_btn.click(
1164
+ fn=refresh_all_data,
1165
  outputs=[
1166
  trending_plot, trending_info, trending_df,
1167
  spaces_pie_chart, spaces_bar_chart,
 
1171
  )
1172
 
1173
  # ์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ ๋กœ๋“œ
1174
+ try:
1175
+ initial_data = refresh_all_data()
1176
+
1177
+ # ์ดˆ๊ธฐ๊ฐ’ ์„ค์ •
1178
+ trending_plot.value = initial_data[0]
1179
+ trending_info.value = initial_data[1]
1180
+ trending_df.value = initial_data[2]
1181
+ spaces_pie_chart.value = initial_data[3]
1182
+ spaces_bar_chart.value = initial_data[4]
1183
+ models_plot.value = initial_data[5]
1184
+ models_info.value = initial_data[6]
1185
+ models_df.value = initial_data[7]
1186
+ models_pie_chart.value = initial_data[8]
1187
+ models_bar_chart.value = initial_data[9]
1188
+ except Exception as e:
1189
+ print(f"Error loading initial data: {str(e)}")
1190
+ gr.Warning("์ดˆ๊ธฐ ๋ฐ์ดํ„ฐ ๋กœ๋“œ ์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.")
1191
+
1192
+ # CSS ์Šคํƒ€์ผ ์ถ”๊ฐ€
1193
+ demo.load("""
1194
+ <style>
1195
+ #spaces_pie, #models_pie {
1196
+ min-height: 400px;
1197
+ border-radius: 10px;
1198
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
1199
+ }
1200
+ #spaces_bar, #models_bar {
1201
+ min-height: 400px;
1202
+ border-radius: 10px;
1203
+ box-shadow: 0 2px 4px rgba(0,0,0,0.1);
1204
+ }
1205
+ </style>
1206
+ """)
1207
 
1208
  # Gradio ์•ฑ ์‹คํ–‰
1209
  demo.launch(
1210
  server_name="0.0.0.0",
1211
  server_port=7860,
1212
+ share=False,
1213
+ show_error=True
1214
  )