Jonny001 commited on
Commit
fceb09a
·
verified ·
1 Parent(s): b560f5d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import subprocess
3
+ import torch
4
+ from PIL import Image
5
+ from transformers import AutoProcessor, AutoModelForCausalLM
6
+
7
+
8
+
9
+ subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
10
+
11
+ device = "cuda" if torch.cuda.is_available() else "cpu"
12
+ florence_model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True).to(device).eval()
13
+ florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True)
14
+
15
+ def generate_caption(image):
16
+ if not isinstance(image, Image.Image):
17
+ image = Image.fromarray(image)
18
+
19
+ inputs = florence_processor(text="<MORE_DETAILED_CAPTION>", images=image, return_tensors="pt").to(device)
20
+ generated_ids = florence_model.generate(
21
+ input_ids=inputs["input_ids"],
22
+ pixel_values=inputs["pixel_values"],
23
+ max_new_tokens=1024,
24
+ early_stopping=False,
25
+ do_sample=False,
26
+ num_beams=3,
27
+ )
28
+ generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
29
+ parsed_answer = florence_processor.post_process_generation(
30
+ generated_text,
31
+ task="<MORE_DETAILED_CAPTION>",
32
+ image_size=(image.width, image.height)
33
+ )
34
+ prompt = parsed_answer["<MORE_DETAILED_CAPTION>"]
35
+ print("\n\nGeneration completed!:"+ prompt)
36
+ return prompt
37
+
38
+ io = gr.Interface(generate_caption,
39
+ inputs=[gr.Image(label="Input Image")],
40
+ outputs = [gr.Textbox(label="Output Prompt", lines=2, show_copy_button = True),
41
+ ]
42
+ )
43
+ io.launch(debug=True)