Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -11,14 +11,14 @@ if not os.path.exists(DATA_FILE):
|
|
| 11 |
json.dump([], file) # Initialize empty list
|
| 12 |
|
| 13 |
# Load existing data
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
# Function to add a new query
|
| 18 |
def add_query(category, query, positive1, positive2, negative1, negative2):
|
| 19 |
-
|
| 20 |
|
| 21 |
-
# Create structured entry
|
| 22 |
new_entry = {
|
| 23 |
"Category": category,
|
| 24 |
"Query": query,
|
|
@@ -28,32 +28,25 @@ def add_query(category, query, positive1, positive2, negative1, negative2):
|
|
| 28 |
"Negative_2": negative2
|
| 29 |
}
|
| 30 |
|
| 31 |
-
|
| 32 |
|
| 33 |
-
# Save to JSON file
|
| 34 |
with open(DATA_FILE, "w") as file:
|
| 35 |
-
json.dump(
|
| 36 |
-
|
| 37 |
-
return f"✅ Added query: {query} under category '{category}' with responses."
|
| 38 |
|
| 39 |
-
|
| 40 |
-
def download_json():
|
| 41 |
-
with open(DATA_FILE, "r") as file:
|
| 42 |
-
return file.read()
|
| 43 |
|
| 44 |
# Gradio UI
|
| 45 |
with gr.Blocks() as app:
|
| 46 |
gr.Markdown("# 📝 Query Storage App with Responses")
|
| 47 |
-
gr.Markdown("### Enter queries and their responses to store in a JSON file.")
|
| 48 |
-
|
| 49 |
-
category_input = gr.Textbox(label="Category", placeholder="Enter category (e.g., Rhyme, Aviation and Math)")
|
| 50 |
-
query_input = gr.Textbox(label="Query", placeholder="Enter query text", lines=2)
|
| 51 |
|
| 52 |
-
gr.
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
| 57 |
|
| 58 |
submit_button = gr.Button("Add Query")
|
| 59 |
status_output = gr.Textbox(label="Status", interactive=False)
|
|
@@ -64,10 +57,7 @@ with gr.Blocks() as app:
|
|
| 64 |
outputs=status_output
|
| 65 |
)
|
| 66 |
|
| 67 |
-
gr.Markdown("###
|
| 68 |
-
download_button = gr.
|
| 69 |
-
download_output = gr.File(label="Download File")
|
| 70 |
-
|
| 71 |
-
download_button.click(download_json, outputs=download_output)
|
| 72 |
|
| 73 |
app.launch()
|
|
|
|
| 11 |
json.dump([], file) # Initialize empty list
|
| 12 |
|
| 13 |
# Load existing data
|
| 14 |
+
def load_data():
|
| 15 |
+
with open(DATA_FILE, "r") as file:
|
| 16 |
+
return json.load(file)
|
| 17 |
|
| 18 |
+
# Function to add a new query
|
| 19 |
def add_query(category, query, positive1, positive2, negative1, negative2):
|
| 20 |
+
data = load_data() # Load latest data
|
| 21 |
|
|
|
|
| 22 |
new_entry = {
|
| 23 |
"Category": category,
|
| 24 |
"Query": query,
|
|
|
|
| 28 |
"Negative_2": negative2
|
| 29 |
}
|
| 30 |
|
| 31 |
+
data.append(new_entry)
|
| 32 |
|
| 33 |
+
# Save updated data to JSON file
|
| 34 |
with open(DATA_FILE, "w") as file:
|
| 35 |
+
json.dump(data, file, indent=4)
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
return f"✅ Added query: {query} under category '{category}'"
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
# Gradio UI
|
| 40 |
with gr.Blocks() as app:
|
| 41 |
gr.Markdown("# 📝 Query Storage App with Responses")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
+
category_input = gr.Textbox(label="Category", placeholder="Enter category")
|
| 44 |
+
query_input = gr.Textbox(label="Query", placeholder="Enter query text")
|
| 45 |
+
|
| 46 |
+
positive1_input = gr.Textbox(label="Positive Response 1", placeholder="Enter first positive response")
|
| 47 |
+
positive2_input = gr.Textbox(label="Positive Response 2", placeholder="Enter second positive response")
|
| 48 |
+
negative1_input = gr.Textbox(label="Negative Response 1", placeholder="Enter first negative response")
|
| 49 |
+
negative2_input = gr.Textbox(label="Negative Response 2", placeholder="Enter second negative response")
|
| 50 |
|
| 51 |
submit_button = gr.Button("Add Query")
|
| 52 |
status_output = gr.Textbox(label="Status", interactive=False)
|
|
|
|
| 57 |
outputs=status_output
|
| 58 |
)
|
| 59 |
|
| 60 |
+
gr.Markdown("### Download Stored Queries:")
|
| 61 |
+
download_button = gr.File(DATA_FILE, label="Download JSON")
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
app.launch()
|