Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,58 +1,28 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
from vectordb import Memory
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
memory = Memory()
|
| 6 |
-
|
| 7 |
-
# Define a function to save new text and metadata
|
| 8 |
-
def save_data(texts, metadata):
|
| 9 |
try:
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
memory.save(
|
| 14 |
-
return "Data saved successfully!"
|
| 15 |
-
except Exception as e:
|
| 16 |
-
return f"Error saving data: {e}"
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
return
|
| 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("
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 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 |
-
#
|
| 58 |
demo.launch()
|
|
|
|
|
|
|
|
|
| 1 |
from vectordb import Memory
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import json
|
| 4 |
|
| 5 |
+
def process_json(json_input):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
try:
|
| 7 |
+
input = json.loads(json_input)
|
| 8 |
+
|
| 9 |
+
memory = Memory(embedding_model="TaylorAI/bge-micro-v2")
|
| 10 |
+
memory.save(input['terms'], input['metadata'])
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
results = memory.search(input['prompt'], top_n=input['topN'])
|
| 13 |
+
|
| 14 |
+
return json.dumps(results, indent=4)
|
| 15 |
+
except json.JSONDecodeError:
|
| 16 |
+
return "Invalid JSON input."
|
|
|
|
|
|
|
| 17 |
|
|
|
|
| 18 |
with gr.Blocks() as demo:
|
| 19 |
+
gr.Markdown("## *VectorDB* based Paragraph Embedder")
|
| 20 |
+
input_json = gr.Textbox(label="Input", lines=10, placeholder='{"topN": 5, "prompt": "yellow", "metadata": [], "terms": ["banana", "blueberry", "apple"]}')
|
| 21 |
+
output_json = gr.Textbox(label="Output", lines=10, interactive=False)
|
| 22 |
+
process_button = gr.Button("Process")
|
| 23 |
|
| 24 |
+
process_button.click(process_json, inputs=input_json, outputs=output_json)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Launch the app
|
| 27 |
demo.launch()
|
| 28 |
+
|