Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
seawolf2357
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -6,17 +6,21 @@ from datetime import datetime, timedelta
|
|
6 |
from huggingface_hub import InferenceClient # LLM ์ฌ์ฉ์ ์ํด ํ์
|
7 |
|
8 |
# ํ๊ฒฝ ๋ณ์์์ API ํค ๊ฐ์ ธ์ค๊ธฐ
|
9 |
-
API_KEY = os.getenv("SERPHOUSE_API_KEY")
|
10 |
-
HF_TOKEN = os.getenv("HF_TOKEN")
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
"
|
15 |
-
"
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
20 |
|
21 |
def search_serphouse(query, country, page=1, num_result=10):
|
22 |
url = "https://api.serphouse.com/serp/live"
|
@@ -25,14 +29,11 @@ def search_serphouse(query, country, page=1, num_result=10):
|
|
25 |
yesterday = now - timedelta(days=1)
|
26 |
date_range = f"{yesterday.strftime('%Y-%m-%d')},{now.strftime('%Y-%m-%d')}"
|
27 |
|
28 |
-
# ๊ตญ๊ฐ ์ด๋ฆ์ ์์น ID๋ก ๋ณํ
|
29 |
-
loc_id = COUNTRY_CODE_MAPPING.get(country, "2840") # ๊ธฐ๋ณธ๊ฐ์ ๋ฏธ๊ตญ
|
30 |
-
|
31 |
payload = {
|
32 |
"data": {
|
33 |
"q": query,
|
34 |
"domain": "google.com",
|
35 |
-
"loc":
|
36 |
"lang": "en",
|
37 |
"device": "desktop",
|
38 |
"serp_type": "news",
|
@@ -135,7 +136,7 @@ footer {
|
|
135 |
"""
|
136 |
|
137 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
138 |
-
with gr.Blocks(css=css, title="NewsAI ์๋น์ค") as iface:
|
139 |
gr.Markdown("๊ฒ์์ด๋ฅผ ์
๋ ฅํ๊ณ ์ํ๋ ๊ตญ๊ฐ๋ฅผ ์ ํํ๋ฉด, ๊ฒ์์ด์ ์ผ์นํ๋ 24์๊ฐ ์ด๋ด ๋ด์ค๋ฅผ ์ต๋ 10๊ฐ ์ถ๋ ฅํฉ๋๋ค.")
|
140 |
|
141 |
with gr.Column():
|
@@ -144,8 +145,8 @@ with gr.Blocks(css=css, title="NewsAI ์๋น์ค") as iface:
|
|
144 |
country = gr.Dropdown(MAJOR_COUNTRIES, label="๊ตญ๊ฐ", value="South Korea")
|
145 |
search_button = gr.Button("๊ฒ์")
|
146 |
|
147 |
-
# ์ํ ๋ฉ์์ง ์ปดํฌ๋ํธ
|
148 |
-
|
149 |
|
150 |
# ๊ธฐ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ์ํ ๋ณ์
|
151 |
articles_state = gr.State([]) # ์ด๊ธฐ๊ฐ์ ๋น ๋ฆฌ์คํธ๋ก ์ค์
|
@@ -173,14 +174,44 @@ with gr.Blocks(css=css, title="NewsAI ์๋น์ค") as iface:
|
|
173 |
})
|
174 |
|
175 |
def search_and_display(query, country, articles_state):
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
outputs.extend([
|
185 |
gr.update(visible=False), # group
|
186 |
gr.update(), # title
|
@@ -189,39 +220,10 @@ with gr.Blocks(css=css, title="NewsAI ์๋น์ค") as iface:
|
|
189 |
gr.update(), # info
|
190 |
gr.update(visible=False), # summary_output
|
191 |
])
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
if idx < len(articles):
|
197 |
-
article = articles[idx]
|
198 |
-
# ์ด๋ฏธ์ง ์ฒ๋ฆฌ ์์
|
199 |
-
image_url = article['image_url']
|
200 |
-
if image_url and not image_url.startswith('data:image'):
|
201 |
-
image_update = gr.update(value=image_url, visible=True)
|
202 |
-
else:
|
203 |
-
image_update = gr.update(value=None, visible=False)
|
204 |
-
|
205 |
-
outputs.extend([
|
206 |
-
gr.update(visible=True), # group
|
207 |
-
gr.update(value=f"### [{article['title']}]({article['link']})"), # title
|
208 |
-
image_update, # image
|
209 |
-
gr.update(value=f"**์์ฝ:** {article['snippet']}"), # snippet
|
210 |
-
gr.update(value=f"**์ถ์ฒ:** {article['channel']} | **์๊ฐ:** {article['time']}"), # info
|
211 |
-
gr.update(visible=False), # summary_output
|
212 |
-
])
|
213 |
-
else:
|
214 |
-
outputs.extend([
|
215 |
-
gr.update(visible=False), # group
|
216 |
-
gr.update(), # title
|
217 |
-
gr.update(), # image
|
218 |
-
gr.update(), # snippet
|
219 |
-
gr.update(), # info
|
220 |
-
gr.update(visible=False), # summary_output
|
221 |
-
])
|
222 |
-
articles_state = articles # articles_state ์
๋ฐ์ดํธ
|
223 |
-
outputs.append(articles_state)
|
224 |
-
# ์ํ ๋ฉ์์ง ์จ๊น (Progress๋ ์๋์ผ๋ก ์ฌ๋ผ์ง๋๋ค)
|
225 |
return outputs
|
226 |
|
227 |
# search_button ํด๋ฆญ ์ ์
๋ฐ์ดํธ๋ ์ถ๋ ฅ ์ปดํฌ๋ํธ ๋ชฉ๋ก ์์ฑ
|
@@ -236,32 +238,37 @@ with gr.Blocks(css=css, title="NewsAI ์๋น์ค") as iface:
|
|
236 |
search_outputs.append(comp['info'])
|
237 |
search_outputs.append(comp['summary_output'])
|
238 |
search_outputs.append(articles_state)
|
239 |
-
#
|
240 |
|
241 |
search_button.click(
|
242 |
search_and_display,
|
243 |
inputs=[query, country, articles_state],
|
244 |
-
outputs=search_outputs
|
|
|
245 |
)
|
246 |
|
247 |
# ๋ถ์ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ ์ค์
|
248 |
for idx, comp in enumerate(article_components):
|
249 |
def create_analyze_function(index=idx):
|
250 |
def analyze_article(articles):
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
|
|
|
|
259 |
return analyze_article
|
260 |
|
|
|
261 |
comp['analyze_button'].click(
|
262 |
create_analyze_function(),
|
263 |
inputs=[articles_state],
|
264 |
-
outputs=comp['summary_output']
|
|
|
265 |
)
|
266 |
|
267 |
iface.launch(auth=("gini", "pick"))
|
|
|
6 |
from huggingface_hub import InferenceClient # LLM ์ฌ์ฉ์ ์ํด ํ์
|
7 |
|
8 |
# ํ๊ฒฝ ๋ณ์์์ API ํค ๊ฐ์ ธ์ค๊ธฐ
|
9 |
+
API_KEY = os.getenv("SERPHOUSE_API_KEY") # ๋ณธ์ธ์ SerpHouse API ํค๋ฅผ ํ๊ฒฝ ๋ณ์๋ก ์ค์ ํ์ธ์.
|
10 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # Hugging Face API ํ ํฐ์ ํ๊ฒฝ ๋ณ์๋ก ์ค์ ํ์ธ์.
|
11 |
+
|
12 |
+
MAJOR_COUNTRIES = [
|
13 |
+
"United States", "United Kingdom", "Canada", "Australia", "Germany",
|
14 |
+
"France", "Japan", "South Korea", "China", "India",
|
15 |
+
"Brazil", "Mexico", "Russia", "Italy", "Spain",
|
16 |
+
"Netherlands", "Sweden", "Switzerland", "Norway", "Denmark",
|
17 |
+
"Finland", "Belgium", "Austria", "New Zealand", "Ireland",
|
18 |
+
"Singapore", "Hong Kong", "Israel", "United Arab Emirates", "Saudi Arabia",
|
19 |
+
"South Africa", "Turkey", "Egypt", "Poland", "Czech Republic",
|
20 |
+
"Hungary", "Greece", "Portugal", "Argentina", "Chile",
|
21 |
+
"Colombia", "Peru", "Venezuela", "Thailand", "Malaysia",
|
22 |
+
"Indonesia", "Philippines", "Vietnam", "Pakistan", "Bangladesh"
|
23 |
+
]
|
24 |
|
25 |
def search_serphouse(query, country, page=1, num_result=10):
|
26 |
url = "https://api.serphouse.com/serp/live"
|
|
|
29 |
yesterday = now - timedelta(days=1)
|
30 |
date_range = f"{yesterday.strftime('%Y-%m-%d')},{now.strftime('%Y-%m-%d')}"
|
31 |
|
|
|
|
|
|
|
32 |
payload = {
|
33 |
"data": {
|
34 |
"q": query,
|
35 |
"domain": "google.com",
|
36 |
+
"loc": country, # ๊ตญ๊ฐ ์ด๋ฆ์ ์ง์ ์ฌ์ฉํฉ๋๋ค.
|
37 |
"lang": "en",
|
38 |
"device": "desktop",
|
39 |
"serp_type": "news",
|
|
|
136 |
"""
|
137 |
|
138 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
139 |
+
with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as iface:
|
140 |
gr.Markdown("๊ฒ์์ด๋ฅผ ์
๋ ฅํ๊ณ ์ํ๋ ๊ตญ๊ฐ๋ฅผ ์ ํํ๋ฉด, ๊ฒ์์ด์ ์ผ์นํ๋ 24์๊ฐ ์ด๋ด ๋ด์ค๋ฅผ ์ต๋ 10๊ฐ ์ถ๋ ฅํฉ๋๋ค.")
|
141 |
|
142 |
with gr.Column():
|
|
|
145 |
country = gr.Dropdown(MAJOR_COUNTRIES, label="๊ตญ๊ฐ", value="South Korea")
|
146 |
search_button = gr.Button("๊ฒ์")
|
147 |
|
148 |
+
# ์ํ ๋ฉ์์ง ์ปดํฌ๋ํธ ์ถ๊ฐ
|
149 |
+
status_message = gr.Markdown(visible=False)
|
150 |
|
151 |
# ๊ธฐ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ์ํ ๋ณ์
|
152 |
articles_state = gr.State([]) # ์ด๊ธฐ๊ฐ์ ๋น ๋ฆฌ์คํธ๋ก ์ค์
|
|
|
174 |
})
|
175 |
|
176 |
def search_and_display(query, country, articles_state):
|
177 |
+
# ์ํ ๋ฉ์์ง ํ์
|
178 |
+
status_update = gr.update(value="์ฒ๋ฆฌ์ค์
๋๋ค. ์ ์๋ง ๊ธฐ๋ค๋ฆฌ์ธ์.", visible=True)
|
179 |
+
|
180 |
+
error_message, articles = serphouse_search(query, country)
|
181 |
+
outputs = []
|
182 |
+
if error_message:
|
183 |
+
outputs.append(gr.update(value=error_message, visible=True))
|
184 |
+
for comp in article_components:
|
185 |
+
outputs.extend([
|
186 |
+
gr.update(visible=False), # group
|
187 |
+
gr.update(), # title
|
188 |
+
gr.update(), # image
|
189 |
+
gr.update(), # snippet
|
190 |
+
gr.update(), # info
|
191 |
+
gr.update(visible=False), # summary_output
|
192 |
+
])
|
193 |
+
articles_state = []
|
194 |
+
else:
|
195 |
+
outputs.append(gr.update(value="", visible=False))
|
196 |
+
for idx, comp in enumerate(article_components):
|
197 |
+
if idx < len(articles):
|
198 |
+
article = articles[idx]
|
199 |
+
# ์ด๋ฏธ์ง ์ฒ๋ฆฌ ์์
|
200 |
+
image_url = article['image_url']
|
201 |
+
if image_url and not image_url.startswith('data:image'):
|
202 |
+
image_update = gr.update(value=image_url, visible=True)
|
203 |
+
else:
|
204 |
+
image_update = gr.update(value=None, visible=False)
|
205 |
+
|
206 |
+
outputs.extend([
|
207 |
+
gr.update(visible=True), # group
|
208 |
+
gr.update(value=f"### [{article['title']}]({article['link']})"), # title
|
209 |
+
image_update, # image
|
210 |
+
gr.update(value=f"**์์ฝ:** {article['snippet']}"), # snippet
|
211 |
+
gr.update(value=f"**์ถ์ฒ:** {article['channel']} | **์๊ฐ:** {article['time']}"), # info
|
212 |
+
gr.update(visible=False), # summary_output
|
213 |
+
])
|
214 |
+
else:
|
215 |
outputs.extend([
|
216 |
gr.update(visible=False), # group
|
217 |
gr.update(), # title
|
|
|
220 |
gr.update(), # info
|
221 |
gr.update(visible=False), # summary_output
|
222 |
])
|
223 |
+
articles_state = articles # articles_state ์
๋ฐ์ดํธ
|
224 |
+
outputs.append(articles_state)
|
225 |
+
# ์ํ ๋ฉ์์ง ์จ๊น
|
226 |
+
outputs.append(gr.update(visible=False))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
227 |
return outputs
|
228 |
|
229 |
# search_button ํด๋ฆญ ์ ์
๋ฐ์ดํธ๋ ์ถ๋ ฅ ์ปดํฌ๋ํธ ๋ชฉ๋ก ์์ฑ
|
|
|
238 |
search_outputs.append(comp['info'])
|
239 |
search_outputs.append(comp['summary_output'])
|
240 |
search_outputs.append(articles_state)
|
241 |
+
search_outputs.append(status_message) # ์ํ ๋ฉ์์ง ์ถ๋ ฅ์ ์ถ๊ฐ
|
242 |
|
243 |
search_button.click(
|
244 |
search_and_display,
|
245 |
inputs=[query, country, articles_state],
|
246 |
+
outputs=search_outputs,
|
247 |
+
show_progress=False # Gradio์ ๊ธฐ๋ณธ ๋ก๋ฉ ํ์๋ฅผ ๋๋๋ค.
|
248 |
)
|
249 |
|
250 |
# ๋ถ์ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ ์ค์
|
251 |
for idx, comp in enumerate(article_components):
|
252 |
def create_analyze_function(index=idx):
|
253 |
def analyze_article(articles):
|
254 |
+
# ์ํ ๋ฉ์์ง ํ์
|
255 |
+
status_update = gr.update(value="์ฒ๋ฆฌ์ค์
๋๋ค. ์ ์๋ง ๊ธฐ๋ค๋ฆฌ์ธ์.", visible=True)
|
256 |
+
if articles and index < len(articles):
|
257 |
+
article = articles[index]
|
258 |
+
summary = summarize_article(article['title'], article['snippet'])
|
259 |
+
# ์ํ ๋ฉ์์ง ์จ๊น
|
260 |
+
return gr.update(value=summary, visible=True), gr.update(visible=False)
|
261 |
+
else:
|
262 |
+
# ์ํ ๋ฉ์์ง ์จ๊น
|
263 |
+
return gr.update(value="๊ธฐ์ฌ ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.", visible=True), gr.update(visible=False)
|
264 |
return analyze_article
|
265 |
|
266 |
+
# ๋ถ์ ๋ฒํผ์ ์ถ๋ ฅ์ ์ํ ๋ฉ์์ง๋ฅผ ์ถ๊ฐํฉ๋๋ค.
|
267 |
comp['analyze_button'].click(
|
268 |
create_analyze_function(),
|
269 |
inputs=[articles_state],
|
270 |
+
outputs=[comp['summary_output'], status_message],
|
271 |
+
show_progress=False # Gradio์ ๊ธฐ๋ณธ ๋ก๋ฉ ํ์๋ฅผ ๋๋๋ค.
|
272 |
)
|
273 |
|
274 |
iface.launch(auth=("gini", "pick"))
|