seawolf2357 commited on
Commit
3e9e78b
1 Parent(s): 6238bd4

Update app-backup.py

Browse files
Files changed (1) hide show
  1. app-backup.py +46 -18
app-backup.py CHANGED
@@ -6,6 +6,7 @@ from datetime import datetime, timedelta
6
 
7
  API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
8
 
 
9
  MAJOR_COUNTRIES = [
10
  "United States", "United Kingdom", "Canada", "Australia", "Germany",
11
  "France", "Japan", "South Korea", "China", "India",
@@ -59,30 +60,38 @@ def search_serphouse(query, country, page, num_result):
59
 
60
  def format_results_from_raw(results):
61
  try:
62
- debug_info = f"Raw API Response:\n{json.dumps(results, indent=2, ensure_ascii=False)}"
63
 
64
  if isinstance(results, dict) and "error" in results:
65
- return "Error: " + results["error"], debug_info
66
 
67
  if not isinstance(results, dict):
68
  raise ValueError("결과가 사전 형식이 아닙니다.")
69
 
70
- # 'results' 키 내부의 구조 확인
71
  if 'results' in results:
72
  results_content = results['results']
73
- # 'news' 확인
74
- if 'news' in results_content:
75
- news_results = results_content['news']
 
 
 
 
76
  else:
77
  news_results = []
78
  else:
79
  news_results = []
80
 
81
  if not news_results:
82
- return "검색 결과가 없습니다.", debug_info
83
 
 
84
  formatted_articles = ""
85
- for result in news_results:
 
 
 
86
  title = result.get("title", "제목 없음")
87
  link = result.get("url", result.get("link", "#"))
88
  snippet = result.get("snippet", "내용 없음")
@@ -96,6 +105,7 @@ def format_results_from_raw(results):
96
  else:
97
  image_html = ''
98
 
 
99
  article_html = f"""
100
  <div style="margin-bottom: 20px; border-bottom: 1px solid #ccc; padding-bottom: 20px;">
101
  <h3><a href="{link}" target="_blank">{title}</a></h3>
@@ -106,17 +116,32 @@ def format_results_from_raw(results):
106
  """
107
  formatted_articles += article_html
108
 
109
- return formatted_articles, debug_info
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
  except Exception as e:
112
  error_message = f"결과 처리 중 오류 발생: {str(e)}"
113
- debug_info = f"Error: {error_message}\n"
114
- return "Error: " + error_message, debug_info
115
 
116
  def serphouse_search(query, country, page, num_result):
117
  results = search_serphouse(query, country, page, num_result)
118
- formatted_articles, debug_info = format_results_from_raw(results)
119
- return formatted_articles, debug_info
120
 
121
  css = """
122
  footer {
@@ -137,22 +162,25 @@ with gr.Blocks(css=css, title="24시간 이내 뉴스 검색 인터페이스") a
137
  page = gr.Slider(1, 10, 1, label="페이지")
138
  num_result = gr.Slider(1, 100, 10, label="결과 수")
139
 
140
- search_button = gr.Button("검색")
141
 
142
  with gr.Tab("뉴스 결과"):
143
  news_output = gr.HTML(label="뉴스 결과")
144
 
 
 
 
145
  with gr.Tab("디버그 정보"):
146
  debug_output = gr.Textbox(label="디버그 정보", lines=10)
147
 
148
  def search_and_display(query, country, page, num_result):
149
- articles, debug_info = serphouse_search(query, country, page, num_result)
150
- return {news_output: articles, debug_output: debug_info}
151
 
152
  search_button.click(
153
  search_and_display,
154
  inputs=[query, country, page, num_result],
155
- outputs=[news_output, debug_output]
156
  )
157
 
158
- iface.launch(auth=("gini", "pick"))
 
6
 
7
  API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
8
 
9
+
10
  MAJOR_COUNTRIES = [
11
  "United States", "United Kingdom", "Canada", "Australia", "Germany",
12
  "France", "Japan", "South Korea", "China", "India",
 
60
 
61
  def format_results_from_raw(results):
62
  try:
63
+ debug_info = f"<pre>Raw API Response:\n{json.dumps(results, indent=2, ensure_ascii=False)}</pre>"
64
 
65
  if isinstance(results, dict) and "error" in results:
66
+ return "Error: " + results["error"] + debug_info, "", debug_info
67
 
68
  if not isinstance(results, dict):
69
  raise ValueError("결과가 사전 형식이 아닙니다.")
70
 
71
+ # 'results' 키 내부의 구조 확인 (중첩된 'results' 처리)
72
  if 'results' in results:
73
  results_content = results['results']
74
+ if 'results' in results_content:
75
+ results_content = results_content['results']
76
+ # 'news' 키 확인
77
+ if 'news' in results_content:
78
+ news_results = results_content['news']
79
+ else:
80
+ news_results = []
81
  else:
82
  news_results = []
83
  else:
84
  news_results = []
85
 
86
  if not news_results:
87
+ return "검색 결과가 없습니다." + debug_info, "", debug_info
88
 
89
+ # 뉴스 결과를 HTML 형식으로 포맷팅
90
  formatted_articles = ""
91
+ # 뉴스 결과를 리스트 형태로 포맷팅
92
+ list_output = ""
93
+
94
+ for idx, result in enumerate(news_results, 1):
95
  title = result.get("title", "제목 없음")
96
  link = result.get("url", result.get("link", "#"))
97
  snippet = result.get("snippet", "내용 없음")
 
105
  else:
106
  image_html = ''
107
 
108
+ # HTML 형식의 기사
109
  article_html = f"""
110
  <div style="margin-bottom: 20px; border-bottom: 1px solid #ccc; padding-bottom: 20px;">
111
  <h3><a href="{link}" target="_blank">{title}</a></h3>
 
116
  """
117
  formatted_articles += article_html
118
 
119
+ # 리스트 형식의 기사
120
+ list_item = f"""
121
+ {idx}. 제목: {title}
122
+ 링크: {link}
123
+ 요약: {snippet}
124
+ 출처: {channel}
125
+ 시간: {time}
126
+ 이미지 URL: {image_url}
127
+ ------------------------------
128
+ """
129
+ list_output += list_item
130
+
131
+ # 뉴스 결과와 디버그 정보를 반환
132
+ combined_output = formatted_articles + debug_info
133
+
134
+ return combined_output, list_output, debug_info
135
 
136
  except Exception as e:
137
  error_message = f"결과 처리 중 오류 발생: {str(e)}"
138
+ debug_info = f"<pre>Error: {error_message}\n</pre>"
139
+ return "Error: " + error_message + debug_info, "", debug_info
140
 
141
  def serphouse_search(query, country, page, num_result):
142
  results = search_serphouse(query, country, page, num_result)
143
+ combined_output, list_output, debug_info = format_results_from_raw(results)
144
+ return combined_output, list_output, debug_info
145
 
146
  css = """
147
  footer {
 
162
  page = gr.Slider(1, 10, 1, label="페이지")
163
  num_result = gr.Slider(1, 100, 10, label="결과 수")
164
 
165
+ search_button = gr.Button("검색") # 누락된 부분 추가
166
 
167
  with gr.Tab("뉴스 결과"):
168
  news_output = gr.HTML(label="뉴스 결과")
169
 
170
+ with gr.Tab("리스트"):
171
+ list_output = gr.Textbox(label="리스트 결과", lines=20)
172
+
173
  with gr.Tab("디버그 정보"):
174
  debug_output = gr.Textbox(label="디버그 정보", lines=10)
175
 
176
  def search_and_display(query, country, page, num_result):
177
+ combined_output, list_output_text, debug_info = serphouse_search(query, country, page, num_result)
178
+ return {news_output: combined_output, list_output: list_output_text, debug_output: debug_info}
179
 
180
  search_button.click(
181
  search_and_display,
182
  inputs=[query, country, page, num_result],
183
+ outputs=[news_output, list_output, debug_output]
184
  )
185
 
186
+ iface.launch(auth=("gini", "pick"))