boomcheng commited on
Commit
bdb9a9a
·
verified ·
1 Parent(s): 0908d26

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -31
app.py CHANGED
@@ -37,15 +37,21 @@ def submit_prompt(prompt):
37
  # Function to add a new object with validation
38
  def add_object(object_class, bbox):
39
  try:
 
40
  x1, y1, x2, y2 = map(int, bbox.split(","))
 
 
41
  if x2 < x1 or y2 < y1:
42
  return "Error: x2 cannot be less than x1 and y2 cannot be less than y1.", []
43
  if x1 < 0 or y1 < 0 or x2 > 512 or y2 > 512:
44
  return "Error: Coordinates must be between 0 and 512.", []
 
 
45
  object_classes_list.append(object_class)
46
  object_bboxes_list.append(bbox)
47
  combined_list = [[cls, bbox] for cls, bbox in zip(object_classes_list, object_bboxes_list)]
48
  return combined_list
 
49
  except ValueError:
50
  return "Error: Invalid input format. Use x1,y1,x2,y2.", []
51
 
@@ -80,29 +86,23 @@ def generate_image(prompt, guidance_scale, num_inference_steps, randomize_seed,
80
 
81
  return image, seed
82
 
83
- # Function to clear all arrays and reset the UI
84
- def clear_arrays():
85
- object_classes_list.clear()
86
- object_bboxes_list.clear()
87
- return [], gr.update(value="", interactive=True) # Clear the objects and reset the prompt
88
-
89
- # Gradio UI with custom CSS for orange buttons
90
- css = """
91
- button {
92
- background-color: orange !important;
93
- color: white !important;
94
- border: none !important;
95
- font-weight: bold;
96
- }
97
- """
98
-
99
- with gr.Blocks(css=css) as demo:
100
  gr.Markdown("# Text-to-Image Generator with Object Addition")
101
 
102
- # Put prompt and submit button in the same row and adjust sizes
103
- with gr.Row():
104
- prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here").style(width=500)
105
- submit_button = gr.Button("Submit Prompt").style(width=100)
 
 
 
 
 
 
 
 
 
106
 
107
  # Always visible DataFrame
108
  objects_display = gr.Dataframe(
@@ -115,7 +115,6 @@ with gr.Blocks(css=css) as demo:
115
  bbox_input = gr.Textbox(label="Bounding Box (x1,y1,x2,y2)", placeholder="Enter bounding box coordinates")
116
 
117
  add_button = gr.Button("Add Object")
118
- refresh_button = gr.Button("Refresh") # New Refresh button
119
 
120
  # Advanced settings in a collapsible accordion
121
  with gr.Accordion("Advanced Settings", open=False):
@@ -145,7 +144,7 @@ with gr.Blocks(css=css) as demo:
145
  submit_button.click(
146
  fn=submit_prompt,
147
  inputs=prompt,
148
- outputs=[objects_display, prompt]
149
  )
150
 
151
  # Add object and update display
@@ -155,13 +154,6 @@ with gr.Blocks(css=css) as demo:
155
  outputs=[objects_display]
156
  )
157
 
158
- # Refresh button to clear arrays and reset inputs
159
- refresh_button.click(
160
- fn=clear_arrays,
161
- inputs=None,
162
- outputs=[objects_display, prompt]
163
- )
164
-
165
  # Generate image based on added objects
166
  generate_button.click(
167
  fn=generate_image,
@@ -170,4 +162,4 @@ with gr.Blocks(css=css) as demo:
170
  )
171
 
172
  if __name__ == "__main__":
173
- demo.launch()
 
37
  # Function to add a new object with validation
38
  def add_object(object_class, bbox):
39
  try:
40
+ # Split and convert bbox string into integers
41
  x1, y1, x2, y2 = map(int, bbox.split(","))
42
+
43
+ # Validate the coordinates
44
  if x2 < x1 or y2 < y1:
45
  return "Error: x2 cannot be less than x1 and y2 cannot be less than y1.", []
46
  if x1 < 0 or y1 < 0 or x2 > 512 or y2 > 512:
47
  return "Error: Coordinates must be between 0 and 512.", []
48
+
49
+ # If validation passes, add to the lists
50
  object_classes_list.append(object_class)
51
  object_bboxes_list.append(bbox)
52
  combined_list = [[cls, bbox] for cls, bbox in zip(object_classes_list, object_bboxes_list)]
53
  return combined_list
54
+
55
  except ValueError:
56
  return "Error: Invalid input format. Use x1,y1,x2,y2.", []
57
 
 
86
 
87
  return image, seed
88
 
89
+ # Gradio UI
90
+ with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
  gr.Markdown("# Text-to-Image Generator with Object Addition")
92
 
93
+ # Put prompt and submit button in the same row
94
+ with gr.Group():
95
+ with gr.Row():
96
+ # Replace gr.Textbox with gr.Text for a single-line input field
97
+ prompt = gr.Text(
98
+ label="Prompt", # Label for the input field
99
+ show_label=False, # Hide the label
100
+ max_lines=1, # Single-line input
101
+ placeholder="Enter your prompt here", # Placeholder text
102
+ container=False # Remove the container background
103
+ )
104
+ # Replace the button with the simplified "Run" button
105
+ submit_button = gr.Button("Submit Prompt", scale=0) # Add scale for button size
106
 
107
  # Always visible DataFrame
108
  objects_display = gr.Dataframe(
 
115
  bbox_input = gr.Textbox(label="Bounding Box (x1,y1,x2,y2)", placeholder="Enter bounding box coordinates")
116
 
117
  add_button = gr.Button("Add Object")
 
118
 
119
  # Advanced settings in a collapsible accordion
120
  with gr.Accordion("Advanced Settings", open=False):
 
144
  submit_button.click(
145
  fn=submit_prompt,
146
  inputs=prompt,
147
+ outputs=[objects_display, prompt] # Update both the display and prompt input
148
  )
149
 
150
  # Add object and update display
 
154
  outputs=[objects_display]
155
  )
156
 
 
 
 
 
 
 
 
157
  # Generate image based on added objects
158
  generate_button.click(
159
  fn=generate_image,
 
162
  )
163
 
164
  if __name__ == "__main__":
165
+ demo.launch()