Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
seawolf2357
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -136,23 +136,16 @@ def translate_query(query, country):
|
|
136 |
def search_serphouse(query, country, page=1, num_result=10):
|
137 |
url = "https://api.serphouse.com/serp/live"
|
138 |
|
139 |
-
# ๊ฒ์์ด ๋ฒ์ญ
|
140 |
-
translated_query = translate_query(query, country)
|
141 |
-
print(f"Original query: {query}")
|
142 |
-
print(f"Translated query: {translated_query}")
|
143 |
-
|
144 |
payload = {
|
145 |
"data": {
|
146 |
-
"q":
|
147 |
"domain": "google.com",
|
148 |
"loc": COUNTRY_LOCATIONS.get(country, "United States"),
|
149 |
-
"lang":
|
150 |
"device": "desktop",
|
151 |
"serp_type": "news",
|
152 |
"page": "1",
|
153 |
-
"num": "10"
|
154 |
-
"verbatim": "0",
|
155 |
-
"gfilter": "0"
|
156 |
}
|
157 |
}
|
158 |
|
@@ -168,9 +161,9 @@ def search_serphouse(query, country, page=1, num_result=10):
|
|
168 |
print("Response status:", response.status_code)
|
169 |
|
170 |
response.raise_for_status()
|
171 |
-
return response.json()
|
172 |
except requests.RequestException as e:
|
173 |
-
return {"error": f"Error: {str(e)}"}
|
174 |
|
175 |
def format_results_from_raw(results, translated_query):
|
176 |
if isinstance(results, dict) and "error" in results:
|
@@ -205,6 +198,7 @@ css = """
|
|
205 |
footer {visibility: hidden;}
|
206 |
"""
|
207 |
|
|
|
208 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as iface:
|
209 |
gr.Markdown("๊ฒ์์ด๋ฅผ ์
๋ ฅํ๊ณ ์ํ๋ ๊ตญ๊ฐ๋ฅผ ์ ํํ๋ฉด, ๊ฒ์์ด์ ์ผ์นํ๋ 24์๊ฐ ์ด๋ด ๋ด์ค๋ฅผ ์ต๋ 100๊ฐ ์ถ๋ ฅํฉ๋๋ค.")
|
210 |
|
@@ -212,7 +206,11 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
212 |
with gr.Row():
|
213 |
query = gr.Textbox(label="๊ฒ์์ด")
|
214 |
country = gr.Dropdown(MAJOR_COUNTRIES, label="๊ตญ๊ฐ", value="South Korea")
|
215 |
-
|
|
|
|
|
|
|
|
|
216 |
|
217 |
progress = gr.Progress()
|
218 |
status_message = gr.Markdown(visible=False)
|
@@ -236,10 +234,17 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
236 |
})
|
237 |
|
238 |
def search_and_display(query, country, articles_state, progress=gr.Progress()):
|
239 |
-
progress(0, desc="
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
error_message, articles = serphouse_search(query, country)
|
241 |
progress(0.5, desc="๊ฒฐ๊ณผ ์ฒ๋ฆฌ ์ค...")
|
242 |
-
|
|
|
243 |
|
244 |
if error_message:
|
245 |
outputs.append(gr.update(value=error_message, visible=True))
|
@@ -258,12 +263,10 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
258 |
article = articles[idx]
|
259 |
image_url = article['image_url']
|
260 |
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)
|
261 |
-
|
262 |
-
translated_info = f"\n**๋ฒ์ญ๋ ๊ฒ์์ด:** {article['translated_query']}" if article['translated_query'] != query else ""
|
263 |
|
264 |
outputs.extend([
|
265 |
gr.update(visible=True),
|
266 |
-
gr.update(value=f"### [{article['title']}]({article['link']})
|
267 |
image_update,
|
268 |
gr.update(value=f"**์์ฝ:** {article['snippet']}"),
|
269 |
gr.update(value=f"**์ถ์ฒ:** {article['channel']} | **์๊ฐ:** {article['time']}")
|
@@ -280,7 +283,7 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
280 |
outputs.append(gr.update(visible=False))
|
281 |
return outputs
|
282 |
|
283 |
-
search_outputs = [gr.Markdown(visible=False)]
|
284 |
for comp in article_components:
|
285 |
search_outputs.extend([comp['group'], comp['title'], comp['image'],
|
286 |
comp['snippet'], comp['info']])
|
|
|
136 |
def search_serphouse(query, country, page=1, num_result=10):
|
137 |
url = "https://api.serphouse.com/serp/live"
|
138 |
|
|
|
|
|
|
|
|
|
|
|
139 |
payload = {
|
140 |
"data": {
|
141 |
+
"q": query, # ์๋ณธ ๊ฒ์์ด ์ฌ์ฉ
|
142 |
"domain": "google.com",
|
143 |
"loc": COUNTRY_LOCATIONS.get(country, "United States"),
|
144 |
+
"lang": "en", # ๊ธฐ๋ณธ ์์ด๋ก ์ค์
|
145 |
"device": "desktop",
|
146 |
"serp_type": "news",
|
147 |
"page": "1",
|
148 |
+
"num": "10"
|
|
|
|
|
149 |
}
|
150 |
}
|
151 |
|
|
|
161 |
print("Response status:", response.status_code)
|
162 |
|
163 |
response.raise_for_status()
|
164 |
+
return response.json()
|
165 |
except requests.RequestException as e:
|
166 |
+
return {"error": f"Error: {str(e)}"}
|
167 |
|
168 |
def format_results_from_raw(results, translated_query):
|
169 |
if isinstance(results, dict) and "error" in results:
|
|
|
198 |
footer {visibility: hidden;}
|
199 |
"""
|
200 |
|
201 |
+
# Gradio ์ธํฐํ์ด์ค ์์
|
202 |
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as iface:
|
203 |
gr.Markdown("๊ฒ์์ด๋ฅผ ์
๋ ฅํ๊ณ ์ํ๋ ๊ตญ๊ฐ๋ฅผ ์ ํํ๋ฉด, ๊ฒ์์ด์ ์ผ์นํ๋ 24์๊ฐ ์ด๋ด ๋ด์ค๋ฅผ ์ต๋ 100๊ฐ ์ถ๋ ฅํฉ๋๋ค.")
|
204 |
|
|
|
206 |
with gr.Row():
|
207 |
query = gr.Textbox(label="๊ฒ์์ด")
|
208 |
country = gr.Dropdown(MAJOR_COUNTRIES, label="๊ตญ๊ฐ", value="South Korea")
|
209 |
+
|
210 |
+
# ๋ฒ์ญ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ฃผ๋ ์ปดํฌ๋ํธ ์ถ๊ฐ
|
211 |
+
translated_query_display = gr.Markdown(visible=False)
|
212 |
+
|
213 |
+
search_button = gr.Button("๊ฒ์", variant="primary")
|
214 |
|
215 |
progress = gr.Progress()
|
216 |
status_message = gr.Markdown(visible=False)
|
|
|
234 |
})
|
235 |
|
236 |
def search_and_display(query, country, articles_state, progress=gr.Progress()):
|
237 |
+
progress(0, desc="๊ฒ์์ด ๋ฒ์ญ ์ค...")
|
238 |
+
|
239 |
+
# ๊ฒ์์ด ๋ฒ์ญ
|
240 |
+
translated_query = translate_query(query, country)
|
241 |
+
translated_display = f"**์๋ณธ ๊ฒ์์ด:** {query}\n**๋ฒ์ญ๋ ๊ฒ์์ด:** {translated_query}" if translated_query != query else f"**๊ฒ์์ด:** {query}"
|
242 |
+
|
243 |
+
progress(0.2, desc="๊ฒ์ ์์...")
|
244 |
error_message, articles = serphouse_search(query, country)
|
245 |
progress(0.5, desc="๊ฒฐ๊ณผ ์ฒ๋ฆฌ ์ค...")
|
246 |
+
|
247 |
+
outputs = [gr.update(value=translated_display, visible=True)] # ๋ฒ์ญ ๊ฒฐ๊ณผ ํ์
|
248 |
|
249 |
if error_message:
|
250 |
outputs.append(gr.update(value=error_message, visible=True))
|
|
|
263 |
article = articles[idx]
|
264 |
image_url = article['image_url']
|
265 |
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)
|
|
|
|
|
266 |
|
267 |
outputs.extend([
|
268 |
gr.update(visible=True),
|
269 |
+
gr.update(value=f"### [{article['title']}]({article['link']})"),
|
270 |
image_update,
|
271 |
gr.update(value=f"**์์ฝ:** {article['snippet']}"),
|
272 |
gr.update(value=f"**์ถ์ฒ:** {article['channel']} | **์๊ฐ:** {article['time']}")
|
|
|
283 |
outputs.append(gr.update(visible=False))
|
284 |
return outputs
|
285 |
|
286 |
+
search_outputs = [translated_query_display, gr.Markdown(visible=False)] # ๋ฒ์ญ ๊ฒฐ๊ณผ ์ปดํฌ๋ํธ ์ถ๊ฐ
|
287 |
for comp in article_components:
|
288 |
search_outputs.extend([comp['group'], comp['title'], comp['image'],
|
289 |
comp['snippet'], comp['info']])
|