Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
seawolf2357
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
|
5 |
+
API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
|
6 |
+
|
7 |
+
MAJOR_COUNTRIES = [
|
8 |
+
"United States", "United Kingdom", "Canada", "Australia", "Germany",
|
9 |
+
"France", "Japan", "South Korea", "China", "India",
|
10 |
+
"Brazil", "Mexico", "Russia", "Italy", "Spain",
|
11 |
+
"Netherlands", "Sweden", "Switzerland", "Norway", "Denmark",
|
12 |
+
"Finland", "Belgium", "Austria", "New Zealand", "Ireland",
|
13 |
+
"Singapore", "Hong Kong", "Israel", "United Arab Emirates", "Saudi Arabia",
|
14 |
+
"South Africa", "Turkey", "Egypt", "Poland", "Czech Republic",
|
15 |
+
"Hungary", "Greece", "Portugal", "Argentina", "Chile",
|
16 |
+
"Colombia", "Peru", "Venezuela", "Thailand", "Malaysia",
|
17 |
+
"Indonesia", "Philippines", "Vietnam", "Pakistan", "Bangladesh"
|
18 |
+
]
|
19 |
+
|
20 |
+
def search_serphouse(query, country, page, num_result):
|
21 |
+
url = "https://api.serphouse.com/serp/live"
|
22 |
+
payload = {
|
23 |
+
"data": {
|
24 |
+
"q": query,
|
25 |
+
"domain": "google.com",
|
26 |
+
"loc": country,
|
27 |
+
"lang": "en",
|
28 |
+
"device": "desktop",
|
29 |
+
"serp_type": "news",
|
30 |
+
"page": str(page),
|
31 |
+
"verbatim": "1",
|
32 |
+
"num": str(num_result)
|
33 |
+
}
|
34 |
+
}
|
35 |
+
|
36 |
+
headers = {
|
37 |
+
"accept": "application/json",
|
38 |
+
"content-type": "application/json",
|
39 |
+
"authorization": f"Bearer {API_KEY}"
|
40 |
+
}
|
41 |
+
|
42 |
+
try:
|
43 |
+
response = requests.post(url, json=payload, headers=headers)
|
44 |
+
response.raise_for_status()
|
45 |
+
return response.json()
|
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:
|
59 |
+
if not isinstance(results, dict):
|
60 |
+
raise ValueError("๊ฒฐ๊ณผ๊ฐ ์ฌ์ ํ์์ด ์๋๋๋ค.")
|
61 |
+
|
62 |
+
if "results" not in results:
|
63 |
+
raise ValueError("'results' ํค๊ฐ ์๋ต์ ์์ต๋๋ค.")
|
64 |
+
|
65 |
+
news_results = results["results"].get("news", [])
|
66 |
+
debug_info += f"๋ด์ค ๊ฒฐ๊ณผ ์: {len(news_results)}\n\n"
|
67 |
+
|
68 |
+
for idx, result in enumerate(news_results):
|
69 |
+
title = result.get("title", "์ ๋ชฉ ์์")
|
70 |
+
url = result.get("url", "#")
|
71 |
+
snippet = result.get("snippet", "๋ด์ฉ ์์")
|
72 |
+
channel = result.get("channel", "์ ์ ์์")
|
73 |
+
time_str = result.get("time", "์ ์ ์๋ ์๊ฐ")
|
74 |
+
|
75 |
+
single_debug_info = f"๋ด์ค {idx + 1} - ์ ๋ชฉ: {title}, ๋งํฌ: {url}, ์๊ฐ: {time_str}, ์ถ์ฒ: {channel}"
|
76 |
+
|
77 |
+
result_table += f"| {title[:30]}... | [{url[:30]}...]({url}) | {time_str} | {channel} | {single_debug_info[:50]}... |\n"
|
78 |
+
|
79 |
+
article_info = f"""
|
80 |
+
### [{title}]({url})
|
81 |
+
{snippet}
|
82 |
+
**์ถ์ฒ:** {channel} - {time_str}
|
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 |
+
|
92 |
+
result_table += f"| ์ค๋ฅ ๋ฐ์ | - | - | - | {error_message} |\n"
|
93 |
+
|
94 |
+
return all_results, result_table + "\n\n" + debug_info
|
95 |
+
|
96 |
+
|
97 |
+
def serphouse_search(query, country, page, num_result):
|
98 |
+
results = search_serphouse(query, country, page, num_result)
|
99 |
+
all_results, result_table_and_debug = format_results(results)
|
100 |
+
return all_results, result_table_and_debug
|
101 |
+
|
102 |
+
css = """
|
103 |
+
footer {
|
104 |
+
visibility: hidden;
|
105 |
+
}
|
106 |
+
"""
|
107 |
+
|
108 |
+
iface = gr.Interface(
|
109 |
+
fn=serphouse_search,
|
110 |
+
inputs=[
|
111 |
+
gr.Textbox(label="๊ฒ์์ด"),
|
112 |
+
gr.Dropdown(MAJOR_COUNTRIES, label="๊ตญ๊ฐ"),
|
113 |
+
gr.Slider(1, 10, 1, label="ํ์ด์ง"),
|
114 |
+
gr.Slider(1, 100, 10, label="๊ฒฐ๊ณผ ์")
|
115 |
+
],
|
116 |
+
outputs=[
|
117 |
+
gr.Markdown(label="๋ชจ๋ ๊ฒฐ๊ณผ"),
|
118 |
+
gr.Markdown(label="๊ฒฐ๊ณผ ํ ๋ฐ ๋๋ฒ๊ทธ ์ ๋ณด")
|
119 |
+
],
|
120 |
+
title="SERPHouse ๋ด์ค ๊ฒ์ ์ธํฐํ์ด์ค",
|
121 |
+
description="๊ฒ์์ด๋ฅผ ์
๋ ฅํ๊ณ ๊ตญ๊ฐ๋ฅผ ์ ํํ์ฌ SERPHouse API์์ ๋ด์ค ๊ฒฐ๊ณผ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.",
|
122 |
+
theme="Nymbo/Nymbo_Theme",
|
123 |
+
css=css
|
124 |
+
)
|
125 |
+
|
126 |
+
iface.launch()
|