Update app.py
Browse files
app.py
CHANGED
|
@@ -4,63 +4,71 @@ from huggingface_hub import InferenceApi
|
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
# --- Configuration ---
|
| 7 |
-
# Set HF_TOKEN in your Space
|
| 8 |
-
|
| 9 |
-
|
| 10 |
|
| 11 |
# Initialize the Hugging Face Inference API client
|
| 12 |
inference = InferenceApi(
|
| 13 |
-
repo_id=
|
| 14 |
-
token=
|
| 15 |
)
|
| 16 |
|
| 17 |
# --- Core Generation Function ---
|
| 18 |
-
def generate_alt_text(
|
| 19 |
"""
|
| 20 |
-
Call the HF Inference API to get alt text for each uploaded image.
|
| 21 |
"""
|
| 22 |
captions = []
|
| 23 |
-
for path in
|
| 24 |
# Read image bytes
|
| 25 |
-
with open(path, "rb") as
|
| 26 |
-
img_bytes =
|
| 27 |
-
# Invoke the hosted model
|
| 28 |
response = inference(
|
| 29 |
inputs=img_bytes,
|
| 30 |
params={"max_new_tokens": int(max_new_tokens)},
|
| 31 |
request_format="binary"
|
| 32 |
)
|
| 33 |
-
# Extract generated text
|
| 34 |
text = response[0].get("generated_text", "").strip()
|
| 35 |
if text:
|
| 36 |
text = text[0].upper() + text[1:]
|
| 37 |
captions.append(text)
|
| 38 |
-
return "
|
| 39 |
-
|
| 40 |
-
".join(captions) "\n\n".join(captions)
|
| 41 |
|
| 42 |
# --- Gradio Interface ---
|
| 43 |
-
with gr.Blocks(title="Qwen2.5-VL-7B
|
| 44 |
-
gr.Markdown("# Qwen2.5-VL-7B-Instruct Alt Text Generator (Inference API)")
|
| 45 |
-
gr.Markdown("
|
| 46 |
|
| 47 |
with gr.Row():
|
| 48 |
-
file_input = gr.File(
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
generate_btn = gr.Button("Generate Alt Text")
|
| 51 |
clear_btn = gr.Button("Clear All")
|
| 52 |
|
| 53 |
gallery = gr.Gallery(label="Image Preview", columns=4)
|
| 54 |
output = gr.Textbox(label="Generated Alt Texts", lines=10, show_copy_button=True)
|
| 55 |
|
| 56 |
-
# Preview images in the gallery
|
| 57 |
file_input.change(
|
| 58 |
fn=lambda paths: [Image.open(p).convert("RGB") for p in (paths or [])],
|
| 59 |
inputs=file_input,
|
| 60 |
outputs=gallery
|
| 61 |
)
|
| 62 |
|
| 63 |
-
# Generate alt text
|
| 64 |
generate_btn.click(
|
| 65 |
fn=generate_alt_text,
|
| 66 |
inputs=[file_input, max_new_tokens],
|
|
@@ -68,7 +76,7 @@ with gr.Blocks(title="Qwen2.5-VL-7B via Inference API") as demo:
|
|
| 68 |
queue=True
|
| 69 |
)
|
| 70 |
|
| 71 |
-
# Clear all
|
| 72 |
clear_btn.click(
|
| 73 |
fn=lambda: ([], 128, [], ""),
|
| 74 |
outputs=[file_input, max_new_tokens, gallery, output]
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
|
| 6 |
# --- Configuration ---
|
| 7 |
+
# Set HF_TOKEN in your Space Settings → Secrets
|
| 8 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
+
REPO_ID = "Qwen/Qwen2.5-VL-7B-Instruct"
|
| 10 |
|
| 11 |
# Initialize the Hugging Face Inference API client
|
| 12 |
inference = InferenceApi(
|
| 13 |
+
repo_id=REPO_ID,
|
| 14 |
+
token=HF_TOKEN,
|
| 15 |
)
|
| 16 |
|
| 17 |
# --- Core Generation Function ---
|
| 18 |
+
def generate_alt_text(file_paths, max_new_tokens):
|
| 19 |
"""
|
| 20 |
+
Call the HF Inference API to get alt text for each uploaded image path.
|
| 21 |
"""
|
| 22 |
captions = []
|
| 23 |
+
for path in file_paths or []:
|
| 24 |
# Read image bytes
|
| 25 |
+
with open(path, "rb") as f:
|
| 26 |
+
img_bytes = f.read()
|
| 27 |
+
# Invoke the hosted model
|
| 28 |
response = inference(
|
| 29 |
inputs=img_bytes,
|
| 30 |
params={"max_new_tokens": int(max_new_tokens)},
|
| 31 |
request_format="binary"
|
| 32 |
)
|
| 33 |
+
# Extract and clean the generated text
|
| 34 |
text = response[0].get("generated_text", "").strip()
|
| 35 |
if text:
|
| 36 |
text = text[0].upper() + text[1:]
|
| 37 |
captions.append(text)
|
| 38 |
+
return "\n\n".join(captions)
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# --- Gradio Interface ---
|
| 41 |
+
with gr.Blocks(title="Qwen2.5-VL-7B-Instruct Alt Text Generator") as demo:
|
| 42 |
+
gr.Markdown("## Qwen2.5-VL-7B-Instruct Alt Text Generator (via HF Inference API)")
|
| 43 |
+
gr.Markdown("Uses Hugging Face’s hosted API—no GPU memory required on the Space.")
|
| 44 |
|
| 45 |
with gr.Row():
|
| 46 |
+
file_input = gr.File(
|
| 47 |
+
label="Upload Images",
|
| 48 |
+
file_count="multiple",
|
| 49 |
+
file_types=["image"]
|
| 50 |
+
)
|
| 51 |
+
max_new_tokens = gr.Slider(
|
| 52 |
+
minimum=16,
|
| 53 |
+
maximum=512,
|
| 54 |
+
step=16,
|
| 55 |
+
value=128,
|
| 56 |
+
label="Max New Tokens"
|
| 57 |
+
)
|
| 58 |
generate_btn = gr.Button("Generate Alt Text")
|
| 59 |
clear_btn = gr.Button("Clear All")
|
| 60 |
|
| 61 |
gallery = gr.Gallery(label="Image Preview", columns=4)
|
| 62 |
output = gr.Textbox(label="Generated Alt Texts", lines=10, show_copy_button=True)
|
| 63 |
|
| 64 |
+
# Preview images in the gallery when files are selected
|
| 65 |
file_input.change(
|
| 66 |
fn=lambda paths: [Image.open(p).convert("RGB") for p in (paths or [])],
|
| 67 |
inputs=file_input,
|
| 68 |
outputs=gallery
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# Generate alt text when button clicked
|
| 72 |
generate_btn.click(
|
| 73 |
fn=generate_alt_text,
|
| 74 |
inputs=[file_input, max_new_tokens],
|
|
|
|
| 76 |
queue=True
|
| 77 |
)
|
| 78 |
|
| 79 |
+
# Clear all inputs and outputs
|
| 80 |
clear_btn.click(
|
| 81 |
fn=lambda: ([], 128, [], ""),
|
| 82 |
outputs=[file_input, max_new_tokens, gallery, output]
|