Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
seawolf2357
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
import json
|
|
|
4 |
|
5 |
API_KEY = "V38CNn4HXpLtynJQyOeoUensTEYoFy8PBUxKpDqAW1pawT1vfJ2BWtPQ98h6"
|
6 |
|
7 |
-
# List of 50 major countries
|
8 |
MAJOR_COUNTRIES = [
|
9 |
"United States", "United Kingdom", "Canada", "Australia", "Germany",
|
10 |
"France", "Japan", "South Korea", "China", "India",
|
@@ -49,26 +49,29 @@ def search_serphouse(query, country, verbatim, page, num_result):
|
|
49 |
return f"Error: {response.status_code} - {response.text}"
|
50 |
|
51 |
def format_results(results):
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
if
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
68 |
else:
|
69 |
-
|
70 |
-
|
71 |
-
return
|
72 |
|
73 |
def serphouse_search(query, country, verbatim, page, num_result):
|
74 |
verbatim = "1" if verbatim else "0"
|
@@ -76,7 +79,13 @@ def serphouse_search(query, country, verbatim, page, num_result):
|
|
76 |
results = search_serphouse(query, country, verbatim, page, num_result)
|
77 |
return format_results(results)
|
78 |
|
79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
fn=serphouse_search,
|
81 |
inputs=[
|
82 |
gr.Textbox(label="Search Query"),
|
@@ -85,9 +94,8 @@ iface = gr.Interface(
|
|
85 |
gr.Slider(1, 10, 1, label="Page"),
|
86 |
gr.Slider(1, 100, 10, label="Number of Results")
|
87 |
],
|
88 |
-
outputs="
|
89 |
-
title="
|
90 |
-
description="Enter your search query and select a country to get news results from the SERPHouse API."
|
91 |
)
|
92 |
|
93 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
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",
|
10 |
"France", "Japan", "South Korea", "China", "India",
|
|
|
49 |
return f"Error: {response.status_code} - {response.text}"
|
50 |
|
51 |
def format_results(results):
|
52 |
+
now = datetime.now()
|
53 |
+
html_output = "<h2>Search Results</h2>"
|
54 |
+
|
55 |
+
if isinstance(results, dict) and "results" in results:
|
56 |
+
news_results = results["results"].get("news", [])
|
57 |
+
for result in news_results:
|
58 |
+
time_str = result.get("time", "").strip()
|
59 |
+
if time_str.endswith("ago"):
|
60 |
+
time_value = int(time_str.split()[0])
|
61 |
+
time_unit = time_str.split()[1]
|
62 |
+
|
63 |
+
if time_unit in ["minute", "minutes", "hour", "hours"] and time_value <= 24:
|
64 |
+
html_output += f"""
|
65 |
+
<div style='border: 1px solid #ddd; padding: 10px; margin-bottom: 10px;'>
|
66 |
+
<h3><a href='{result.get('url', '#')}'>{result.get('title', 'No Title')}</a></h3>
|
67 |
+
<p>{result.get('snippet', 'No Snippet')}</p>
|
68 |
+
<p><small>Source: {result.get('channel', 'Unknown')} - {result.get('time', 'Unknown time')}</small></p>
|
69 |
+
</div>
|
70 |
+
"""
|
71 |
else:
|
72 |
+
html_output += "<p>No results found or unexpected response format.</p>"
|
73 |
+
|
74 |
+
return html_output
|
75 |
|
76 |
def serphouse_search(query, country, verbatim, page, num_result):
|
77 |
verbatim = "1" if verbatim else "0"
|
|
|
79 |
results = search_serphouse(query, country, verbatim, page, num_result)
|
80 |
return format_results(results)
|
81 |
|
82 |
+
css = """
|
83 |
+
footer {
|
84 |
+
visibility: hidden;
|
85 |
+
}
|
86 |
+
"""
|
87 |
+
|
88 |
+
iface = gr.Interface(theme="Nymbo/Nymbo_Theme", css=css,
|
89 |
fn=serphouse_search,
|
90 |
inputs=[
|
91 |
gr.Textbox(label="Search Query"),
|
|
|
94 |
gr.Slider(1, 10, 1, label="Page"),
|
95 |
gr.Slider(1, 100, 10, label="Number of Results")
|
96 |
],
|
97 |
+
outputs="html",
|
98 |
+
title="Global News Search-AI",
|
|
|
99 |
)
|
100 |
|
101 |
iface.launch()
|