benchang323 commited on
Commit
db4566a
·
verified ·
1 Parent(s): d45d6f9

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -25
app.py CHANGED
@@ -2,44 +2,46 @@ import gradio as gr
2
  import json
3
  import os
4
 
5
- # JSON file to store data
6
  DATA_FILE = "queries.json"
7
 
8
- # Load existing data if available
9
- if os.path.exists(DATA_FILE):
10
- with open(DATA_FILE, "r") as file:
11
- stored_data = json.load(file)
12
- else:
13
- stored_data = []
 
 
 
 
 
 
 
14
 
15
- # Function to add a new query
16
- def add_query(category, query):
17
- new_entry = {"Category": category, "Query": query}
18
- stored_data.append(new_entry)
19
 
20
  # Save to JSON file
21
  with open(DATA_FILE, "w") as file:
22
  json.dump(stored_data, file, indent=4)
23
 
24
- return f"Added: {category} - {query}"
25
 
26
- # Function to download the stored dataset
27
- def download_json():
28
- return DATA_FILE
29
-
30
- # Gradio Interface
31
  with gr.Blocks() as app:
32
- gr.Markdown("# 📝 Query Collection App")
33
- gr.Markdown("### Manually enter queries to store in a JSON file:")
34
 
35
- category_input = gr.Textbox(label="Category")
36
- query_input = gr.Textbox(label="Query")
37
- submit_button = gr.Button("Add Query")
38
- status_output = gr.Textbox(label="Status", interactive=False)
39
 
40
- submit_button.click(add_query, inputs=[category_input, query_input], outputs=status_output)
 
41
 
42
- gr.Markdown("### Download the stored dataset:")
43
  download_button = gr.File(DATA_FILE, label="Download JSON")
44
 
45
  app.launch()
 
2
  import json
3
  import os
4
 
5
+ # Define JSON storage file
6
  DATA_FILE = "queries.json"
7
 
8
+ # Ensure the file exists
9
+ if not os.path.exists(DATA_FILE):
10
+ with open(DATA_FILE, "w") as file:
11
+ json.dump([], file) # Initialize empty list
12
+
13
+ # Load existing data
14
+ with open(DATA_FILE, "r") as file:
15
+ stored_data = json.load(file)
16
+
17
+ # Function to add bulk queries
18
+ def add_bulk_queries(category, queries):
19
+ global stored_data
20
+ queries_list = queries.split("\n") # Split input by new lines
21
 
22
+ for query in queries_list:
23
+ if query.strip(): # Avoid empty lines
24
+ stored_data.append({"Category": category, "Query": query.strip()})
 
25
 
26
  # Save to JSON file
27
  with open(DATA_FILE, "w") as file:
28
  json.dump(stored_data, file, indent=4)
29
 
30
+ return f"Added {len(queries_list)} queries under category '{category}'"
31
 
32
+ # Gradio UI
 
 
 
 
33
  with gr.Blocks() as app:
34
+ gr.Markdown("# 📝 Bulk Query Input & Storage")
35
+ gr.Markdown("### Paste multiple queries and store them in a JSON file.")
36
 
37
+ category_input = gr.Textbox(label="Category", placeholder="Enter category (e.g., Rhyme, Aviation and Math)")
38
+ query_input = gr.Textbox(label="Queries (one per line)", lines=10, placeholder="Paste queries here...")
39
+ submit_button = gr.Button("Add Queries")
 
40
 
41
+ status_output = gr.Textbox(label="Status", interactive=False)
42
+ submit_button.click(add_bulk_queries, inputs=[category_input, query_input], outputs=status_output)
43
 
44
+ gr.Markdown("### Download Stored Queries:")
45
  download_button = gr.File(DATA_FILE, label="Download JSON")
46
 
47
  app.launch()