Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# Define the number of results per page and total results to generate
|
| 11 |
RESULTS_PER_PAGE = 10
|
|
@@ -24,13 +26,13 @@ def fetch_search_results(query):
|
|
| 24 |
"""
|
| 25 |
|
| 26 |
try:
|
| 27 |
-
response =
|
| 28 |
model="gemini-2.0-flash-lite", # 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
|
|
@@ -46,12 +48,8 @@ def fetch_search_results(query):
|
|
| 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"
|
| 55 |
|
| 56 |
def display_search_results(query, page=1):
|
| 57 |
"""Display search results for the given query and page number."""
|
|
@@ -108,8 +106,6 @@ def display_search_results(query, page=1):
|
|
| 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):
|
|
@@ -139,7 +135,7 @@ with gr.Blocks(title="LLM Search Engine") as app:
|
|
| 139 |
outputs=[output_html, page_state]
|
| 140 |
)
|
| 141 |
|
| 142 |
-
#
|
| 143 |
with gr.Row():
|
| 144 |
prev_button = gr.Button("Previous", visible=False)
|
| 145 |
next_button = gr.Button("Next", visible=False)
|
|
|
|
| 1 |
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(
|
| 8 |
+
api_key=os.environ["OPENAI_API_KEY"],
|
| 9 |
+
base_url=os.environ["OPENAI_BASE_URL"]
|
| 10 |
+
)
|
| 11 |
|
| 12 |
# Define the number of results per page and total results to generate
|
| 13 |
RESULTS_PER_PAGE = 10
|
|
|
|
| 26 |
"""
|
| 27 |
|
| 28 |
try:
|
| 29 |
+
response = client.chat.completions.create(
|
| 30 |
model="gemini-2.0-flash-lite", # Adjust model name as needed
|
| 31 |
messages=[
|
| 32 |
{"role": "system", "content": "You are a helpful search engine."},
|
| 33 |
{"role": "user", "content": prompt}
|
| 34 |
],
|
| 35 |
+
response_format={"type": "json_object"} # Updated for latest SDK
|
| 36 |
)
|
| 37 |
|
| 38 |
content = response.choices[0].message.content
|
|
|
|
| 48 |
|
| 49 |
return results, None
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
except Exception as e:
|
| 52 |
+
return None, f"Error: {str(e)}"
|
| 53 |
|
| 54 |
def display_search_results(query, page=1):
|
| 55 |
"""Display search results for the given query and page number."""
|
|
|
|
| 106 |
html += f'<button onclick="update_page({page + 1})">Next</button>'
|
| 107 |
html += '</div></div>'
|
| 108 |
|
|
|
|
|
|
|
| 109 |
return html, page - 1 if page > 1 else None, page + 1 if page < total_pages else None
|
| 110 |
|
| 111 |
def search_handler(query, page):
|
|
|
|
| 135 |
outputs=[output_html, page_state]
|
| 136 |
)
|
| 137 |
|
| 138 |
+
# Pagination buttons
|
| 139 |
with gr.Row():
|
| 140 |
prev_button = gr.Button("Previous", visible=False)
|
| 141 |
next_button = gr.Button("Next", visible=False)
|