Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from flask import Flask, request, render_template_string
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
import json
|
|
@@ -17,8 +17,8 @@ client = OpenAI(
|
|
| 17 |
RESULTS_PER_PAGE = 10
|
| 18 |
TOTAL_RESULTS = 30 # Generate 30 results to allow pagination
|
| 19 |
|
| 20 |
-
def fetch_search_results(query):
|
| 21 |
-
"""Fetch search results from the LLM
|
| 22 |
if not query.strip():
|
| 23 |
return None, "Please enter a search query."
|
| 24 |
|
|
@@ -39,21 +39,25 @@ def fetch_search_results(query):
|
|
| 39 |
{"role": "system", "content": "You are a helpful search engine."},
|
| 40 |
{"role": "user", "content": prompt}
|
| 41 |
],
|
| 42 |
-
response_format={"type": "json_object"}
|
|
|
|
| 43 |
)
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
# Handle different possible JSON structures
|
| 49 |
-
if isinstance(results, dict) and "results" in results:
|
| 50 |
-
results = results["results"]
|
| 51 |
-
elif isinstance(results, list):
|
| 52 |
-
pass
|
| 53 |
else:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
except Exception as e:
|
| 59 |
error_msg = str(e)
|
|
@@ -64,11 +68,131 @@ def fetch_search_results(query):
|
|
| 64 |
else:
|
| 65 |
return None, f"Error: {error_msg}"
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
@app.route('/', methods=['GET'])
|
| 68 |
def search_page():
|
| 69 |
"""Generate and serve the search results page styled like Google."""
|
| 70 |
query = request.args.get('query', '')
|
| 71 |
page = request.args.get('page', '1')
|
|
|
|
| 72 |
try:
|
| 73 |
page = int(page)
|
| 74 |
except ValueError:
|
|
@@ -83,11 +207,11 @@ def search_page():
|
|
| 83 |
body { font-family: Arial, sans-serif; margin: 0; padding: 20px; }
|
| 84 |
.header { text-align: center; margin-bottom: 20px; }
|
| 85 |
.logo { font-size: 36px; font-weight: bold; }
|
| 86 |
-
.logo span:nth-child(1) { color: #4285f4; }
|
| 87 |
-
.logo span:nth-child(2) { color: #ea4335; }
|
| 88 |
-
.logo span:nth-child(3) { color: #fbbc05; }
|
| 89 |
-
.logo span:nth-child(4) { color: #4285f4; }
|
| 90 |
-
.logo span:nth-child(5) { color: #34a853; }
|
| 91 |
.search-box { max-width: 584px; margin: 0 auto; }
|
| 92 |
.search-box input[type="text"] {
|
| 93 |
width: 100%; padding: 12px 20px; font-size: 16px;
|
|
@@ -116,8 +240,8 @@ def search_page():
|
|
| 116 |
<input type="text" name="query" placeholder="Search...">
|
| 117 |
<input type="hidden" name="page" value="1">
|
| 118 |
<div class="search-buttons">
|
| 119 |
-
<input type="submit" value="LLM Search">
|
| 120 |
-
<input type="submit" value="I'm Feeling Lucky"
|
| 121 |
</div>
|
| 122 |
</form>
|
| 123 |
</div>
|
|
@@ -126,267 +250,36 @@ def search_page():
|
|
| 126 |
"""
|
| 127 |
return render_template_string(html_content)
|
| 128 |
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
<
|
| 136 |
-
|
| 137 |
-
body {{ font-family: Arial, sans-serif; margin: 0; padding: 20px; }}
|
| 138 |
-
.header {{ text-align: center; margin-bottom: 20px; }}
|
| 139 |
-
.logo {{ font-size: 36px; font-weight: bold; }}
|
| 140 |
-
.logo span:nth-child(1) {{ color: #4285f4; }}
|
| 141 |
-
.logo span:nth-child(2) {{ color: #ea4335; }}
|
| 142 |
-
.logo span:nth-child(3) {{ color: #fbbc05; }}
|
| 143 |
-
.logo span:nth-child(4) {{ color: #4285f4; }}
|
| 144 |
-
.logo span:nth-child(5) {{ color: #34a853; }}
|
| 145 |
-
.search-box {{ max-width: 584px; margin: 0 auto; }}
|
| 146 |
-
.search-box input[type="text"] {{
|
| 147 |
-
width: 100%; padding: 12px 20px; font-size: 16px;
|
| 148 |
-
border: 1px solid #dfe1e5; border-radius: 24px;
|
| 149 |
-
box-shadow: 0 1px 6px rgba(32,33,36,0.28);
|
| 150 |
-
}}
|
| 151 |
-
.search-box input[type="submit"] {{
|
| 152 |
-
background-color: #f8f9fa; border: 1px solid #f8f9fa;
|
| 153 |
-
border-radius: 4px; color: #3c4043; font-size: 14px;
|
| 154 |
-
padding: 10px 16px; margin: 11px 4px; cursor: pointer;
|
| 155 |
-
}}
|
| 156 |
-
.search-box input[type="submit"]:hover {{
|
| 157 |
-
border: 1px solid #dadce0; box-shadow: 0 1px 2px rgba(0,0,0,0.1);
|
| 158 |
-
}}
|
| 159 |
-
.search-buttons {{ text-align: center; }}
|
| 160 |
-
.error {{ color: red; text-align: center; }}
|
| 161 |
-
</style>
|
| 162 |
-
</head>
|
| 163 |
-
<body>
|
| 164 |
-
<div class="header">
|
| 165 |
-
<div class="logo">
|
| 166 |
-
<span>L</span><span>L</span><span>M</span><span> </span><span>Search</span>
|
| 167 |
-
</div>
|
| 168 |
-
</div>
|
| 169 |
-
<div class="search-box">
|
| 170 |
<form method="get" action="/">
|
| 171 |
<input type="text" name="query" value="{html.escape(query)}">
|
| 172 |
-
<input type="
|
| 173 |
-
<div class="search-buttons">
|
| 174 |
-
<input type="submit" value="LLM Search">
|
| 175 |
-
<input type="submit" value="I'm Feeling Lucky" disabled>
|
| 176 |
-
</div>
|
| 177 |
</form>
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
"""
|
| 183 |
-
return
|
| 184 |
-
|
| 185 |
-
# Calculate pagination boundaries
|
| 186 |
-
start_idx = (page - 1) * RESULTS_PER_PAGE
|
| 187 |
-
end_idx = start_idx + RESULTS_PER_PAGE
|
| 188 |
-
total_pages = (len(results) + RESULTS_PER_PAGE - 1) // RESULTS_PER_PAGE
|
| 189 |
-
|
| 190 |
-
# Ensure indices are within bounds
|
| 191 |
-
if start_idx >= len(results):
|
| 192 |
-
html_content = f"""
|
| 193 |
<html>
|
| 194 |
<head>
|
| 195 |
-
<
|
| 196 |
-
<style>
|
| 197 |
-
body {{ font-family: Arial, sans-serif; margin: 0; padding: 20px; }}
|
| 198 |
-
.header {{ text-align: center; margin-bottom: 20px; }}
|
| 199 |
-
.logo {{ font-size: 36px; font-weight: bold; }}
|
| 200 |
-
.logo span:nth-child(1) {{ color: #4285f4; }}
|
| 201 |
-
.logo span:nth-child(2) {{ color: #ea4335; }}
|
| 202 |
-
.logo span:nth-child(3) {{ color: #fbbc05; }}
|
| 203 |
-
.logo span:nth-child(4) {{ color: #4285f4; }}
|
| 204 |
-
.logo span:nth-child(5) {{ color: #34a853; }}
|
| 205 |
-
.search-box {{ max-width: 584px; margin: 0 auto; }}
|
| 206 |
-
.search-box input[type="text"] {{
|
| 207 |
-
width: 100%; padding: 12px 20px; font-size: 16px;
|
| 208 |
-
border: 1px solid #dfe1e5; border-radius: 24px;
|
| 209 |
-
box-shadow: 0 1px 6px rgba(32,33,36,0.28);
|
| 210 |
-
}}
|
| 211 |
-
.search-box input[type="submit"] {{
|
| 212 |
-
background-color: #f8f9fa; border: 1px solid #f8f9fa;
|
| 213 |
-
border-radius: 4px; color: #3c4043; font-size: 14px;
|
| 214 |
-
padding: 10px 16px; margin: 11px 4px; cursor: pointer;
|
| 215 |
-
}}
|
| 216 |
-
.search-box input[type="submit"]:hover {{
|
| 217 |
-
border: 1px solid #dadce0; box-shadow: 0 1px 2px rgba(0,0,0,0.1);
|
| 218 |
-
}}
|
| 219 |
-
.search-buttons {{ text-align: center; }}
|
| 220 |
-
</style>
|
| 221 |
</head>
|
| 222 |
<body>
|
| 223 |
-
<
|
| 224 |
-
<div class="logo">
|
| 225 |
-
<span>L</span><span>L</span><span>M</span><span> </span><span>Search</span>
|
| 226 |
-
</div>
|
| 227 |
-
</div>
|
| 228 |
-
<div class="search-box">
|
| 229 |
-
<form method="get" action="/">
|
| 230 |
-
<input type="text" name="query" value="{html.escape(query)}">
|
| 231 |
-
<input type="hidden" name="page" value="1">
|
| 232 |
-
<div class="search-buttons">
|
| 233 |
-
<input type="submit" value="LLM Search">
|
| 234 |
-
<input type="submit" value="I'm Feeling Lucky" disabled>
|
| 235 |
-
</div>
|
| 236 |
-
</form>
|
| 237 |
-
</div>
|
| 238 |
-
<p style="text-align: center;">No more results to display.</p>
|
| 239 |
</body>
|
| 240 |
</html>
|
| 241 |
-
"""
|
| 242 |
-
return render_template_string(html_content)
|
| 243 |
-
|
| 244 |
-
paginated_results = results[start_idx:end_idx]
|
| 245 |
-
|
| 246 |
-
# Generate full HTML page styled like Google
|
| 247 |
-
html_content = f"""
|
| 248 |
-
<html>
|
| 249 |
-
<head>
|
| 250 |
-
<title>LLM Search Engine</title>
|
| 251 |
-
<style>
|
| 252 |
-
body {{
|
| 253 |
-
font-family: Arial, sans-serif;
|
| 254 |
-
margin: 0;
|
| 255 |
-
padding: 0;
|
| 256 |
-
color: #202124;
|
| 257 |
-
}}
|
| 258 |
-
.header {{
|
| 259 |
-
text-align: center;
|
| 260 |
-
padding: 20px 0;
|
| 261 |
-
}}
|
| 262 |
-
.logo {{
|
| 263 |
-
font-size: 36px;
|
| 264 |
-
font-weight: bold;
|
| 265 |
-
}}
|
| 266 |
-
.logo span:nth-child(1) {{ color: #4285f4; }}
|
| 267 |
-
.logo span:nth-child(2) {{ color: #ea4335; }}
|
| 268 |
-
.logo span:nth-child(3) {{ color: #fbbc05; }}
|
| 269 |
-
.logo span:nth-child(4) {{ color: #4285f4; }}
|
| 270 |
-
.logo span:nth-child(5) {{ color: #34a853; }}
|
| 271 |
-
.search-box {{
|
| 272 |
-
max-width: 584px;
|
| 273 |
-
margin: 0 auto 20px;
|
| 274 |
-
}}
|
| 275 |
-
.search-box input[type="text"] {{
|
| 276 |
-
width: 100%;
|
| 277 |
-
padding: 12px 20px;
|
| 278 |
-
font-size: 16px;
|
| 279 |
-
border: 1px solid #dfe1e5;
|
| 280 |
-
border-radius: 24px;
|
| 281 |
-
box-shadow: 0 1px 6px rgba(32,33,36,0.28);
|
| 282 |
-
outline: none;
|
| 283 |
-
}}
|
| 284 |
-
.search-box input[type="submit"] {{
|
| 285 |
-
background-color: #f8f9fa;
|
| 286 |
-
border: 1px solid #f8f9fa;
|
| 287 |
-
border-radius: 4px;
|
| 288 |
-
color: #3c4043;
|
| 289 |
-
font-size: 14px;
|
| 290 |
-
padding: 10px 16px;
|
| 291 |
-
margin: 11px 4px;
|
| 292 |
-
cursor: pointer;
|
| 293 |
-
}}
|
| 294 |
-
.search-box input[type="submit"]:hover {{
|
| 295 |
-
border: 1px solid #dadce0;
|
| 296 |
-
box-shadow: 0 1px 2px rgba(0,0,0,0.1);
|
| 297 |
-
}}
|
| 298 |
-
.search-buttons {{
|
| 299 |
-
text-align: center;
|
| 300 |
-
}}
|
| 301 |
-
.results {{
|
| 302 |
-
max-width: 652px;
|
| 303 |
-
margin: 0 auto;
|
| 304 |
-
}}
|
| 305 |
-
.search-result {{
|
| 306 |
-
margin-bottom: 28px;
|
| 307 |
-
}}
|
| 308 |
-
.search-result a {{
|
| 309 |
-
color: #1a0dab;
|
| 310 |
-
font-size: 20px;
|
| 311 |
-
text-decoration: none;
|
| 312 |
-
}}
|
| 313 |
-
.search-result a:hover {{
|
| 314 |
-
text-decoration: underline;
|
| 315 |
-
}}
|
| 316 |
-
.search-result .url {{
|
| 317 |
-
color: #006621;
|
| 318 |
-
font-size: 14px;
|
| 319 |
-
line-height: 20px;
|
| 320 |
-
}}
|
| 321 |
-
.search-result p {{
|
| 322 |
-
color: #4d5156;
|
| 323 |
-
font-size: 14px;
|
| 324 |
-
line-height: 22px;
|
| 325 |
-
margin: 0;
|
| 326 |
-
}}
|
| 327 |
-
.pagination {{
|
| 328 |
-
text-align: center;
|
| 329 |
-
margin: 40px 0;
|
| 330 |
-
}}
|
| 331 |
-
.pagination a, .pagination span {{
|
| 332 |
-
color: #1a0dab;
|
| 333 |
-
font-size: 14px;
|
| 334 |
-
margin: 0 8px;
|
| 335 |
-
text-decoration: none;
|
| 336 |
-
}}
|
| 337 |
-
.pagination a:hover {{
|
| 338 |
-
text-decoration: underline;
|
| 339 |
-
}}
|
| 340 |
-
</style>
|
| 341 |
-
</head>
|
| 342 |
-
<body>
|
| 343 |
-
<div class="header">
|
| 344 |
-
<div class="logo">
|
| 345 |
-
<span>L</span><span>L</span><span>M</span><span> </span><span>Search</span>
|
| 346 |
-
</div>
|
| 347 |
-
</div>
|
| 348 |
-
<div class="search-box">
|
| 349 |
-
<form method="get" action="/">
|
| 350 |
-
<input type="text" name="query" value="{html.escape(query)}">
|
| 351 |
-
<input type="hidden" name="page" value="1">
|
| 352 |
-
<div class="search-buttons">
|
| 353 |
-
<input type="submit" value="LLM Search">
|
| 354 |
-
<input type="submit" value="I'm Feeling Lucky" disabled>
|
| 355 |
-
</div>
|
| 356 |
-
</form>
|
| 357 |
-
</div>
|
| 358 |
-
<div class="results">
|
| 359 |
-
<h2 style="font-size: 18px; color: #70757a; margin-bottom: 20px;">Results for '{html.escape(query)}' (Page {page} of {total_pages})</h2>
|
| 360 |
-
"""
|
| 361 |
-
|
| 362 |
-
for result in paginated_results:
|
| 363 |
-
title = html.escape(result.get("title", "No title"))
|
| 364 |
-
snippet = html.escape(result.get("snippet", "No snippet"))
|
| 365 |
-
url = html.escape(result.get("url", "#"))
|
| 366 |
-
html_content += f"""
|
| 367 |
-
<div class="search-result">
|
| 368 |
-
<a href="{url}" target="_blank">{title}</a>
|
| 369 |
-
<div class="url">{url}</div>
|
| 370 |
-
<p>{snippet}</p>
|
| 371 |
-
</div>
|
| 372 |
-
"""
|
| 373 |
-
|
| 374 |
-
# Pagination links
|
| 375 |
-
encoded_query = quote(query)
|
| 376 |
-
prev_link = f'<a href="/?query={encoded_query}&page={page-1}">Previous</a>' if page > 1 else '<span>Previous</span>'
|
| 377 |
-
next_link = f'<a href="/?query={encoded_query}&page={page+1}">Next</a>' if page < total_pages else '<span>Next</span>'
|
| 378 |
-
html_content += f"""
|
| 379 |
-
</div>
|
| 380 |
-
<div class="pagination">
|
| 381 |
-
{prev_link}
|
| 382 |
-
<span>Page {page} of {total_pages}</span>
|
| 383 |
-
{next_link}
|
| 384 |
-
</div>
|
| 385 |
-
</body>
|
| 386 |
-
</html>
|
| 387 |
-
"""
|
| 388 |
|
| 389 |
-
|
|
|
|
| 390 |
|
| 391 |
if __name__ == '__main__':
|
| 392 |
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get("PORT", 5000)))
|
|
|
|
| 1 |
+
from flask import Flask, request, render_template_string, Response
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
import json
|
|
|
|
| 17 |
RESULTS_PER_PAGE = 10
|
| 18 |
TOTAL_RESULTS = 30 # Generate 30 results to allow pagination
|
| 19 |
|
| 20 |
+
def fetch_search_results(query, stream=False):
|
| 21 |
+
"""Fetch search results from the LLM, with optional streaming."""
|
| 22 |
if not query.strip():
|
| 23 |
return None, "Please enter a search query."
|
| 24 |
|
|
|
|
| 39 |
{"role": "system", "content": "You are a helpful search engine."},
|
| 40 |
{"role": "user", "content": prompt}
|
| 41 |
],
|
| 42 |
+
response_format={"type": "json_object"},
|
| 43 |
+
stream=stream # Enable streaming if requested
|
| 44 |
)
|
| 45 |
|
| 46 |
+
if stream:
|
| 47 |
+
return response, None # Return raw streaming response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
else:
|
| 49 |
+
content = response.choices[0].message.content
|
| 50 |
+
results = json.loads(content)
|
| 51 |
+
|
| 52 |
+
# Handle different possible JSON structures
|
| 53 |
+
if isinstance(results, dict) and "results" in results:
|
| 54 |
+
results = results["results"]
|
| 55 |
+
elif isinstance(results, list):
|
| 56 |
+
pass
|
| 57 |
+
else:
|
| 58 |
+
return None, "Error: Unexpected JSON structure."
|
| 59 |
+
|
| 60 |
+
return results, None
|
| 61 |
|
| 62 |
except Exception as e:
|
| 63 |
error_msg = str(e)
|
|
|
|
| 68 |
else:
|
| 69 |
return None, f"Error: {error_msg}"
|
| 70 |
|
| 71 |
+
def stream_search_results(query, page):
|
| 72 |
+
"""Stream search results incrementally."""
|
| 73 |
+
stream_response, error = fetch_search_results(query, stream=True)
|
| 74 |
+
if error:
|
| 75 |
+
yield f"<p style='color: red; text-align: center;'>{error}</p>"
|
| 76 |
+
return
|
| 77 |
+
|
| 78 |
+
# Generate header
|
| 79 |
+
header = """
|
| 80 |
+
<html>
|
| 81 |
+
<head>
|
| 82 |
+
<title>LLM Search Engine</title>
|
| 83 |
+
<style>
|
| 84 |
+
body { font-family: Arial, sans-serif; margin: 0; padding: 0; color: #202124; }
|
| 85 |
+
.header { text-align: center; padding: 20px 0; }
|
| 86 |
+
.logo { font-size: 36px; font-weight: bold; }
|
| 87 |
+
.logo span:nth-child(1) { color: #4285f4; }
|
| 88 |
+
.logo span:nth-child(2) { color: #ea4335; }
|
| 89 |
+
.logo span:nth-child(3) { color: #fbbc05; }
|
| 90 |
+
.logo span:nth-child(4) { color: #4285f4; }
|
| 91 |
+
.logo span:nth-child(5) { color: #34a853; }
|
| 92 |
+
.search-box { max-width: 584px; margin: 0 auto 20px; }
|
| 93 |
+
.search-box input[type="text"] {
|
| 94 |
+
width: 100%; padding: 12px 20px; font-size: 16px;
|
| 95 |
+
border: 1px solid #dfe1e5; border-radius: 24px;
|
| 96 |
+
box-shadow: 0 1px 6px rgba(32,33,36,0.28); outline: none;
|
| 97 |
+
}
|
| 98 |
+
.search-box input[type="submit"] {
|
| 99 |
+
background-color: #f8f9fa; border: 1px solid #f8f9fa;
|
| 100 |
+
border-radius: 4px; color: #3c4043; font-size: 14px;
|
| 101 |
+
padding: 10px 16px; margin: 11px 4px; cursor: pointer;
|
| 102 |
+
}
|
| 103 |
+
.search-box input[type="submit"]:hover {
|
| 104 |
+
border: 1px solid #dadce0; box-shadow: 0 1px 2px rgba(0,0,0,0.1);
|
| 105 |
+
}
|
| 106 |
+
.search-buttons { text-align: center; }
|
| 107 |
+
.results { max-width: 652px; margin: 0 auto; }
|
| 108 |
+
.search-result { margin-bottom: 28px; }
|
| 109 |
+
.search-result a { color: #1a0dab; font-size: 20px; text-decoration: none; }
|
| 110 |
+
.search-result a:hover { text-decoration: underline; }
|
| 111 |
+
.search-result .url { color: #006621; font-size: 14px; line-height: 20px; }
|
| 112 |
+
.search-result p { color: #4d5156; font-size: 14px; line-height: 22px; margin: 0; }
|
| 113 |
+
.pagination { text-align: center; margin: 40px 0; }
|
| 114 |
+
.pagination a, .pagination span {
|
| 115 |
+
color: #1a0dab; font-size: 14px; margin: 0 8px; text-decoration: none;
|
| 116 |
+
}
|
| 117 |
+
.pagination a:hover { text-decoration: underline; }
|
| 118 |
+
</style>
|
| 119 |
+
</head>
|
| 120 |
+
<body>
|
| 121 |
+
<div class="header">
|
| 122 |
+
<div class="logo">
|
| 123 |
+
<span>L</span><span>L</span><span>M</span><span> </span><span>Search</span>
|
| 124 |
+
</div>
|
| 125 |
+
</div>
|
| 126 |
+
<div class="search-box">
|
| 127 |
+
<form method="get" action="/">
|
| 128 |
+
<input type="text" name="query" value="{{query}}">
|
| 129 |
+
<input type="hidden" name="page" value="1">
|
| 130 |
+
<div class="search-buttons">
|
| 131 |
+
<input type="submit" name="btn" value="LLM Search">
|
| 132 |
+
<input type="submit" name="btn" value="I'm Feeling Lucky">
|
| 133 |
+
</div>
|
| 134 |
+
</form>
|
| 135 |
+
</div>
|
| 136 |
+
<div class="results">
|
| 137 |
+
<h2 style="font-size: 18px; color: #70757a; margin-bottom: 20px;">Results for '{{query}}' (Page {{page}})</h2>
|
| 138 |
+
""".replace('{{query}}', html.escape(query)).replace('{{page}}', str(page))
|
| 139 |
+
yield header
|
| 140 |
+
|
| 141 |
+
# Stream and parse results
|
| 142 |
+
buffer = ""
|
| 143 |
+
results = []
|
| 144 |
+
for chunk in stream_response:
|
| 145 |
+
if chunk.choices[0].delta.content:
|
| 146 |
+
buffer += chunk.choices[0].delta.content
|
| 147 |
+
try:
|
| 148 |
+
# Try to parse the buffer as JSON incrementally
|
| 149 |
+
temp_results = json.loads(buffer)
|
| 150 |
+
if isinstance(temp_results, list):
|
| 151 |
+
results = temp_results
|
| 152 |
+
elif isinstance(temp_results, dict) and "results" in temp_results:
|
| 153 |
+
results = temp_results["results"]
|
| 154 |
+
# Process only new results
|
| 155 |
+
for i, result in enumerate(results[len(results) - (len(results) % RESULTS_PER_PAGE):]):
|
| 156 |
+
if i >= start_idx and i < end_idx:
|
| 157 |
+
title = html.escape(result.get("title", "No title"))
|
| 158 |
+
snippet = html.escape(result.get("snippet", "No snippet"))
|
| 159 |
+
url = html.escape(result.get("url", "#"))
|
| 160 |
+
yield f"""
|
| 161 |
+
<div class="search-result">
|
| 162 |
+
<a href="{url}" target="_blank">{title}</a>
|
| 163 |
+
<div class="url">{url}</div>
|
| 164 |
+
<p>{snippet}</p>
|
| 165 |
+
</div>
|
| 166 |
+
"""
|
| 167 |
+
except json.JSONDecodeError:
|
| 168 |
+
continue # Keep buffering until complete JSON
|
| 169 |
+
|
| 170 |
+
# Calculate pagination
|
| 171 |
+
start_idx = (page - 1) * RESULTS_PER_PAGE
|
| 172 |
+
end_idx = start_idx + RESULTS_PER_PAGE
|
| 173 |
+
total_pages = (len(results) + RESULTS_PER_PAGE - 1) // RESULTS_PER_PAGE
|
| 174 |
+
|
| 175 |
+
# Pagination links
|
| 176 |
+
encoded_query = quote(query)
|
| 177 |
+
prev_link = f'<a href="/?query={encoded_query}&page={page-1}&btn=LLM+Search">Previous</a>' if page > 1 else '<span>Previous</span>'
|
| 178 |
+
next_link = f'<a href="/?query={encoded_query}&page={page+1}&btn=LLM+Search">Next</a>' if page < total_pages else '<span>Next</span>'
|
| 179 |
+
yield f"""
|
| 180 |
+
</div>
|
| 181 |
+
<div class="pagination">
|
| 182 |
+
{prev_link}
|
| 183 |
+
<span>Page {page} of {total_pages}</span>
|
| 184 |
+
{next_link}
|
| 185 |
+
</div>
|
| 186 |
+
</body>
|
| 187 |
+
</html>
|
| 188 |
+
"""
|
| 189 |
+
|
| 190 |
@app.route('/', methods=['GET'])
|
| 191 |
def search_page():
|
| 192 |
"""Generate and serve the search results page styled like Google."""
|
| 193 |
query = request.args.get('query', '')
|
| 194 |
page = request.args.get('page', '1')
|
| 195 |
+
btn = request.args.get('btn', 'LLM Search')
|
| 196 |
try:
|
| 197 |
page = int(page)
|
| 198 |
except ValueError:
|
|
|
|
| 207 |
body { font-family: Arial, sans-serif; margin: 0; padding: 20px; }
|
| 208 |
.header { text-align: center; margin-bottom: 20px; }
|
| 209 |
.logo { font-size: 36px; font-weight: bold; }
|
| 210 |
+
.logo span:nth-child(1) { color: #4285f4; }
|
| 211 |
+
.logo span:nth-child(2) { color: #ea4335; }
|
| 212 |
+
.logo span:nth-child(3) { color: #fbbc05; }
|
| 213 |
+
.logo span:nth-child(4) { color: #4285f4; }
|
| 214 |
+
.logo span:nth-child(5) { color: #34a853; }
|
| 215 |
.search-box { max-width: 584px; margin: 0 auto; }
|
| 216 |
.search-box input[type="text"] {
|
| 217 |
width: 100%; padding: 12px 20px; font-size: 16px;
|
|
|
|
| 240 |
<input type="text" name="query" placeholder="Search...">
|
| 241 |
<input type="hidden" name="page" value="1">
|
| 242 |
<div class="search-buttons">
|
| 243 |
+
<input type="submit" name="btn" value="LLM Search">
|
| 244 |
+
<input type="submit" name="btn" value="I'm Feeling Lucky">
|
| 245 |
</div>
|
| 246 |
</form>
|
| 247 |
</div>
|
|
|
|
| 250 |
"""
|
| 251 |
return render_template_string(html_content)
|
| 252 |
|
| 253 |
+
if btn == "I'm Feeling Lucky":
|
| 254 |
+
results, error = fetch_search_results(query, stream=False)
|
| 255 |
+
if error:
|
| 256 |
+
return render_template_string(f"""
|
| 257 |
+
<html>
|
| 258 |
+
<head><title>LLM Search Engine</title></head>
|
| 259 |
+
<body style="font-family: Arial, sans-serif;">
|
| 260 |
+
<h1>LLM Search Engine</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
<form method="get" action="/">
|
| 262 |
<input type="text" name="query" value="{html.escape(query)}">
|
| 263 |
+
<input type="submit" value="Search">
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
</form>
|
| 265 |
+
<p style="color: red;">{error}</p>
|
| 266 |
+
</body>
|
| 267 |
+
</html>
|
| 268 |
+
""")
|
| 269 |
+
first_url = results[0].get("url", "#") if results else "#"
|
| 270 |
+
return Response(f"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 271 |
<html>
|
| 272 |
<head>
|
| 273 |
+
<meta http-equiv="refresh" content="0; url={first_url}">
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
</head>
|
| 275 |
<body>
|
| 276 |
+
<p>Redirecting to {first_url}...</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
</body>
|
| 278 |
</html>
|
| 279 |
+
""", mimetype="text/html")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
+
# Stream results for "LLM Search"
|
| 282 |
+
return Response(stream_search_results(query, page), mimetype="text/html")
|
| 283 |
|
| 284 |
if __name__ == '__main__':
|
| 285 |
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get("PORT", 5000)))
|