openfree commited on
Commit
242d23e
Β·
verified Β·
1 Parent(s): 7683f53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +98 -109
app.py CHANGED
@@ -1000,32 +1000,24 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI μ„œλΉ„μŠ€") as
1000
 
1001
  with gr.Tab("전세계"):
1002
  gr.Markdown("λŒ€λ₯™λ³„λ‘œ 24μ‹œκ°„ 이내 λ‰΄μŠ€λ₯Ό κ²€μƒ‰ν•©λ‹ˆλ‹€.")
1003
-
1004
  with gr.Column():
1005
  with gr.Column(elem_id="status_area"):
1006
  with gr.Row():
1007
  query_global = gr.Textbox(label="검색어")
1008
  region_select = gr.Dropdown(
1009
- choices=[
1010
- "λ™μ•„μ‹œμ•„",
1011
- "λ™λ‚¨μ•„μ‹œμ•„/μ˜€μ„Έμ•„λ‹ˆμ•„",
1012
- "λ™μœ λŸ½",
1013
- "μ„œμœ λŸ½",
1014
- "쀑동/아프리카",
1015
- "아메리카"
1016
- ],
1017
  label="μ§€μ—­ 선택",
1018
  value="λ™μ•„μ‹œμ•„"
1019
  )
1020
  search_button_global = gr.Button("검색", variant="primary")
1021
-
1022
  status_message_global = gr.Markdown("")
1023
  translated_query_display_global = gr.Markdown("")
1024
-
1025
  with gr.Column(elem_id="results_area"):
1026
  articles_state_global = gr.State([])
1027
  global_article_components = create_article_components(MAX_GLOBAL_RESULTS)
1028
-
1029
 
1030
  global_article_components = []
1031
  for i in range(1000):
@@ -1136,112 +1128,109 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI μ„œλΉ„μŠ€") as
1136
  return outputs
1137
 
1138
 
1139
- def get_region_countries(region):
1140
  """μ„ νƒλœ μ§€μ—­μ˜ κ΅­κ°€ 및 μ–Έμ–΄ 정보 λ°˜ν™˜"""
1141
- if region == "λ™μ•„μ‹œμ•„":
1142
- return COUNTRY_LOCATIONS_EAST_ASIA, COUNTRY_LANGUAGES_EAST_ASIA
1143
- elif region == "λ™λ‚¨μ•„μ‹œμ•„/μ˜€μ„Έμ•„λ‹ˆμ•„":
1144
- return COUNTRY_LOCATIONS_SOUTHEAST_ASIA_OCEANIA, COUNTRY_LANGUAGES_SOUTHEAST_ASIA_OCEANIA
1145
- elif region == "λ™μœ λŸ½":
1146
- return COUNTRY_LOCATIONS_EAST_EUROPE, COUNTRY_LANGUAGES_EAST_EUROPE
1147
- elif region == "μ„œμœ λŸ½":
1148
- return COUNTRY_LOCATIONS_WEST_EUROPE, COUNTRY_LANGUAGES_WEST_EUROPE
1149
- elif region == "쀑동/아프리카":
1150
- return COUNTRY_LOCATIONS_ARAB_AFRICA, COUNTRY_LANGUAGES_ARAB_AFRICA
1151
- elif region == "아메리카":
1152
- return COUNTRY_LOCATIONS_AMERICA, COUNTRY_LANGUAGES_AMERICA
1153
- return {}, {}
1154
-
1155
-
1156
-
1157
- def search_global(query, region, articles_state_global):
1158
  """지역별 검색 ν•¨μˆ˜"""
1159
- status_msg = f"{region} μ§€μ—­ 검색을 μ‹œμž‘ν•©λ‹ˆλ‹€..."
1160
- all_results = []
 
 
 
 
 
1161
 
1162
- outputs = [
1163
- gr.update(value=status_msg, visible=True),
1164
- gr.update(value=f"**검색어:** {query}", visible=True),
1165
- ]
1166
-
1167
- for _ in global_article_components:
1168
- outputs.extend([
1169
- gr.update(visible=False), gr.update(), gr.update(),
1170
- gr.update(), gr.update()
1171
- ])
1172
- outputs.append([])
1173
-
1174
- yield outputs
1175
 
1176
- # μ„ νƒλœ μ§€μ—­μ˜ κ΅­κ°€ 정보 κ°€μ Έμ˜€κΈ°
1177
- locations, languages = get_region_countries(region)
1178
- total_countries = len(locations)
1179
 
1180
- for idx, (country, location) in enumerate(locations.items(), 1):
1181
- try:
1182
- status_msg = f"{region} - {country} 검색 쀑... ({idx}/{total_countries} κ΅­κ°€)"
1183
- outputs[0] = gr.update(value=status_msg, visible=True)
1184
- yield outputs
1185
-
1186
- error_message, articles = serphouse_search(query, country)
1187
- if not error_message and articles:
1188
- for article in articles:
1189
- article['source_country'] = country
1190
- article['region'] = region
1191
-
1192
- all_results.extend(articles)
1193
- sorted_results = sorted(all_results, key=lambda x: x.get('time', ''), reverse=True)
1194
-
1195
- seen_urls = set()
1196
- unique_results = []
1197
- for article in sorted_results:
1198
- url = article.get('link', '')
1199
- if url not in seen_urls:
1200
- seen_urls.add(url)
1201
- unique_results.append(article)
1202
-
1203
- unique_results = unique_results[:MAX_GLOBAL_RESULTS]
1204
 
