Spaces:
Running
Running
Project Implementation
Browse files- app.py +24 -0
- core/__init__.py +0 -0
- core/model_loader.py +47 -0
- requirements.txt +4 -0
- ui/__init__.py +0 -0
- ui/layout.py +53 -0
- ui/styles.py +16 -0
- ui/theme.py +28 -0
- utils/__init__.py +0 -0
- utils/warnings.py +4 -0
app.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from utils.warnings import suppress_warnings
|
| 2 |
+
from core.model_loader import load_model
|
| 3 |
+
from ui.theme import OrangeRedTheme
|
| 4 |
+
from ui.styles import CSS_STYLE
|
| 5 |
+
from ui.layout import build_ui
|
| 6 |
+
|
| 7 |
+
def main():
|
| 8 |
+
suppress_warnings()
|
| 9 |
+
|
| 10 |
+
model, processor = load_model()
|
| 11 |
+
theme = OrangeRedTheme()
|
| 12 |
+
|
| 13 |
+
demo = build_ui(model, processor)
|
| 14 |
+
demo.queue().launch(
|
| 15 |
+
theme=theme,
|
| 16 |
+
css=CSS_STYLE,
|
| 17 |
+
show_error=True,
|
| 18 |
+
server_name="0.0.0.0",
|
| 19 |
+
server_port=7860,
|
| 20 |
+
debug=True
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
if __name__ == "__main__":
|
| 24 |
+
main()
|
core/__init__.py
ADDED
|
File without changes
|
core/model_loader.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
+
|
| 4 |
+
MODEL_ID = "Salesforce/blip-image-captioning-base"
|
| 5 |
+
DEVICE = torch.device("cpu")
|
| 6 |
+
|
| 7 |
+
# Prompt templates
|
| 8 |
+
PROMPTS = {
|
| 9 |
+
"Short Caption": "a photo of",
|
| 10 |
+
"Detailed Caption": "a detailed description of",
|
| 11 |
+
"Creative Caption": "an artistic depiction of",
|
| 12 |
+
"Image Explanation": "explain what is happening in this image"
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
def load_model():
|
| 16 |
+
processor = BlipProcessor.from_pretrained(MODEL_ID)
|
| 17 |
+
model = BlipForConditionalGeneration.from_pretrained(MODEL_ID)
|
| 18 |
+
model.to(DEVICE)
|
| 19 |
+
model.eval()
|
| 20 |
+
return model, processor
|
| 21 |
+
|
| 22 |
+
def generate_caption(
|
| 23 |
+
model,
|
| 24 |
+
processor,
|
| 25 |
+
image,
|
| 26 |
+
style
|
| 27 |
+
):
|
| 28 |
+
prompt = PROMPTS.get(style, "a photo of")
|
| 29 |
+
|
| 30 |
+
inputs = processor(
|
| 31 |
+
images=image,
|
| 32 |
+
text=prompt,
|
| 33 |
+
return_tensors="pt"
|
| 34 |
+
).to(DEVICE)
|
| 35 |
+
|
| 36 |
+
max_len = 30 if style == "Short Caption" else 60
|
| 37 |
+
|
| 38 |
+
with torch.inference_mode():
|
| 39 |
+
output_ids = model.generate(
|
| 40 |
+
**inputs,
|
| 41 |
+
max_length=max_len
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
return processor.decode(
|
| 45 |
+
output_ids[0],
|
| 46 |
+
skip_special_tokens=True
|
| 47 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio>=4.0.0
|
| 2 |
+
torch>=2.0.0
|
| 3 |
+
transformers>=4.35.0
|
| 4 |
+
pillow
|
ui/__init__.py
ADDED
|
File without changes
|
ui/layout.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from core.model_loader import generate_caption
|
| 3 |
+
|
| 4 |
+
def build_ui(model, processor):
|
| 5 |
+
|
| 6 |
+
def run_caption(image, style):
|
| 7 |
+
if image is None:
|
| 8 |
+
return "Please upload an image."
|
| 9 |
+
image = image.convert("RGB")
|
| 10 |
+
return generate_caption(
|
| 11 |
+
model=model,
|
| 12 |
+
processor=processor,
|
| 13 |
+
image=image,
|
| 14 |
+
style=style
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
with gr.Blocks() as demo:
|
| 18 |
+
gr.Markdown("## 🖼️ BLIP Image Captioning (Zero-GPU)")
|
| 19 |
+
gr.Markdown(
|
| 20 |
+
"Generate captions or explanations from images using a CPU-only BLIP model."
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
with gr.Row():
|
| 24 |
+
image_input = gr.Image(
|
| 25 |
+
type="pil",
|
| 26 |
+
label="Upload Image"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
with gr.Column():
|
| 30 |
+
style_select = gr.Dropdown(
|
| 31 |
+
choices=[
|
| 32 |
+
"Short Caption",
|
| 33 |
+
"Detailed Caption",
|
| 34 |
+
"Creative Caption",
|
| 35 |
+
"Image Explanation"
|
| 36 |
+
],
|
| 37 |
+
value="Detailed Caption",
|
| 38 |
+
label="Caption Style"
|
| 39 |
+
)
|
| 40 |
+
output_text = gr.Textbox(
|
| 41 |
+
label="Generated Output",
|
| 42 |
+
lines=4
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
generate_btn = gr.Button("Generate")
|
| 46 |
+
|
| 47 |
+
generate_btn.click(
|
| 48 |
+
fn=run_caption,
|
| 49 |
+
inputs=[image_input, style_select],
|
| 50 |
+
outputs=output_text
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
return demo
|
ui/styles.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
CSS_STYLE = """
|
| 2 |
+
#container {
|
| 3 |
+
max-width: 1280px;
|
| 4 |
+
margin: auto;
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
@media (min-width: 1600px) {
|
| 8 |
+
#container {
|
| 9 |
+
max-width: 1440px;
|
| 10 |
+
}
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
#title h1 {
|
| 14 |
+
font-size: 2.4em !important;
|
| 15 |
+
}
|
| 16 |
+
"""
|
ui/theme.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from gradio.themes import Soft
|
| 2 |
+
from gradio.themes.utils import colors, fonts, sizes
|
| 3 |
+
|
| 4 |
+
colors.orange_red = colors.Color(
|
| 5 |
+
name="orange_red",
|
| 6 |
+
c50="#FFF0E5", c100="#FFE0CC", c200="#FFC299", c300="#FFA366",
|
| 7 |
+
c400="#FF8533", c500="#FF4500", c600="#E63E00", c700="#CC3700",
|
| 8 |
+
c800="#B33000", c900="#992900", c950="#802200",
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
class OrangeRedTheme(Soft):
|
| 12 |
+
def __init__(self):
|
| 13 |
+
super().__init__(
|
| 14 |
+
primary_hue=colors.orange_red,
|
| 15 |
+
secondary_hue=colors.orange_red,
|
| 16 |
+
neutral_hue=colors.slate,
|
| 17 |
+
text_size=sizes.text_lg,
|
| 18 |
+
font=(fonts.GoogleFont("Outfit"), "Arial", "sans-serif"),
|
| 19 |
+
font_mono=(fonts.GoogleFont("IBM Plex Mono"), "monospace"),
|
| 20 |
+
)
|
| 21 |
+
super().set(
|
| 22 |
+
body_background_fill="linear-gradient(135deg, *primary_200, *primary_100)",
|
| 23 |
+
button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)",
|
| 24 |
+
button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)",
|
| 25 |
+
button_primary_text_color="white",
|
| 26 |
+
block_border_width="3px",
|
| 27 |
+
block_shadow="*shadow_drop_lg",
|
| 28 |
+
)
|
utils/__init__.py
ADDED
|
File without changes
|
utils/warnings.py
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import warnings
|
| 2 |
+
|
| 3 |
+
def suppress_warnings():
|
| 4 |
+
warnings.filterwarnings("ignore")
|