seawolf2357
commited on
Commit
โข
c57002d
1
Parent(s):
12d3fa4
Update app-backup.py
Browse files- app-backup.py +97 -85
app-backup.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import json
|
@@ -6,25 +7,25 @@ from datetime import datetime, timedelta
|
|
6 |
API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
|
7 |
|
8 |
MAJOR_COUNTRIES = [
|
9 |
-
"United States", "United Kingdom", "Canada", "Australia", "Germany",
|
10 |
-
"France", "Japan", "South Korea", "China", "India",
|
11 |
-
"Brazil", "Mexico", "Russia", "Italy", "Spain",
|
12 |
-
"Netherlands", "Sweden", "Switzerland", "Norway", "Denmark",
|
13 |
-
"Finland", "Belgium", "Austria", "New Zealand", "Ireland",
|
14 |
-
"Singapore", "Hong Kong", "Israel", "United Arab Emirates", "Saudi Arabia",
|
15 |
-
"South Africa", "Turkey", "Egypt", "Poland", "Czech Republic",
|
16 |
-
"Hungary", "Greece", "Portugal", "Argentina", "Chile",
|
17 |
-
"Colombia", "Peru", "Venezuela", "Thailand", "Malaysia",
|
18 |
"Indonesia", "Philippines", "Vietnam", "Pakistan", "Bangladesh"
|
19 |
]
|
20 |
|
21 |
def search_serphouse(query, country, page, num_result):
|
22 |
url = "https://api.serphouse.com/serp/live"
|
23 |
-
|
24 |
now = datetime.utcnow()
|
25 |
yesterday = now - timedelta(days=1)
|
26 |
date_range = f"{yesterday.strftime('%Y-%m-%d')},{now.strftime('%Y-%m-%d')}"
|
27 |
-
|
28 |
payload = {
|
29 |
"data": {
|
30 |
"q": query,
|
@@ -39,13 +40,13 @@ def search_serphouse(query, country, page, num_result):
|
|
39 |
"date_range": date_range
|
40 |
}
|
41 |
}
|
42 |
-
|
43 |
headers = {
|
44 |
"accept": "application/json",
|
45 |
"content-type": "application/json",
|
46 |
"authorization": f"Bearer {API_KEY}"
|
47 |
}
|
48 |
-
|
49 |
try:
|
50 |
response = requests.post(url, json=payload, headers=headers)
|
51 |
response.raise_for_status()
|
@@ -56,91 +57,102 @@ def search_serphouse(query, country, page, num_result):
|
|
56 |
error_msg += f"\nResponse content: {response.text}"
|
57 |
return {"error": error_msg}
|
58 |
|
59 |
-
def
|
60 |
-
all_results = "<h2>๋ชจ๋ ๋ด์ค ๊ฒฐ๊ณผ (24์๊ฐ ์ด๋ด)</h2>"
|
61 |
-
debug_info = "<h2>๋๋ฒ๊ทธ ์ ๋ณด</h2>"
|
62 |
-
|
63 |
-
if isinstance(results, dict) and "error" in results:
|
64 |
-
all_results += f"<p>์ค๋ฅ ๋ฐ์: {results['error']}</p>"
|
65 |
-
debug_info += f"<pre>{results['error']}</pre>"
|
66 |
-
return all_results, debug_info
|
67 |
-
|
68 |
-
debug_info += f"<pre>{json.dumps(results, indent=2, ensure_ascii=False)}</pre>"
|
69 |
-
|
70 |
try:
|
|
|
|
|
|
|
|
|
|
|
71 |
if not isinstance(results, dict):
|
72 |
raise ValueError("๊ฒฐ๊ณผ๊ฐ ์ฌ์ ํ์์ด ์๋๋๋ค.")
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
else:
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
except Exception as e:
|
103 |
error_message = f"๊ฒฐ๊ณผ ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}"
|
104 |
-
debug_info
|
105 |
-
|
106 |
-
|
107 |
-
return all_results, debug_info
|
108 |
|
109 |
def serphouse_search(query, country, page, num_result):
|
110 |
results = search_serphouse(query, country, page, num_result)
|
111 |
-
|
112 |
-
return
|
113 |
|
114 |
css = """
|
115 |
footer {
|
116 |
visibility: hidden;
|
117 |
}
|
118 |
-
ol {
|
119 |
-
padding-left: 20px;
|
120 |
-
}
|
121 |
-
li {
|
122 |
-
margin-bottom: 20px;
|
123 |
-
}
|
124 |
"""
|
125 |
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
gr.
|
133 |
-
|
134 |
-
|
135 |
-
gr.
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
)
|
145 |
-
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
import gradio as gr
|
3 |
import requests
|
4 |
import json
|
|
|
7 |
API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
|
8 |
|
9 |
MAJOR_COUNTRIES = [
|
10 |
+
"United States", "United Kingdom", "Canada", "Australia", "Germany",
|
11 |
+
"France", "Japan", "South Korea", "China", "India",
|
12 |
+
"Brazil", "Mexico", "Russia", "Italy", "Spain",
|
13 |
+
"Netherlands", "Sweden", "Switzerland", "Norway", "Denmark",
|
14 |
+
"Finland", "Belgium", "Austria", "New Zealand", "Ireland",
|
15 |
+
"Singapore", "Hong Kong", "Israel", "United Arab Emirates", "Saudi Arabia",
|
16 |
+
"South Africa", "Turkey", "Egypt", "Poland", "Czech Republic",
|
17 |
+
"Hungary", "Greece", "Portugal", "Argentina", "Chile",
|
18 |
+
"Colombia", "Peru", "Venezuela", "Thailand", "Malaysia",
|
19 |
"Indonesia", "Philippines", "Vietnam", "Pakistan", "Bangladesh"
|
20 |
]
|
21 |
|
22 |
def search_serphouse(query, country, page, num_result):
|
23 |
url = "https://api.serphouse.com/serp/live"
|
24 |
+
|
25 |
now = datetime.utcnow()
|
26 |
yesterday = now - timedelta(days=1)
|
27 |
date_range = f"{yesterday.strftime('%Y-%m-%d')},{now.strftime('%Y-%m-%d')}"
|
28 |
+
|
29 |
payload = {
|
30 |
"data": {
|
31 |
"q": query,
|
|
|
40 |
"date_range": date_range
|
41 |
}
|
42 |
}
|
43 |
+
|
44 |
headers = {
|
45 |
"accept": "application/json",
|
46 |
"content-type": "application/json",
|
47 |
"authorization": f"Bearer {API_KEY}"
|
48 |
}
|
49 |
+
|
50 |
try:
|
51 |
response = requests.post(url, json=payload, headers=headers)
|
52 |
response.raise_for_status()
|
|
|
57 |
error_msg += f"\nResponse content: {response.text}"
|
58 |
return {"error": error_msg}
|
59 |
|
60 |
+
def format_results_from_raw(results):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
try:
|
62 |
+
debug_info = f"Raw API Response:\n{json.dumps(results, indent=2, ensure_ascii=False)}"
|
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("๊ฒฐ๊ณผ๊ฐ ์ฌ์ ํ์์ด ์๋๋๋ค.")
|
69 |
+
|
70 |
+
# 'results' ํค ๋ด๋ถ์ ๊ตฌ์กฐ ํ์ธ
|
71 |
+
if 'results' in results:
|
72 |
+
results_content = results['results']
|
73 |
+
# 'news' ํค ํ์ธ
|
74 |
+
if 'news' in results_content:
|
75 |
+
news_results = results_content['news']
|
76 |
+
else:
|
77 |
+
news_results = []
|
78 |
else:
|
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 |
+
channel = result.get("channel", result.get("source", "์ ์ ์์"))
|
90 |
+
time = result.get("time", result.get("date", "์ ์ ์๋ ์๊ฐ"))
|
91 |
+
image_url = result.get("img", result.get("thumbnail", ""))
|
92 |
+
|
93 |
+
# base64๋ก ์ธ์ฝ๋ฉ๋ ์ด๋ฏธ์ง๋ฅผ ์ฒ๋ฆฌํ์ง ์์
|
94 |
+
if image_url and not image_url.startswith("data:image"):
|
95 |
+
image_html = f'<img src="{image_url}" alt="Image" style="max-width: 100%; height: auto;">'
|
96 |
+
else:
|
97 |
+
image_html = ''
|
98 |
+
|
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>{channel}</strong> - {time}</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 = """
|
122 |
footer {
|
123 |
visibility: hidden;
|
124 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
"""
|
126 |
|
127 |
+
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
128 |
+
with gr.Blocks(css=css, title="24์๊ฐ ์ด๋ด ๋ด์ค ๊ฒ์ ์ธํฐํ์ด์ค") as iface:
|
129 |
+
gr.Markdown("## 24์๊ฐ ์ด๋ด ๋ด์ค ๊ฒ์ ์ธํฐํ์ด์ค")
|
130 |
+
gr.Markdown("๊ฒ์์ด๋ฅผ ์
๋ ฅํ๊ณ ๊ตญ๊ฐ๋ฅผ ์ ํํ์ฌ 24์๊ฐ ์ด๋ด์ ๋ด์ค ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.")
|
131 |
+
|
132 |
+
with gr.Tab("๊ฒ์"):
|
133 |
+
with gr.Row():
|
134 |
+
query = gr.Textbox(label="๊ฒ์์ด")
|
135 |
+
country = gr.Dropdown(MAJOR_COUNTRIES, label="๊ตญ๊ฐ", value="South Korea")
|
136 |
+
with gr.Row():
|
137 |
+
page = gr.Slider(1, 10, 1, label="ํ์ด์ง")
|
138 |
+
num_result = gr.Slider(1, 100, 10, label="๊ฒฐ๊ณผ ์")
|
139 |
+
|
140 |
+
search_button = gr.Button("๊ฒ์")
|
141 |
+
|
142 |
+
with gr.Tab("๋ด์ค ๊ฒฐ๊ณผ"):
|
143 |
+
news_output = gr.HTML(label="๋ด์ค ๊ฒฐ๊ณผ")
|
144 |
+
|
145 |
+
with gr.Tab("๋๋ฒ๊ทธ ์ ๋ณด"):
|
146 |
+
debug_output = gr.Textbox(label="๋๋ฒ๊ทธ ์ ๋ณด", lines=10)
|
147 |
+
|
148 |
+
def search_and_display(query, country, page, num_result):
|
149 |
+
articles, debug_info = serphouse_search(query, country, page, num_result)
|
150 |
+
return {news_output: articles, debug_output: debug_info}
|
151 |
+
|
152 |
+
search_button.click(
|
153 |
+
search_and_display,
|
154 |
+
inputs=[query, country, page, num_result],
|
155 |
+
outputs=[news_output, debug_output]
|
156 |
+
)
|
157 |
+
|
158 |
+
iface.launch(auth=("gini", "pick"))
|