Spaces:
Running
Running
File size: 2,738 Bytes
e714957 8a690b3 993848f 8a690b3 01e8e77 993848f 8a690b3 993848f 8a690b3 b0ec2aa 8a690b3 f24f5b3 8a690b3 01e8e77 8a690b3 e714957 8a690b3 f24f5b3 8a690b3 f24f5b3 8a690b3 e714957 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | import gradio as gr
from core.model_loader import generate_caption
def build_ui(model, processor):
def run_caption(image, style):
if image is None:
return "Please upload an image."
image = image.convert("RGB")
return generate_caption(
model=model,
processor=processor,
image=image,
style=style
)
with gr.Blocks(
title="BLIP Image Captioning",
fill_height=True
) as demo:
gr.Markdown("## 🖼️ BLIP Image Captioning")
gr.Markdown(
"Generate captions or explanations from images using a CPU-only BLIP model (Salesforce/blip-image-captioning-large)."
)
# -----------------------------
# MAIN FIXED LAYOUT
# -----------------------------
with gr.Row(equal_height=True):
# LEFT: Image Column (Fixed)
with gr.Column(scale=1, min_width=420):
image_input = gr.Image(
type="pil",
label="Upload Image",
height=420,
elem_id="image-box"
)
generate_btn = gr.Button(
"Generate",
variant="primary"
)
# RIGHT: Controls + Output (Fixed)
with gr.Column(scale=1, min_width=420, elem_id="right-panel"):
style_select = gr.Dropdown(
choices=[
"Short Caption",
"Detailed Caption"
],
value="Detailed Caption",
label="Caption Style"
)
output_text = gr.Textbox(
label="Generated Output",
lines=6,
max_lines=8,
elem_id="output-box"
)
# -----------------------------
# EXAMPLES (SEPARATE SECTION)
# -----------------------------
gr.Markdown("### Examples")
gr.Examples(
examples=[
["./assets/zebra.jpg", "Short Caption"],
["./assets/cat.jpg", "Short Caption"],
["./assets/fridge.jpg", "Detailed Caption"],
["./assets/marriage.jpg", "Detailed Caption"],
["./assets/giraffe.jpg", "Detailed Caption"]
],
inputs=[image_input, style_select]
)
# -----------------------------
# EVENT
# -----------------------------
generate_btn.click(
fn=run_caption,
inputs=[image_input, style_select],
outputs=output_text
)
return demo
|