seawolf2357
commited on
Update app.py
Browse files
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,
|
21 |
url = "https://api.serphouse.com/serp/live"
|
22 |
|
23 |
-
|
24 |
-
"
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
}
|
36 |
|
37 |
headers = {
|
38 |
-
"
|
|
|
|
|
39 |
}
|
40 |
|
41 |
-
response = requests.
|
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 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
else:
|
58 |
-
|
|
|
|
|
59 |
|
60 |
-
def serphouse_search(query, country, verbatim,
|
61 |
verbatim = "1" if verbatim else "0"
|
62 |
-
gfilter = "1" if gfilter else "0"
|
63 |
|
64 |
-
results = search_serphouse(query, country, verbatim,
|
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 |
],
|