Jokica17 commited on
Commit
15b0ad3
·
1 Parent(s): cd20a25

- Added frontend gradio app in `fe/gradio_app.py` and its dependencies within `fe/requirements.txt`

Browse files
Files changed (3) hide show
  1. config.py +10 -0
  2. fe/gradio_app.py +56 -0
  3. fe/requirements.txt +2 -0
config.py ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ # Configuration for Backend and Frontend
2
+
3
+ # TODO: add FRONTEND_HOST and change BACKEND_HOST to work both local and within docker container
4
+ # Backend Configuration
5
+ BACKEND_HOST = "backend" # "0.0.0.0"
6
+ BACKEND_PORT = 8000
7
+
8
+ # Frontend Configuration
9
+ FRONTEND_PORT = 7860
10
+ APP_NAME = "Prompt Search Engine"
fe/gradio_app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import sys
2
+ import os
3
+ import gradio as gr
4
+ import requests
5
+ from config import BACKEND_HOST, BACKEND_PORT, FRONTEND_PORT, APP_NAME
6
+
7
+
8
+ # Add the project root directory to the Python path
9
+ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10
+ API_URL = f"http://{BACKEND_HOST}:{BACKEND_PORT}/search"
11
+
12
+
13
+ def search_prompt(query: str, n: int) -> list:
14
+ """
15
+ Query the backend search API and format the results.
16
+ Args:
17
+ query: The search query.
18
+ n: The number of results to return.
19
+ Returns:
20
+ list: A list of formatted results, or an error message
21
+ """
22
+ try:
23
+ response = requests.get(API_URL, params={"query": query, "n": n})
24
+ response.raise_for_status()
25
+ data = response.json()
26
+
27
+ # Extract and sort results
28
+ results = data["results"]
29
+ sorted_results = sorted(results, key=lambda x: x[0], reverse=True)
30
+
31
+ # Format results in a table
32
+ tabular_results = [[result[1], f"{result[0]:.4f}"] for result in sorted_results]
33
+ return tabular_results
34
+
35
+ except Exception as e:
36
+ return [["Error", str(e)]]
37
+
38
+
39
+ with gr.Blocks() as demo:
40
+ gr.Markdown(f"# {APP_NAME}")
41
+
42
+ query = gr.Textbox(label="Enter your query:")
43
+ n = gr.Number(label="Number of top results", value=5, precision=0)
44
+ output = gr.Dataframe(
45
+ headers=["Prompt", "Similarity"],
46
+ datatype=["str", "str"],
47
+ interactive=False,
48
+ label=f"Top results"
49
+ )
50
+
51
+ search_button = gr.Button("Search")
52
+ search_button.click(search_prompt, inputs=[query, n], outputs=[output])
53
+
54
+
55
+ demo.launch(server_name="0.0.0.0", server_port=FRONTEND_PORT)
56
+
fe/requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ requests