Spaces:
openfree
/
Running on CPU Upgrade

seawolf2357 commited on
Commit
68aa430
ยท
verified ยท
1 Parent(s): e683b36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -19
app.py CHANGED
@@ -136,23 +136,16 @@ def translate_query(query, country):
136
  def search_serphouse(query, country, page=1, num_result=10):
137
  url = "https://api.serphouse.com/serp/live"
138
 
139
- # ๊ฒ€์ƒ‰์–ด ๋ฒˆ์—ญ
140
- translated_query = translate_query(query, country)
141
- print(f"Original query: {query}")
142
- print(f"Translated query: {translated_query}")
143
-
144
  payload = {
145
  "data": {
146
- "q": translated_query,
147
  "domain": "google.com",
148
  "loc": COUNTRY_LOCATIONS.get(country, "United States"),
149
- "lang": COUNTRY_LANGUAGES.get(country, "en"),
150
  "device": "desktop",
151
  "serp_type": "news",
152
  "page": "1",
153
- "num": "10",
154
- "verbatim": "0",
155
- "gfilter": "0"
156
  }
157
  }
158
 
@@ -168,9 +161,9 @@ def search_serphouse(query, country, page=1, num_result=10):
168
  print("Response status:", response.status_code)
169
 
170
  response.raise_for_status()
171
- return response.json(), translated_query
172
  except requests.RequestException as e:
173
- return {"error": f"Error: {str(e)}"}, query
174
 
175
  def format_results_from_raw(results, translated_query):
176
  if isinstance(results, dict) and "error" in results:
@@ -205,6 +198,7 @@ css = """
205
  footer {visibility: hidden;}
206
  """
207
 
 
208
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as iface:
209
  gr.Markdown("๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์›ํ•˜๋Š” ๊ตญ๊ฐ€๋ฅผ ์„ ํƒํ•˜๋ฉด, ๊ฒ€์ƒ‰์–ด์™€ ์ผ์น˜ํ•˜๋Š” 24์‹œ๊ฐ„ ์ด๋‚ด ๋‰ด์Šค๋ฅผ ์ตœ๋Œ€ 100๊ฐœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.")
210
 
@@ -212,7 +206,11 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
212
  with gr.Row():
213
  query = gr.Textbox(label="๊ฒ€์ƒ‰์–ด")
214
  country = gr.Dropdown(MAJOR_COUNTRIES, label="๊ตญ๊ฐ€", value="South Korea")
215
- search_button = gr.Button("๊ฒ€์ƒ‰", variant="primary")
 
 
 
 
216
 
217
  progress = gr.Progress()
218
  status_message = gr.Markdown(visible=False)
@@ -236,10 +234,17 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
236
  })
237
 
238
  def search_and_display(query, country, articles_state, progress=gr.Progress()):
239
- progress(0, desc="๊ฒ€์ƒ‰ ์‹œ์ž‘...")
 
 
 
 
 
 
240
  error_message, articles = serphouse_search(query, country)
241
  progress(0.5, desc="๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ์ค‘...")
242
- outputs = []
 
243
 
244
  if error_message:
245
  outputs.append(gr.update(value=error_message, visible=True))
@@ -258,12 +263,10 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
258
  article = articles[idx]
259
  image_url = article['image_url']
260
  image_update = gr.update(value=image_url, visible=True) if image_url and not image_url.startswith('data:image') else gr.update(value=None, visible=False)
261
-
262
- translated_info = f"\n**๋ฒˆ์—ญ๋œ ๊ฒ€์ƒ‰์–ด:** {article['translated_query']}" if article['translated_query'] != query else ""
263
 
