Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
seawolf2357
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -32,28 +32,21 @@ MAJOR_COUNTRIES = list(COUNTRY_LOCATIONS.keys())
|
|
32 |
def search_serphouse(query, country, page=1, num_result=100):
|
33 |
url = "https://api.serphouse.com/serp/live"
|
34 |
|
35 |
-
now = datetime.utcnow()
|
36 |
-
yesterday = now - timedelta(days=1)
|
37 |
-
date_range = f"{yesterday.strftime('%Y-%m-%d')},{now.strftime('%Y-%m-%d')}"
|
38 |
-
|
39 |
payload = {
|
40 |
"data": {
|
41 |
"q": query,
|
42 |
"domain": "google.com",
|
43 |
-
"
|
44 |
-
"loc_id": COUNTRY_LOCATIONS.get(country, "1026201"), # loc_id
|
45 |
"lang": "en",
|
46 |
"device": "desktop",
|
47 |
"serp_type": "news",
|
48 |
-
"page":
|
49 |
-
"verbatim": "0",
|
50 |
-
"gfilter": "0",
|
51 |
-
"
|
52 |
}
|
53 |
}
|
54 |
|
55 |
-
print("Request payload:", json.dumps(payload, indent=2)) # ๋๋ฒ๊น
์ ์ํ ์ถ๋ ฅ
|
56 |
-
|
57 |
headers = {
|
58 |
"accept": "application/json",
|
59 |
"content-type": "application/json",
|
@@ -62,8 +55,6 @@ def search_serphouse(query, country, page=1, num_result=100):
|
|
62 |
|
63 |
try:
|
64 |
response = requests.post(url, json=payload, headers=headers)
|
65 |
-
print("Response status:", response.status_code) # ๋๋ฒ๊น
์ ์ํ ์ถ๋ ฅ
|
66 |
-
print("Response content:", response.text) # ๋๋ฒ๊น
์ ์ํ ์ถ๋ ฅ
|
67 |
response.raise_for_status()
|
68 |
return response.json()
|
69 |
except requests.RequestException as e:
|
@@ -108,8 +99,10 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
108 |
with gr.Row():
|
109 |
query = gr.Textbox(label="๊ฒ์์ด")
|
110 |
country = gr.Dropdown(MAJOR_COUNTRIES, label="๊ตญ๊ฐ", value="South Korea")
|
111 |
-
search_button = gr.Button("๊ฒ์")
|
112 |
|
|
|
|
|
113 |
status_message = gr.Markdown(visible=False)
|
114 |
articles_state = gr.State([])
|
115 |
|
@@ -130,8 +123,10 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
130 |
'index': i,
|
131 |
})
|
132 |
|
133 |
-
def search_and_display(query, country, articles_state):
|
|
|
134 |
error_message, articles = serphouse_search(query, country)
|
|
|
135 |
outputs = []
|
136 |
|
137 |
if error_message:
|
@@ -144,7 +139,9 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
144 |
articles_state = []
|
145 |
else:
|
146 |
outputs.append(gr.update(value="", visible=False))
|
|
|
147 |
for idx, comp in enumerate(article_components):
|
|
|
148 |
if idx < len(articles):
|
149 |
article = articles[idx]
|
150 |
image_url = article['image_url']
|
@@ -164,6 +161,7 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
164 |
])
|
165 |
articles_state = articles
|
166 |
|
|
|
167 |
outputs.append(articles_state)
|
168 |
outputs.append(gr.update(visible=False))
|
169 |
return outputs
|
@@ -178,7 +176,7 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
178 |
search_and_display,
|
179 |
inputs=[query, country, articles_state],
|
180 |
outputs=search_outputs,
|
181 |
-
show_progress=
|
182 |
)
|
183 |
|
184 |
iface.launch()
|
|
|
32 |
def search_serphouse(query, country, page=1, num_result=100):
|
33 |
url = "https://api.serphouse.com/serp/live"
|
34 |
|
|
|
|
|
|
|
|
|
35 |
payload = {
|
36 |
"data": {
|
37 |
"q": query,
|
38 |
"domain": "google.com",
|
39 |
+
"loc_id": COUNTRY_LOCATIONS.get(country, "1026201"),
|
|
|
40 |
"lang": "en",
|
41 |
"device": "desktop",
|
42 |
"serp_type": "news",
|
43 |
+
"page": "1",
|
44 |
+
"verbatim": "0",
|
45 |
+
"gfilter": "0",
|
46 |
+
"num_result": "100" # num_result๋ก ์์
|
47 |
}
|
48 |
}
|
49 |
|
|
|
|
|
50 |
headers = {
|
51 |
"accept": "application/json",
|
52 |
"content-type": "application/json",
|
|
|
55 |
|
56 |
try:
|
57 |
response = requests.post(url, json=payload, headers=headers)
|
|
|
|
|
58 |
response.raise_for_status()
|
59 |
return response.json()
|
60 |
except requests.RequestException as e:
|
|
|
99 |
with gr.Row():
|
100 |
query = gr.Textbox(label="๊ฒ์์ด")
|
101 |
country = gr.Dropdown(MAJOR_COUNTRIES, label="๊ตญ๊ฐ", value="South Korea")
|
102 |
+
search_button = gr.Button("๊ฒ์", variant="primary")
|
103 |
|
104 |
+
# ํ๋ก๊ทธ๋ ์ค๋ฐ ์ถ๊ฐ
|
105 |
+
progress = gr.Progress()
|
106 |
status_message = gr.Markdown(visible=False)
|
107 |
articles_state = gr.State([])
|
108 |
|
|
|
123 |
'index': i,
|
124 |
})
|
125 |
|
126 |
+
def search_and_display(query, country, articles_state, progress=gr.Progress()):
|
127 |
+
progress(0, desc="๊ฒ์ ์์...")
|
128 |
error_message, articles = serphouse_search(query, country)
|
129 |
+
progress(0.5, desc="๊ฒฐ๊ณผ ์ฒ๋ฆฌ ์ค...")
|
130 |
outputs = []
|
131 |
|
132 |
if error_message:
|
|
|
139 |
articles_state = []
|
140 |
else:
|
141 |
outputs.append(gr.update(value="", visible=False))
|
142 |
+
total_articles = len(articles)
|
143 |
for idx, comp in enumerate(article_components):
|
144 |
+
progress((idx + 1) / total_articles, desc=f"๊ฒฐ๊ณผ ํ์ ์ค... {idx + 1}/{total_articles}")
|
145 |
if idx < len(articles):
|
146 |
article = articles[idx]
|
147 |
image_url = article['image_url']
|
|
|
161 |
])
|
162 |
articles_state = articles
|
163 |
|
164 |
+
progress(1.0, desc="์๋ฃ!")
|
165 |
outputs.append(articles_state)
|
166 |
outputs.append(gr.update(visible=False))
|
167 |
return outputs
|
|
|
176 |
search_and_display,
|
177 |
inputs=[query, country, articles_state],
|
178 |
outputs=search_outputs,
|
179 |
+
show_progress=True
|
180 |
)
|
181 |
|
182 |
iface.launch()
|