1205
- outputs = [
1206
- gr.update(value=f"{region} - {idx}/{total_countries} κ΅­κ°€ 검색 μ™„λ£Œ\nν˜„μž¬κΉŒμ§€ 발견된 λ‰΄μŠ€: {len(unique_results)}건", visible=True),
1207
- gr.update(value=f"**검색어:** {query} | **μ§€μ—­:** {region}", visible=True),
1208
- ]
1209
 
1210
- for idx, comp in enumerate(global_article_components):
1211
- if idx < len(unique_results):
1212
- article = unique_results[idx]
1213
- image_url = article.get('image_url', '')
1214
- image_update = gr.update(value=image_url, visible=True) if image_url and not image_url.startswith('data:image') else gr.update(value=None, visible=False)
1215
-
1216
- korean_summary = translate_to_korean(article['snippet'])
1217
-
1218
- outputs.extend([
1219
- gr.update(visible=True),
1220
- gr.update(value=f"### [{article['title']}]({article['link']})"),
1221
- image_update,
1222
- gr.update(value=f"**μš”μ•½:** {article['snippet']}\n\n**ν•œκΈ€ μš”μ•½:** {korean_summary}"),
1223
- gr.update(value=f"**좜처:** {article['channel']} | **κ΅­κ°€:** {article['source_country']} | **μ§€μ—­:** {article['region']} | **μ‹œκ°„:** {article['time']}")
1224
- ])
1225
- else:
1226
- outputs.extend([
1227
- gr.update(visible=False),
1228
- gr.update(),
1229
- gr.update(),
1230
- gr.update(),
1231
- gr.update()
1232
- ])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1233
 
1234
- outputs.append(unique_results)
1235
- yield outputs
1236
-
1237
- except Exception as e:
1238
- print(f"Error searching {country}: {str(e)}")
1239
- continue
1240
-
1241
- final_status = f"{region} 검색 μ™„λ£Œ! 총 {len(unique_results)}개의 λ‰΄μŠ€κ°€ λ°œκ²¬λ˜μ—ˆμŠ΅λ‹ˆλ‹€."
1242
- outputs[0] = gr.update(value=final_status, visible=True)
1243
- yield outputs
1244
 
 
 
 
1245
 
1246
 
1247
 
 
1000
 
1001
  with gr.Tab("전세계"):
1002
  gr.Markdown("λŒ€λ₯™λ³„λ‘œ 24μ‹œκ°„ 이내 λ‰΄μŠ€λ₯Ό κ²€μƒ‰ν•©λ‹ˆλ‹€.")
1003
+
1004
  with gr.Column():
1005
  with gr.Column(elem_id="status_area"):
1006
  with gr.Row():
1007
  query_global = gr.Textbox(label="검색어")
1008
  region_select = gr.Dropdown(
1009
+ choices=REGIONS,
 
 
 
 
 
 
 
1010
  label="μ§€μ—­ 선택",
1011
  value="λ™μ•„μ‹œμ•„"
1012
  )
1013
  search_button_global = gr.Button("검색", variant="primary")
1014
+
1015
  status_message_global = gr.Markdown("")
1016
  translated_query_display_global = gr.Markdown("")
1017
+
1018
  with gr.Column(elem_id="results_area"):
1019
  articles_state_global = gr.State([])
1020
  global_article_components = create_article_components(MAX_GLOBAL_RESULTS)
 
1021
 
1022
  global_article_components = []
1023
  for i in range(1000):
 
1128
  return outputs
1129
 
1130
 
1131
+ def get_region_countries(region):
1132
  """μ„ νƒλœ μ§€μ—­μ˜ κ΅­κ°€ 및 μ–Έμ–΄ 정보 λ°˜ν™˜"""
1133
+ if region == "λ™μ•„μ‹œμ•„":
1134
+ return COUNTRY_LOCATIONS_EAST_ASIA, COUNTRY_LANGUAGES_EAST_ASIA
1135
+ elif region == "λ™λ‚¨μ•„μ‹œμ•„/μ˜€μ„Έμ•„λ‹ˆμ•„":
1136
+ return COUNTRY_LOCATIONS_SOUTHEAST_ASIA_OCEANIA, COUNTRY_LANGUAGES_SOUTHEAST_ASIA_OCEANIA
1137
+ elif region == "λ™μœ λŸ½":
1138
+ return COUNTRY_LOCATIONS_EAST_EUROPE, COUNTRY_LANGUAGES_EAST_EUROPE
1139
+ elif region == "μ„œμœ λŸ½":
1140
+ return COUNTRY_LOCATIONS_WEST_EUROPE, COUNTRY_LANGUAGES_WEST_EUROPE
1141
+ elif region == "쀑동/아프리카":
1142
+ return COUNTRY_LOCATIONS_ARAB_AFRICA, COUNTRY_LANGUAGES_ARAB_AFRICA
1143
+ elif region == "아메리카":
1144
+ return COUNTRY_LOCATIONS_AMERICA, COUNTRY_LANGUAGES_AMERICA
1145
+ return {}, {}
1146
+
1147
+ def search_global(query, region, articles_state_global):
 
 
1148
  """지역별 검색 ν•¨μˆ˜"""
