Workpam commited on
Commit
cb43964
·
verified ·
1 Parent(s): 252d936

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -65
app.py CHANGED
@@ -1,86 +1,51 @@
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
- # 1) Load and preprocess
29
  img = Image.open(f.name).convert("RGB")
30
- inputs = processor(
31
- text=PROMPT,
32
- images=img,
33
- return_tensors="pt"
34
- ).to(device, torch_dtype)
35
-
36
- # 2) Generate
37
- generated_ids = model.generate(
38
- input_ids=inputs["input_ids"],
39
- pixel_values=inputs["pixel_values"],
40
- max_new_tokens=int(max_new_tokens),
41
- num_beams=3,
42
- do_sample=False
43
  )
44
-
45
- # 3) Decode & post‑process
46
- generated_text = processor.batch_decode(
47
- generated_ids,
48
- skip_special_tokens=False
49
- )[0]
50
- parsed = processor.post_process_generation(
51
- generated_text,
52
- task=PROMPT,
53
- image_size=(img.width, img.height)
54
- )
55
- caption = parsed.get(PROMPT, "").strip().capitalize()
56
- captions.append(caption)
57
-
58
  return "\n\n".join(captions)
59
 
60
-
61
- # --- Gradio Interface ---
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 and get detailed `<OD>` 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
  # Show previews immediately
86
  file_input.change(
@@ -89,7 +54,7 @@ with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
89
  outputs=gallery
90
  )
91
 
92
- # Generate captions on click
93
  run_btn.click(
94
  fn=generate_alt_text,
95
  inputs=[file_input, slider],
 
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(
 
54
  outputs=gallery
55
  )
56
 
57
+ # Generate captions
58
  run_btn.click(
59
  fn=generate_alt_text,
60
  inputs=[file_input, slider],