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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
+
# Set OpenAI API key and base URL from environment variables
|
| 7 |
+
openai.api_key = os.environ["OPENAI_API_KEY"]
|
| 8 |
+
openai.base_url = os.environ["OPENAI_BASE_URL"]
|
| 9 |
+
|
| 10 |
+
# Define the number of results per page and total results to generate
|
| 11 |
+
RESULTS_PER_PAGE = 10
|
| 12 |
+
TOTAL_RESULTS = 30 # Generate 30 results to allow pagination
|
| 13 |
+
|
| 14 |
+
def fetch_search_results(query):
|
| 15 |
+
"""Fetch search results from the LLM based on the user's query."""
|
| 16 |
+
if not query.strip():
|
| 17 |
+
return None, "Please enter a search query."
|
| 18 |
+
|
| 19 |
+
prompt = f"""
|
| 20 |
+
You are a search engine that provides informative and relevant results. For the given query '{query}',
|
| 21 |
+
generate {TOTAL_RESULTS} search results, each with a title and a snippet that summarizes the information.
|
| 22 |
+
Format the response as a JSON array of objects, where each object has 'title' and 'snippet' fields.
|
| 23 |
+
Ensure the results are diverse and relevant to the query.
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
response = openai.ChatCompletion.create(
|
| 28 |
+
model="gpt-3.5-turbo", # Adjust model name as needed
|
| 29 |
+
messages=[
|
| 30 |
+
{"role": "system", "content": "You are a helpful search engine."},
|
| 31 |
+
{"role": "user", "content": prompt}
|
| 32 |
+
],
|
| 33 |
+
response_format="json_object"
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
content = response.choices[0].message.content
|
| 37 |
+
results = json.loads(content)
|
| 38 |
+
|
| 39 |
+
# Handle different possible JSON structures
|
| 40 |
+
if isinstance(results, dict) and "results" in results:
|
| 41 |
+
results = results["results"]
|
| 42 |
+
elif isinstance(results, list):
|
| 43 |
+
pass
|
| 44 |
+
else:
|
| 45 |
+
return None, "Error: Unexpected JSON structure."
|
| 46 |
+
|
| 47 |
+
return results, None
|
| 48 |
+
|
| 49 |
+
except openai.error.OpenAIError as e:
|
| 50 |
+
return None, f"Error: {str(e)}"
|
| 51 |
+
except json.JSONDecodeError:
|
| 52 |
+
return None, "Error: Failed to parse JSON response."
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return None, f"Unexpected error: {str(e)}"
|
| 55 |
+
|
| 56 |
+
def display_search_results(query, page=1):
|
| 57 |
+
"""Display search results for the given query and page number."""
|
| 58 |
+
results, error = fetch_search_results(query)
|
| 59 |
+
|
| 60 |
+
if error:
|
| 61 |
+
return error, None, None
|
| 62 |
+
|
| 63 |
+
# Calculate pagination boundaries
|
| 64 |
+
start_idx = (page - 1) * RESULTS_PER_PAGE
|
| 65 |
+
end_idx = start_idx + RESULTS_PER_PAGE
|
| 66 |
+
total_pages = (len(results) + RESULTS_PER_PAGE - 1) // RESULTS_PER_PAGE
|
| 67 |
+
|
| 68 |
+
# Ensure indices are within bounds
|
| 69 |
+
if start_idx >= len(results):
|
| 70 |
+
return "No more results to display.", None, None
|
| 71 |
+
|
| 72 |
+
paginated_results = results[start_idx:end_idx]
|
| 73 |
+
|
| 74 |
+
# Format results into HTML
|
| 75 |
+
html = """
|
| 76 |
+
<style>
|
| 77 |
+
.search-result {
|
| 78 |
+
margin-bottom: 20px;
|
| 79 |
+
}
|
| 80 |
+
.search-result h3 {
|
| 81 |
+
color: blue;
|
| 82 |
+
font-size: 18px;
|
| 83 |
+
margin: 0;
|
| 84 |
+
}
|
| 85 |
+
.search-result p {
|
| 86 |
+
font-size: 14px;
|
| 87 |
+
margin: 5px 0 0 0;
|
| 88 |
+
}
|
| 89 |
+
.pagination {
|
| 90 |
+
margin-top: 20px;
|
| 91 |
+
}
|
| 92 |
+
</style>
|
| 93 |
+
<div>
|
| 94 |
+
"""
|
| 95 |
+
html += f"<h2>Search Results for '{query}' (Page {page} of {total_pages})</h2>"
|
| 96 |
+
html += "<ul>"
|
| 97 |
+
for result in paginated_results:
|
| 98 |
+
title = result.get("title", "No title")
|
| 99 |
+
snippet = result.get("snippet", "No snippet")
|
| 100 |
+
html += f'<li class="search-result"><h3>{title}</h3><p>{snippet}</p></li>'
|
| 101 |
+
html += "</ul>"
|
| 102 |
+
|
| 103 |
+
# Add pagination controls (simulated with buttons)
|
| 104 |
+
html += '<div class="pagination">'
|
| 105 |
+
if page > 1:
|
| 106 |
+
html += f'<button onclick="update_page({page - 1})">Previous</button>'
|
| 107 |
+
if page < total_pages:
|
| 108 |
+
html += f'<button onclick="update_page({page + 1})">Next</button>'
|
| 109 |
+
html += '</div></div>'
|
| 110 |
+
|
| 111 |
+
# Note: Gradio doesn't support interactive JS directly in HTML outputs,
|
| 112 |
+
# so we return page numbers for button functionality
|
| 113 |
+
return html, page - 1 if page > 1 else None, page + 1 if page < total_pages else None
|
| 114 |
+
|
| 115 |
+
def search_handler(query, page):
|
| 116 |
+
"""Handle search submission and pagination."""
|
| 117 |
+
html, prev_page, next_page = display_search_results(query, page)
|
| 118 |
+
return html
|
| 119 |
+
|
| 120 |
+
# Build Gradio interface with Blocks for state management
|
| 121 |
+
with gr.Blocks(title="LLM Search Engine") as app:
|
| 122 |
+
gr.Markdown("# LLM Search Engine")
|
| 123 |
+
gr.Markdown("Enter a query below to search using a large language model.")
|
| 124 |
+
|
| 125 |
+
query_input = gr.Textbox(label="Search Query", placeholder="Type your search here...")
|
| 126 |
+
search_button = gr.Button("Search")
|
| 127 |
+
output_html = gr.HTML()
|
| 128 |
+
|
| 129 |
+
# Hidden state to track current page
|
| 130 |
+
page_state = gr.State(value=1)
|
| 131 |
+
|
| 132 |
+
# Define submit behavior
|
| 133 |
+
def on_submit(query, page):
|
| 134 |
+
return search_handler(query, page), page
|
| 135 |
+
|
| 136 |
+
search_button.click(
|
| 137 |
+
fn=on_submit,
|
| 138 |
+
inputs=[query_input, page_state],
|
| 139 |
+
outputs=[output_html, page_state]
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# Note: For full pagination, we simulate Previous/Next with additional buttons
|
| 143 |
+
with gr.Row():
|
| 144 |
+
prev_button = gr.Button("Previous", visible=False)
|
| 145 |
+
next_button = gr.Button("Next", visible=False)
|
| 146 |
+
|
| 147 |
+
def update_page(query, page, direction):
|
| 148 |
+
new_page = page + direction
|
| 149 |
+
html, prev_page, next_page = display_search_results(query, new_page)
|
| 150 |
+
return html, new_page, gr.update(visible=prev_page is not None), gr.update(visible=next_page is not None)
|
| 151 |
+
|
| 152 |
+
prev_button.click(
|
| 153 |
+
fn=lambda q, p: update_page(q, p, -1),
|
| 154 |
+
inputs=[query_input, page_state],
|
| 155 |
+
outputs=[output_html, page_state, prev_button, next_button]
|
| 156 |
+
)
|
| 157 |
+
|
| 158 |
+
next_button.click(
|
| 159 |
+
fn=lambda q, p: update_page(q, p, 1),
|
| 160 |
+
inputs=[query_input, page_state],
|
| 161 |
+
outputs=[output_html, page_state, prev_button, next_button]
|
| 162 |
+
)
|
| 163 |
+
|
| 164 |
+
# Update button visibility after search
|
| 165 |
+
search_button.click(
|
| 166 |
+
fn=lambda q, p: (search_handler(q, p), p, gr.update(visible=p > 1), gr.update(visible=True)),
|
| 167 |
+
inputs=[query_input, page_state],
|
| 168 |
+
outputs=[output_html, page_state, prev_button, next_button]
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
app.launch()
|