J-LAB commited on
Commit
5826c2f
·
verified ·
1 Parent(s): afe3c68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +2 -117
app.py CHANGED
@@ -119,101 +119,14 @@ def draw_ocr_bboxes(image, prediction):
119
 
120
  def process_image(image, task_prompt, text_input=None, model_id='J-LAB/Florence-Idesire'):
121
  image = Image.fromarray(image) # Convert NumPy array to PIL Image
122
- if task_prompt == 'More Detailed Caption':
123
  task_prompt = '<MORE_DETAILED_CAPTION>'
124
  results = run_example(task_prompt, image, model_id=model_id)
125
  return results, None
126
- elif task_prompt == 'Detailed Caption':
127
- task_prompt = '<DETAILED_CAPTION>'
128
- results = run_example(task_prompt, image, model_id=model_id)
129
- return results, None
130
  elif task_prompt == 'More Detailed Caption':
131
  task_prompt = '<MORE_DETAILED_CAPTION>'
132
  results = run_example(task_prompt, image, model_id=model_id)
133
  return results, None
134
- elif task_prompt == 'Caption + Grounding':
135
- task_prompt = '<CAPTION>'
136
- results = run_example(task_prompt, image, model_id=model_id)
137
- text_input = results[task_prompt]
138
- task_prompt = '<CAPTION_TO_PHRASE_GROUNDING>'
139
- results = run_example(task_prompt, image, text_input, model_id)
140
- results['<CAPTION>'] = text_input
141
- fig = plot_bbox(image, results['<CAPTION_TO_PHRASE_GROUNDING>'])
142
- return results, fig_to_pil(fig)
143
- elif task_prompt == 'Detailed Caption + Grounding':
144
- task_prompt = '<DETAILED_CAPTION>'
145
- results = run_example(task_prompt, image, model_id=model_id)
146
- text_input = results[task_prompt]
147
- task_prompt = '<CAPTION_TO_PHRASE_GROUNDING>'
148
- results = run_example(task_prompt, image, text_input, model_id)
149
- results['<DETAILED_CAPTION>'] = text_input
150
- fig = plot_bbox(image, results['<CAPTION_TO_PHRASE_GROUNDING>'])
151
- return results, fig_to_pil(fig)
152
- elif task_prompt == 'More Detailed Caption + Grounding':
153
- task_prompt = '<MORE_DETAILED_CAPTION>'
154
- results = run_example(task_prompt, image, model_id=model_id)
155
- text_input = results[task_prompt]
156
- task_prompt = '<CAPTION_TO_PHRASE_GROUNDING>'
157
- results = run_example(task_prompt, image, text_input, model_id)
158
- results['<MORE_DETAILED_CAPTION>'] = text_input
159
- fig = plot_bbox(image, results['<CAPTION_TO_PHRASE_GROUNDING>'])
160
- return results, fig_to_pil(fig)
161
- elif task_prompt == 'Object Detection':
162
- task_prompt = '<OD>'
163
- results = run_example(task_prompt, image, model_id=model_id)
164
- fig = plot_bbox(image, results['<OD>'])
165
- return results, fig_to_pil(fig)
166
- elif task_prompt == 'Dense Region Caption':
167
- task_prompt = '<DENSE_REGION_CAPTION>'
168
- results = run_example(task_prompt, image, model_id=model_id)
169
- fig = plot_bbox(image, results['<DENSE_REGION_CAPTION>'])
170
- return results, fig_to_pil(fig)
171
- elif task_prompt == 'Region Proposal':
172
- task_prompt = '<REGION_PROPOSAL>'
173
- results = run_example(task_prompt, image, model_id=model_id)
174
- fig = plot_bbox(image, results['<REGION_PROPOSAL>'])
175
- return results, fig_to_pil(fig)
176
- elif task_prompt == 'Caption to Phrase Grounding':
177
- task_prompt = '<CAPTION_TO_PHRASE_GROUNDING>'
178
- results = run_example(task_prompt, image, text_input, model_id)
179
- fig = plot_bbox(image, results['<CAPTION_TO_PHRASE_GROUNDING>'])
180
- return results, fig_to_pil(fig)
181
- elif task_prompt == 'Referring Expression Segmentation':
182
- task_prompt = '<REFERRING_EXPRESSION_SEGMENTATION>'
183
- results = run_example(task_prompt, image, text_input, model_id)
184
- output_image = copy.deepcopy(image)
185
- output_image = draw_polygons(output_image, results['<REFERRING_EXPRESSION_SEGMENTATION>'], fill_mask=True)
186
- return results, output_image
187
- elif task_prompt == 'Region to Segmentation':
188
- task_prompt = '<REGION_TO_SEGMENTATION>'
189
- results = run_example(task_prompt, image, text_input, model_id)
190
- output_image = copy.deepcopy(image)
191
- output_image = draw_polygons(output_image, results['<REGION_TO_SEGMENTATION>'], fill_mask=True)
192
- return results, output_image
193
- elif task_prompt == 'Open Vocabulary Detection':
194
- task_prompt = '<OPEN_VOCABULARY_DETECTION>'
195
- results = run_example(task_prompt, image, text_input, model_id)
196
- bbox_results = convert_to_od_format(results['<OPEN_VOCABULARY_DETECTION>'])
197
- fig = plot_bbox(image, bbox_results)
198
- return results, fig_to_pil(fig)
199
- elif task_prompt == 'Region to Category':
200
- task_prompt = '<REGION_TO_CATEGORY>'
201
- results = run_example(task_prompt, image, text_input, model_id)
202
- return results, None
203
- elif task_prompt == 'Region to Description':
204
- task_prompt = '<REGION_TO_DESCRIPTION>'
205
- results = run_example(task_prompt, image, text_input, model_id)
206
- return results, None
207
- elif task_prompt == 'OCR':
208
- task_prompt = '<OCR>'
209
- results = run_example(task_prompt, image, model_id=model_id)
210
- return results, None
211
- elif task_prompt == 'OCR with Region':
212
- task_prompt = '<OCR_WITH_REGION>'
213
- results = run_example(task_prompt, image, model_id=model_id)
214
- output_image = copy.deepcopy(image)
215
- output_image = draw_ocr_bboxes(output_image, results['<OCR_WITH_REGION>'])
216
- return results, output_image
217
  else:
