Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,41 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import torch
|
| 4 |
-
from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration, BitsAndBytesConfig
|
| 5 |
|
| 6 |
# --- Configuration ---
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
bnb_4bit_quant_type="nf4",
|
| 15 |
-
bnb_4bit_compute_dtype=torch.float16
|
| 16 |
)
|
| 17 |
|
| 18 |
-
# ---
|
| 19 |
-
processor = AutoProcessor.from_pretrained(
|
| 20 |
-
MODEL_NAME,
|
| 21 |
-
trust_remote_code=True
|
| 22 |
-
)
|
| 23 |
-
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 24 |
-
MODEL_NAME,
|
| 25 |
-
quantization_config=bnb_config,
|
| 26 |
-
device_map="auto",
|
| 27 |
-
torch_dtype=torch.float16,
|
| 28 |
-
trust_remote_code=True
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
# --- Alt Text Generation Function ---
|
| 32 |
def generate_alt_text(files, max_new_tokens):
|
| 33 |
captions = []
|
| 34 |
for f in files or []:
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
return_tensors="pt"
|
| 43 |
-
).to(model.device)
|
| 44 |
-
|
| 45 |
-
# Generate with quantized model
|
| 46 |
-
outputs = model.generate(
|
| 47 |
-
**inputs,
|
| 48 |
-
max_new_tokens=int(max_new_tokens)
|
| 49 |
)
|
| 50 |
-
|
| 51 |
-
#
|
| 52 |
-
text = processor.decode(outputs[0], skip_special_tokens=True).strip()
|
| 53 |
if text:
|
| 54 |
text = text[0].upper() + text[1:]
|
| 55 |
captions.append(text)
|
| 56 |
return "\n\n".join(captions)
|
| 57 |
|
| 58 |
# --- Gradio Interface ---
|
| 59 |
-
with gr.Blocks(title="Qwen2.5-VL-7B
|
| 60 |
-
gr.Markdown("# Qwen2.5-VL-7B-Instruct
|
| 61 |
-
gr.Markdown(
|
| 62 |
|
| 63 |
with gr.Row():
|
| 64 |
file_input = gr.File(
|
|
@@ -83,14 +60,15 @@ with gr.Blocks(title="Qwen2.5-VL-7B-Instruct (4-bit) Alt Text Generator") as dem
|
|
| 83 |
show_copy_button=True
|
| 84 |
)
|
| 85 |
|
| 86 |
-
#
|
| 87 |
file_input.change(
|
| 88 |
-
fn=lambda files: [
|
| 89 |
inputs=file_input,
|
| 90 |
-
outputs=gallery
|
|
|
|
| 91 |
)
|
| 92 |
|
| 93 |
-
# Generate
|
| 94 |
generate_btn.click(
|
| 95 |
fn=generate_alt_text,
|
| 96 |
inputs=[file_input, max_new_tokens],
|
|
@@ -104,4 +82,4 @@ with gr.Blocks(title="Qwen2.5-VL-7B-Instruct (4-bit) Alt Text Generator") as dem
|
|
| 104 |
outputs=[file_input, max_new_tokens, gallery, output]
|
| 105 |
)
|
| 106 |
|
| 107 |
-
demo.launch(
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceApi
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# --- Configuration ---
|
| 6 |
+
# Make sure to set HF_TOKEN in your Space Settings → Secrets
|
| 7 |
+
hf_token = os.getenv("HF_TOKEN")
|
| 8 |
+
repo_id = "Qwen/Qwen2.5-VL-7B-Instruct"
|
| 9 |
|
| 10 |
+
# Initialize remote inference client
|
| 11 |
+
inference = InferenceApi(
|
| 12 |
+
repo_id=repo_id,
|
| 13 |
+
token=hf_token,
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
|
| 16 |
+
# --- Core Generation Function ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
def generate_alt_text(files, max_new_tokens):
|
| 18 |
captions = []
|
| 19 |
for f in files or []:
|
| 20 |
+
# Read raw image bytes
|
| 21 |
+
img_bytes = f.read()
|
| 22 |
+
# Call the hosted model
|
| 23 |
+
response = inference(
|
| 24 |
+
inputs=img_bytes,
|
| 25 |
+
parameters={"max_new_tokens": int(max_new_tokens)},
|
| 26 |
+
request_format="binary"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
)
|
| 28 |
+
text = response[0].get("generated_text", "").strip()
|
| 29 |
+
# Capitalize first letter for readability
|
|
|
|
| 30 |
if text:
|
| 31 |
text = text[0].upper() + text[1:]
|
| 32 |
captions.append(text)
|
| 33 |
return "\n\n".join(captions)
|
| 34 |
|
| 35 |
# --- Gradio Interface ---
|
| 36 |
+
with gr.Blocks(title="Qwen2.5-VL-7B via Inference API") as demo:
|
| 37 |
+
gr.Markdown("# Qwen2.5-VL-7B-Instruct Alt Text Generator (Inference API)")
|
| 38 |
+
gr.Markdown("Uses Hugging Face Inference API, no GPU memory on the Space required.")
|
| 39 |
|
| 40 |
with gr.Row():
|
| 41 |
file_input = gr.File(
|
|
|
|
| 60 |
show_copy_button=True
|
| 61 |
)
|
| 62 |
|
| 63 |
+
# Show image previews
|
| 64 |
file_input.change(
|
| 65 |
+
fn=lambda files: [f.read() and None or None for f in (files or [])],
|
| 66 |
inputs=file_input,
|
| 67 |
+
outputs=gallery,
|
| 68 |
+
preprocess=False
|
| 69 |
)
|
| 70 |
|
| 71 |
+
# Generate captions
|
| 72 |
generate_btn.click(
|
| 73 |
fn=generate_alt_text,
|
| 74 |
inputs=[file_input, max_new_tokens],
|
|
|
|
| 82 |
outputs=[file_input, max_new_tokens, gallery, output]
|
| 83 |
)
|
| 84 |
|
| 85 |
+
demo.launch()
|