Spaces:
Running
Running
Added validation to frontend
Browse files- fe/gradio_app.py +17 -1
fe/gradio_app.py
CHANGED
@@ -36,6 +36,22 @@ def search_prompt(query: str, n: int) -> list:
|
|
36 |
return [["Error", str(e)]]
|
37 |
|
38 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
with gr.Blocks() as demo:
|
40 |
gr.Markdown(f"# {APP_NAME}")
|
41 |
|
@@ -49,7 +65,7 @@ with gr.Blocks() as demo:
|
|
49 |
)
|
50 |
|
51 |
search_button = gr.Button("Search")
|
52 |
-
search_button.click(
|
53 |
|
54 |
|
55 |
demo.launch(server_name="0.0.0.0", server_port=FRONTEND_PORT)
|
|
|
36 |
return [["Error", str(e)]]
|
37 |
|
38 |
|
39 |
+
def validate_and_search(query: str, n: int) -> list:
|
40 |
+
"""
|
41 |
+
Validate inputs and perform the search if inputs are valid.
|
42 |
+
Args:
|
43 |
+
query: The search query.
|
44 |
+
n: The number of results to return.
|
45 |
+
Returns:
|
46 |
+
list: A list of results or an error message.
|
47 |
+
"""
|
48 |
+
if not query.strip():
|
49 |
+
return [["Error", "Your input is empty, please enter a query."]]
|
50 |
+
if n <= 0 or not isinstance(n, int):
|
51 |
+
return [["Error", "Invalid number of top results. Please enter a positive number."]]
|
52 |
+
# Call the backend search function
|
53 |
+
return search_prompt(query, n)
|
54 |
+
|
55 |
with gr.Blocks() as demo:
|
56 |
gr.Markdown(f"# {APP_NAME}")
|
57 |
|
|
|
65 |
)
|
66 |
|
67 |
search_button = gr.Button("Search")
|
68 |
+
search_button.click(validate_and_search, inputs=[query, n], outputs=[output])
|
69 |
|
70 |
|
71 |
demo.launch(server_name="0.0.0.0", server_port=FRONTEND_PORT)
|