Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,8 @@ import gradio as gr
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
-
from
|
|
|
|
| 6 |
|
| 7 |
# Initialize OpenAI client with API key and base URL from environment variables
|
| 8 |
client = OpenAI(
|
|
@@ -10,7 +11,7 @@ client = OpenAI(
|
|
| 10 |
base_url=os.environ["OPENAI_BASE_URL"]
|
| 11 |
)
|
| 12 |
|
| 13 |
-
# Define
|
| 14 |
RESULTS_PER_PAGE = 10
|
| 15 |
TOTAL_RESULTS = 30 # Generate 30 results to allow pagination
|
| 16 |
|
|
@@ -63,6 +64,22 @@ def fetch_search_results(query):
|
|
| 63 |
|
| 64 |
def generate_search_page(query, page=1):
|
| 65 |
"""Generate a full HTML search results page."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
results, error = fetch_search_results(query)
|
| 67 |
|
| 68 |
if error:
|
|
@@ -71,6 +88,11 @@ def generate_search_page(query, page=1):
|
|
| 71 |
<head><title>LLM Search Engine</title></head>
|
| 72 |
<body style="font-family: Arial, sans-serif;">
|
| 73 |
<h1>LLM Search Engine</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
<p style="color: red;">{error}</p>
|
| 75 |
</body>
|
| 76 |
</html>
|
|
@@ -83,11 +105,16 @@ def generate_search_page(query, page=1):
|
|
| 83 |
|
| 84 |
# Ensure indices are within bounds
|
| 85 |
if start_idx >= len(results):
|
| 86 |
-
return """
|
| 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 |
<p>No more results to display.</p>
|
| 92 |
</body>
|
| 93 |
</html>
|
|
@@ -96,92 +123,85 @@ def generate_search_page(query, page=1):
|
|
| 96 |
paginated_results = results[start_idx:end_idx]
|
| 97 |
|
| 98 |
# Generate full HTML page
|
| 99 |
-
|
| 100 |
<html>
|
| 101 |
<head>
|
| 102 |
<title>LLM Search Engine</title>
|
| 103 |
<style>
|
| 104 |
-
body {
|
| 105 |
font-family: Arial, sans-serif;
|
| 106 |
margin: 0;
|
| 107 |
padding: 20px;
|
| 108 |
max-width: 800px;
|
| 109 |
margin-left: auto;
|
| 110 |
margin-right: auto;
|
| 111 |
-
}
|
| 112 |
-
.search-box {
|
| 113 |
-
margin-bottom: 20px;
|
| 114 |
-
}
|
| 115 |
-
.search-box input[type="text"] {
|
| 116 |
width: 70%;
|
| 117 |
padding: 8px;
|
| 118 |
font-size: 16px;
|
| 119 |
border: 1px solid #dfe1e5;
|
| 120 |
border-radius: 4px;
|
| 121 |
-
}
|
| 122 |
-
.search-box input[type="submit"] {
|
| 123 |
padding: 8px 16px;
|
| 124 |
font-size: 14px;
|
| 125 |
background-color: #f8f9fa;
|
| 126 |
border: 1px solid #dfe1e5;
|
| 127 |
border-radius: 4px;
|
| 128 |
cursor: pointer;
|
| 129 |
-
}
|
| 130 |
-
.search-result {
|
| 131 |
margin-bottom: 20px;
|
| 132 |
-
}
|
| 133 |
-
.search-result a {
|
| 134 |
color: #1a0dab;
|
| 135 |
font-size: 18px;
|
| 136 |
text-decoration: none;
|
| 137 |
-
}
|
| 138 |
-
.search-result a:hover {
|
| 139 |
text-decoration: underline;
|
| 140 |
-
}
|
| 141 |
-
.search-result .url {
|
| 142 |
color: #006621;
|
| 143 |
font-size: 14px;
|
| 144 |
margin: 2px 0;
|
| 145 |
-
}
|
| 146 |
-
.search-result p {
|
| 147 |
color: #545454;
|
| 148 |
font-size: 14px;
|
| 149 |
margin: 2px 0;
|
| 150 |
-
}
|
| 151 |
-
.pagination {
|
| 152 |
margin-top: 20px;
|
| 153 |
text-align: center;
|
| 154 |
-
}
|
| 155 |
-
.pagination a {
|
| 156 |
-
color: #1a0dab;
|
| 157 |
margin: 0 10px;
|
|
|
|
| 158 |
text-decoration: none;
|
| 159 |
-
}
|
| 160 |
-
.pagination a:hover {
|
| 161 |
text-decoration: underline;
|
| 162 |
-
}
|
| 163 |
-
.pagination span {
|
| 164 |
-
color: #545454;
|
| 165 |
-
margin: 0 10px;
|
| 166 |
-
}
|
| 167 |
</style>
|
| 168 |
</head>
|
| 169 |
<body>
|
| 170 |
<h1>LLM Search Engine</h1>
|
| 171 |
<form class="search-box" method="get" action="/">
|
| 172 |
-
<input type="text" name="query" value="{query}"
|
| 173 |
<input type="submit" value="Search">
|
| 174 |
<input type="hidden" name="page" value="1">
|
| 175 |
</form>
|
| 176 |
<h2>Results for '{query}' (Page {page} of {total_pages})</h2>
|
|
|
|
| 177 |
"""
|
| 178 |
|
| 179 |
-
# Add search results
|
| 180 |
for result in paginated_results:
|
| 181 |
title = result.get("title", "No title")
|
| 182 |
snippet = result.get("snippet", "No snippet")
|
| 183 |
url = result.get("url", "#")
|
| 184 |
-
|
| 185 |
<div class="search-result">
|
| 186 |
<a href="{url}" target="_blank">{title}</a>
|
| 187 |
<div class="url">{url}</div>
|
|
@@ -189,39 +209,37 @@ def generate_search_page(query, page=1):
|
|
| 189 |
</div>
|
| 190 |
"""
|
| 191 |
|
| 192 |
-
#
|
| 193 |
-
|
| 194 |
-
if page > 1
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
html += '</div>'
|
| 204 |
-
|
| 205 |
-
html += """
|
| 206 |
</body>
|
| 207 |
</html>
|
| 208 |
"""
|
| 209 |
|
| 210 |
-
return
|
| 211 |
|
| 212 |
-
# Define the app with Blocks
|
| 213 |
with gr.Blocks(title="LLM Search Engine") as app:
|
| 214 |
-
|
| 215 |
-
|
|
|
|
| 216 |
try:
|
| 217 |
-
page = int(page
|
| 218 |
-
except
|
| 219 |
page = 1
|
| 220 |
-
|
|
|
|
| 221 |
|
| 222 |
-
#
|
| 223 |
-
app.route("/",
|
| 224 |
-
outputs=gr.HTML(), _js="() => [new URLSearchParams(window.location.search).get('query') || '', new URLSearchParams(window.location.search).get('page') || '1']",
|
| 225 |
-
fn=handle_request)
|
| 226 |
|
|
|
|
| 227 |
app.launch()
|
|
|
|
| 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(
|
|
|
|
| 11 |
base_url=os.environ["OPENAI_BASE_URL"]
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# Define constants for pagination
|
| 15 |
RESULTS_PER_PAGE = 10
|
| 16 |
TOTAL_RESULTS = 30 # Generate 30 results to allow pagination
|
| 17 |
|
|
|
|
| 64 |
|
| 65 |
def generate_search_page(query, page=1):
|
| 66 |
"""Generate a full HTML search results page."""
|
| 67 |
+
if not query.strip():
|
| 68 |
+
return """
|
| 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 method="get" action="/">
|
| 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">
|
| 77 |
+
</form>
|
| 78 |
+
<p>Please enter a search query.</p>
|
| 79 |
+
</body>
|
| 80 |
+
</html>
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
results, error = fetch_search_results(query)
|
| 84 |
|
| 85 |
if error:
|
|
|
|
| 88 |
<head><title>LLM Search Engine</title></head>
|
| 89 |
<body style="font-family: Arial, sans-serif;">
|
| 90 |
<h1>LLM Search Engine</h1>
|
| 91 |
+
<form method="get" action="/">
|
| 92 |
+
<input type="text" name="query" value="{html.escape(query)}">
|
| 93 |
+
<input type="submit" value="Search">
|
| 94 |
+
<input type="hidden" name="page" value="1">
|
| 95 |
+
</form>
|
| 96 |
<p style="color: red;">{error}</p>
|
| 97 |
</body>
|
| 98 |
</html>
|
|
|
|
| 105 |
|
| 106 |
# Ensure indices are within bounds
|
| 107 |
if start_idx >= len(results):
|
| 108 |
+
return f"""
|
| 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 method="get" action="/">
|
| 114 |
+
<input type="text" name="query" value="{html.escape(query)}">
|
| 115 |
+
<input type="submit" value="Search">
|
| 116 |
+
<input type="hidden" name="page" value="1">
|
| 117 |
+
</form>
|
| 118 |
<p>No more results to display.</p>
|
| 119 |
</body>
|
| 120 |
</html>
|
|
|
|
| 123 |
paginated_results = results[start_idx:end_idx]
|
| 124 |
|
| 125 |
# Generate full HTML page
|
| 126 |
+
html_content = f"""
|
| 127 |
<html>
|
| 128 |
<head>
|
| 129 |
<title>LLM Search Engine</title>
|
| 130 |
<style>
|
| 131 |
+
body {{
|
| 132 |
font-family: Arial, sans-serif;
|
| 133 |
margin: 0;
|
| 134 |
padding: 20px;
|
| 135 |
max-width: 800px;
|
| 136 |
margin-left: auto;
|
| 137 |
margin-right: auto;
|
| 138 |
+
}}
|
| 139 |
+
.search-box input[type="text"] {{
|
|
|
|
|
|
|
|
|
|
| 140 |
width: 70%;
|
| 141 |
padding: 8px;
|
| 142 |
font-size: 16px;
|
| 143 |
border: 1px solid #dfe1e5;
|
| 144 |
border-radius: 4px;
|
| 145 |
+
}}
|
| 146 |
+
.search-box input[type="submit"] {{
|
| 147 |
padding: 8px 16px;
|
| 148 |
font-size: 14px;
|
| 149 |
background-color: #f8f9fa;
|
| 150 |
border: 1px solid #dfe1e5;
|
| 151 |
border-radius: 4px;
|
| 152 |
cursor: pointer;
|
| 153 |
+
}}
|
| 154 |
+
.search-result {{
|
| 155 |
margin-bottom: 20px;
|
| 156 |
+
}}
|
| 157 |
+
.search-result a {{
|
| 158 |
color: #1a0dab;
|
| 159 |
font-size: 18px;
|
| 160 |
text-decoration: none;
|
| 161 |
+
}}
|
| 162 |
+
.search-result a:hover {{
|
| 163 |
text-decoration: underline;
|
| 164 |
+
}}
|
| 165 |
+
.search-result .url {{
|
| 166 |
color: #006621;
|
| 167 |
font-size: 14px;
|
| 168 |
margin: 2px 0;
|
| 169 |
+
}}
|
| 170 |
+
.search-result p {{
|
| 171 |
color: #545454;
|
| 172 |
font-size: 14px;
|
| 173 |
margin: 2px 0;
|
| 174 |
+
}}
|
| 175 |
+
.pagination {{
|
| 176 |
margin-top: 20px;
|
| 177 |
text-align: center;
|
| 178 |
+
}}
|
| 179 |
+
.pagination a, .pagination span {{
|
|
|
|
| 180 |
margin: 0 10px;
|
| 181 |
+
color: #1a0dab;
|
| 182 |
text-decoration: none;
|
| 183 |
+
}}
|
| 184 |
+
.pagination a:hover {{
|
| 185 |
text-decoration: underline;
|
| 186 |
+
}}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
</style>
|
| 188 |
</head>
|
| 189 |
<body>
|
| 190 |
<h1>LLM Search Engine</h1>
|
| 191 |
<form class="search-box" method="get" action="/">
|
| 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>
|
| 207 |
<div class="url">{url}</div>
|
|
|
|
| 209 |
</div>
|
| 210 |
"""
|
| 211 |
|
| 212 |
+
# Pagination links
|
| 213 |
+
encoded_query = quote(query)
|
| 214 |
+
prev_link = f'<a href="/?query={encoded_query}&page={page-1}">Previous</a>' if page > 1 else '<span>Previous</span>'
|
| 215 |
+
next_link = f'<a href="/?query={encoded_query}&page={page+1}">Next</a>' if page < total_pages else '<span>Next</span>'
|
| 216 |
+
html_content += f"""
|
| 217 |
+
</div>
|
| 218 |
+
<div class="pagination">
|
| 219 |
+
{prev_link}
|
| 220 |
+
<span>Page {page} of {total_pages}</span>
|
| 221 |
+
{next_link}
|
| 222 |
+
</div>
|
|
|
|
|
|
|
|
|
|
| 223 |
</body>
|
| 224 |
</html>
|
| 225 |
"""
|
| 226 |
|
| 227 |
+
return html_content
|
| 228 |
|
| 229 |
+
# Define the Gradio app with Blocks
|
| 230 |
with gr.Blocks(title="LLM Search Engine") as app:
|
| 231 |
+
def handle_request(request: gr.Request):
|
| 232 |
+
"""Handle the incoming request and return the HTML response."""
|
| 233 |
+
query = request.query_params.get("query", "")
|
| 234 |
try:
|
| 235 |
+
page = int(request.query_params.get("page", "1"))
|
| 236 |
+
except ValueError:
|
| 237 |
page = 1
|
| 238 |
+
html = generate_search_page(query, page)
|
| 239 |
+
return html
|
| 240 |
|
| 241 |
+
# Define the route for the root URL
|
| 242 |
+
app.route("/", fn=handle_request)
|
|
|
|
|
|
|
| 243 |
|
| 244 |
+
# Launch the app
|
| 245 |
app.launch()
|