Spaces:
Sleeping
Sleeping
File size: 2,567 Bytes
d45d6f9 db4566a d45d6f9 03a4fde db4566a 03a4fde db4566a 03a4fde 312b004 03a4fde db4566a 312b004 6b99753 312b004 d45d6f9 6b99753 312b004 d45d6f9 312b004 d45d6f9 312b004 1109795 b228632 03a4fde d45d6f9 db4566a d45d6f9 6b99753 312b004 d45d6f9 6b99753 db4566a 6b99753 b228632 6b99753 d45d6f9 312b004 03a4fde d45d6f9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
import gradio as gr
import json
import os
# Define JSON storage file
DATA_FILE = "queries.json"
# Ensure JSON file exists
if not os.path.exists(DATA_FILE):
with open(DATA_FILE, "w") as file:
json.dump([], file) # Initialize an empty list
# Function to load existing data
def load_data():
if os.path.exists(DATA_FILE):
with open(DATA_FILE, "r") as file:
return json.load(file)
return []
# Function to add a new query
def add_query(category, query, positive1, positive2, negative1, negative2):
data = load_data() # Load latest data
new_entry = {
"Category": category,
"Query": query,
"Positive_1": positive1,
"Positive_2": positive2,
"Negative_1": negative1,
"Negative_2": negative2
}
data.append(new_entry)
# Save updated data to JSON file
with open(DATA_FILE, "w") as file:
json.dump(data, file, indent=4)
# Return success message and clear all inputs
return (
f"✅ Query added under category '{category}'!",
"", "", "", "", "", "" # Clearing all input fields
)
# Function to prepare the JSON file for download
def prepare_download():
return DATA_FILE
# Gradio UI
with gr.Blocks() as app:
gr.Markdown("# 📝 Query Storage App with Responses")
category_input = gr.Textbox(label="Category", placeholder="Enter category")
query_input = gr.Textbox(label="Query", placeholder="Enter query text")
positive1_input = gr.Textbox(label="Positive Response 1", placeholder="Enter first positive response")
positive2_input = gr.Textbox(label="Positive Response 2", placeholder="Enter second positive response")
negative1_input = gr.Textbox(label="Negative Response 1", placeholder="Enter first negative response")
negative2_input = gr.Textbox(label="Negative Response 2", placeholder="Enter second negative response")
submit_button = gr.Button("Add Query")
status_output = gr.Textbox(label="Status", interactive=False)
submit_button.click(
add_query,
inputs=[category_input, query_input, positive1_input, positive2_input, negative1_input, negative2_input],
outputs=[status_output, category_input, query_input, positive1_input, positive2_input, negative1_input, negative2_input] # Clears inputs
)
gr.Markdown("### Download Stored Queries:")
download_button = gr.Button("Download JSON")
download_output = gr.File(label="Download File")
download_button.click(prepare_download, outputs=download_output)
app.launch() |