Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,27 +4,55 @@ from vectordb import Memory
|
|
4 |
# Initialize Memory
|
5 |
memory = Memory()
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
)
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
# Define a function for querying
|
14 |
-
def search_query(query):
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
17 |
|
18 |
# Create Gradio interface
|
19 |
with gr.Blocks() as demo:
|
20 |
-
gr.Markdown("### VectorDB Search")
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
with gr.Row():
|
23 |
input_query = gr.Textbox(label="Enter your query")
|
|
|
24 |
output_result = gr.Textbox(label="Search Results", interactive=False)
|
25 |
|
26 |
search_button = gr.Button("Search")
|
27 |
-
search_button.click(search_query, inputs=input_query, outputs=output_result)
|
28 |
|
29 |
# Run the Gradio app
|
30 |
demo.launch()
|
|
|
4 |
# Initialize Memory
|
5 |
memory = Memory()
|
6 |
|
7 |
+
# Define a function to save new text and metadata
|
8 |
+
def save_data(texts, metadata):
|
9 |
+
try:
|
10 |
+
# Split texts and metadata by lines for simplicity
|
11 |
+
text_list = texts.strip().split("\n")
|
12 |
+
metadata_list = [eval(meta.strip()) for meta in metadata.strip().split("\n")]
|
13 |
+
memory.save(text_list, metadata_list)
|
14 |
+
return "Data saved successfully!"
|
15 |
+
except Exception as e:
|
16 |
+
return f"Error saving data: {e}"
|
17 |
|
18 |
# Define a function for querying
|
19 |
+
def search_query(query, top_n):
|
20 |
+
try:
|
21 |
+
results = memory.search(query, top_n=int(top_n)) # Search for top_n results
|
22 |
+
return results
|
23 |
+
except Exception as e:
|
24 |
+
return f"Error during search: {e}"
|
25 |
|
26 |
# Create Gradio interface
|
27 |
with gr.Blocks() as demo:
|
28 |
+
gr.Markdown("### VectorDB Search App")
|
29 |
|
30 |
+
# Save Data Section
|
31 |
+
gr.Markdown("#### Save Data")
|
32 |
+
with gr.Row():
|
33 |
+
input_texts = gr.Textbox(
|
34 |
+
label="Enter text (one per line)",
|
35 |
+
lines=5,
|
36 |
+
placeholder="Example:\napples are green\noranges are orange"
|
37 |
+
)
|
38 |
+
input_metadata = gr.Textbox(
|
39 |
+
label="Enter metadata (one per line, matching the texts)",
|
40 |
+
lines=5,
|
41 |
+
placeholder='Example:\n{"url": "https://apples.com"}\n{"url": "https://oranges.com"}'
|
42 |
+
)
|
43 |
+
save_button = gr.Button("Save Data")
|
44 |
+
save_status = gr.Textbox(label="Status", interactive=False)
|
45 |
+
save_button.click(save_data, inputs=[input_texts, input_metadata], outputs=save_status)
|
46 |
+
|
47 |
+
# Search Section
|
48 |
+
gr.Markdown("#### Search")
|
49 |
with gr.Row():
|
50 |
input_query = gr.Textbox(label="Enter your query")
|
51 |
+
input_top_n = gr.Number(label="Top N results", value=1)
|
52 |
output_result = gr.Textbox(label="Search Results", interactive=False)
|
53 |
|
54 |
search_button = gr.Button("Search")
|
55 |
+
search_button.click(search_query, inputs=[input_query, input_top_n], outputs=output_result)
|
56 |
|
57 |
# Run the Gradio app
|
58 |
demo.launch()
|