Spaces:
Sleeping
Sleeping
Herdman - Unit 8 Assignment
Browse files- app.py +44 -197
- requirements.txt +3 -6
app.py
CHANGED
|
@@ -1,207 +1,54 @@
|
|
| 1 |
-
|
| 2 |
-
import torch
|
| 3 |
-
from diffusers import AutoPipelineForText2Image
|
| 4 |
-
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 5 |
from PIL import Image
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
-
|
| 10 |
-
-
|
| 11 |
-
"""
|
| 12 |
-
|
| 13 |
-
# model configuration
|
| 14 |
-
TEXT_TO_IMAGE_MODEL_ID = "stabilityai/sd-turbo"
|
| 15 |
-
CAPTION_MODEL_ID = "Salesforce/blip-image-captioning-base"
|
| 16 |
-
|
| 17 |
-
# device and dtype setup
|
| 18 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 19 |
-
dtype = torch.float16 if device == "cuda" else torch.float32
|
| 20 |
-
|
| 21 |
-
# load text to image pipeline (SD-Turbo)
|
| 22 |
-
text2img_pipe = AutoPipelineForText2Image.from_pretrained(
|
| 23 |
-
TEXT_TO_IMAGE_MODEL_ID,
|
| 24 |
-
torch_dtype=dtype,
|
| 25 |
)
|
| 26 |
-
text2img_pipe = text2img_pipe.to(device)
|
| 27 |
-
|
| 28 |
-
# memory optimization
|
| 29 |
-
if hasattr(text2img_pipe, "enable_attention_slicing"):
|
| 30 |
-
text2img_pipe.enable_attention_slicing()
|
| 31 |
-
|
| 32 |
-
# load image captioning model (BLIP)
|
| 33 |
-
caption_processor = BlipProcessor.from_pretrained(CAPTION_MODEL_ID)
|
| 34 |
-
caption_model = BlipForConditionalGeneration.from_pretrained(
|
| 35 |
-
CAPTION_MODEL_ID
|
| 36 |
-
).to(device)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
# text to image generation function
|
| 40 |
-
def generate_image(prompt, steps, guidance, seed):
|
| 41 |
-
"""
|
| 42 |
-
Generate an image from a text prompt using the SD-Turbo pipeline.
|
| 43 |
-
"""
|
| 44 |
-
if not prompt or not prompt.strip():
|
| 45 |
-
prompt = "a watercolor painting of a quiet cabin in the forest at sunrise"
|
| 46 |
-
|
| 47 |
-
# optional seeding for reproducibility
|
| 48 |
-
generator = None
|
| 49 |
-
if seed is not None and str(seed).strip() != "":
|
| 50 |
-
try:
|
| 51 |
-
seed_int = int(seed)
|
| 52 |
-
generator = torch.Generator(device=device).manual_seed(seed_int)
|
| 53 |
-
except ValueError:
|
| 54 |
-
# ignore invalid seeds and use a random generator instead
|
| 55 |
-
generator = None
|
| 56 |
-
|
| 57 |
-
# expose a slider for steps
|
| 58 |
-
# even though SD-Turbo is noted to be designed for a low number of steps (1–4)
|
| 59 |
-
steps = int(steps)
|
| 60 |
-
guidance = float(guidance)
|
| 61 |
-
|
| 62 |
-
result = text2img_pipe(
|
| 63 |
-
prompt=prompt,
|
| 64 |
-
num_inference_steps=steps,
|
| 65 |
-
guidance_scale=guidance,
|
| 66 |
-
generator=generator,
|
| 67 |
-
).images[0]
|
| 68 |
-
|
| 69 |
-
return result
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
|
| 73 |
-
def caption_image(image, max_length, num_beams):
|
| 74 |
-
"""
|
| 75 |
-
Generate a text caption for an uploaded image.
|
| 76 |
-
"""
|
| 77 |
if image is None:
|
| 78 |
-
return "Please upload an image
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
)
|
| 89 |
-
caption = caption_processor.decode(output_ids[0], skip_special_tokens=True).strip()
|
| 90 |
-
return caption
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
# build Gradio UI
|
| 94 |
-
# includes blocks and tabs
|
| 95 |
-
with gr.Blocks() as assignment8:
|
| 96 |
-
gr.Markdown(
|
| 97 |
-
"""
|
| 98 |
-
# Multimodal Assignment 8: Text to Image and Image Captioning
|
| 99 |
-
|
| 100 |
-
This Space demonstrates two multimodal capabilities:
|
| 101 |
-
1. Text to Image generation using Stability AI's SD-Turbo model
|
| 102 |
-
2. Image to Text captioning using a BLIP image captioning model
|
| 103 |
-
"""
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
-
with gr.Tab("Text to Image"):
|
| 107 |
-
with gr.Row():
|
| 108 |
-
with gr.Column(scale=1):
|
| 109 |
-
prompt_in = gr.Textbox(
|
| 110 |
-
label="Prompt",
|
| 111 |
-
lines=5, # taller so full prompts are visible
|
| 112 |
-
placeholder="Describe the image you want the model to generate.",
|
| 113 |
-
)
|
| 114 |
-
steps_in = gr.Slider(
|
| 115 |
-
minimum=1,
|
| 116 |
-
maximum=8,
|
| 117 |
-
value=4,
|
| 118 |
-
step=1,
|
| 119 |
-
label="Number of inference steps",
|
| 120 |
-
)
|
| 121 |
-
guidance_in = gr.Slider(
|
| 122 |
-
minimum=0.0,
|
| 123 |
-
maximum=3.0,
|
| 124 |
-
value=0.0,
|
| 125 |
-
step=0.1,
|
| 126 |
-
label="Guidance scale",
|
| 127 |
-
)
|
| 128 |
-
seed_in = gr.Textbox(
|
| 129 |
-
label="Seed (optional, integer)",
|
| 130 |
-
placeholder="Ex. 42. Leave blank for random seed.",
|
| 131 |
-
)
|
| 132 |
-
generate_button = gr.Button("Generate Image")
|
| 133 |
-
|
| 134 |
-
with gr.Column(scale=1):
|
| 135 |
-
image_out = gr.Image(label="Generated Image")
|
| 136 |
-
|
| 137 |
-
gr.Examples(
|
| 138 |
-
examples=[
|
| 139 |
-
[
|
| 140 |
-
"a focused student working at a computer late at night",
|
| 141 |
-
4,
|
| 142 |
-
3.0,
|
| 143 |
-
"",
|
| 144 |
-
],
|
| 145 |
-
[
|
| 146 |
-
"a golden retriever playing in a field of flowers",
|
| 147 |
-
4,
|
| 148 |
-
0.0,
|
| 149 |
-
"42",
|
| 150 |
-
],
|
| 151 |
-
[
|
| 152 |
-
"a martial artist performing a high kick, dynamic motion",
|
| 153 |
-
5,
|
| 154 |
-
1.0,
|
| 155 |
-
"",
|
| 156 |
-
],
|
| 157 |
-
],
|
| 158 |
-
inputs=[prompt_in, steps_in, guidance_in, seed_in],
|
| 159 |
-
outputs=image_out,
|
| 160 |
-
fn=generate_image,
|
| 161 |
-
cache_examples=False,
|
| 162 |
-
)
|
| 163 |
-
|
| 164 |
-
generate_button.click(
|
| 165 |
-
fn=generate_image,
|
| 166 |
-
inputs=[prompt_in, steps_in, guidance_in, seed_in],
|
| 167 |
-
outputs=image_out,
|
| 168 |
-
)
|
| 169 |
-
|
| 170 |
-
with gr.Tab("Image to Text (Captioning)"):
|
| 171 |
-
with gr.Row():
|
| 172 |
-
with gr.Column(scale=1):
|
| 173 |
-
image_in = gr.Image(
|
| 174 |
-
type="pil",
|
| 175 |
-
label="Upload an image",
|
| 176 |
-
)
|
| 177 |
-
max_length_in = gr.Slider(
|
| 178 |
-
minimum=16,
|
| 179 |
-
maximum=64,
|
| 180 |
-
value=32,
|
| 181 |
-
step=4,
|
| 182 |
-
label="Max caption length",
|
| 183 |
-
)
|
| 184 |
-
num_beams_in = gr.Slider(
|
| 185 |
-
minimum=1,
|
| 186 |
-
maximum=6,
|
| 187 |
-
value=4,
|
| 188 |
-
step=1,
|
| 189 |
-
label="Beam search width",
|
| 190 |
-
)
|
| 191 |
-
caption_button = gr.Button("Generate Caption")
|
| 192 |
-
|
| 193 |
-
with gr.Column(scale=1):
|
| 194 |
-
caption_out = gr.Textbox(
|
| 195 |
-
label="Generated Caption",
|
| 196 |
-
lines=4,
|
| 197 |
-
)
|
| 198 |
-
|
| 199 |
-
caption_button.click(
|
| 200 |
-
fn=caption_image,
|
| 201 |
-
inputs=[image_in, max_length_in, num_beams_in],
|
| 202 |
-
outputs=caption_out,
|
| 203 |
-
)
|
| 204 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
if __name__ == "__main__":
|
| 207 |
-
|
|
|
|
| 1 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
+
import gradio as gr
|
| 4 |
|
| 5 |
+
# VQA pipeline
|
| 6 |
+
vqa_pipeline = pipeline(
|
| 7 |
+
"visual-question-answering",
|
| 8 |
+
model="dandelin/vilt-b32-finetuned-vqa"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
# English -> Korean translator (reliable alternative)
|
| 12 |
+
translator = pipeline(
|
| 13 |
+
"translation",
|
| 14 |
+
model="facebook/m2m100_418M"
|
| 15 |
+
)
|
| 16 |
|
| 17 |
+
def vqa_answer(image: Image.Image, question: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
if image is None:
|
| 19 |
+
return "Please upload an image."
|
| 20 |
+
if not question or not question.strip():
|
| 21 |
+
return "Please enter a question about the image."
|
| 22 |
+
|
| 23 |
+
# VQA
|
| 24 |
+
result = vqa_pipeline(image=image, question=question)
|
| 25 |
+
top = result[0]
|
| 26 |
+
answer = top["answer"]
|
| 27 |
+
score = top.get("score", None)
|
| 28 |
+
score_str = f"{score:.3f}" if isinstance(score, (float, int)) else "N/A"
|
| 29 |
+
|
| 30 |
+
# Translate EN → KO
|
| 31 |
+
translated = translator(
|
| 32 |
+
answer,
|
| 33 |
+
src_lang="en",
|
| 34 |
+
tgt_lang="ko"
|
| 35 |
+
)[0]["translation_text"]
|
| 36 |
+
|
| 37 |
+
return (
|
| 38 |
+
f"Answer (EN): {answer} (score: {score_str})\n\n"
|
| 39 |
+
f"번역 (KO): {translated}"
|
| 40 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
demo = gr.Interface(
|
| 43 |
+
fn=vqa_answer,
|
| 44 |
+
inputs=[
|
| 45 |
+
gr.Image(type="pil", label="Upload an image"),
|
| 46 |
+
gr.Textbox(lines=2, label="Question about the image")
|
| 47 |
+
],
|
| 48 |
+
outputs=gr.Textbox(label="VQA Result (English + Korean)"),
|
| 49 |
+
title="Visual Question Answering + Korean Translation",
|
| 50 |
+
description="Upload an image, ask a question, and see the answer in English and Korean."
|
| 51 |
+
)
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,8 +1,5 @@
|
|
| 1 |
-
gradio>=4.0.0
|
| 2 |
-
torch
|
| 3 |
-
torchvision
|
| 4 |
-
Pillow
|
| 5 |
-
diffusers
|
| 6 |
transformers
|
| 7 |
accelerate
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
transformers
|
| 2 |
accelerate
|
| 3 |
+
pillow
|
| 4 |
+
sentencepiece
|
| 5 |
+
gradio
|