Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -198,6 +198,32 @@ def translate_query(query, country):
|
|
198 |
print(f"๋ฒ์ญ ์ค๋ฅ: {str(e)}")
|
199 |
return query
|
200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
def search_serphouse(query, country, page=1, num_result=10):
|
202 |
"""
|
203 |
SerpHouse API ์ค์๊ฐ ๊ฒ์ -> 'news' (sort_by=date)
|
@@ -335,14 +361,19 @@ def serphouse_search(query, country):
|
|
335 |
def display_results(articles):
|
336 |
"""
|
337 |
๊ธฐ์ฌ ๋ชฉ๋ก์ Markdown ๋ฌธ์์ด๋ก ๋ณํ
|
|
|
338 |
"""
|
339 |
output = ""
|
340 |
for idx, article in enumerate(articles, 1):
|
|
|
|
|
|
|
341 |
output += f"### {idx}. {article['title']}\n"
|
342 |
output += f"์ถ์ฒ: {article['channel']}\n"
|
343 |
output += f"์๊ฐ: {article['time']}\n"
|
344 |
output += f"๋งํฌ: {article['link']}\n"
|
345 |
-
output += f"
|
|
|
346 |
return output
|
347 |
|
348 |
######################################################################
|
|
|
198 |
print(f"๋ฒ์ญ ์ค๋ฅ: {str(e)}")
|
199 |
return query
|
200 |
|
201 |
+
@lru_cache(maxsize=200)
|
202 |
+
def translate_to_korean(text):
|
203 |
+
"""
|
204 |
+
snippet ๋ฑ์ ํ๊ธ๋ก ๋ฒ์ญํ๊ธฐ ์ํ ํจ์
|
205 |
+
"""
|
206 |
+
try:
|
207 |
+
url = "https://translate.googleapis.com/translate_a/single"
|
208 |
+
params = {
|
209 |
+
"client": "gtx",
|
210 |
+
"sl": "auto",
|
211 |
+
"tl": "ko",
|
212 |
+
"dt": "t",
|
213 |
+
"q": text
|
214 |
+
}
|
215 |
+
|
216 |
+
session = requests.Session()
|
217 |
+
retries = Retry(total=3, backoff_factor=0.5)
|
218 |
+
session.mount('https://', HTTPAdapter(max_retries=retries))
|
219 |
+
|
220 |
+
response = session.get(url, params=params, timeout=(5, 10))
|
221 |
+
translated_text = response.json()[0][0][0]
|
222 |
+
return translated_text
|
223 |
+
except Exception as e:
|
224 |
+
print(f"ํ๊ธ ๋ฒ์ญ ์ค๋ฅ: {str(e)}")
|
225 |
+
return text
|
226 |
+
|
227 |
def search_serphouse(query, country, page=1, num_result=10):
|
228 |
"""
|
229 |
SerpHouse API ์ค์๊ฐ ๊ฒ์ -> 'news' (sort_by=date)
|
|
|
361 |
def display_results(articles):
|
362 |
"""
|
363 |
๊ธฐ์ฌ ๋ชฉ๋ก์ Markdown ๋ฌธ์์ด๋ก ๋ณํ
|
364 |
+
- snippet(์๋ฌธ) + ํ๊ธ ๋ฒ์ญ(snippet)์ ํจ๊ป ํ์
|
365 |
"""
|
366 |
output = ""
|
367 |
for idx, article in enumerate(articles, 1):
|
368 |
+
# snippet์ ํ๊ตญ์ด๋ก ๋ฒ์ญ
|
369 |
+
korean_snippet = translate_to_korean(article['snippet'])
|
370 |
+
|
371 |
output += f"### {idx}. {article['title']}\n"
|
372 |
output += f"์ถ์ฒ: {article['channel']}\n"
|
373 |
output += f"์๊ฐ: {article['time']}\n"
|
374 |
output += f"๋งํฌ: {article['link']}\n"
|
375 |
+
output += f"์์ฝ(์๋ฌธ): {article['snippet']}\n"
|
376 |
+
output += f"์์ฝ(ํ๊ตญ์ด): {korean_snippet}\n\n"
|
377 |
return output
|
378 |
|
379 |
######################################################################
|