Workpam commited on
Commit
f65bd79
·
verified ·
1 Parent(s): d20beec

Update app.py

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