1149
+ status_msg = f"{region} μ§€μ—­ 검색을 μ‹œμž‘ν•©λ‹ˆλ‹€..."
1150
+ all_results = []
1151
+
1152
+ outputs = [
1153
+ gr.update(value=status_msg, visible=True),
1154
+ gr.update(value=f"**검색어:** {query}", visible=True),
1155
+ ]
1156
 
1157
+ for _ in global_article_components:
1158
+ outputs.extend([
1159
+ gr.update(visible=False), gr.update(), gr.update(),
1160
+ gr.update(), gr.update()
1161
+ ])
1162
+ outputs.append([])
 
 
 
 
 
 
 
1163
 
1164
+ yield outputs
 
 
1165
 
1166
+ # μ„ νƒλœ μ§€μ—­μ˜ κ΅­κ°€ 정보 κ°€μ Έμ˜€κΈ°
1167
+ locations, languages = get_region_countries(region)
1168
+ total_countries = len(locations)
1169
+
1170
+ for idx, (country, location) in enumerate(locations.items(), 1):
1171
+ try:
1172
+ status_msg = f"{region} - {country} 검색 쀑... ({idx}/{total_countries} κ΅­κ°€)"
1173
+ outputs[0] = gr.update(value=status_msg, visible=True)
1174
+ yield outputs
1175
+
1176
+ error_message, articles = serphouse_search(query, country)
1177
+ if not error_message and articles:
1178
+ for article in articles:
1179
+ article['source_country'] = country
1180
+ article['region'] = region
 
 
 
 
 
 
 
 
 
1181
 
1182
+ all_results.extend(articles)
1183
+ sorted_results = sorted(all_results, key=lambda x: x.get('time', ''), reverse=True)
 
 
1184
 
1185
+ seen_urls = set()
1186
+ unique_results = []
1187
+ for article in sorted_results:
1188
+ url = article.get('link', '')
1189
+ if url not in seen_urls:
1190
+ seen_urls.add(url)
1191
+ unique_results.append(article)
1192
+
1193
+ unique_results = unique_results[:MAX_GLOBAL_RESULTS]
1194
+
1195
+ outputs = [
1196
+ gr.update(value=f"{region} - {idx}/{total_countries} κ΅­κ°€ 검색 μ™„λ£Œ\nν˜„μž¬κΉŒμ§€ 발견된 λ‰΄μŠ€: {len(unique_results)}건", visible=True),
1197
+ gr.update(value=f"**검색어:** {query} | **μ§€μ—­:** {region}", visible=True),
1198
+ ]
1199
+
1200
+ for idx, comp in enumerate(global_article_components):
1201
+ if idx < len(unique_results):
1202
+ article = unique_results[idx]
1203
+ image_url = article.get('image_url', '')
1204
+ image_update = gr.update(value=image_url, visible=True) if image_url and not image_url.startswith('data:image') else gr.update(value=None, visible=False)
1205
+
1206
+ korean_summary = translate_to_korean(article['snippet'])
1207
+
1208
+ outputs.extend([
1209
+ gr.update(visible=True),
1210
+ gr.update(value=f"### [{article['title']}]({article['link']})"),
1211
+ image_update,
1212
+ gr.update(value=f"**μš”μ•½:** {article['snippet']}\n\n**ν•œκΈ€ μš”μ•½:** {korean_summary}"),
1213
+ gr.update(value=f"**좜처:** {article['channel']} | **κ΅­κ°€:** {article['source_country']} | **μ§€μ—­:** {article['region']} | **μ‹œκ°„:** {article['time']}")
1214
+ ])
1215
+ else:
1216
+ outputs.extend([
1217
+ gr.update(visible=False),
1218
+ gr.update(),
1219
+ gr.update(),
1220
+ gr.update(),
1221
+ gr.update()
1222
+ ])
1223
+
1224
+ outputs.append(unique_results)
1225
+ yield outputs
1226
 
1227
+ except Exception as e:
1228
+ print(f"Error searching {country}: {str(e)}")
1229
+ continue
 
 
 
 
 
 
 
1230
 
1231
+ final_status = f"{region} 검색 μ™„λ£Œ! 총 {len(unique_results)}개의 λ‰΄μŠ€κ°€ λ°œκ²¬λ˜μ—ˆμŠ΅λ‹ˆλ‹€."
1232
+ outputs[0] = gr.update(value=final_status, visible=True)
1233
+ yield outputs
1234
 
1235
 
1236