Workpam commited on
Commit
cf437ca
·
verified ·
1 Parent(s): bfe3a14

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -87
app.py CHANGED
@@ -1,106 +1,51 @@
 
1
  import gradio as gr
2
- import torch
3
  from PIL import Image
4
- from transformers import AutoProcessor, AutoModelForCausalLM
5
- import torch.nn as _nn
6
 
7
- # --- Monkey‑patch to bypass SDPA check ---
8
- # Whenever a model asks for `_supports_sdpa`, return False
9
- _orig_getattr = _nn.Module.__getattr__
10
- def _patched_getattr(self, name):
11
- if name == '_supports_sdpa':
12
- return False
13
- return _orig_getattr(self, name)
14
- _nn.Module.__getattr__ = _patched_getattr
15
-
16
- # --- Device & Model Setup ---
17
- device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
18
- torch_dtype = torch.float16 if device.type == 'cuda' else torch.float32
19
-
20
- model = AutoModelForCausalLM.from_pretrained(
21
- 'microsoft/Florence-2-large',
22
- torch_dtype=torch_dtype,
23
- trust_remote_code=True
24
- ).to(device)
25
- processor = AutoProcessor.from_pretrained(
26
- 'microsoft/Florence-2-large',
27
- trust_remote_code=True
28
  )
29
- PROMPT = '<OD>'
30
 
31
- # --- Alt‑Text Generation Function ---
32
- def generate_alt_text(files, max_new_tokens):
33
  captions = []
34
  for f in files or []:
35
- # 1) Load image
36
- img = Image.open(f.name).convert('RGB')
37
- # 2) Preprocess inputs
38
- inputs = processor(
39
- text=PROMPT,
40
- images=img,
41
- return_tensors='pt'
42
- ).to(device, torch_dtype)
43
- # 3) Generate with caching disabled
44
- outputs = model.generate(
45
- input_ids=inputs['input_ids'],
46
- pixel_values=inputs['pixel_values'],
47
- max_new_tokens=int(max_new_tokens),
48
- num_beams=3,
49
- do_sample=False,
50
- use_cache=False
51
  )
52
- # 4) Decode & post‑process
53
- raw_text = processor.batch_decode(outputs, skip_special_tokens=False)[0]
54
- parsed = processor.post_process_generation(
55
- raw_text,
56
- task=PROMPT,
57
- image_size=(img.width, img.height)
58
- )
59
- # 5) Extract caption text
60
- data = parsed.get(PROMPT, '')
61
- if isinstance(data, dict):
62
- text = data.get('text', '')
63
- else:
64
- text = data
65
- caption = text.strip().capitalize()
66
- captions.append(caption)
67
- return '\n\n'.join(captions)
68
 
69
- # --- Gradio Interface ---
70
- with gr.Blocks(title='Florence-2-large Alt-Text Generator') as demo:
71
- gr.Markdown('# Florence-2-large Alt-Text Generator')
72
- gr.Markdown('Upload images to get `<OD>`-style descriptions.')
73
 
74
  with gr.Row():
75
- file_input = gr.File(
76
- label='Images', file_count='multiple', file_types=['image']
77
- )
78
- slider = gr.Slider(
79
- minimum=64, maximum=4096, step=64, value=512,
80
- label='Max New Tokens'
81
- )
82
- gen_btn = gr.Button('Generate Alt-Text')
83
- clear_btn = gr.Button('Clear All')
84
 
85
- gallery = gr.Gallery(label='Preview', columns=4)
86
- output = gr.Textbox(
87
- label='Generated Alt-Text', lines=5, show_copy_button=True
88
- )
89
 
90
  file_input.change(
91
- fn=lambda files: [Image.open(f.name).convert('RGB') for f in (files or [])],
92
  inputs=file_input,
93
  outputs=gallery
94
  )
95
- gen_btn.click(
96
- fn=generate_alt_text,
97
- inputs=[file_input, slider],
98
- outputs=output,
99
- queue=True
100
- )
101
- clear_btn.click(
102
- fn=lambda: ([], 512, [], ''),
103
- outputs=[file_input, slider, gallery, output]
104
- )
105
 
106
  demo.launch()
 
1
+ # app.py
2
  import gradio as gr
3
+ from transformers import pipeline
4
  from PIL import Image
 
 
5
 
6
+ # --- Lightweight pipeline ---
7
+ captioner = pipeline(
8
+ "image-to-text",
9
+ model="nlpconnect/vit-gpt2-image-captioning",
10
+ device=0 # set to -1 for CPU only
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  )
 
12
 
13
+ # --- Generation fn ---
14
+ def generate_alt_text(files, max_length):
15
  captions = []
16
  for f in files or []:
17
+ img = Image.open(f.name).convert("RGB")
18
+ out = captioner(
19
+ img,
20
+ generate_kwargs={
21
+ "max_length": int(max_length),
22
+ "num_beams": 3
23
+ }
 
 
 
 
 
 
 
 
 
24
  )
25
+ text = out[0].get("generated_text", "").strip().capitalize()
26
+ captions.append(text)
27
+ return "\n\n".join(captions)
 
 
 
 
 
 
 
 
 
 
 
 
 
28
 
29
+ # --- Gradio UI ---
30
+ with gr.Blocks(title="Fast Alt‑Text Generator") as demo:
31
+ gr.Markdown("## Fast & Lightweight Alt‑Text Generator")
32
+ gr.Markdown("Model: `nlpconnect/vit-gpt2-image-captioning`")
33
 
34
  with gr.Row():
35
+ file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
36
+ slider = gr.Slider(16, 64, value=32, step=8, label="Max Tokens")
37
+ run_btn = gr.Button("Generate")
38
+ clear_btn = gr.Button("Clear")
 
 
 
 
 
39
 
40
+ gallery = gr.Gallery(label="Preview", columns=4)
41
+ output = gr.Textbox(label="Captions", lines=6, show_copy_button=True)
 
 
42
 
43
  file_input.change(
44
+ fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
45
  inputs=file_input,
46
  outputs=gallery
47
  )
48
+ run_btn.click(generate_alt_text, [file_input, slider], output, queue=True)
49
+ clear_btn.click(lambda: ([], 32, [], ""), outputs=[file_input, slider, gallery, output])
 
 
 
 
 
 
 
 
50
 
51
  demo.launch()