Spaces:
Sleeping
Sleeping
| 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() |