Spaces:
openfree
/
Running on CPU Upgrade

seawolf2357 commited on
Commit
c7302ea
ยท
verified ยท
1 Parent(s): 695d9f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -70
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
- # ๊ตญ๊ฐ€ ์ด๋ฆ„๊ณผ ์œ„์น˜ 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,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": loc_id, # ์œ„์น˜ ID ์‚ฌ์šฉ
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
- # ์ƒํƒœ ๋ฉ”์‹œ์ง€ ์ปดํฌ๋„ŒํŠธ ์ œ๊ฑฐ (Progress๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๋ถˆํ•„์š”)
148
- # status_message = gr.Markdown(visible=False)
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
- 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,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
- 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,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
- # 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"))
 
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"))