Workpam commited on
Commit
549d9c1
·
verified ·
1 Parent(s): cb43964

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -29
app.py CHANGED
@@ -1,60 +1,95 @@
1
  # app.py
2
  import gradio as gr
3
- from transformers import pipeline
4
- from PIL import Image
5
  import torch
 
 
 
 
 
 
6
 
7
- # --- Pipeline Setup ---
8
- # Use GPU 0 if available, otherwise CPU.
9
- device = 0 if torch.cuda.is_available() else -1
 
 
10
 
11
- captioner = pipeline(
12
- "image-to-text",
13
- model="microsoft/Florence-2-large",
14
- trust_remote_code=True,
15
- device=device
16
  )
17
 
18
- # --- Generation Function ---
 
 
19
  def generate_alt_text(files, max_new_tokens):
20
  captions = []
21
  for f in files or []:
 
22
  img = Image.open(f.name).convert("RGB")
23
- # Pass everything via generate_kwargs
24
- out = captioner(
25
- img,
26
- generate_kwargs={
27
- "max_new_tokens": int(max_new_tokens),
28
- "num_beams": 3,
29
- "do_sample": False
30
- }
 
 
 
 
 
 
31
  )
32
- text = out[0].get("generated_text", "").strip().capitalize()
33
- captions.append(text)
 
 
 
 
 
 
 
 
 
 
 
 
34
  return "\n\n".join(captions)
35
 
36
  # --- Gradio UI ---
37
  with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
38
  gr.Markdown("## Florence‑2‑large Alt‑Text Generator")
39
- gr.Markdown("Drop/upload images and get detailed, `<OD>`‑style descriptions.")
40
 
41
  with gr.Row():
42
- file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
43
- slider = gr.Slider(64, 2048, value=512, step=64, label="Max New Tokens")
44
- run_btn = gr.Button("Generate Alt‑Text")
45
- clear_btn = gr.Button("Clear All")
 
 
 
 
 
 
 
 
 
 
46
 
47
  gallery = gr.Gallery(label="Image Preview", columns=4)
48
- output = gr.Textbox(label="Generated Alt‑Text", lines=6, show_copy_button=True)
49
 
50
- # Show previews immediately
51
  file_input.change(
52
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
53
  inputs=file_input,
54
  outputs=gallery
55
  )
56
 
57
- # Generate captions
58
  run_btn.click(
59
  fn=generate_alt_text,
60
  inputs=[file_input, slider],
 
1
  # app.py
2
  import gradio as gr
 
 
3
  import torch
4
+ from PIL import Image
5
+ from transformers import AutoProcessor, AutoModelForCausalLM
6
+
7
+ # --- Device & Model Setup ---
8
+ device = "cuda:0" if torch.cuda.is_available() else "cpu"
9
+ torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
10
 
11
+ model = AutoModelForCausalLM.from_pretrained(
12
+ "microsoft/Florence-2-large",
13
+ torch_dtype=torch_dtype,
14
+ trust_remote_code=True
15
+ ).to(device)
16
 
17
+ processor = AutoProcessor.from_pretrained(
18
+ "microsoft/Florence-2-large",
19
+ trust_remote_code=True
 
 
20
  )
21
 
22
+ PROMPT = "<OD>"
23
+
24
+ # --- Alt‑Text Generation Function ---
25
  def generate_alt_text(files, max_new_tokens):
26
  captions = []
27
  for f in files or []:
28
+ # Load image
29
  img = Image.open(f.name).convert("RGB")
30
+ # Prepare inputs exactly as in your snippet
31
+ inputs = processor(
32
+ text=PROMPT,
33
+ images=img,
34
+ return_tensors="pt"
35
+ ).to(device, torch_dtype)
36
+
37
+ # Generate
38
+ generated_ids = model.generate(
39
+ input_ids=inputs["input_ids"],
40
+ pixel_values=inputs["pixel_values"],
41
+ max_new_tokens=int(max_new_tokens),
42
+ num_beams=3,
43
+ do_sample=False
44
  )
45
+
46
+ # Decode & post‑process
47
+ generated_text = processor.batch_decode(
48
+ generated_ids,
49
+ skip_special_tokens=False
50
+ )[0]
51
+ parsed = processor.post_process_generation(
52
+ generated_text,
53
+ task=PROMPT,
54
+ image_size=(img.width, img.height)
55
+ )
56
+ caption = parsed.get(PROMPT, "").strip().capitalize()
57
+ captions.append(caption)
58
+
59
  return "\n\n".join(captions)
60
 
61
  # --- Gradio UI ---
62
  with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
63
  gr.Markdown("## Florence‑2‑large Alt‑Text Generator")
64
+ gr.Markdown("Drop/upload images below to get `<OD>`‑style descriptions.")
65
 
66
  with gr.Row():
67
+ file_input = gr.File(
68
+ label="Upload Images",
69
+ file_count="multiple",
70
+ file_types=["image"]
71
+ )
72
+ slider = gr.Slider(
73
+ minimum=64,
74
+ maximum=4096,
75
+ step=64,
76
+ value=512,
77
+ label="Max New Tokens"
78
+ )
79
+ run_btn = gr.Button("Generate Alt‑Text")
80
+ clear_btn = gr.Button("Clear All")
81
 
82
  gallery = gr.Gallery(label="Image Preview", columns=4)
83
+ output = gr.Textbox(label="Generated Alt‑Text", lines=5, show_copy_button=True)
84
 
85
+ # Preview images
86
  file_input.change(
87
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
88
  inputs=file_input,
89
  outputs=gallery
90
  )
91
 
92
+ # Generate alt‑text
93
  run_btn.click(
94
  fn=generate_alt_text,
95
  inputs=[file_input, slider],