218
  return "", None # Return empty string and None for unknown task prompts
219
 
@@ -227,25 +140,10 @@ css = """
227
 
228
 
229
  single_task_list =[
230
- 'Caption', 'Detailed Caption', 'More Detailed Caption', 'Object Detection',
231
- 'Dense Region Caption', 'Region Proposal', 'Caption to Phrase Grounding',
232
- 'Referring Expression Segmentation', 'Region to Segmentation',
233
- 'Open Vocabulary Detection', 'Region to Category', 'Region to Description',
234
- 'OCR', 'OCR with Region'
235
- ]
236
-
237
- cascased_task_list =[
238
- 'Caption + Grounding', 'Detailed Caption + Grounding', 'More Detailed Caption + Grounding'
239
  ]
240
 
241
 
242
- def update_task_dropdown(choice):
243
- if choice == 'Cascased task':
244
- return gr.Dropdown(choices=cascased_task_list, value='Caption + Grounding')
245
- else:
246
- return gr.Dropdown(choices=single_task_list, value='Caption')
247
-
248
-
249
 
250
  with gr.Blocks(css=css) as demo:
251
  gr.Markdown(DESCRIPTION)
@@ -256,25 +154,12 @@ with gr.Blocks(css=css) as demo:
256
  model_selector = gr.Dropdown(choices=list(models.keys()), label="Model", value='J-LAB/Florence-Idesire')
257
  task_type = gr.Radio(choices=['Single task', 'Cascased task'], label='Task type selector', value='Single task')
258
  task_prompt = gr.Dropdown(choices=single_task_list, label="Task Prompt", value="Caption")
259
- task_type.change(fn=update_task_dropdown, inputs=task_type, outputs=task_prompt)
260
  text_input = gr.Textbox(label="Text Input (optional)")
261
  submit_btn = gr.Button(value="Submit")
262
  with gr.Column():
263
  output_text = gr.Textbox(label="Output Text")
264
  output_img = gr.Image(label="Output Image")
265
 
266
- gr.Examples(
267
- examples=[
268
- ["image1.jpg", 'Object Detection'],
269
- ["image2.jpg", 'OCR with Region']
270
- ],
271
- inputs=[input_img, task_prompt],
272
- outputs=[output_text, output_img],
273
- fn=process_image,
274
- cache_examples=True,
275
- label='Try examples'
276
- )
277
-
278
  submit_btn.click(process_image, [input_img, task_prompt, text_input, model_selector], [output_text, output_img])
279
 
280
  demo.launch(debug=True)
 
119
 
120
  def process_image(image, task_prompt, text_input=None, model_id='J-LAB/Florence-Idesire'):
121
  image = Image.fromarray(image) # Convert NumPy array to PIL Image
122
+ if task_prompt == 'Product Caption':
123
  task_prompt = '<MORE_DETAILED_CAPTION>'
124
  results = run_example(task_prompt, image, model_id=model_id)
125
  return results, None
 
 
 
 
126
  elif task_prompt == 'More Detailed Caption':
127
  task_prompt = '<MORE_DETAILED_CAPTION>'
128
  results = run_example(task_prompt, image, model_id=model_id)
129
  return results, None
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  else:
131
  return "", None # Return empty string and None for unknown task prompts
132
 
 
140
 
141
 
142
  single_task_list =[
143
+ 'More Detailed Caption', 'Product Caption'
 
 
 
 
 
 
 
 
144
  ]
145
 
146
 
 
 
 
 
 
 
 
147
 
148
  with gr.Blocks(css=css) as demo:
149
  gr.Markdown(DESCRIPTION)
 
154
  model_selector = gr.Dropdown(choices=list(models.keys()), label="Model", value='J-LAB/Florence-Idesire')
155
  task_type = gr.Radio(choices=['Single task', 'Cascased task'], label='Task type selector', value='Single task')
156
  task_prompt = gr.Dropdown(choices=single_task_list, label="Task Prompt", value="Caption")
 
157
  text_input = gr.Textbox(label="Text Input (optional)")
158
  submit_btn = gr.Button(value="Submit")
159
  with gr.Column():
160
  output_text = gr.Textbox(label="Output Text")
161
  output_img = gr.Image(label="Output Image")
162
 
 
 
 
 
 
 
 
 
 
 
 
 
163
  submit_btn.click(process_image, [input_img, task_prompt, text_input, model_selector], [output_text, output_img])
164
 
165
  demo.launch(debug=True)