Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,21 +6,17 @@ 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 |
-
"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,11 +25,14 @@ def search_serphouse(query, country, page=1, num_result=10):
|
|
| 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":
|
| 37 |
"lang": "en",
|
| 38 |
"device": "desktop",
|
| 39 |
"serp_type": "news",
|
|
@@ -136,7 +135,7 @@ footer {
|
|
| 136 |
"""
|
| 137 |
|
| 138 |
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 139 |
-
with gr.Blocks(
|
| 140 |
gr.Markdown("๊ฒ์์ด๋ฅผ ์
๋ ฅํ๊ณ ์ํ๋ ๊ตญ๊ฐ๋ฅผ ์ ํํ๋ฉด, ๊ฒ์์ด์ ์ผ์นํ๋ 24์๊ฐ ์ด๋ด ๋ด์ค๋ฅผ ์ต๋ 10๊ฐ ์ถ๋ ฅํฉ๋๋ค.")
|
| 141 |
|
| 142 |
with gr.Column():
|
|
@@ -145,8 +144,8 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
| 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,44 +173,14 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
| 174 |
})
|
| 175 |
|
| 176 |
def search_and_display(query, country, articles_state):
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 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,10 +189,39 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
| 220 |
gr.update(), # info
|
| 221 |
gr.update(visible=False), # summary_output
|
| 222 |
])
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
return outputs
|
| 228 |
|
| 229 |
# search_button ํด๋ฆญ ์ ์
๋ฐ์ดํธ๋ ์ถ๋ ฅ ์ปดํฌ๋ํธ ๋ชฉ๋ก ์์ฑ
|
|
@@ -238,37 +236,32 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์๋น์ค") as
|
|
| 238 |
search_outputs.append(comp['info'])
|
| 239 |
search_outputs.append(comp['summary_output'])
|
| 240 |
search_outputs.append(articles_state)
|
| 241 |
-
|
| 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 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 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=
|
| 271 |
-
show_progress=False # Gradio์ ๊ธฐ๋ณธ ๋ก๋ฉ ํ์๋ฅผ ๋๋๋ค.
|
| 272 |
)
|
| 273 |
|
| 274 |
iface.launch(auth=("gini", "pick"))
|
|
|
|
| 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 |
+
# ๊ตญ๊ฐ ์ด๋ฆ๊ณผ ์์น ID๋ฅผ ๋งคํ
|
| 13 |
+
COUNTRY_CODE_MAPPING = {
|
| 14 |
+
"United States": "2840",
|
| 15 |
+
"South Korea": "2458",
|
| 16 |
+
# ๋ค๋ฅธ ๊ตญ๊ฐ๋ค์ ์์น ID๋ฅผ ์ถ๊ฐํ์ธ์
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
MAJOR_COUNTRIES = list(COUNTRY_CODE_MAPPING.keys())
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
def search_serphouse(query, country, page=1, num_result=10):
|
| 22 |
url = "https://api.serphouse.com/serp/live"
|
|
|
|
| 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": loc_id, # ์์น ID ์ฌ์ฉ
|
| 36 |
"lang": "en",
|
| 37 |
"device": "desktop",
|
| 38 |
"serp_type": "news",
|
|
|
|
| 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 |
country = gr.Dropdown(MAJOR_COUNTRIES, label="๊ตญ๊ฐ", value="South Korea")
|
| 145 |
search_button = gr.Button("๊ฒ์")
|
| 146 |
|
| 147 |
+
# ์ํ ๋ฉ์์ง ์ปดํฌ๋ํธ ์ ๊ฑฐ (Progress๋ฅผ ์ฌ์ฉํ๋ฏ๋ก ๋ถํ์)
|
| 148 |
+
# status_message = gr.Markdown(visible=False)
|
| 149 |
|
| 150 |
# ๊ธฐ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ ์ฅํ ์ํ ๋ณ์
|
| 151 |
articles_state = gr.State([]) # ์ด๊ธฐ๊ฐ์ ๋น ๋ฆฌ์คํธ๋ก ์ค์
|
|
|
|
| 173 |
})
|
| 174 |
|
| 175 |
def search_and_display(query, country, articles_state):
|
| 176 |
+
with gr.Progress() as progress:
|
| 177 |
+
progress(0, desc="์ฒ๋ฆฌ์ค์
๋๋ค. ์ ์๋ง ๊ธฐ๋ค๋ฆฌ์ธ์.")
|
| 178 |
+
|
| 179 |
+
error_message, articles = serphouse_search(query, country)
|
| 180 |
+
outputs = []
|
| 181 |
+
if error_message:
|
| 182 |
+
outputs.append(gr.update(value=error_message, visible=True))
|
| 183 |
+
for comp in article_components:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
outputs.extend([
|
| 185 |
gr.update(visible=False), # group
|
| 186 |
gr.update(), # title
|
|
|
|
| 189 |
gr.update(), # info
|
| 190 |
gr.update(visible=False), # summary_output
|
| 191 |
])
|
| 192 |
+
articles_state = []
|
| 193 |
+
else:
|
| 194 |
+
outputs.append(gr.update(value="", visible=False))
|
| 195 |
+
for idx, comp in enumerate(article_components):
|
| 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 |
search_outputs.append(comp['info'])
|
| 237 |
search_outputs.append(comp['summary_output'])
|
| 238 |
search_outputs.append(articles_state)
|
| 239 |
+
# status_message ์ปดํฌ๋ํธ ์ ๊ฑฐ
|
| 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 |
+
with gr.Progress() as progress:
|
| 252 |
+
progress(0, desc="์ฒ๋ฆฌ์ค์
๋๋ค. ์ ์๋ง ๊ธฐ๋ค๋ฆฌ์ธ์.")
|
| 253 |
+
if articles and index < len(articles):
|
| 254 |
+
article = articles[index]
|
| 255 |
+
summary = summarize_article(article['title'], article['snippet'])
|
| 256 |
+
return gr.update(value=summary, visible=True)
|
| 257 |
+
else:
|
| 258 |
+
return gr.update(value="๊ธฐ์ฌ ์ ๋ณด๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค.", visible=True)
|
|
|
|
|
|
|
| 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"))
|