Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -70,7 +70,7 @@ def generate_search_page(query, page=1):
|
|
| 70 |
<head><title>LLM Search Engine</title></head>
|
| 71 |
<body style="font-family: Arial, sans-serif;">
|
| 72 |
<h1>LLM Search Engine</h1>
|
| 73 |
-
<form
|
| 74 |
<input type="text" name="query" placeholder="Type your search here...">
|
| 75 |
<input type="submit" value="Search">
|
| 76 |
<input type="hidden" name="page" value="1">
|
|
@@ -88,7 +88,7 @@ def generate_search_page(query, page=1):
|
|
| 88 |
<head><title>LLM Search Engine</title></head>
|
| 89 |
<body style="font-family: Arial, sans-serif;">
|
| 90 |
<h1>LLM Search Engine</h1>
|
| 91 |
-
<form
|
| 92 |
<input type="text" name="query" value="{html.escape(query)}">
|
| 93 |
<input type="submit" value="Search">
|
| 94 |
<input type="hidden" name="page" value="1">
|
|
@@ -110,7 +110,7 @@ def generate_search_page(query, page=1):
|
|
| 110 |
<head><title>LLM Search Engine</title></head>
|
| 111 |
<body style="font-family: Arial, sans-serif;">
|
| 112 |
<h1>LLM Search Engine</h1>
|
| 113 |
-
<form
|
| 114 |
<input type="text" name="query" value="{html.escape(query)}">
|
| 115 |
<input type="submit" value="Search">
|
| 116 |
<input type="hidden" name="page" value="1">
|
|
@@ -188,19 +188,19 @@ def generate_search_page(query, page=1):
|
|
| 188 |
</head>
|
| 189 |
<body>
|
| 190 |
<h1>LLM Search Engine</h1>
|
| 191 |
-
<form class="search-box"
|
| 192 |
<input type="text" name="query" value="{html.escape(query)}">
|
| 193 |
<input type="submit" value="Search">
|
| 194 |
<input type="hidden" name="page" value="1">
|
| 195 |
</form>
|
| 196 |
-
<h2>Results for '{query}' (Page {page} of {total_pages})</h2>
|
| 197 |
<div class="results">
|
| 198 |
"""
|
| 199 |
|
| 200 |
for result in paginated_results:
|
| 201 |
-
title = result.get("title", "No title")
|
| 202 |
-
snippet = result.get("snippet", "No snippet")
|
| 203 |
-
url = result.get("url", "#")
|
| 204 |
html_content += f"""
|
| 205 |
<div class="search-result">
|
| 206 |
<a href="{url}" target="_blank">{title}</a>
|
|
@@ -211,8 +211,8 @@ def generate_search_page(query, page=1):
|
|
| 211 |
|
| 212 |
# Pagination links
|
| 213 |
encoded_query = quote(query)
|
| 214 |
-
prev_link = f'<a href="
|
| 215 |
-
next_link = f'<a href="
|
| 216 |
html_content += f"""
|
| 217 |
</div>
|
| 218 |
<div class="pagination">
|
|
@@ -220,6 +220,21 @@ def generate_search_page(query, page=1):
|
|
| 220 |
<span>Page {page} of {total_pages}</span>
|
| 221 |
{next_link}
|
| 222 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 223 |
</body>
|
| 224 |
</html>
|
| 225 |
"""
|
|
@@ -228,18 +243,21 @@ def generate_search_page(query, page=1):
|
|
| 228 |
|
| 229 |
# Define the Gradio app with Blocks
|
| 230 |
with gr.Blocks(title="LLM Search Engine") as app:
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
try:
|
| 235 |
-
page = int(
|
| 236 |
-
except ValueError:
|
| 237 |
page = 1
|
| 238 |
-
|
| 239 |
-
return html
|
| 240 |
|
| 241 |
-
#
|
| 242 |
-
app.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
|
| 244 |
-
# Launch the app
|
| 245 |
app.launch()
|
|
|
|
| 70 |
<head><title>LLM Search Engine</title></head>
|
| 71 |
<body style="font-family: Arial, sans-serif;">
|
| 72 |
<h1>LLM Search Engine</h1>
|
| 73 |
+
<form id="search-form">
|
| 74 |
<input type="text" name="query" placeholder="Type your search here...">
|
| 75 |
<input type="submit" value="Search">
|
| 76 |
<input type="hidden" name="page" value="1">
|
|
|
|
| 88 |
<head><title>LLM Search Engine</title></head>
|
| 89 |
<body style="font-family: Arial, sans-serif;">
|
| 90 |
<h1>LLM Search Engine</h1>
|
| 91 |
+
<form id="search-form">
|
| 92 |
<input type="text" name="query" value="{html.escape(query)}">
|
| 93 |
<input type="submit" value="Search">
|
| 94 |
<input type="hidden" name="page" value="1">
|
|
|
|
| 110 |
<head><title>LLM Search Engine</title></head>
|
| 111 |
<body style="font-family: Arial, sans-serif;">
|
| 112 |
<h1>LLM Search Engine</h1>
|
| 113 |
+
<form id="search-form">
|
| 114 |
<input type="text" name="query" value="{html.escape(query)}">
|
| 115 |
<input type="submit" value="Search">
|
| 116 |
<input type="hidden" name="page" value="1">
|
|
|
|
| 188 |
</head>
|
| 189 |
<body>
|
| 190 |
<h1>LLM Search Engine</h1>
|
| 191 |
+
<form class="search-box" id="search-form">
|
| 192 |
<input type="text" name="query" value="{html.escape(query)}">
|
| 193 |
<input type="submit" value="Search">
|
| 194 |
<input type="hidden" name="page" value="1">
|
| 195 |
</form>
|
| 196 |
+
<h2>Results for '{html.escape(query)}' (Page {page} of {total_pages})</h2>
|
| 197 |
<div class="results">
|
| 198 |
"""
|
| 199 |
|
| 200 |
for result in paginated_results:
|
| 201 |
+
title = html.escape(result.get("title", "No title"))
|
| 202 |
+
snippet = html.escape(result.get("snippet", "No snippet"))
|
| 203 |
+
url = html.escape(result.get("url", "#"))
|
| 204 |
html_content += f"""
|
| 205 |
<div class="search-result">
|
| 206 |
<a href="{url}" target="_blank">{title}</a>
|
|
|
|
| 211 |
|
| 212 |
# Pagination links
|
| 213 |
encoded_query = quote(query)
|
| 214 |
+
prev_link = f'<a href="?query={encoded_query}&page={page-1}" onclick="updatePage(event, \'{encoded_query}\', {page-1})">Previous</a>' if page > 1 else '<span>Previous</span>'
|
| 215 |
+
next_link = f'<a href="?query={encoded_query}&page={page+1}" onclick="updatePage(event, \'{encoded_query}\', {page+1})">Next</a>' if page < total_pages else '<span>Next</span>'
|
| 216 |
html_content += f"""
|
| 217 |
</div>
|
| 218 |
<div class="pagination">
|
|
|
|
| 220 |
<span>Page {page} of {total_pages}</span>
|
| 221 |
{next_link}
|
| 222 |
</div>
|
| 223 |
+
<script>
|
| 224 |
+
function updatePage(event, query, page) {{
|
| 225 |
+
event.preventDefault();
|
| 226 |
+
window.history.pushState({{}}, '', '?query=' + encodeURIComponent(query) + '&page=' + page);
|
| 227 |
+
document.getElementById('search-form').query.value = query;
|
| 228 |
+
document.getElementById('search-form').page.value = page;
|
| 229 |
+
document.getElementById('search-form').onsubmit(event);
|
| 230 |
+
}}
|
| 231 |
+
document.getElementById('search-form').onsubmit = function(event) {{
|
| 232 |
+
event.preventDefault();
|
| 233 |
+
var query = this.query.value;
|
| 234 |
+
var page = this.page.value;
|
| 235 |
+
window.location.href = '?query=' + encodeURIComponent(query) + '&page=' + page;
|
| 236 |
+
}};
|
| 237 |
+
</script>
|
| 238 |
</body>
|
| 239 |
</html>
|
| 240 |
"""
|
|
|
|
| 243 |
|
| 244 |
# Define the Gradio app with Blocks
|
| 245 |
with gr.Blocks(title="LLM Search Engine") as app:
|
| 246 |
+
output_html = gr.HTML()
|
| 247 |
+
|
| 248 |
+
def update_page(query, page):
|
| 249 |
try:
|
| 250 |
+
page = int(page)
|
| 251 |
+
except (ValueError, TypeError):
|
| 252 |
page = 1
|
| 253 |
+
return generate_search_page(query, page)
|
|
|
|
| 254 |
|
| 255 |
+
# Initial load with URL parameters
|
| 256 |
+
app.load(
|
| 257 |
+
fn=update_page,
|
| 258 |
+
inputs=None,
|
| 259 |
+
outputs=output_html,
|
| 260 |
+
_js="() => [new URLSearchParams(window.location.search).get('query') || '', new URLSearchParams(window.location.search).get('page') || '1']"
|
| 261 |
+
)
|
| 262 |
|
|
|
|
| 263 |
app.launch()
|