seawolf2357 commited on
Commit
70ba540
Β·
verified Β·
1 Parent(s): d2cccd5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -41
app.py CHANGED
@@ -172,20 +172,17 @@ def translate_query(query, country):
172
  target_lang = COUNTRY_LANGUAGES[country]
173
  print(f"λ²ˆμ—­ μ‹œλ„: {query} -> {country}({target_lang})")
174
 
175
- # Google Translate API URL
176
  url = f"https://translate.googleapis.com/translate_a/single"
177
-
178
  params = {
179
  "client": "gtx",
180
- "sl": "auto", # μžλ™ μ–Έμ–΄ 감지
181
- "tl": target_lang, # λŒ€μƒ μ–Έμ–΄
182
  "dt": "t",
183
  "q": query
184
  }
185
 
186
  response = requests.get(url, params=params)
187
  translated_text = response.json()[0][0][0]
188
-
189
  print(f"λ²ˆμ—­ μ™„λ£Œ: {query} -> {translated_text} ({country})")
190
  return translated_text
191
 
@@ -195,16 +192,26 @@ def translate_query(query, country):
195
  print(f"λ²ˆμ—­ 였λ₯˜: {str(e)}")
196
  return query
197
 
198
- def is_english(text):
199
- # μ˜μ–΄μ™€ 일반적인 기호만 ν¬ν•¨λœ 경우 True λ°˜ν™˜
200
- return all(ord(char) < 128 for char in text.replace(' ', '').replace('-', '').replace('_', ''))
201
-
202
-
203
- def is_korean(text):
204
- return any('\uAC00' <= char <= '\uD7A3' for char in text)
 
 
 
 
 
 
 
 
 
 
205
 
206
  def is_english(text):
207
- return all(ord(char) < 128 for char in text.replace(' ', ''))
208
 
209
  def is_korean(text):
210
  return any('\uAC00' <= char <= '\uD7A3' for char in text)
@@ -212,22 +219,20 @@ def is_korean(text):
212
  def search_serphouse(query, country, page=1, num_result=10):
213
  url = "https://api.serphouse.com/serp/live"
214
 
215
- # 24μ‹œκ°„ 이내 λ‚ μ§œ λ²”μœ„ μ„€μ •
216
  now = datetime.utcnow()
217
  yesterday = now - timedelta(days=1)
218
  date_range = f"{yesterday.strftime('%Y-%m-%d')},{now.strftime('%Y-%m-%d')}"
219
 
220
- # 검색어 λ²ˆμ—­
221
  translated_query = translate_query(query, country)
222
  print(f"Original query: {query}")
223
  print(f"Translated query: {translated_query}")
224
 
