Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,26 +4,19 @@ 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 |
-
|
14 |
-
def generate_caption(image, user_prompt_text=""):
|
15 |
if not isinstance(image, Image.Image):
|
16 |
image = Image.fromarray(image)
|
17 |
|
18 |
-
|
19 |
-
# Combining a user prompt directly might not work as expected for its internal tasks.
|
20 |
-
# However, we can try to append it or use it to filter/refine the output later.
|
21 |
-
|
22 |
-
# For Florence-2's internal mechanism, we still use its task prompt.
|
23 |
-
# The user_prompt_text will be used *after* Florence-2 generates its raw description.
|
24 |
-
florence_task_prompt = "<MORE_DETAILED_CAPTION>"
|
25 |
-
|
26 |
-
inputs = florence_processor(text=florence_task_prompt, images=image, return_tensors="pt").to(device)
|
27 |
generated_ids = florence_model.generate(
|
28 |
input_ids=inputs["input_ids"],
|
29 |
pixel_values=inputs["pixel_values"],
|
@@ -33,39 +26,20 @@ def generate_caption(image, user_prompt_text=""):
|
|
33 |
num_beams=3,
|
34 |
)
|
35 |
generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
36 |
-
|
37 |
generated_text,
|
38 |
-
task=
|
39 |
image_size=(image.width, image.height)
|
40 |
-
)
|
41 |
-
|
42 |
-
print(
|
43 |
-
|
44 |
-
# --- POST-PROCESSING STEP ---
|
45 |
-
# Now, we use a separate LLM (like the one I'm operating as) to refine the raw_caption
|
46 |
-
# based on the user_prompt_text. This is crucial because Florence-2 itself
|
47 |
-
# isn't designed for arbitrary stylistic prompting like "focus on clothes, age, footwear".
|
48 |
-
|
49 |
-
# In a real deployed app, you'd integrate another API call here (e.g., to OpenAI, Gemini, etc.)
|
50 |
-
# For this example, I'll simulate it by returning both.
|
51 |
-
|
52 |
-
# You would replace this with an actual call to another LLM to refine 'raw_caption'
|
53 |
-
# using 'user_prompt_text' as a guide.
|
54 |
-
refined_prompt_output = f"Original Caption: {raw_caption}\n\nRefinement Request: {user_prompt_text}\n\n(Note: A secondary AI would process this for your desired output.)"
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
],
|
64 |
-
outputs=[
|
65 |
-
gr.Textbox(label="Raw Florence-2 Caption", lines=3, show_copy_button=True),
|
66 |
-
gr.Textbox(label="Refined Output (Requires Secondary AI)", lines=5, show_copy_button=True) # Output for the refined prompt
|
67 |
-
],
|
68 |
-
theme="Yntec/HaleyCH_Theme_Orange",
|
69 |
-
description="⚠ Sorry for the inconvenience. The space are currently running on the CPU, which might affect performance. We appreciate your understanding."
|
70 |
-
)
|
71 |
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"],
|
|
|
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)
|