Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,12 @@
|
|
| 1 |
-
import
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
from urllib.parse import quote
|
| 6 |
import html
|
| 7 |
|
|
|
|
|
|
|
| 8 |
# Initialize OpenAI client with API key and base URL from environment variables
|
| 9 |
client = OpenAI(
|
| 10 |
api_key=os.environ["OPENAI_API_KEY"],
|
|
@@ -62,15 +64,23 @@ def fetch_search_results(query):
|
|
| 62 |
else:
|
| 63 |
return None, f"Error: {error_msg}"
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
if not query.strip():
|
| 68 |
-
|
| 69 |
<html>
|
| 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">
|
|
@@ -79,16 +89,17 @@ def generate_search_page(query, page=1):
|
|
| 79 |
</body>
|
| 80 |
</html>
|
| 81 |
"""
|
|
|
|
| 82 |
|
| 83 |
results, error = fetch_search_results(query)
|
| 84 |
|
| 85 |
if error:
|
| 86 |
-
|
| 87 |
<html>
|
| 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">
|
|
@@ -97,6 +108,7 @@ def generate_search_page(query, page=1):
|
|
| 97 |
</body>
|
| 98 |
</html>
|
| 99 |
"""
|
|
|
|
| 100 |
|
| 101 |
# Calculate pagination boundaries
|
| 102 |
start_idx = (page - 1) * RESULTS_PER_PAGE
|
|
@@ -105,12 +117,12 @@ def generate_search_page(query, page=1):
|
|
| 105 |
|
| 106 |
# Ensure indices are within bounds
|
| 107 |
if start_idx >= len(results):
|
| 108 |
-
|
| 109 |
<html>
|
| 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">
|
|
@@ -119,6 +131,7 @@ def generate_search_page(query, page=1):
|
|
| 119 |
</body>
|
| 120 |
</html>
|
| 121 |
"""
|
|
|
|
| 122 |
|
| 123 |
paginated_results = results[start_idx:end_idx]
|
| 124 |
|
|
@@ -188,7 +201,7 @@ 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">
|
|
@@ -211,8 +224,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,44 +233,11 @@ def generate_search_page(query, page=1):
|
|
| 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 |
"""
|
| 241 |
|
| 242 |
-
return html_content
|
| 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 |
-
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, render_template_string
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
from urllib.parse import quote
|
| 6 |
import html
|
| 7 |
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
|
| 10 |
# Initialize OpenAI client with API key and base URL from environment variables
|
| 11 |
client = OpenAI(
|
| 12 |
api_key=os.environ["OPENAI_API_KEY"],
|
|
|
|
| 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."""
|
| 70 |
+
query = request.args.get('query', '')
|
| 71 |
+
page = request.args.get('page', '1')
|
| 72 |
+
try:
|
| 73 |
+
page = int(page)
|
| 74 |
+
except ValueError:
|
| 75 |
+
page = 1
|
| 76 |
+
|
| 77 |
if not query.strip():
|
| 78 |
+
html_content = """
|
| 79 |
<html>
|
| 80 |
<head><title>LLM Search Engine</title></head>
|
| 81 |
<body style="font-family: Arial, sans-serif;">
|
| 82 |
<h1>LLM Search Engine</h1>
|
| 83 |
+
<form method="get" action="/">
|
| 84 |
<input type="text" name="query" placeholder="Type your search here...">
|
| 85 |
<input type="submit" value="Search">
|
| 86 |
<input type="hidden" name="page" value="1">
|
|
|
|
| 89 |
</body>
|
| 90 |
</html>
|
| 91 |
"""
|
| 92 |
+
return render_template_string(html_content)
|
| 93 |
|
| 94 |
results, error = fetch_search_results(query)
|
| 95 |
|
| 96 |
if error:
|
| 97 |
+
html_content = f"""
|
| 98 |
<html>
|
| 99 |
<head><title>LLM Search Engine</title></head>
|
| 100 |
<body style="font-family: Arial, sans-serif;">
|
| 101 |
<h1>LLM Search Engine</h1>
|
| 102 |
+
<form method="get" action="/">
|
| 103 |
<input type="text" name="query" value="{html.escape(query)}">
|
| 104 |
<input type="submit" value="Search">
|
| 105 |
<input type="hidden" name="page" value="1">
|
|
|
|
| 108 |
</body>
|
| 109 |
</html>
|
| 110 |
"""
|
| 111 |
+
return render_template_string(html_content)
|
| 112 |
|
| 113 |
# Calculate pagination boundaries
|
| 114 |
start_idx = (page - 1) * RESULTS_PER_PAGE
|
|
|
|
| 117 |
|
| 118 |
# Ensure indices are within bounds
|
| 119 |
if start_idx >= len(results):
|
| 120 |
+
html_content = f"""
|
| 121 |
<html>
|
| 122 |
<head><title>LLM Search Engine</title></head>
|
| 123 |
<body style="font-family: Arial, sans-serif;">
|
| 124 |
<h1>LLM Search Engine</h1>
|
| 125 |
+
<form method="get" action="/">
|
| 126 |
<input type="text" name="query" value="{html.escape(query)}">
|
| 127 |
<input type="submit" value="Search">
|
| 128 |
<input type="hidden" name="page" value="1">
|
|
|
|
| 131 |
</body>
|
| 132 |
</html>
|
| 133 |
"""
|
| 134 |
+
return render_template_string(html_content)
|
| 135 |
|
| 136 |
paginated_results = results[start_idx:end_idx]
|
| 137 |
|
|
|
|
| 201 |
</head>
|
| 202 |
<body>
|
| 203 |
<h1>LLM Search Engine</h1>
|
| 204 |
+
<form class="search-box" method="get" action="/">
|
| 205 |
<input type="text" name="query" value="{html.escape(query)}">
|
| 206 |
<input type="submit" value="Search">
|
| 207 |
<input type="hidden" name="page" value="1">
|
|
|
|
| 224 |
|
| 225 |
# Pagination links
|
| 226 |
encoded_query = quote(query)
|
| 227 |
+
prev_link = f'<a href="/?query={encoded_query}&page={page-1}">Previous</a>' if page > 1 else '<span>Previous</span>'
|
| 228 |
+
next_link = f'<a href="/?query={encoded_query}&page={page+1}">Next</a>' if page < total_pages else '<span>Next</span>'
|
| 229 |
html_content += f"""
|
| 230 |
</div>
|
| 231 |
<div class="pagination">
|
|
|
|
| 233 |
<span>Page {page} of {total_pages}</span>
|
| 234 |
{next_link}
|
| 235 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
</body>
|
| 237 |
</html>
|
| 238 |
"""
|
| 239 |
|
| 240 |
+
return render_template_string(html_content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
|
| 242 |
+
if __name__ == '__main__':
|
| 243 |
+
app.run(debug=True, host='0.0.0.0', port=int(os.environ.get("PORT", 5000)))
|