seawolf2357
commited on
Update app.py
Browse files
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
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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))
|
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 |
-
|
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 |
-
|
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 |
-
|
346 |
-
|
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 |
-
|
387 |
-
outputs[0] = search_status_output
|
388 |
|
389 |
return outputs
|
390 |
|
391 |
search_outputs = [
|
392 |
-
|
393 |
-
translated_query_display,
|
394 |
gr.Markdown(visible=False)
|
395 |
]
|
|
|
396 |
for comp in article_components:
|
397 |
-
search_outputs.extend([
|
398 |
-
|
399 |
-
|
|
|
|
|
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,
|