Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -4,46 +4,42 @@ import torch
|
|
4 |
from PIL import Image
|
5 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
6 |
|
|
|
|
|
7 |
subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
|
8 |
|
9 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
10 |
florence_model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True).to(device).eval()
|
11 |
florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True)
|
12 |
|
13 |
-
def
|
14 |
if not isinstance(image, Image.Image):
|
15 |
image = Image.fromarray(image)
|
16 |
|
17 |
inputs = florence_processor(text="<MORE_DETAILED_CAPTION>", images=image, return_tensors="pt").to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
do_sample=True,
|
27 |
-
temperature=0.7 + i * 0.1,
|
28 |
-
num_beams=3
|
29 |
-
)
|
30 |
-
generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
31 |
-
parsed_answer = florence_processor.post_process_generation(
|
32 |
-
generated_text,
|
33 |
-
task="<MORE_DETAILED_CAPTION>",
|
34 |
-
image_size=(image.width, image.height)
|
35 |
-
)
|
36 |
-
prompt = parsed_answer["<MORE_DETAILED_CAPTION>"]
|
37 |
-
captions.append(prompt)
|
38 |
-
print(f"\n\nGeneration {i+1} completed!:" + prompt)
|
39 |
-
|
40 |
-
return "\n\n".join([f"Caption {i+1}: {caption}" for i, caption in enumerate(captions)])
|
41 |
-
|
42 |
-
io = gr.Interface(
|
43 |
-
generate_captions,
|
44 |
-
inputs=[gr.Image(label="Input Image")],
|
45 |
-
outputs=[gr.Textbox(label="Output Captions", lines=10, show_copy_button=True)],
|
46 |
-
theme="Yntec/HaleyCH_Theme_Orange"
|
47 |
-
)
|
48 |
-
|
49 |
io.launch(debug=True)
|
|
|
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=3, show_copy_button = True),
|
41 |
+
],
|
42 |
+
theme="Yntec/HaleyCH_Theme_Orange",
|
43 |
+
description="⚠ Sorry for the inconvenience. The space are currently running on the CPU, which might affect performance. We appreciate your understanding."
|
44 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
io.launch(debug=True)
|