DigiP-AI commited on
Commit
6a8ba31
·
verified ·
1 Parent(s): 26785ab

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -0
app.py CHANGED
@@ -186,4 +186,44 @@ with gr.Blocks(theme=theme, css=css, elem_id="app-container") as app:
186
 
187
  text_button.click(query, inputs=[custom_lora, text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height], outputs=[image_output, seed_output])
188
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
189
  app.launch(show_api=False, share=False)
 
186
 
187
  text_button.click(query, inputs=[custom_lora, text_prompt, negative_prompt, steps, cfg, method, seed, strength, width, height], outputs=[image_output, seed_output])
188
 
189
+ with gr.Tab("Image to Prompt"):
190
+ subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
191
+
192
+ # Initialize Florence model
193
+ device = "cuda" if torch.cuda.is_available() else "cpu"
194
+ florence_model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True).to(device).eval()
195
+ florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True)
196
+
197
+ # api_key = os.getenv("HF_READ_TOKEN")
198
+
199
+ def generate_caption(image):
200
+ if not isinstance(image, Image.Image):
201
+ image = Image.fromarray(image)
202
+
203
+ inputs = florence_processor(text="<MORE_DETAILED_CAPTION>", images=image, return_tensors="pt").to(device)
204
+ generated_ids = florence_model.generate(
205
+ input_ids=inputs["input_ids"],
206
+ pixel_values=inputs["pixel_values"],
207
+ max_new_tokens=1024,
208
+ early_stopping=False,
209
+ do_sample=False,
210
+ num_beams=3,
211
+ )
212
+ generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
213
+ parsed_answer = florence_processor.post_process_generation(
214
+ generated_text,
215
+ task="<MORE_DETAILED_CAPTION>",
216
+ image_size=(image.width, image.height)
217
+ )
218
+ prompt = parsed_answer["<MORE_DETAILED_CAPTION>"]
219
+ print("\n\nGeneration completed!:"+ prompt)
220
+ return prompt
221
+
222
+ io = gr.Interface(generate_caption,
223
+ inputs=[gr.Image(label="Input Image")],
224
+ outputs = [gr.Textbox(label="Output Prompt", lines=2, show_copy_button = True),
225
+ # gr.Image(label="Output Image")
226
+ ]
227
+ )
228
+
229
  app.launch(show_api=False, share=False)