seawolf2357 commited on
Commit
9178c02
·
verified ·
1 Parent(s): 473dd97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -41
app.py CHANGED
@@ -3,7 +3,7 @@ import requests
3
  import json
4
  from datetime import datetime, timedelta
5
 
6
- API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
7
 
8
  MAJOR_COUNTRIES = [
9
  "United States", "United Kingdom", "Canada", "Australia", "Germany",
@@ -18,7 +18,7 @@ MAJOR_COUNTRIES = [
18
  "Indonesia", "Philippines", "Vietnam", "Pakistan", "Bangladesh"
19
  ]
20
 
21
- def search_serphouse(query, country, page, num_result):
22
  url = "https://api.serphouse.com/serp/live"
23
 
24
  now = datetime.utcnow()
@@ -58,10 +58,11 @@ def search_serphouse(query, country, page, num_result):
58
 
59
  def format_results_from_raw(results):
60
  try:
61
- debug_info = f"<pre>Raw API Response:\n{json.dumps(results, indent=2, ensure_ascii=False)}</pre>"
 
62
 
63
  if isinstance(results, dict) and "error" in results:
64
- return "Error: " + results["error"] + debug_info, "", debug_info
65
 
66
  if not isinstance(results, dict):
67
  raise ValueError("결과가 사전 형식이 아닙니다.")
@@ -82,10 +83,8 @@ def format_results_from_raw(results):
82
  news_results = []
83
 
84
  if not news_results:
85
- return "검색 결과가 없습니다." + debug_info, "", debug_info
86
 
87
- # 뉴스 결과를 HTML 형식으로 포맷팅
88
- formatted_articles = ""
89
  # 뉴스 결과를 리스트 형태로 포맷팅 (이미지 썸네일 포함)
90
  list_output = ""
91
 
@@ -99,23 +98,10 @@ def format_results_from_raw(results):
99
 
100
  # base64로 인코딩된 이미지를 처리하지 않음
101
  if image_url and not image_url.startswith("data:image"):
102
- image_html = f'<img src="{image_url}" alt="Image" style="max-width: 100%; height: auto;">'
103
  thumbnail_html = f'<img src="{image_url}" alt="Thumbnail" style="width: 100px; height: auto;">'
104
  else:
105
- image_html = ''
106
  thumbnail_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>
112
- <p><strong>{channel}</strong> - {time}</p>
113
- {image_html}
114
- <p>{snippet}</p>
115
- </div>
116
- """
117
- formatted_articles += article_html
118
-
119
  # 리스트 형식의 기사 (이미지 썸네일 포함)
120
  list_item = f"""
121
  <div style="margin-bottom: 20px;">
@@ -128,25 +114,32 @@ def format_results_from_raw(results):
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 {
148
  visibility: hidden;
149
  }
 
 
 
 
 
 
 
 
150
  """
151
 
152
  # Gradio 인터페이스 구성
@@ -158,29 +151,31 @@ with gr.Blocks(theme="Nymbo/Nymbo_Theme", css=css, title="24시간 이내 뉴스
158
  with gr.Row():
159
  query = gr.Textbox(label="검색어")
160
  country = gr.Dropdown(MAJOR_COUNTRIES, label="국가", value="South Korea")
161
- with gr.Row():
162
- page = gr.Slider(1, 10, 1, label="페이지")
163
- num_result = gr.Slider(1, 100, 100, 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.HTML(label="리스트 결과") # HTML로 변경
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"))
 
3
  import json
4
  from datetime import datetime, timedelta
5
 
6
+ API_KEY = "YOUR_API_KEY_HERE" # 여기에 본인의 API 키를 입력하세요.
7
 
8
  MAJOR_COUNTRIES = [
9
  "United States", "United Kingdom", "Canada", "Australia", "Germany",
 
18
  "Indonesia", "Philippines", "Vietnam", "Pakistan", "Bangladesh"
19
  ]
20
 
21
+ def search_serphouse(query, country, page=1, num_result=100):
22
  url = "https://api.serphouse.com/serp/live"
23
 
24
  now = datetime.utcnow()
 
58
 
59
  def format_results_from_raw(results):
60
  try:
61
+ # 디버그 정보 생략
62
+ debug_info = ""
63
 
64
  if isinstance(results, dict) and "error" in results:
65
+ return "Error: " + results["error"], ""
66
 
67
  if not isinstance(results, dict):
68
  raise ValueError("결과가 사전 형식이 아닙니다.")
 
83
  news_results = []
84
 
85
  if not news_results:
86
+ return "검색 결과가 없습니다.", ""
87
 
 
 
88
  # 뉴스 결과를 리스트 형태로 포맷팅 (이미지 썸네일 포함)
89
  list_output = ""
90
 
 
98
 
99
  # base64로 인코딩된 이미지를 처리하지 않음
100
  if image_url and not image_url.startswith("data:image"):
 
101
  thumbnail_html = f'<img src="{image_url}" alt="Thumbnail" style="width: 100px; height: auto;">'
102
  else:
 
103
  thumbnail_html = ''
104
 
 
 
 
 
 
 
 
 
 
 
 
105
  # 리스트 형식의 기사 (이미지 썸네일 포함)
106
  list_item = f"""
107
  <div style="margin-bottom: 20px;">
 
114
  """
115
  list_output += list_item
116
 
117
+ return list_output, ""
 
 
 
118
 
119
  except Exception as e:
120
  error_message = f"결과 처리 중 오류 발생: {str(e)}"
121
+ return "Error: " + error_message, ""
 
122
 
123
+ def serphouse_search(query, country):
124
+ # 페이지와 결과 수의 기본값을 설정합니다.
125
+ page = 1
126
+ num_result = 100
127
  results = search_serphouse(query, country, page, num_result)
128
+ list_output, debug_info = format_results_from_raw(results)
129
+ return list_output
130
 
131
  css = """
132
  footer {
133
  visibility: hidden;
134
  }
135
+ /* '뉴스 결과'와 '디버그 정보' 탭 숨기기 */
136
+ #tab-뉴스_결과, #tab-디버그_정보 {
137
+ display: none !important;
138
+ }
139
+ /* '페이지'와 '결과 수' 입력 요소 숨기기 */
140
+ .slider-container {
141
+ display: none !important;
142
+ }
143
  """
144
 
145
  # Gradio 인터페이스 구성
 
151
  with gr.Row():
152
  query = gr.Textbox(label="검색어")
153
  country = gr.Dropdown(MAJOR_COUNTRIES, label="국가", value="South Korea")
154
+ # '페이지'와 '결과 수' 입력 요소 제거
155
+ # with gr.Row():
156
+ # page = gr.Slider(1, 10, 1, label="페이지")
157
+ # num_result = gr.Slider(1, 100, 100, label="결과 수")
158
 
159
  search_button = gr.Button("검색")
160
 
161
+ # '뉴스 결과'와 '디버그 정보' 탭 제거
162
+ # with gr.Tab("뉴스 결과"):
163
+ # news_output = gr.HTML(label="뉴스 결과")
164
 
165
  with gr.Tab("리스트"):
166
  list_output = gr.HTML(label="리스트 결과") # HTML로 변경
167
 
168
+ # with gr.Tab("디버그 정보"):
169
+ # debug_output = gr.Textbox(label="디버그 정보", lines=10)
170
 
171
+ def search_and_display(query, country):
172
+ list_output_text = serphouse_search(query, country)
173
+ return {list_output: list_output_text}
174
 
175
  search_button.click(
176
  search_and_display,
177
+ inputs=[query, country],
178
+ outputs=[list_output]
179
  )
180
 
181
  iface.launch(auth=("gini", "pick"))