seawolf2357 commited on
Commit
8415ea5
1 Parent(s): a31c3ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -25
app.py CHANGED
@@ -18,7 +18,7 @@ MAJOR_COUNTRIES = [
18
  "Indonesia", "Philippines", "Vietnam", "Pakistan", "Bangladesh"
19
  ]
20
 
21
- def search_serphouse(query, country, verbatim, page, num_result):
22
  url = "https://api.serphouse.com/serp/live"
23
 
24
  payload = {
@@ -30,7 +30,7 @@ def search_serphouse(query, country, verbatim, page, num_result):
30
  "device": "desktop",
31
  "serp_type": "news",
32
  "page": str(page),
33
- "verbatim": verbatim,
34
  "num": str(num_result)
35
  }
36
  }
@@ -49,17 +49,20 @@ def search_serphouse(query, country, verbatim, page, num_result):
49
  return f"Error: {str(e)}"
50
 
51
  def is_recent_news(time_str):
52
- time_parts = time_str.split()
53
- if len(time_parts) >= 2:
54
- try:
55
- value = int(time_parts[0])
56
- unit = time_parts[1].lower()
57
- if unit in ['minute', 'minutes', 'hour', 'hours']:
58
- return True
59
- elif unit in ['day', 'days'] and value <= 2: # 최대 2일까지 허용
60
- return True
61
- except ValueError:
62
- pass
 
 
 
63
  return False
64
 
65
  def format_results(results):
@@ -79,9 +82,9 @@ def format_results(results):
79
  """
80
 
81
  try:
82
- if isinstance(results, dict) and "results" in results:
83
- news_results = results["results"].get("news", [])
84
- filtered_results = [result for result in news_results if is_recent_news(result.get("time", "").strip())]
85
 
86
  if not filtered_results:
87
  html_output += '<p class="no-results">No recent news results found.</p>'
@@ -95,20 +98,18 @@ def format_results(results):
95
  </div>
96
  """
97
  else:
98
- html_output += f'<p class="no-results">Unexpected response format: {type(results)}</p>'
99
  except Exception as e:
100
  html_output += f'<p class="no-results">Error processing results: {str(e)}</p>'
101
 
102
- # 디버그 정보 추가 (실제 사용 시 제거 가능)
103
  html_output += f'<div class="debug-info"><h3>Debug Info:</h3><pre>{json.dumps(results, indent=2)}</pre></div>'
104
 
105
  html_output += "</div>"
106
  return html_output
107
 
108
- def serphouse_search(query, country, verbatim, page, num_result):
109
- verbatim = "1" if verbatim else "0"
110
-
111
- results = search_serphouse(query, country, verbatim, page, num_result)
112
  return format_results(results)
113
 
114
  css = """
@@ -122,13 +123,11 @@ iface = gr.Interface(
122
  inputs=[
123
  gr.Textbox(label="Search Query"),
124
  gr.Dropdown(MAJOR_COUNTRIES, label="Country"),
125
- gr.Checkbox(label="Verbatim"),
126
  gr.Slider(1, 10, 1, label="Page"),
127
  gr.Slider(1, 100, 10, label="Number of Results")
128
  ],
129
  outputs="html",
130
- title="SERPHouse News Search Interface",
131
- description="Enter your search query and select a country to get recent news results from the SERPHouse API.",
132
  theme="Nymbo/Nymbo_Theme",
133
  css=css
134
  )
 
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
  payload = {
 
30
  "device": "desktop",
31
  "serp_type": "news",
32
  "page": str(page),
33
+ "verbatim": "1",
34
  "num": str(num_result)
35
  }
36
  }
 
49
  return f"Error: {str(e)}"
50
 
51
  def is_recent_news(time_str):
52
+ if not time_str:
53
+ return False
54
+ time_parts = time_str.lower().split()
55
+ if len(time_parts) < 2:
56
+ return False
57
+ try:
58
+ value = int(time_parts[0])
59
+ unit = time_parts[1]
60
+ if unit in ['minute', 'minutes', 'hour', 'hours']:
61
+ return True
62
+ elif unit in ['day', 'days']:
63
+ return value <= 2
64
+ except ValueError:
65
+ return False
66
  return False
67
 
68
  def format_results(results):
 
82
  """
83
 
84
  try:
85
+ if isinstance(results, dict) and "results" in results and "news" in results["results"]:
86
+ news_results = results["results"]["news"]
87
+ filtered_results = [result for result in news_results if is_recent_news(result.get("time", ""))]
88
 
89
  if not filtered_results:
90
  html_output += '<p class="no-results">No recent news results found.</p>'
 
98
  </div>
99
  """
100
  else:
101
+ html_output += '<p class="no-results">No valid news results found in the API response.</p>'
102
  except Exception as e:
103
  html_output += f'<p class="no-results">Error processing results: {str(e)}</p>'
104
 
105
+ # 디버그 정보 추가
106
  html_output += f'<div class="debug-info"><h3>Debug Info:</h3><pre>{json.dumps(results, indent=2)}</pre></div>'
107
 
108
  html_output += "</div>"
109
  return html_output
110
 
111
+ def serphouse_search(query, country, page, num_result):
112
+ results = search_serphouse(query, country, page, num_result)
 
 
113
  return format_results(results)
114
 
115
  css = """
 
123
  inputs=[
124
  gr.Textbox(label="Search Query"),
125
  gr.Dropdown(MAJOR_COUNTRIES, label="Country"),
 
126
  gr.Slider(1, 10, 1, label="Page"),
127
  gr.Slider(1, 100, 10, label="Number of Results")
128
  ],
129
  outputs="html",
130
+ title="Global News Search AI",
 
131
  theme="Nymbo/Nymbo_Theme",
132
  css=css
133
  )