Workpam commited on
Commit
36d795f
·
verified ·
1 Parent(s): 9463664

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -13
app.py CHANGED
@@ -3,33 +3,41 @@ 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"])
@@ -40,7 +48,7 @@ with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
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,
@@ -55,7 +63,7 @@ with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
55
  queue=True
56
  )
57
 
58
- # Clear everything
59
  clear_btn.click(
60
  fn=lambda: ([], 512, [], ""),
61
  outputs=[file_input, slider, gallery, output]
 
3
  from PIL import Image
4
  from huggingface_hub import InferenceApi
5
 
6
+ # 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
+ # 1) Read image bytes
13
+ img_bytes = open(f.name, "rb").read()
14
+
15
+ # 2) Call the hosted Florence endpoint
16
+ # Use `parameters=` and drop `request_format`
17
  result = inference(
18
  inputs=img_bytes,
19
+ parameters={
20
  "max_new_tokens": int(max_new_tokens),
21
  "num_beams": 3,
22
  "do_sample": False
23
+ }
 
24
  )
25
+
26
+ # 3) Extract and clean up the generated text
27
+ # The API returns either a string or list of dicts
28
+ if isinstance(result, list) and "generated_text" in result[0]:
29
+ text = result[0]["generated_text"]
30
+ else:
31
+ text = result if isinstance(result, str) else ""
32
+ text = text.strip().capitalize()
33
  captions.append(text)
34
+
35
  return "\n\n".join(captions)
36
 
37
+ # --- Gradio UI ---
38
  with gr.Blocks(title="Florence‑2‑large Alt‑Text Generator") as demo:
39
  gr.Markdown("## Florence‑2‑large Alt‑Text Generator")
40
+ gr.Markdown("Powered by the Hugging Face Inference API—no tokens or local weights needed.")
41
 
42
  with gr.Row():
43
  file_input = gr.File(label="Upload Images", file_count="multiple", file_types=["image"])
 
48
  gallery = gr.Gallery(label="Image Preview", columns=4)
49
  output = gr.Textbox(label="Generated Alt‑Text", lines=6, show_copy_button=True)
50
 
51
+ # Preview immediately
52
  file_input.change(
53
  fn=lambda files: [Image.open(f.name).convert("RGB") for f in (files or [])],
54
  inputs=file_input,
 
63
  queue=True
64
  )
65
 
66
+ # Clear all
67
  clear_btn.click(
68
  fn=lambda: ([], 512, [], ""),
69
  outputs=[file_input, slider, gallery, output]