seawolf2357 commited on
Commit
eec4d01
·
verified ·
1 Parent(s): 5d3044b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -28
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  import requests
 
3
 
4
  API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
5
 
@@ -17,28 +18,30 @@ MAJOR_COUNTRIES = [
17
  "Indonesia", "Philippines", "Vietnam", "Pakistan", "Bangladesh"
18
  ]
19
 
20
- def search_serphouse(query, country, verbatim, gfilter, page, num_result):
21
  url = "https://api.serphouse.com/serp/live"
22
 
23
- params = {
24
- "q": query,
25
- "domain": "google.com",
26
- "lang": "en",
27
- "device": "desktop",
28
- "serp_type": "news",
29
- "loc": country,
30
- "country": country,
31
- "verbatim": verbatim,
32
- "gfilter": gfilter,
33
- "page": page,
34
- "num_result": num_result
35
  }
36
 
37
  headers = {
38
- "Authorization": f"Bearer {API_KEY}"
 
 
39
  }
40
 
41
- response = requests.get(url, params=params, headers=headers)
42
 
43
  if response.status_code == 200:
44
  return response.json()
@@ -46,22 +49,31 @@ def search_serphouse(query, country, verbatim, gfilter, page, num_result):
46
  return f"Error: {response.status_code} - {response.text}"
47
 
48
  def format_results(results):
49
- if isinstance(results, dict) and "results" in results:
50
- news_results = results["results"].get("news", [])
51
- formatted_results = ""
52
- for i, result in enumerate(news_results, 1):
53
- formatted_results += f"{i}. {result['title']}\n"
54
- formatted_results += f" URL: {result['link']}\n"
55
- formatted_results += f" Snippet: {result['snippet']}\n\n"
56
- return formatted_results
 
 
 
 
 
 
 
 
57
  else:
58
- return str(results)
 
 
59
 
60
- def serphouse_search(query, country, verbatim, gfilter, page, num_result):
61
  verbatim = "1" if verbatim else "0"
62
- gfilter = "1" if gfilter else "0"
63
 
64
- results = search_serphouse(query, country, verbatim, gfilter, page, num_result)
65
  return format_results(results)
66
 
67
  iface = gr.Interface(
@@ -70,7 +82,6 @@ iface = gr.Interface(
70
  gr.Textbox(label="Search Query"),
71
  gr.Dropdown(MAJOR_COUNTRIES, label="Country"),
72
  gr.Checkbox(label="Verbatim"),
73
- gr.Checkbox(label="Google Filter"),
74
  gr.Slider(1, 10, 1, label="Page"),
75
  gr.Slider(1, 100, 10, label="Number of Results")
76
  ],
 
1
  import gradio as gr
2
  import requests
3
+ import json
4
 
5
  API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
6
 
 
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 = {
25
+ "data": {
26
+ "q": query,
27
+ "domain": "google.com",
28
+ "loc": country,
29
+ "lang": "en",
30
+ "device": "desktop",
31
+ "serp_type": "news",
32
+ "page": str(page),
33
+ "verbatim": verbatim,
34
+ "num": str(num_result)
35
+ }
36
  }
37
 
38
  headers = {
39
+ "accept": "application/json",
40
+ "content-type": "application/json",
41
+ "authorization": f"Bearer {API_KEY}"
42
  }
43
 
44
+ response = requests.post(url, json=payload, headers=headers)
45
 
46
  if response.status_code == 200:
47
  return response.json()
 
49
  return f"Error: {response.status_code} - {response.text}"
50
 
51
  def format_results(results):
52
+ formatted_results = "API Response:\n"
53
+ formatted_results += json.dumps(results, indent=2)
54
+ formatted_results += "\n\nProcessed Results:\n"
55
+
56
+ if isinstance(results, dict):
57
+ if "results" in results:
58
+ news_results = results["results"].get("news", [])
59
+ if news_results:
60
+ for i, result in enumerate(news_results, 1):
61
+ formatted_results += f"{i}. {result.get('title', 'No Title')}\n"
62
+ formatted_results += f" URL: {result.get('link', 'No Link')}\n"
63
+ formatted_results += f" Snippet: {result.get('snippet', 'No Snippet')}\n\n"
64
+ else:
65
+ formatted_results += "No news results found in the API response.\n"
66
+ else:
67
+ formatted_results += "No 'results' key found in the API response.\n"
68
  else:
69
+ formatted_results += "Unexpected response format.\n"
70
+
71
+ return formatted_results
72
 
73
+ def serphouse_search(query, country, verbatim, page, num_result):
74
  verbatim = "1" if verbatim else "0"
 
75
 
76
+ results = search_serphouse(query, country, verbatim, page, num_result)
77
  return format_results(results)
78
 
79
  iface = gr.Interface(
 
82
  gr.Textbox(label="Search Query"),
83
  gr.Dropdown(MAJOR_COUNTRIES, label="Country"),
84
  gr.Checkbox(label="Verbatim"),
 
85
  gr.Slider(1, 10, 1, label="Page"),
86
  gr.Slider(1, 100, 10, label="Number of Results")
87
  ],