Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
import json
|
|
|
|
| 5 |
|
| 6 |
# Initialize OpenAI client with API key and base URL from environment variables
|
| 7 |
client = OpenAI(
|
|
@@ -20,14 +21,17 @@ def fetch_search_results(query):
|
|
| 20 |
|
| 21 |
prompt = f"""
|
| 22 |
You are a search engine that provides informative and relevant results. For the given query '{query}',
|
| 23 |
-
generate {TOTAL_RESULTS} search results
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
"""
|
| 27 |
|
| 28 |
try:
|
| 29 |
response = client.chat.completions.create(
|
| 30 |
-
model="gemini-2.0-flash-lite", #
|
| 31 |
messages=[
|
| 32 |
{"role": "system", "content": "You are a helpful search engine."},
|
| 33 |
{"role": "user", "content": prompt}
|
|
@@ -57,12 +61,20 @@ def fetch_search_results(query):
|
|
| 57 |
else:
|
| 58 |
return None, f"Error: {error_msg}"
|
| 59 |
|
| 60 |
-
def
|
| 61 |
-
"""
|
| 62 |
results, error = fetch_search_results(query)
|
| 63 |
|
| 64 |
if error:
|
| 65 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
# Calculate pagination boundaries
|
| 68 |
start_idx = (page - 1) * RESULTS_PER_PAGE
|
|
@@ -71,118 +83,145 @@ def display_search_results(query, page=1):
|
|
| 71 |
|
| 72 |
# Ensure indices are within bounds
|
| 73 |
if start_idx >= len(results):
|
| 74 |
-
return "
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
paginated_results = results[start_idx:end_idx]
|
| 77 |
|
| 78 |
-
#
|
| 79 |
html = """
|
| 80 |
-
<
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
"""
|
| 99 |
-
|
| 100 |
-
|
| 101 |
for result in paginated_results:
|
| 102 |
title = result.get("title", "No title")
|
| 103 |
snippet = result.get("snippet", "No snippet")
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
html += '<div class="pagination">'
|
| 109 |
if page > 1:
|
| 110 |
-
html += f'<
|
|
|
|
|
|
|
|
|
|
| 111 |
if page < total_pages:
|
| 112 |
-
html += f'<
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
return html, page - 1 if page > 1 else None, page + 1 if page < total_pages else None
|
| 116 |
-
|
| 117 |
-
def search_handler(query, page):
|
| 118 |
-
"""Handle search submission and pagination."""
|
| 119 |
-
html, prev_page, next_page = display_search_results(query, page)
|
| 120 |
return html
|
| 121 |
|
| 122 |
-
#
|
| 123 |
with gr.Blocks(title="LLM Search Engine") as app:
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
return search_handler(query, page), page
|
| 137 |
-
|
| 138 |
-
# Trigger search on Enter key or button click
|
| 139 |
-
query_input.submit(
|
| 140 |
-
fn=on_submit,
|
| 141 |
-
inputs=[query_input, page_state],
|
| 142 |
-
outputs=[output_html, page_state]
|
| 143 |
-
)
|
| 144 |
-
search_button.click(
|
| 145 |
-
fn=on_submit,
|
| 146 |
-
inputs=[query_input, page_state],
|
| 147 |
-
outputs=[output_html, page_state]
|
| 148 |
-
)
|
| 149 |
-
|
| 150 |
-
# Pagination buttons
|
| 151 |
-
with gr.Row():
|
| 152 |
-
prev_button = gr.Button("Previous", visible=False)
|
| 153 |
-
next_button = gr.Button("Next", visible=False)
|
| 154 |
-
|
| 155 |
-
def update_page(query, page, direction):
|
| 156 |
-
new_page = page + direction
|
| 157 |
-
html, prev_page, next_page = display_search_results(query, new_page)
|
| 158 |
-
return html, new_page, gr.update(visible=prev_page is not None), gr.update(visible=next_page is not None)
|
| 159 |
-
|
| 160 |
-
prev_button.click(
|
| 161 |
-
fn=lambda q, p: update_page(q, p, -1),
|
| 162 |
-
inputs=[query_input, page_state],
|
| 163 |
-
outputs=[output_html, page_state, prev_button, next_button]
|
| 164 |
-
)
|
| 165 |
-
|
| 166 |
-
next_button.click(
|
| 167 |
-
fn=lambda q, p: update_page(q, p, 1),
|
| 168 |
-
inputs=[query_input, page_state],
|
| 169 |
-
outputs=[output_html, page_state, prev_button, next_button]
|
| 170 |
-
)
|
| 171 |
-
|
| 172 |
-
# Update button visibility after search
|
| 173 |
-
def update_visibility(query, page):
|
| 174 |
-
html, prev_page, next_page = display_search_results(query, page)
|
| 175 |
-
return html, page, gr.update(visible=prev_page is not None), gr.update(visible=next_page is not None)
|
| 176 |
-
|
| 177 |
-
query_input.submit(
|
| 178 |
-
fn=update_visibility,
|
| 179 |
-
inputs=[query_input, page_state],
|
| 180 |
-
outputs=[output_html, page_state, prev_button, next_button]
|
| 181 |
-
)
|
| 182 |
-
search_button.click(
|
| 183 |
-
fn=update_visibility,
|
| 184 |
-
inputs=[query_input, page_state],
|
| 185 |
-
outputs=[output_html, page_state, prev_button, next_button]
|
| 186 |
-
)
|
| 187 |
|
| 188 |
app.launch()
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
+
from functools import partial
|
| 6 |
|
| 7 |
# Initialize OpenAI client with API key and base URL from environment variables
|
| 8 |
client = OpenAI(
|
|
|
|
| 21 |
|
| 22 |
prompt = f"""
|
| 23 |
You are a search engine that provides informative and relevant results. For the given query '{query}',
|
| 24 |
+
generate {TOTAL_RESULTS} search results. Each result should include:
|
| 25 |
+
- 'title': A concise, descriptive title of the result.
|
| 26 |
+
- 'snippet': A short summary (2-3 sentences) of the content.
|
| 27 |
+
- 'url': A plausible, clickable URL where the information might be found (e.g., a real or hypothetical website).
|
| 28 |
+
Format the response as a JSON array of objects, where each object has 'title', 'snippet', and 'url' fields.
|
| 29 |
+
Ensure the results are diverse, relevant to the query, and the URLs are realistic (e.g., https://example.com/page).
|
| 30 |
"""
|
| 31 |
|
| 32 |
try:
|
| 33 |
response = client.chat.completions.create(
|
| 34 |
+
model="gemini-2.0-flash-lite", # Updated model name
|
| 35 |
messages=[
|
| 36 |
{"role": "system", "content": "You are a helpful search engine."},
|
| 37 |
{"role": "user", "content": prompt}
|
|
|
|
| 61 |
else:
|
| 62 |
return None, f"Error: {error_msg}"
|
| 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:
|
| 69 |
+
return f"""
|
| 70 |
+
<html>
|
| 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>
|
| 77 |
+
"""
|
| 78 |
|
| 79 |
# Calculate pagination boundaries
|
| 80 |
start_idx = (page - 1) * RESULTS_PER_PAGE
|
|
|
|
| 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>
|
| 94 |
+
"""
|
| 95 |
|
| 96 |
paginated_results = results[start_idx:end_idx]
|
| 97 |
|
| 98 |
+
# Generate full HTML page
|
| 99 |
html = """
|
| 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}" placeholder="Type your search here...">
|
| 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 |
+
html += f"""
|
| 185 |
+
<div class="search-result">
|
| 186 |
+
<a href="{url}" target="_blank">{title}</a>
|
| 187 |
+
<div class="url">{url}</div>
|
| 188 |
+
<p>{snippet}</p>
|
| 189 |
+
</div>
|
| 190 |
+
"""
|
| 191 |
+
|
| 192 |
+
# Add pagination
|
| 193 |
html += '<div class="pagination">'
|
| 194 |
if page > 1:
|
| 195 |
+
html += f'<a href="/?query={query}&page={page - 1}">Previous</a>'
|
| 196 |
+
else:
|
| 197 |
+
html += '<span>Previous</span>'
|
| 198 |
+
html += f'<span>Page {page} of {total_pages}</span>'
|
| 199 |
if page < total_pages:
|
| 200 |
+
html += f'<a href="/?query={query}&page={page + 1}">Next</a>'
|
| 201 |
+
else:
|
| 202 |
+
html += '<span>Next</span>'
|
| 203 |
+
html += '</div>'
|
| 204 |
+
|
| 205 |
+
html += """
|
| 206 |
+
</body>
|
| 207 |
+
</html>
|
| 208 |
+
"""
|
| 209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
return html
|
| 211 |
|
| 212 |
+
# Define the app with Blocks
|
| 213 |
with gr.Blocks(title="LLM Search Engine") as app:
|
| 214 |
+
# Custom route handler
|
| 215 |
+
def handle_request(query, page):
|
| 216 |
+
try:
|
| 217 |
+
page = int(page) if page else 1
|
| 218 |
+
except (ValueError, TypeError):
|
| 219 |
+
page = 1
|
| 220 |
+
return generate_search_page(query, page)
|
| 221 |
+
|
| 222 |
+
# Use a Route to serve raw HTML
|
| 223 |
+
app.route("/", inputs=[gr.Textbox(visible=False, value=""), gr.Number(visible=False, value=1)],
|
| 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()
|