Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,79 @@
|
|
|
|
|
| 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 |
-
|
| 10 |
-
|
| 11 |
|
| 12 |
-
|
| 13 |
|
| 14 |
-
# Initialize Florence model
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
|
| 21 |
def generate_caption(image):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
if not isinstance(image, Image.Image):
|
| 23 |
image = Image.fromarray(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 35 |
parsed_answer = florence_processor.post_process_generation(
|
| 36 |
-
generated_text,
|
| 37 |
-
task=
|
| 38 |
image_size=(image.width, image.height)
|
| 39 |
)
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
#
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
#
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
# prompt=prompt,
|
| 51 |
-
# seed=seed,
|
| 52 |
-
# width=width,
|
| 53 |
-
# height=height,
|
| 54 |
-
# api_name="/generate_image"
|
| 55 |
-
# )
|
| 56 |
-
# # Extract the image path from the result tuple
|
| 57 |
-
# image_path = result[0]
|
| 58 |
-
# return image_path
|
| 59 |
-
# except Exception as e:
|
| 60 |
-
# raise Exception(f"Error generating image: {str(e)}")
|
| 61 |
-
|
| 62 |
-
io = gr.Interface(generate_caption,
|
| 63 |
-
inputs=[gr.Image(label="Input Image")],
|
| 64 |
-
outputs = [gr.Textbox(label="Output Prompt", lines=2, show_copy_button = True),
|
| 65 |
-
# gr.Image(label="Output Image")
|
| 66 |
-
],
|
| 67 |
-
deep_link=False
|
| 68 |
-
)
|
| 69 |
-
io.launch(debug=True)
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 6 |
|
| 7 |
+
# 1. Determine the available hardware device
|
| 8 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
|
|
|
| 10 |
|
| 11 |
+
print(f"Loading model on device: {device}")
|
| 12 |
|
| 13 |
+
# 2. Initialize Florence-2 model and processor
|
| 14 |
+
# Using trust_remote_code=True as required by the Florence architecture
|
| 15 |
+
model_id = 'microsoft/Florence-2-base'
|
| 16 |
+
florence_model = AutoModelForCausalLM.from_pretrained(
|
| 17 |
+
model_id,
|
| 18 |
+
trust_remote_code=True,
|
| 19 |
+
torch_dtype=torch_dtype
|
| 20 |
+
).to(device).eval()
|
| 21 |
|
| 22 |
+
florence_processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
| 23 |
|
| 24 |
def generate_caption(image):
|
| 25 |
+
if image is None:
|
| 26 |
+
return "Please upload an image first."
|
| 27 |
+
|
| 28 |
+
# Ensure the input is a valid PIL Image
|
| 29 |
if not isinstance(image, Image.Image):
|
| 30 |
image = Image.fromarray(image)
|
| 31 |
+
|
| 32 |
+
# Execute the detailed captioning task
|
| 33 |
+
prompt_task = "<MORE_DETAILED_CAPTION>"
|
| 34 |
+
inputs = florence_processor(text=prompt_task, images=image, return_tensors="pt").to(device)
|
| 35 |
|
| 36 |
+
# Match data type to the model precision
|
| 37 |
+
if torch.cuda.is_available():
|
| 38 |
+
inputs = {k: v.to(torch.float16) if k == "pixel_values" else v for k, v in inputs.items()}
|
| 39 |
+
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
generated_ids = florence_model.generate(
|
| 42 |
+
input_ids=inputs["input_ids"],
|
| 43 |
+
pixel_values=inputs["pixel_values"],
|
| 44 |
+
max_new_tokens=1024,
|
| 45 |
+
early_stopping=False,
|
| 46 |
+
do_sample=False,
|
| 47 |
+
num_beams=3,
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
| 51 |
parsed_answer = florence_processor.post_process_generation(
|
| 52 |
+
generated_text,
|
| 53 |
+
task=prompt_task,
|
| 54 |
image_size=(image.width, image.height)
|
| 55 |
)
|
| 56 |
+
|
| 57 |
+
base_caption = parsed_answer[prompt_task]
|
| 58 |
+
|
| 59 |
+
# Appends styling parameters for prompt generation formatting
|
| 60 |
+
final_prompt = f"{base_caption}. Detailed features, high quality, highly descriptive cinematic lighting."
|
| 61 |
+
return final_prompt
|
| 62 |
+
|
| 63 |
+
# 3. Construct the Gradio Interface UI
|
| 64 |
+
with gr.Blocks() as demo:
|
| 65 |
+
gr.Markdown("# Detailed Image Captioning Tool")
|
| 66 |
+
gr.Markdown("Upload an image to generate highly descriptive text prompts using Florence-2.")
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
with gr.Column():
|
| 70 |
+
input_img = gr.Image(label="Upload Image", type="pil")
|
| 71 |
+
run_btn = gr.Button("Generate Detailed Prompt", variant="primary")
|
| 72 |
+
with gr.Column():
|
| 73 |
+
output_text = gr.Textbox(label="Generated Output", lines=5, show_copy_button=True)
|
| 74 |
+
|
| 75 |
+
run_btn.click(fn=generate_caption, inputs=input_img, outputs=output_text)
|
| 76 |
|
| 77 |
+
# Launch the app container
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|