seawolf2357 commited on
Commit
e63a0fd
ยท
verified ยท
1 Parent(s): 2e959ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -47
app.py CHANGED
@@ -57,15 +57,8 @@ def search_serphouse(query, country, page, num_result):
57
  return {"error": error_msg}
58
 
59
  def format_results(results):
60
- all_results = "<h2>๋ชจ๋“  ๋‰ด์Šค ๊ฒฐ๊ณผ (24์‹œ๊ฐ„ ์ด๋‚ด)</h2>"
61
- debug_info = "<h2>๋””๋ฒ„๊ทธ ์ •๋ณด</h2>"
62
-
63
  if isinstance(results, dict) and "error" in results:
64
- all_results += f"<p>์˜ค๋ฅ˜ ๋ฐœ์ƒ: {results['error']}</p>"
65
- debug_info += f"<pre>{results['error']}</pre>"
66
- return all_results, debug_info
67
-
68
- debug_info += f"<pre>{json.dumps(results, indent=2, ensure_ascii=False)}</pre>"
69
 
70
  try:
71
  if not isinstance(results, dict):
@@ -75,52 +68,38 @@ def format_results(results):
75
  raise ValueError("'results' ํ‚ค๊ฐ€ ์‘๋‹ต์— ์—†์Šต๋‹ˆ๋‹ค.")
76
 
77
  news_results = results["results"].get("news", [])
78
- debug_info += f"<p>๋‰ด์Šค ๊ฒฐ๊ณผ ์ˆ˜: {len(news_results)}</p>"
79
 
80
  if not news_results:
81
- all_results += "<p>๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.</p>"
82
- else:
83
- all_results += "<ol>"
84
- for result in news_results:
85
- title = result.get("title", "์ œ๋ชฉ ์—†์Œ")
86
- url = result.get("url", "#")
87
- snippet = result.get("snippet", "๋‚ด์šฉ ์—†์Œ")
88
- channel = result.get("channel", "์•Œ ์ˆ˜ ์—†์Œ")
89
- time_str = result.get("time", "์•Œ ์ˆ˜ ์—†๋Š” ์‹œ๊ฐ„")
90
-
91
- article_info = f"""
92
- <li>
93
- <h3><a href="{url}" target="_blank">{title}</a></h3>
94
- <p>{snippet}</p>
95
- <p><strong>์ถœ์ฒ˜:</strong> {channel} - {time_str}</p>
96
- </li>
97
- """
98
- all_results += article_info
99
 
100
- all_results += "</ol>"
 
 
 
 
 
 
 
101
 
102
  except Exception as e:
103
- error_message = f"๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"
104
- debug_info += f"<p>{error_message}</p>"
105
- all_results += f"<p>{error_message}</p>"
106
-
107
- return all_results, debug_info
108
 
109
  def serphouse_search(query, country, page, num_result):
110
  results = search_serphouse(query, country, page, num_result)
111
- all_results, debug_info = format_results(results)
112
- return all_results, debug_info
113
 
114
  css = """
115
  footer {
116
  visibility: hidden;
117
  }
118
- ol {
119
- padding-left: 20px;
120
- }
121
- li {
122
- margin-bottom: 20px;
123
- }
124
  """
125
 
126
  iface = gr.Interface(
@@ -131,12 +110,7 @@ iface = gr.Interface(
131
  gr.Slider(1, 10, 1, label="ํŽ˜์ด์ง€"),
132
  gr.Slider(1, 100, 10, label="๊ฒฐ๊ณผ ์ˆ˜")
133
  ],
134
- outputs=[
135
- gr.HTML(label="๋ชจ๋“  ๊ฒฐ๊ณผ"),
136
- gr.HTML(label="๋””๋ฒ„๊ทธ ์ •๋ณด")
137
- ],
138
-
139
-
140
  title="24์‹œ๊ฐ„ ์ด๋‚ด ๋‰ด์Šค ๊ฒ€์ƒ‰ ์ธํ„ฐํŽ˜์ด์Šค",
141
  description="๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๊ตญ๊ฐ€๋ฅผ ์„ ํƒํ•˜์—ฌ 24์‹œ๊ฐ„ ์ด๋‚ด์˜ ๋‰ด์Šค ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.",
142
  theme="Nymbo/Nymbo_Theme",
 
57
  return {"error": error_msg}
58
 
59
  def format_results(results):
 
 
 
60
  if isinstance(results, dict) and "error" in results:
61
+ return [f"์˜ค๋ฅ˜ ๋ฐœ์ƒ: {results['error']}"]
 
 
 
 
62
 
63
  try:
64
  if not isinstance(results, dict):
 
68
  raise ValueError("'results' ํ‚ค๊ฐ€ ์‘๋‹ต์— ์—†์Šต๋‹ˆ๋‹ค.")
69
 
70
  news_results = results["results"].get("news", [])
 
71
 
72
  if not news_results:
73
+ return ["๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค."]
74
+
75
+ formatted_results = []
76
+ for result in news_results:
77
+ title = result.get("title", "์ œ๋ชฉ ์—†์Œ")
78
+ url = result.get("url", "#")
79
+ snippet = result.get("snippet", "๋‚ด์šฉ ์—†์Œ")
80
+ channel = result.get("channel", "์•Œ ์ˆ˜ ์—†์Œ")
81
+ time_str = result.get("time", "์•Œ ์ˆ˜ ์—†๋Š” ์‹œ๊ฐ„")
 
 
 
 
 
 
 
 
 
82
 
83
+ article_info = f"""์ œ๋ชฉ: {title}
84
+ ๋งํฌ: {url}
85
+ ๋‚ด์šฉ: {snippet}
86
+ ์ถœ์ฒ˜: {channel} - {time_str}
87
+ ---"""
88
+ formatted_results.append(article_info)
89
+
90
+ return formatted_results
91
 
92
  except Exception as e:
93
+ return [f"๊ฒฐ๊ณผ ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {str(e)}"]
 
 
 
 
94
 
95
  def serphouse_search(query, country, page, num_result):
96
  results = search_serphouse(query, country, page, num_result)
97
+ return format_results(results)
 
98
 
99
  css = """
100
  footer {
101
  visibility: hidden;
102
  }
 
 
 
 
 
 
103
  """
104
 
105
  iface = gr.Interface(
 
110
  gr.Slider(1, 10, 1, label="ํŽ˜์ด์ง€"),
111
  gr.Slider(1, 100, 10, label="๊ฒฐ๊ณผ ์ˆ˜")
112
  ],
113
+ outputs=gr.outputs.Textbox(label="๋‰ด์Šค ๊ฒฐ๊ณผ", lines=20),
 
 
 
 
 
114
  title="24์‹œ๊ฐ„ ์ด๋‚ด ๋‰ด์Šค ๊ฒ€์ƒ‰ ์ธํ„ฐํŽ˜์ด์Šค",
115
  description="๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๊ณ  ๊ตญ๊ฐ€๋ฅผ ์„ ํƒํ•˜์—ฌ 24์‹œ๊ฐ„ ์ด๋‚ด์˜ ๋‰ด์Šค ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.",
116
  theme="Nymbo/Nymbo_Theme",