Update app.py
Browse files
app.py
CHANGED
|
@@ -7,12 +7,12 @@ from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration
|
|
| 7 |
# Instruction-tuned vision-language model
|
| 8 |
MODEL_NAME = "Qwen/Qwen2.5-VL-7B-Instruct"
|
| 9 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
processor = AutoProcessor.from_pretrained(MODEL_NAME)
|
| 13 |
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 14 |
MODEL_NAME,
|
| 15 |
-
torch_dtype=(torch.float16 if DEVICE=="cuda" else torch.float32),
|
|
|
|
| 16 |
device_map="auto"
|
| 17 |
)
|
| 18 |
|
|
@@ -23,19 +23,24 @@ def generate_alt_text(files, max_new_tokens):
|
|
| 23 |
"""
|
| 24 |
captions = []
|
| 25 |
for f in files or []:
|
| 26 |
-
# Load image
|
| 27 |
img = Image.open(f.name).convert("RGB")
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
inputs = processor(
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
outputs = model.generate(
|
| 33 |
**inputs,
|
| 34 |
max_new_tokens=int(max_new_tokens)
|
| 35 |
)
|
| 36 |
-
# Decode and
|
| 37 |
text = processor.decode(outputs[0], skip_special_tokens=True).strip()
|
| 38 |
-
|
|
|
|
|
|
|
| 39 |
return "\n\n".join(captions)
|
| 40 |
|
| 41 |
# --- Gradio Interface ---
|
|
@@ -87,4 +92,4 @@ with gr.Blocks(title="Qwen2.5-VL-7B-Instruct Alt Text Generator") as demo:
|
|
| 87 |
outputs=[file_input, max_new_tokens, gallery, output]
|
| 88 |
)
|
| 89 |
|
| 90 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 7 |
# Instruction-tuned vision-language model
|
| 8 |
MODEL_NAME = "Qwen/Qwen2.5-VL-7B-Instruct"
|
| 9 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
\# Load processor and model with remote code trust
|
| 11 |
+
processor = AutoProcessor.from_pretrained(MODEL_NAME, trust_remote_code=True)
|
|
|
|
| 12 |
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
|
| 13 |
MODEL_NAME,
|
| 14 |
+
torch_dtype=(torch.float16 if DEVICE == "cuda" else torch.float32),
|
| 15 |
+
trust_remote_code=True,
|
| 16 |
device_map="auto"
|
| 17 |
)
|
| 18 |
|
|
|
|
| 23 |
"""
|
| 24 |
captions = []
|
| 25 |
for f in files or []:
|
|
|
|
| 26 |
img = Image.open(f.name).convert("RGB")
|
| 27 |
+
# Prepare inputs: include a prompt for the instruction-tuned model
|
| 28 |
+
prompt = "Generate a concise, detailed alt text for the image."
|
| 29 |
+
inputs = processor(
|
| 30 |
+
images=img,
|
| 31 |
+
text=prompt,
|
| 32 |
+
return_tensors="pt"
|
| 33 |
+
).to(model.device)
|
| 34 |
+
# Generate with token limit
|
| 35 |
outputs = model.generate(
|
| 36 |
**inputs,
|
| 37 |
max_new_tokens=int(max_new_tokens)
|
| 38 |
)
|
| 39 |
+
# Decode and format
|
| 40 |
text = processor.decode(outputs[0], skip_special_tokens=True).strip()
|
| 41 |
+
if text:
|
| 42 |
+
text = text[0].upper() + text[1:]
|
| 43 |
+
captions.append(text)
|
| 44 |
return "\n\n".join(captions)
|
| 45 |
|
| 46 |
# --- Gradio Interface ---
|
|
|
|
| 92 |
outputs=[file_input, max_new_tokens, gallery, output]
|
| 93 |
)
|
| 94 |
|
| 95 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|