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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -26
app.py CHANGED
@@ -1,59 +1,63 @@
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()
 
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]
61
  )
 
 
62
 
63
  demo.launch()