225
  payload = {
226
  "data": {
227
- "q": translated_query, # μ—¬κΈ°λ₯Ό λ²ˆμ—­λœ κ²€μƒ‰μ–΄λ‘œ λ³€κ²½
228
  "domain": "google.com",
229
  "loc": COUNTRY_LOCATIONS.get(country, "United States"),
230
- "lang": COUNTRY_LANGUAGES.get(country, "en"), # μ–Έμ–΄ μ½”λ“œλ„ ν•΄λ‹Ή κ΅­κ°€ μ–Έμ–΄λ‘œ λ³€κ²½
231
  "device": "desktop",
232
  "serp_type": "news",
233
  "page": "1",
@@ -245,7 +250,7 @@ def search_serphouse(query, country, page=1, num_result=10):
245
 
246
  try:
247
  response = requests.post(url, json=payload, headers=headers)
248
- print("Request payload:", json.dumps(payload, indent=2, ensure_ascii=False)) # ensure_ascii=False μΆ”κ°€
249
  print("Response status:", response.status_code)
250
 
251
  response.raise_for_status()
@@ -291,23 +296,22 @@ footer {visibility: hidden;}
291
 
292
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI μ„œλΉ„μŠ€") as iface:
293
  gr.Markdown("검색어λ₯Ό μž…λ ₯ν•˜κ³  μ›ν•˜λŠ” κ΅­κ°€(67개ꡭ)λ₯Ό μ„ νƒν•˜λ©΄, 검색어와 μΌμΉ˜ν•˜λŠ” 24μ‹œκ°„ 이내 λ‰΄μŠ€λ₯Ό μ΅œλŒ€ 100개 좜λ ₯ν•©λ‹ˆλ‹€.")
294
- gr.Markdown("κ΅­κ°€ 선택후 검색어에 'ν•œκΈ€'을 μž…λ ₯ν•˜λ©΄ ν˜„μ§€ μ–Έμ–΄λ‘œ λ²ˆμ—­λ˜μ–΄ κ²€μƒ‰ν•©λ‹ˆλ‹€. 예: 'Taiwan' κ΅­κ°€ 선택후 'μ‚Όμ„±' μž…λ ₯μ‹œ 'δΈ‰ζ˜Ÿ'으둜 μžλ™ 검색 ")
295
 
296
  with gr.Column():
297
  with gr.Row():
298
  query = gr.Textbox(label="검색어")
299
  country = gr.Dropdown(MAJOR_COUNTRIES, label="κ΅­κ°€", value="South Korea")
300
-
301
- # 검색 μƒνƒœ λ©”μ‹œμ§€ μ»΄ν¬λ„ŒνŠΈ μΆ”κ°€
302
- search_status = gr.Markdown(visible=False)
303
 
304
  # λ²ˆμ—­ κ²°κ³Όλ₯Ό λ³΄μ—¬μ£ΌλŠ” μ»΄ν¬λ„ŒνŠΈ
305
  translated_query_display = gr.Markdown(visible=False)
306
-
307
  search_button = gr.Button("검색", variant="primary")
308
-
309
  progress = gr.Progress()
310
- status_message = gr.Markdown(visible=False)
311
  articles_state = gr.State([])
312
 
313
  article_components = []
@@ -317,7 +321,7 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI μ„œλΉ„μŠ€") as
317
  image = gr.Image(width=200, height=150)
318
  snippet = gr.Markdown()
319
  info = gr.Markdown()
320
-
321
  article_components.append({
322
  'group': article_group,
323
  'title': title,
@@ -328,8 +332,8 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI μ„œλΉ„μŠ€") as
328
  })
329
 
330
  def search_and_display(query, country, articles_state, progress=gr.Progress()):
331
- # 검색 μ‹œμž‘ λ©”μ‹œμ§€ ν‘œμ‹œ
332
- search_status_output = gr.update(value="검색을 μ§„ν–‰μ€‘μž…λ‹ˆλ‹€. μž μ‹œλ§Œ κΈ°λ‹€λ¦¬μ„Έμš”...", visible=True)
333
 
334
  progress(0, desc="검색어 λ²ˆμ—­ 쀑...")
335
 
@@ -341,10 +345,9 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI μ„œλΉ„μŠ€") as
341
  error_message, articles = serphouse_search(query, country)
342
  progress(0.5, desc="κ²°κ³Ό 처리 쀑...")
343
 
344
- outputs = [
345
- search_status_output, # 검색 μƒνƒœ λ©”μ‹œμ§€
346
- gr.update(value=translated_display, visible=True) # λ²ˆμ—­ κ²°κ³Ό ν‘œμ‹œ
347
- ]
348
 
349
  if error_message:
350
  outputs.append(gr.update(value=error_message, visible=True))
@@ -364,11 +367,14 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI μ„œλΉ„μŠ€") as
364
  image_url = article['image_url']
365
  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)
366
 
 
 
 
367
  outputs.extend([
368
  gr.update(visible=True),
369
  gr.update(value=f"### [{article['title']}]({article['link']})"),
370
  image_update,
371
- gr.update(value=f"**μš”μ•½:** {article['snippet']}"),
372
  gr.update(value=f"**좜처:** {article['channel']} | **μ‹œκ°„:** {article['time']}")
373
  ])
374
  else:
@@ -380,23 +386,24 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI μ„œλΉ„μŠ€") as
380
 
381
  progress(1.0, desc="μ™„λ£Œ!")
382
  outputs.append(articles_state)
383
- outputs.append(gr.update(visible=False))
384
 
385
  # 검색 μ™„λ£Œ ν›„ μƒνƒœ λ©”μ‹œμ§€ μˆ¨κΉ€
386
- search_status_output = gr.update(visible=False)
387
- outputs[0] = search_status_output
388
 
389
  return outputs
390
 
391
  search_outputs = [
392
- search_status, # 검색 μƒνƒœ λ©”μ‹œμ§€ 좜λ ₯ μΆ”κ°€
393
- translated_query_display,
394
  gr.Markdown(visible=False)
395
  ]
 
396
  for comp in article_components:
397
- search_outputs.extend([comp['group'], comp['title'], comp['image'],
398
- comp['snippet'], comp['info']])
399
- search_outputs.extend([articles_state, status_message])
 
 
400
 
401
  search_button.click(
402
  search_and_display,
 
172
  target_lang = COUNTRY_LANGUAGES[country]
173
  print(f"λ²ˆμ—­ μ‹œλ„: {query} -> {country}({target_lang})")
174
 
 
175
  url = f"https://translate.googleapis.com/translate_a/single"
 
176
  params = {
177
  "client": "gtx",
178
+ "sl": "auto",
179
+ "tl": target_lang,
180
  "dt": "t",
181
  "q": query
182
  }
183
 
184
  response = requests.get(url, params=params)
185
  translated_text = response.json()[0][0][0]
 
186
  print(f"λ²ˆμ—­ μ™„λ£Œ: {query} -> {translated_text} ({country})")
187
  return translated_text
188
 
 
192
  print(f"λ²ˆμ—­ 였λ₯˜: {str(e)}")
193
  return query
194
 
195
+ def translate_to_korean(text):
196
+ try:
197
+ url = "https://translate.googleapis.com/translate_a/single"
198
+ params = {
199
+ "client": "gtx",
200
+ "sl": "auto",
201
+ "tl": "ko",
202
+ "dt": "t",
203
+ "q": text
204
+ }
205
+
206
+ response = requests.get(url, params=params)
207
+ translated_text = response.json()[0][0][0]
208
+ return translated_text
209
+ except Exception as e:
210
+ print(f"ν•œκΈ€ λ²ˆμ—­ 였λ₯˜: {str(e)}")
211
+ return text
212
 
213
  def is_english(text):
214
+ return all(ord(char) < 128 for char in text.replace(' ', '').replace('-', '').replace('_', ''))
215
 
216
  def is_korean(text):
217
  return any('\uAC00' <= char <= '\uD7A3' for char in text)
 
219
  def search_serphouse(query, country, page=1, num_result=10):
220
  url = "https://api.serphouse.com/serp/live"
221
 
 
222
  now = datetime.utcnow()
223
  yesterday = now - timedelta(days=1)
224
  date_range = f"{yesterday.strftime('%Y-%m-%d')},{now.strftime('%Y-%m-%d')}"
225
 
 
226
  translated_query = translate_query(query, country)
227
  print(f"Original query: {query}")
228
  print(f"Translated query: {translated_query}")
229
 
230
  payload = {
231
  "data": {
232
+ "q": translated_query,
233
  "domain": "google.com",
234
  "loc": COUNTRY_LOCATIONS.get(country, "United States"),
235
+ "lang": COUNTRY_LANGUAGES.get(country, "en"),
236
  "device": "desktop",
237
  "serp_type": "news",
238
  "page": "1",
 
250
 
251
  try:
252
  response = requests.post(url, json=payload, headers=headers)
253
+ print("Request payload:", json.dumps(payload, indent=2, ensure_ascii=False))
254
  print("Response status:", response.status_code)
255
 
256
  response.raise_for_status()
 
296
 
297
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI μ„œλΉ„μŠ€") as iface:
298
  gr.Markdown("검색어λ₯Ό μž…λ ₯ν•˜κ³  μ›ν•˜λŠ” κ΅­κ°€(67개ꡭ)λ₯Ό μ„ νƒν•˜λ©΄, 검색어와 μΌμΉ˜ν•˜λŠ” 24μ‹œκ°„ 이내 λ‰΄μŠ€λ₯Ό μ΅œλŒ€ 100개 좜λ ₯ν•©λ‹ˆλ‹€.")
299
+ gr.Markdown("κ΅­κ°€ 선택후 검색어에 'ν•œκΈ€'을 μž…λ ₯ν•˜λ©΄ ν˜„μ§€ μ–Έμ–΄λ‘œ λ²ˆμ—­λ˜μ–΄ κ²€μƒ‰ν•©λ‹ˆλ‹€. 예: 'Taiwan' κ΅­κ°€ 선택후 'μ‚Όμ„±' μž…λ ₯μ‹œ 'δΈ‰ζ˜Ÿ'으둜 μžλ™ 검색")
300
 
301
  with gr.Column():
302
  with gr.Row():
303
  query = gr.Textbox(label="검색어")
304
  country = gr.Dropdown(MAJOR_COUNTRIES, label="κ΅­κ°€", value="South Korea")
305
+
306
+ # 검색 μƒνƒœ λ©”μ‹œμ§€
307
+ status_message = gr.Markdown("", visible=True)
308
 
309
  # λ²ˆμ—­ κ²°κ³Όλ₯Ό λ³΄μ—¬μ£ΌλŠ” μ»΄ν¬λ„ŒνŠΈ
310
  translated_query_display = gr.Markdown(visible=False)
311
+
312
  search_button = gr.Button("검색", variant="primary")
313
+
314
  progress = gr.Progress()
 
315
  articles_state = gr.State([])
316
 
317
  article_components = []
 
321
  image = gr.Image(width=200, height=150)
322
  snippet = gr.Markdown()
323
  info = gr.Markdown()
324
+
325
  article_components.append({
326
  'group': article_group,
327
  'title': title,
 
332
  })
333
 
334
  def search_and_display(query, country, articles_state, progress=gr.Progress()):
335
+ # 검색 μƒνƒœ λ©”μ‹œμ§€ μ—…λ°μ΄νŠΈ
336
+ status_msg = "검색을 μ§„ν–‰μ€‘μž…λ‹ˆλ‹€. μž μ‹œλ§Œ κΈ°λ‹€λ¦¬μ„Έμš”..."
337
 
338
  progress(0, desc="검색어 λ²ˆμ—­ 쀑...")
339
 
 
345
  error_message, articles = serphouse_search(query, country)
346
  progress(0.5, desc="κ²°κ³Ό 처리 쀑...")
347
 
348
+ outputs = []
349
+ outputs.append(gr.update(value=status_msg, visible=True)) # μƒνƒœ λ©”μ‹œμ§€
350
+ outputs.append(gr.update(value=translated_display, visible=True)) # λ²ˆμ—­ κ²°κ³Ό
 
351
 
352
  if error_message:
353
  outputs.append(gr.update(value=error_message, visible=True))
 
367
  image_url = article['image_url']
368
  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)
369
 
370
+ # μš”μ•½ λ‚΄μš© ν•œκΈ€ λ²ˆμ—­
371
+ korean_summary = translate_to_korean(article['snippet'])
372
+
373
  outputs.extend([
374
  gr.update(visible=True),
375
  gr.update(value=f"### [{article['title']}]({article['link']})"),
376
  image_update,
377
+ gr.update(value=f"**μš”μ•½:** {article['snippet']}\n\n**ν•œκΈ€ μš”μ•½:** {korean_summary}"),
378
  gr.update(value=f"**좜처:** {article['channel']} | **μ‹œκ°„:** {article['time']}")
379
  ])
380
  else:
 
386
 
387
  progress(1.0, desc="μ™„λ£Œ!")
388
  outputs.append(articles_state)
 
389
 
390
  # 검색 μ™„λ£Œ ν›„ μƒνƒœ λ©”μ‹œμ§€ μˆ¨κΉ€
391
+ outputs[0] = gr.update(value="", visible=False)
 
392
 
393
  return outputs
394
 
395
  search_outputs = [
396
+ status_message,
397
+ translated_query_display,
398
  gr.Markdown(visible=False)
399
  ]
400
+
401
  for comp in article_components:
402
+ search_outputs.extend([
403
+ comp['group'], comp['title'], comp['image'],
404
+ comp['snippet'], comp['info']
405
+ ])
406
+ search_outputs.append(articles_state)
407
 
408
  search_button.click(
409
  search_and_display,