Workpam commited on
Commit
9463664
·
verified ·
1 Parent(s): 3a1be56

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -44
app.py CHANGED
@@ -1,71 +1,61 @@
 
1
  import gradio as gr
2
- import torch
3
  from PIL import Image
4
- from transformers import AutoProcessor, AutoModelForCausalLM
5
- from transformers.modeling_utils import PreTrainedModel
6
 
7
- # --- Monkey-patch to disable SDPA checks on all models ---
8
- PreTrainedModel._supports_sdpa = False
9
 
10
- # --- Device & Model Setup ---
11
- device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
12
- torch_dtype = torch.float16 if device.type == "cuda" else torch.float32
13
-
14
- # Load Florence-2-large without SDPA errors
15
- model = AutoModelForCausalLM.from_pretrained(
16
- "microsoft/Florence-2-large",
17
- torch_dtype=torch_dtype,
18
- trust_remote_code=True
19
- ).to(device)
20
- processor = AutoProcessor.from_pretrained(
21
- "microsoft/Florence-2-large",
22
- trust_remote_code=True
23
- )
24
- PROMPT = "<OD>"
25
-
26
- # --- Alt-Text Generation Function ---
27
  def generate_alt_text(files, max_new_tokens):
28
  captions = []
29
  for f in files or []:
30
- img = Image.open(f.name).convert("RGB")
31
- inputs = processor(text=PROMPT, images=img, return_tensors="pt").to(device, torch_dtype)
32
- outputs = model.generate(
33
- input_ids=inputs["input_ids"],
34
- pixel_values=inputs["pixel_values"],
35
- max_new_tokens=int(max_new_tokens),
36
- num_beams=3,
37
- do_sample=False
 
 
 
 
38
  )
39
- raw_text = processor.batch_decode(outputs, skip_special_tokens=False)[0]
40
- parsed = processor.post_process_generation(raw_text, task=PROMPT, image_size=(img.width, img.height))
41
- caption = parsed.get(PROMPT, "").strip().capitalize()
42
- captions.append(caption)
43
  return "\n\n".join(captions)
44
 
45
- # --- Gradio Interface ---
46
- with gr.Blocks(title="Florence-2-large Alt-Text Generator") as demo:
47
- gr.Markdown("# Florence-2-large Alt-Text Generator")
48
- gr.Markdown("Upload images to get `<OD>`-style descriptions from Florence-2-large.")
49
 
50
- file_input = gr.File(label="Images", file_count="multiple", file_types=["image"])
51
- slider = gr.Slider(64, 4096, step=64, value=512, label="Max New Tokens")
52
- gen_btn = gr.Button("Generate Alt-Text")
53
- clear_btn = gr.Button("Clear All")
 
54
 
55
- gallery = gr.Gallery(label="Preview", columns=4)
56
- output = gr.Textbox(label="Generated Alt-Text", lines=5, show_copy_button=True)
57
 
 
58
  file_input.change(
59
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
60
  inputs=file_input,
61
  outputs=gallery
62
  )
 
 
63
  gen_btn.click(
64
  fn=generate_alt_text,
65
  inputs=[file_input, slider],
66
  outputs=output,
67
  queue=True
68
  )
 
 
69
  clear_btn.click(
70
  fn=lambda: ([], 512, [], ""),
71
  outputs=[file_input, slider, gallery, output]
 
1
+ # app.py
2
  import gradio as gr
 
3
  from PIL import Image
4
+ from huggingface_hub import InferenceApi
 
5
 
6
+ # Instantiate the HF Inference client for the public Florence‑2‑large model
7
+ inference = InferenceApi(repo_id="microsoft/Florence-2-large")
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def generate_alt_text(files, max_new_tokens):
10
  captions = []
11
  for f in files or []:
12
+ # Read image bytes
13
+ with open(f.name, "rb") as img_f:
14
+ img_bytes = img_f.read()
15
+ # Call the hosted Florence endpoint
16
+ result = inference(
17
+ inputs=img_bytes,
18
+ params={ # <-- must be 'params', not 'parameters'
19
+ "max_new_tokens": int(max_new_tokens),
20
+ "num_beams": 3,
21
+ "do_sample": False
22
+ },
23
+ request_format="binary"
24
  )
25
+ # Extract and clean up the generated text
26
+ text = result[0].get("generated_text", "").strip().capitalize()
27
+ captions.append(text)
 
28
  return "\n\n".join(captions)
29
 
30
+ with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
31
+ gr.Markdown("## Florence‑2‑large Alt‑Text Generator")
32
+ gr.Markdown("No tokens, no local weights—powered by the HF Inference API.")
 
33
 
34
+ with gr.Row():
35
+ file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
36
+ slider = gr.Slider(64, 4096, value=512, step=64, label="Max New Tokens")
37
+ gen_btn = gr.Button("Generate Alt‑Text")
38
+ clear_btn = gr.Button("Clear All")
39
 
40
+ gallery = gr.Gallery(label="Image Preview", columns=4)
41
+ output = gr.Textbox(label="Generated Alt‑Text", lines=6, show_copy_button=True)
42
 
43
+ # Preview images immediately
44
  file_input.change(
45
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
46
  inputs=file_input,
47
  outputs=gallery
48
  )
49
+
50
+ # Generate captions
51
  gen_btn.click(
52
  fn=generate_alt_text,
53
  inputs=[file_input, slider],
54
  outputs=output,
55
  queue=True
56
  )
57
+
58
+ # Clear everything
59
  clear_btn.click(
60
  fn=lambda: ([], 512, [], ""),
61
  outputs=[file_input, slider, gallery, output]