Update app.py
Browse files
app.py
CHANGED
|
@@ -1,83 +1,59 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from PIL import Image
|
| 4 |
-
from
|
| 5 |
|
| 6 |
-
# ---
|
| 7 |
-
|
| 8 |
-
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
"microsoft/Florence-2-large",
|
| 13 |
-
|
| 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 |
-
|
| 22 |
-
|
| 23 |
-
# --- Alt-Text Generation Function ---
|
| 24 |
def generate_alt_text(files, max_new_tokens):
|
| 25 |
captions = []
|
| 26 |
for f in files or []:
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 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 |
-
|
| 51 |
-
|
|
|
|
| 52 |
return "\n\n".join(captions)
|
| 53 |
|
| 54 |
-
# --- Gradio
|
| 55 |
-
with gr.Blocks(title="Florence
|
| 56 |
-
gr.Markdown("# Florence
|
| 57 |
-
gr.Markdown("
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
gallery = gr.Gallery(label="Preview", columns=4)
|
| 65 |
-
output
|
| 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()
|