Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,7 @@ import requests
|
|
3 |
import json
|
4 |
from datetime import datetime, timedelta
|
5 |
|
6 |
-
API_KEY = "
|
7 |
|
8 |
MAJOR_COUNTRIES = [
|
9 |
"United States", "United Kingdom", "Canada", "Australia", "Germany",
|
@@ -61,27 +61,35 @@ def format_results_from_raw(results):
|
|
61 |
debug_info = f"<pre>Raw API Response:\n{json.dumps(results, indent=2, ensure_ascii=False)}</pre>"
|
62 |
|
63 |
if isinstance(results, dict) and "error" in results:
|
64 |
-
return "Error: " + results["error"] + debug_info
|
65 |
|
66 |
if not isinstance(results, dict):
|
67 |
raise ValueError("๊ฒฐ๊ณผ๊ฐ ์ฌ์ ํ์์ด ์๋๋๋ค.")
|
68 |
|
69 |
-
# 'results' ํค ๋ด๋ถ์ ๊ตฌ์กฐ ํ์ธ
|
70 |
if 'results' in results:
|
71 |
results_content = results['results']
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
75 |
else:
|
76 |
news_results = []
|
77 |
else:
|
78 |
news_results = []
|
79 |
|
80 |
if not news_results:
|
81 |
-
return "๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค." + debug_info
|
82 |
|
|
|
83 |
formatted_articles = ""
|
84 |
-
|
|
|
|
|
|
|
85 |
title = result.get("title", "์ ๋ชฉ ์์")
|
86 |
link = result.get("url", result.get("link", "#"))
|
87 |
snippet = result.get("snippet", "๋ด์ฉ ์์")
|
@@ -95,6 +103,7 @@ def format_results_from_raw(results):
|
|
95 |
else:
|
96 |
image_html = ''
|
97 |
|
|
|
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>
|
@@ -105,20 +114,32 @@ def format_results_from_raw(results):
|
|
105 |
"""
|
106 |
formatted_articles += article_html
|
107 |
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
combined_output = formatted_articles + debug_info
|
110 |
|
111 |
-
return combined_output
|
112 |
|
113 |
except Exception as e:
|
114 |
error_message = f"๊ฒฐ๊ณผ ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
115 |
debug_info = f"<pre>Error: {error_message}\n</pre>"
|
116 |
-
return "Error: " + error_message + debug_info
|
117 |
|
118 |
def serphouse_search(query, country, page, num_result):
|
119 |
results = search_serphouse(query, country, page, num_result)
|
120 |
-
combined_output = format_results_from_raw(results)
|
121 |
-
return combined_output
|
122 |
|
123 |
css = """
|
124 |
footer {
|
@@ -127,7 +148,7 @@ footer {
|
|
127 |
"""
|
128 |
|
129 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
130 |
-
with gr.Blocks(css=css, title="24์๊ฐ ์ด๋ด ๋ด์ค ๊ฒ์ ์ธํฐํ์ด์ค") as iface:
|
131 |
gr.Markdown("## 24์๊ฐ ์ด๋ด ๋ด์ค ๊ฒ์ ์ธํฐํ์ด์ค")
|
132 |
gr.Markdown("๊ฒ์์ด๋ฅผ ์
๋ ฅํ๊ณ ๊ตญ๊ฐ๋ฅผ ์ ํํ์ฌ 24์๊ฐ ์ด๋ด์ ๋ด์ค ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.")
|
133 |
|
@@ -139,23 +160,4 @@ with gr.Blocks(css=css, title="24์๊ฐ ์ด๋ด ๋ด์ค ๊ฒ์ ์ธํฐํ์ด์ค") a
|
|
139 |
page = gr.Slider(1, 10, 1, label="ํ์ด์ง")
|
140 |
num_result = gr.Slider(1, 100, 10, label="๊ฒฐ๊ณผ ์")
|
141 |
|
142 |
-
search_button =
|
143 |
-
|
144 |
-
with gr.Tab("๋ด์ค ๊ฒฐ๊ณผ"):
|
145 |
-
news_output = gr.HTML(label="๋ด์ค ๊ฒฐ๊ณผ")
|
146 |
-
|
147 |
-
# ๋๋ฒ๊ทธ ์ ๋ณด ํญ ์ ๊ฑฐ ๋๋ ์ฃผ์ ์ฒ๋ฆฌ
|
148 |
-
# with gr.Tab("๋๋ฒ๊ทธ ์ ๋ณด"):
|
149 |
-
# debug_output = gr.Textbox(label="๋๋ฒ๊ทธ ์ ๋ณด", lines=10)
|
150 |
-
|
151 |
-
def search_and_display(query, country, page, num_result):
|
152 |
-
combined_output = serphouse_search(query, country, page, num_result)
|
153 |
-
return {news_output: combined_output}
|
154 |
-
|
155 |
-
search_button.click(
|
156 |
-
search_and_display,
|
157 |
-
inputs=[query, country, page, num_result],
|
158 |
-
outputs=[news_output]
|
159 |
-
)
|
160 |
-
|
161 |
-
iface.launch(auth=("gini", "pick"))
|
|
|
3 |
import json
|
4 |
from datetime import datetime, timedelta
|
5 |
|
6 |
+
API_KEY = "YOUR_API_KEY_HERE" # ์ฌ๊ธฐ์ ๋ณธ์ธ์ API ํค๋ฅผ ์
๋ ฅํ์ธ์.
|
7 |
|
8 |
MAJOR_COUNTRIES = [
|
9 |
"United States", "United Kingdom", "Canada", "Australia", "Germany",
|
|
|
61 |
debug_info = f"<pre>Raw API Response:\n{json.dumps(results, indent=2, ensure_ascii=False)}</pre>"
|
62 |
|
63 |
if isinstance(results, dict) and "error" in results:
|
64 |
+
return "Error: " + results["error"] + debug_info, "", debug_info
|
65 |
|
66 |
if not isinstance(results, dict):
|
67 |
raise ValueError("๊ฒฐ๊ณผ๊ฐ ์ฌ์ ํ์์ด ์๋๋๋ค.")
|
68 |
|
69 |
+
# 'results' ํค ๋ด๋ถ์ ๊ตฌ์กฐ ํ์ธ (์ค์ฒฉ๋ 'results' ์ฒ๋ฆฌ)
|
70 |
if 'results' in results:
|
71 |
results_content = results['results']
|
72 |
+
if 'results' in results_content:
|
73 |
+
results_content = results_content['results']
|
74 |
+
# 'news' ํค ํ์ธ
|
75 |
+
if 'news' in results_content:
|
76 |
+
news_results = results_content['news']
|
77 |
+
else:
|
78 |
+
news_results = []
|
79 |
else:
|
80 |
news_results = []
|
81 |
else:
|
82 |
news_results = []
|
83 |
|
84 |
if not news_results:
|
85 |
+
return "๊ฒ์ ๊ฒฐ๊ณผ๊ฐ ์์ต๋๋ค." + debug_info, "", debug_info
|
86 |
|
87 |
+
# ๋ด์ค ๊ฒฐ๊ณผ๋ฅผ HTML ํ์์ผ๋ก ํฌ๋งทํ
|
88 |
formatted_articles = ""
|
89 |
+
# ๋ด์ค ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌ์คํธ ํํ๋ก ํฌ๋งทํ
|
90 |
+
list_output = ""
|
91 |
+
|
92 |
+
for idx, result in enumerate(news_results, 1):
|
93 |
title = result.get("title", "์ ๋ชฉ ์์")
|
94 |
link = result.get("url", result.get("link", "#"))
|
95 |
snippet = result.get("snippet", "๋ด์ฉ ์์")
|
|
|
103 |
else:
|
104 |
image_html = ''
|
105 |
|
106 |
+
# HTML ํ์์ ๊ธฐ์ฌ
|
107 |
article_html = f"""
|
108 |
<div style="margin-bottom: 20px; border-bottom: 1px solid #ccc; padding-bottom: 20px;">
|
109 |
<h3><a href="{link}" target="_blank">{title}</a></h3>
|
|
|
114 |
"""
|
115 |
formatted_articles += article_html
|
116 |
|
117 |
+
# ๋ฆฌ์คํธ ํ์์ ๊ธฐ์ฌ
|
118 |
+
list_item = f"""
|
119 |
+
{idx}. ์ ๋ชฉ: {title}
|
120 |
+
๋งํฌ: {link}
|
121 |
+
์์ฝ: {snippet}
|
122 |
+
์ถ์ฒ: {channel}
|
123 |
+
์๊ฐ: {time}
|
124 |
+
์ด๋ฏธ์ง URL: {image_url}
|
125 |
+
------------------------------
|
126 |
+
"""
|
127 |
+
list_output += list_item
|
128 |
+
|
129 |
+
# ๋ด์ค ๊ฒฐ๊ณผ์ ๋๋ฒ๊ทธ ์ ๋ณด๋ฅผ ๋ฐํ
|
130 |
combined_output = formatted_articles + debug_info
|
131 |
|
132 |
+
return combined_output, list_output, debug_info
|
133 |
|
134 |
except Exception as e:
|
135 |
error_message = f"๊ฒฐ๊ณผ ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
136 |
debug_info = f"<pre>Error: {error_message}\n</pre>"
|
137 |
+
return "Error: " + error_message + debug_info, "", debug_info
|
138 |
|
139 |
def serphouse_search(query, country, page, num_result):
|
140 |
results = search_serphouse(query, country, page, num_result)
|
141 |
+
combined_output, list_output, debug_info = format_results_from_raw(results)
|
142 |
+
return combined_output, list_output, debug_info
|
143 |
|
144 |
css = """
|
145 |
footer {
|
|
|
148 |
"""
|
149 |
|
150 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
151 |
+
with gr.Blocks(theme="Nymbo/Nymbo_Theme",css=css, title="24์๊ฐ ์ด๋ด ๋ด์ค ๊ฒ์ ์ธํฐํ์ด์ค") as iface:
|
152 |
gr.Markdown("## 24์๊ฐ ์ด๋ด ๋ด์ค ๊ฒ์ ์ธํฐํ์ด์ค")
|
153 |
gr.Markdown("๊ฒ์์ด๋ฅผ ์
๋ ฅํ๊ณ ๊ตญ๊ฐ๋ฅผ ์ ํํ์ฌ 24์๊ฐ ์ด๋ด์ ๋ด์ค ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.")
|
154 |
|
|
|
160 |
page = gr.Slider(1, 10, 1, label="ํ์ด์ง")
|
161 |
num_result = gr.Slider(1, 100, 10, label="๊ฒฐ๊ณผ ์")
|
162 |
|
163 |
+
search_button =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|