benchang323 commited on
Commit
312b004
·
verified ·
1 Parent(s): 1109795

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -28
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
- with open(DATA_FILE, "r") as file:
15
- stored_data = json.load(file)
 
16
 
17
- # Function to add a new query with positive and negative responses
18
  def add_query(category, query, positive1, positive2, negative1, negative2):
19
- global stored_data
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
- stored_data.append(new_entry)
32
 
33
- # Save to JSON file
34
  with open(DATA_FILE, "w") as file:
35
- json.dump(stored_data, file, indent=4)
36
-
37
- return f"✅ Added query: {query} under category '{category}' with responses."
38
 
39
- # Function to serve the updated JSON file for download
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.Markdown("### Provide Responses")
53
- positive1_input = gr.Textbox(label="Positive Response 1", placeholder="Enter first positive response", lines=3)
54
- positive2_input = gr.Textbox(label="Positive Response 2", placeholder="Enter second positive response", lines=3)
55
- negative1_input = gr.Textbox(label="Negative Response 1", placeholder="Enter first negative response", lines=3)
56
- negative2_input = gr.Textbox(label="Negative Response 2", placeholder="Enter second negative response", lines=3)
 
 
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("### Save & Download Stored Queries:")
68
- download_button = gr.Button("Download JSON")
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()