264
  outputs.extend([
265
  gr.update(visible=True),
266
- gr.update(value=f"### [{article['title']}]({article['link']}){translated_info}"),
267
  image_update,
268
  gr.update(value=f"**์š”์•ฝ:** {article['snippet']}"),
269
  gr.update(value=f"**์ถœ์ฒ˜:** {article['channel']} | **์‹œ๊ฐ„:** {article['time']}")
@@ -280,7 +283,7 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as
280
  outputs.append(gr.update(visible=False))
281
  return outputs
282
 
283
- search_outputs = [gr.Markdown(visible=False)]
284
  for comp in article_components:
285
  search_outputs.extend([comp['group'], comp['title'], comp['image'],
286
  comp['snippet'], comp['info']])
 
136
  def search_serphouse(query, country, page=1, num_result=10):
137
  url = "https://api.serphouse.com/serp/live"
138
 
 
 
 
 
 
139
  payload = {
140
  "data": {
141
+ "q": query, # ์›๋ณธ ๊ฒ€์ƒ‰์–ด ์‚ฌ์šฉ
142
  "domain": "google.com",
143
  "loc": COUNTRY_LOCATIONS.get(country, "United States"),
144
+ "lang": "en", # ๊ธฐ๋ณธ ์˜์–ด๋กœ ์„ค์ •
145
  "device": "desktop",
146
  "serp_type": "news",
147
  "page": "1",
148
+ "num": "10"
 
 
149
  }
150
  }
151
 
 
161
  print("Response status:", response.status_code)
162
 
163
  response.raise_for_status()
164
+ return response.json()
165
  except requests.RequestException as e:
166
+ return {"error": f"Error: {str(e)}"}
167
 
168
  def format_results_from_raw(results, translated_query):
169
  if isinstance(results, dict) and "error" in results:
 
198
  footer {visibility: hidden;}
199
  """
200
 
201
+ # Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ˆ˜์ •
202
  with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="NewsAI ์„œ๋น„์Šค") as iface:
203
  gr.Markdown("๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ์›ํ•˜๋Š” ๊ตญ๊ฐ€๋ฅผ ์„ ํƒํ•˜๋ฉด, ๊ฒ€์ƒ‰์–ด์™€ ์ผ์น˜ํ•˜๋Š” 24์‹œ๊ฐ„ ์ด๋‚ด ๋‰ด์Šค๋ฅผ ์ตœ๋Œ€ 100๊ฐœ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.")
204
 
 
206
  with gr.Row():
207
  query = gr.Textbox(label="๊ฒ€์ƒ‰์–ด")
208
  country = gr.Dropdown(MAJOR_COUNTRIES, label="๊ตญ๊ฐ€", value="South Korea")
209
+
210
+ # ๋ฒˆ์—ญ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ์ปดํฌ๋„ŒํŠธ ์ถ”๊ฐ€
211
+ translated_query_display = gr.Markdown(visible=False)
212
+
213
+ search_button = gr.Button("๊ฒ€์ƒ‰", variant="primary")
214
 
215
  progress = gr.Progress()
216
  status_message = gr.Markdown(visible=False)
 
234
  })
235
 
236
  def search_and_display(query, country, articles_state, progress=gr.Progress()):
237
+ progress(0, desc="๊ฒ€์ƒ‰์–ด ๋ฒˆ์—ญ ์ค‘...")
238
+
239
+ # ๊ฒ€์ƒ‰์–ด ๋ฒˆ์—ญ
240
+ translated_query = translate_query(query, country)
241
+ translated_display = f"**์›๋ณธ ๊ฒ€์ƒ‰์–ด:** {query}\n**๋ฒˆ์—ญ๋œ ๊ฒ€์ƒ‰์–ด:** {translated_query}" if translated_query != query else f"**๊ฒ€์ƒ‰์–ด:** {query}"
242
+
243
+ progress(0.2, desc="๊ฒ€์ƒ‰ ์‹œ์ž‘...")
244
  error_message, articles = serphouse_search(query, country)
245
  progress(0.5, desc="๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ์ค‘...")
246
+
247
+ outputs = [gr.update(value=translated_display, visible=True)] # ๋ฒˆ์—ญ ๊ฒฐ๊ณผ ํ‘œ์‹œ
248
 
249
  if error_message:
250
  outputs.append(gr.update(value=error_message, visible=True))
 
263
  article = articles[idx]
264
  image_url = article['image_url']
265
  image_update = gr.update(value=image_url, visible=True) if image_url and not image_url.startswith('data:image') else gr.update(value=None, visible=False)
 
 
266
 
267
  outputs.extend([
268
  gr.update(visible=True),
269
+ gr.update(value=f"### [{article['title']}]({article['link']})"),
270
  image_update,
271
  gr.update(value=f"**์š”์•ฝ:** {article['snippet']}"),
272
  gr.update(value=f"**์ถœ์ฒ˜:** {article['channel']} | **์‹œ๊ฐ„:** {article['time']}")
 
283
  outputs.append(gr.update(visible=False))
284
  return outputs
285
 
286
+ search_outputs = [translated_query_display, gr.Markdown(visible=False)] # ๋ฒˆ์—ญ ๊ฒฐ๊ณผ ์ปดํฌ๋„ŒํŠธ ์ถ”๊ฐ€
287
  for comp in article_components:
288
  search_outputs.extend([comp['group'], comp['title'], comp['image'],
289
  comp['snippet'], comp['info']])