Spaces:
Building
Building
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import json
|
4 |
-
from datetime import datetime, timedelta
|
5 |
|
6 |
API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
|
7 |
|
@@ -47,29 +46,13 @@ def search_serphouse(query, country, page, num_result):
|
|
47 |
except requests.RequestException as e:
|
48 |
return f"Error: {str(e)}"
|
49 |
|
50 |
-
def is_recent_news(time_str):
|
51 |
-
if not time_str:
|
52 |
-
return False
|
53 |
-
|
54 |
-
time_str = time_str.lower().strip()
|
55 |
-
|
56 |
-
if "1 day ago" in time_str:
|
57 |
-
return True
|
58 |
-
|
59 |
-
if "hours ago" in time_str:
|
60 |
-
return True
|
61 |
-
|
62 |
-
return False
|
63 |
-
|
64 |
def format_results(results):
|
65 |
all_results = "## 모든 뉴스 결과\n\n"
|
66 |
-
recent_results = "## 최근 뉴스 결과 (1일 이내)\n\n"
|
67 |
-
debug_info = "## 디버그 정보\n\n"
|
68 |
-
|
69 |
result_table = "## 뉴스 결과 표\n\n"
|
70 |
-
result_table += "| 제목 | 링크 | 시간 |
|
71 |
-
result_table += "
|
72 |
|
|
|
73 |
debug_info += f"Raw API Response:\n```json\n{json.dumps(results, indent=2)}\n```\n\n"
|
74 |
|
75 |
try:
|
@@ -90,11 +73,8 @@ def format_results(results):
|
|
90 |
channel = result.get("channel", "알 수 없음")
|
91 |
time_str = result.get("time", "알 수 없는 시간")
|
92 |
|
93 |
-
is_recent = is_recent_news(time_str)
|
94 |
-
debug_info += f"기사: {title}\n시간: {time_str}, 최근 여부: {is_recent}\n\n"
|
95 |
-
|
96 |
# 결과 표에 행 추가
|
97 |
-
result_table += f"| {title[:30]}... | [{url[:30]}...]({url}) | {time_str} | {
|
98 |
|
99 |
article_info = f"""
|
100 |
### [{title}]({url})
|
@@ -103,25 +83,19 @@ def format_results(results):
|
|
103 |
---
|
104 |
"""
|
105 |
all_results += article_info
|
106 |
-
if is_recent:
|
107 |
-
recent_results += article_info
|
108 |
-
|
109 |
-
if recent_results == "## 최근 뉴스 결과 (1일 이내)\n\n":
|
110 |
-
recent_results += "*1일 이내의 최근 뉴스 결과가 없습니다.*\n\n"
|
111 |
|
112 |
except Exception as e:
|
113 |
error_message = f"결과 처리 중 오류 발생: {str(e)}"
|
114 |
debug_info += error_message + "\n"
|
115 |
all_results = error_message + "\n\n"
|
116 |
-
recent_results = error_message + "\n\n"
|
117 |
result_table += f"| 오류 발생 | - | - | - |\n"
|
118 |
|
119 |
-
return all_results,
|
120 |
|
121 |
def serphouse_search(query, country, page, num_result):
|
122 |
results = search_serphouse(query, country, page, num_result)
|
123 |
-
all_results,
|
124 |
-
return all_results,
|
125 |
|
126 |
css = """
|
127 |
footer {
|
@@ -139,11 +113,10 @@ iface = gr.Interface(
|
|
139 |
],
|
140 |
outputs=[
|
141 |
gr.Markdown(label="모든 결과"),
|
142 |
-
gr.Markdown(label="최근 결과 (1일 이내)"),
|
143 |
gr.Markdown(label="결과 표 및 디버그 정보")
|
144 |
],
|
145 |
title="SERPHouse 뉴스 검색 인터페이스",
|
146 |
-
description="검색어를 입력하고 국가를 선택하여 SERPHouse API에서 뉴스 결과를 가져옵니다.
|
147 |
theme="Nymbo/Nymbo_Theme",
|
148 |
css=css
|
149 |
)
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import json
|
|
|
4 |
|
5 |
API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
|
6 |
|
|
|
46 |
except requests.RequestException as e:
|
47 |
return f"Error: {str(e)}"
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
def format_results(results):
|
50 |
all_results = "## 모든 뉴스 결과\n\n"
|
|
|
|
|
|
|
51 |
result_table = "## 뉴스 결과 표\n\n"
|
52 |
+
result_table += "| 제목 | 링크 | 시간 | 출처 |\n"
|
53 |
+
result_table += "|------|------|------|------|\n"
|
54 |
|
55 |
+
debug_info = "## 디버그 정보\n\n"
|
56 |
debug_info += f"Raw API Response:\n```json\n{json.dumps(results, indent=2)}\n```\n\n"
|
57 |
|
58 |
try:
|
|
|
73 |
channel = result.get("channel", "알 수 없음")
|
74 |
time_str = result.get("time", "알 수 없는 시간")
|
75 |
|
|
|
|
|
|
|
76 |
# 결과 표에 행 추가
|
77 |
+
result_table += f"| {title[:30]}... | [{url[:30]}...]({url}) | {time_str} | {channel} |\n"
|
78 |
|
79 |
article_info = f"""
|
80 |
### [{title}]({url})
|
|
|
83 |
---
|
84 |
"""
|
85 |
all_results += article_info
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
except Exception as e:
|
88 |
error_message = f"결과 처리 중 오류 발생: {str(e)}"
|
89 |
debug_info += error_message + "\n"
|
90 |
all_results = error_message + "\n\n"
|
|
|
91 |
result_table += f"| 오류 발생 | - | - | - |\n"
|
92 |
|
93 |
+
return all_results, result_table + "\n\n" + debug_info
|
94 |
|
95 |
def serphouse_search(query, country, page, num_result):
|
96 |
results = search_serphouse(query, country, page, num_result)
|
97 |
+
all_results, result_table_and_debug = format_results(results)
|
98 |
+
return all_results, result_table_and_debug
|
99 |
|
100 |
css = """
|
101 |
footer {
|
|
|
113 |
],
|
114 |
outputs=[
|
115 |
gr.Markdown(label="모든 결과"),
|
|
|
116 |
gr.Markdown(label="결과 표 및 디버그 정보")
|
117 |
],
|
118 |
title="SERPHouse 뉴스 검색 인터페이스",
|
119 |
+
description="검색어를 입력하고 국가를 선택하여 SERPHouse API에서 뉴스 결과를 가져옵니다.",
|
120 |
theme="Nymbo/Nymbo_Theme",
|
121 |
css=css
|
122 |
)
|