Workpam commited on
Commit
5775068
·
verified ·
1 Parent(s): 4bd9ace

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -61
app.py CHANGED
@@ -1,83 +1,59 @@
 
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]
81
  )
 
 
82
 
83
  demo.launch()
 
1
+ import os
2
  import gradio as gr
3
  import torch
4
  from PIL import Image
5
+ from huggingface_hub import InferenceApi
6
 
7
+ # --- Config & Client ---
8
+ HF_TOKEN = os.getenv("HF_TOKEN")
9
+ assert HF_TOKEN, "Please set HF_TOKEN in your environment"
10
 
11
+ # Florence‑2‑large inference client
12
+ inference = InferenceApi(
13
+ repo_id="microsoft/Florence-2-large",
14
+ token=HF_TOKEN
 
 
 
 
 
15
  )
16
 
17
+ # --- Generation fn ---
 
 
18
  def generate_alt_text(files, max_new_tokens):
19
  captions = []
20
  for f in files or []:
21
+ # Read raw bytes
22
+ img_bytes = open(f.name, "rb").read()
23
+ # Call the hosted Florence endpoint
24
+ result = inference(
25
+ inputs=img_bytes,
26
+ params={
27
+ "max_new_tokens": int(max_new_tokens),
28
+ "num_beams": 3,
29
+ "do_sample": False
30
+ },
31
+ request_format="binary"
 
 
 
 
 
 
 
 
 
 
 
32
  )
33
+ # The API returns a list of dicts
34
+ text = result[0].get("generated_text", "").strip().capitalize()
35
+ captions.append(text)
36
  return "\n\n".join(captions)
37
 
38
+ # --- Gradio UI ---
39
+ with gr.Blocks(title="Florence‑2‑large Alt‑Text (API)") as demo:
40
+ gr.Markdown("# Florence‑2‑large Alt‑Text Generator")
41
+ gr.Markdown("Uses the Hugging Face Inference API—no local model load needed.")
42
 
43
+ with gr.Row():
44
+ file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
45
+ slider = gr.Slider(64, 2048, value=512, step=64, label="Max New Tokens")
46
+ run_btn = gr.Button("Generate Alt‑Text")
47
+ clear_btn = gr.Button("Clear All")
48
 
49
  gallery = gr.Gallery(label="Preview", columns=4)
50
+ output = gr.Textbox(label="Generated Alt‑Text", lines=5, show_copy_button=True)
51
 
52
  file_input.change(
53
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
54
+ inputs=file_input, outputs=gallery
 
 
 
 
 
 
 
 
 
 
 
55
  )
56
+ run_btn.click(generate_alt_text, [file_input, slider], output, queue=True)
57
+ clear_btn.click(lambda: ([], 512, [], ""), outputs=[file_input, slider, gallery, output])
58
 
59
  demo.launch()