Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -58,8 +58,11 @@ def search_serphouse(query, country, page, num_result):
|
|
| 58 |
|
| 59 |
def format_results_from_raw(results):
|
| 60 |
try:
|
|
|
|
|
|
|
|
|
|
| 61 |
if isinstance(results, dict) and "error" in results:
|
| 62 |
-
return "Error: " + results["error"]
|
| 63 |
|
| 64 |
if not isinstance(results, dict):
|
| 65 |
raise ValueError("๊ฒฐ๊ณผ๊ฐ ์ฌ์ ํ์์ด ์๋๋๋ค.")
|
|
@@ -67,10 +70,8 @@ def format_results_from_raw(results):
|
|
| 67 |
# 'results' ํค ๋ด๋ถ์ ๊ตฌ์กฐ ํ์ธ
|
| 68 |
if 'results' in results:
|
| 69 |
results_content = results['results']
|
| 70 |
-
# '
|
| 71 |
-
if '
|
| 72 |
-
news_results = results_content['news']
|
| 73 |
-
elif 'organic' in results_content:
|
| 74 |
news_results = results_content['organic']
|
| 75 |
else:
|
| 76 |
news_results = []
|
|
@@ -78,16 +79,16 @@ def format_results_from_raw(results):
|
|
| 78 |
news_results = []
|
| 79 |
|
| 80 |
if not news_results:
|
| 81 |
-
return "๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค."
|
| 82 |
|
| 83 |
formatted_articles = ""
|
| 84 |
for result in news_results:
|
| 85 |
title = result.get("title", "์ ๋ชฉ ์์")
|
| 86 |
link = result.get("url", result.get("link", "#"))
|
| 87 |
snippet = result.get("snippet", "๋ด์ฉ ์์")
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
image_url = result.get("
|
| 91 |
|
| 92 |
# ์ด๋ฏธ์ง ํ๊ทธ ์์ฑ
|
| 93 |
if image_url:
|
|
@@ -98,25 +99,23 @@ def format_results_from_raw(results):
|
|
| 98 |
article_html = f"""
|
| 99 |
<div style="margin-bottom: 20px; border-bottom: 1px solid #ccc; padding-bottom: 20px;">
|
| 100 |
<h3><a href="{link}" target="_blank">{title}</a></h3>
|
| 101 |
-
<p><strong>{
|
| 102 |
{image_html}
|
| 103 |
<p>{snippet}</p>
|
| 104 |
</div>
|
| 105 |
"""
|
| 106 |
formatted_articles += article_html
|
| 107 |
|
| 108 |
-
return formatted_articles
|
| 109 |
|
| 110 |
except Exception as e:
|
| 111 |
-
|
|
|
|
|
|
|
| 112 |
|
| 113 |
def serphouse_search(query, country, page, num_result):
|
| 114 |
results = search_serphouse(query, country, page, num_result)
|
| 115 |
-
|
| 116 |
-
debug_info = f"Raw API Response:\n{json.dumps(results, indent=2, ensure_ascii=False)}\n\n"
|
| 117 |
-
# ๊ธฐ์ฌ ๋ฆฌ์คํธ ์์ฑ
|
| 118 |
-
formatted_articles = format_results_from_raw(results)
|
| 119 |
-
# ๊ธฐ์ฌ ๋ฆฌ์คํธ์ ๋๋ฒ๊ทธ ์ ๋ณด๋ฅผ ๋ฐํ
|
| 120 |
return formatted_articles, debug_info
|
| 121 |
|
| 122 |
css = """
|
|
@@ -139,7 +138,7 @@ iface = gr.Interface(
|
|
| 139 |
],
|
| 140 |
title="24์๊ฐ ์ด๋ด ๋ด์ค ๊ฒ์ ์ธํฐํ์ด์ค",
|
| 141 |
description="๊ฒ์์ด๋ฅผ ์
๋ ฅํ๊ณ ๊ตญ๊ฐ๋ฅผ ์ ํํ์ฌ 24์๊ฐ ์ด๋ด์ ๋ด์ค ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.",
|
| 142 |
-
theme="
|
| 143 |
css=css
|
| 144 |
)
|
| 145 |
|
|
|
|
| 58 |
|
| 59 |
def format_results_from_raw(results):
|
| 60 |
try:
|
| 61 |
+
# ๋๋ฒ๊ทธ ์ ๋ณด ์ถ๋ ฅ
|
| 62 |
+
debug_info = f"Raw API Response:\n{json.dumps(results, indent=2, ensure_ascii=False)}\n\n"
|
| 63 |
+
|
| 64 |
if isinstance(results, dict) and "error" in results:
|
| 65 |
+
return "Error: " + results["error"], debug_info
|
| 66 |
|
| 67 |
if not isinstance(results, dict):
|
| 68 |
raise ValueError("๊ฒฐ๊ณผ๊ฐ ์ฌ์ ํ์์ด ์๋๋๋ค.")
|
|
|
|
| 70 |
# 'results' ํค ๋ด๋ถ์ ๊ตฌ์กฐ ํ์ธ
|
| 71 |
if 'results' in results:
|
| 72 |
results_content = results['results']
|
| 73 |
+
# 'organic' ํค ํ์ธ
|
| 74 |
+
if 'organic' in results_content:
|
|
|
|
|
|
|
| 75 |
news_results = results_content['organic']
|
| 76 |
else:
|
| 77 |
news_results = []
|
|
|
|
| 79 |
news_results = []
|
| 80 |
|
| 81 |
if not news_results:
|
| 82 |
+
return "๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค.", debug_info
|
| 83 |
|
| 84 |
formatted_articles = ""
|
| 85 |
for result in news_results:
|
| 86 |
title = result.get("title", "์ ๋ชฉ ์์")
|
| 87 |
link = result.get("url", result.get("link", "#"))
|
| 88 |
snippet = result.get("snippet", "๋ด์ฉ ์์")
|
| 89 |
+
source = result.get("source", result.get("channel", "์ ์ ์์"))
|
| 90 |
+
date = result.get("date", result.get("time", "์ ์ ์๋ ์๊ฐ"))
|
| 91 |
+
image_url = result.get("thumbnail", result.get("img", ""))
|
| 92 |
|
| 93 |
# ์ด๋ฏธ์ง ํ๊ทธ ์์ฑ
|
| 94 |
if image_url:
|
|
|
|
| 99 |
article_html = f"""
|
| 100 |
<div style="margin-bottom: 20px; border-bottom: 1px solid #ccc; padding-bottom: 20px;">
|
| 101 |
<h3><a href="{link}" target="_blank">{title}</a></h3>
|
| 102 |
+
<p><strong>{source}</strong> - {date}</p>
|
| 103 |
{image_html}
|
| 104 |
<p>{snippet}</p>
|
| 105 |
</div>
|
| 106 |
"""
|
| 107 |
formatted_articles += article_html
|
| 108 |
|
| 109 |
+
return formatted_articles, debug_info
|
| 110 |
|
| 111 |
except Exception as e:
|
| 112 |
+
error_message = f"๊ฒฐ๊ณผ ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
| 113 |
+
debug_info = f"Error: {error_message}\n"
|
| 114 |
+
return "Error: " + error_message, debug_info
|
| 115 |
|
| 116 |
def serphouse_search(query, country, page, num_result):
|
| 117 |
results = search_serphouse(query, country, page, num_result)
|
| 118 |
+
formatted_articles, debug_info = format_results_from_raw(results)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
return formatted_articles, debug_info
|
| 120 |
|
| 121 |
css = """
|
|
|
|
| 138 |
],
|
| 139 |
title="24์๊ฐ ์ด๋ด ๋ด์ค ๊ฒ์ ์ธํฐํ์ด์ค",
|
| 140 |
description="๊ฒ์์ด๋ฅผ ์
๋ ฅํ๊ณ ๊ตญ๊ฐ๋ฅผ ์ ํํ์ฌ 24์๊ฐ ์ด๋ด์ ๋ด์ค ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.",
|
| 141 |
+
theme="default",
|
| 142 |
css=css
|
| 143 |
)
|
| 144 |
|