Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,8 @@
|
|
| 1 |
-
import
|
| 2 |
import subprocess
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
| 6 |
-
import gradio as gr
|
| 7 |
-
from gradio.themes import Soft
|
| 8 |
-
from gradio.themes.utils import colors, fonts, sizes
|
| 9 |
-
from typing import Iterable
|
| 10 |
-
|
| 11 |
-
# --- Theme and CSS Definition ---
|
| 12 |
-
|
| 13 |
-
colors.steel_blue = colors.Color(
|
| 14 |
-
name="steel_blue",
|
| 15 |
-
c50="#EBF3F8",
|
| 16 |
-
c100="#D3E5F0",
|
| 17 |
-
c200="#A8CCE1",
|
| 18 |
-
c300="#7DB3D2",
|
| 19 |
-
c400="#529AC3",
|
| 20 |
-
c500="#4682B4", # SteelBlue base color
|
| 21 |
-
c600="#3E72A0",
|
| 22 |
-
c700="#36638C",
|
| 23 |
-
c800="#2E5378",
|
| 24 |
-
c900="#264364",
|
| 25 |
-
c950="#1E3450",
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
class SteelBlueTheme(Soft):
|
| 29 |
-
def __init__(
|
| 30 |
-
self,
|
| 31 |
-
*,
|
| 32 |
-
primary_hue: colors.Color | str = colors.gray,
|
| 33 |
-
secondary_hue: colors.Color | str = colors.steel_blue,
|
| 34 |
-
neutral_hue: colors.Color | str = colors.slate,
|
| 35 |
-
text_size: sizes.Size | str = sizes.text_lg,
|
| 36 |
-
font: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 37 |
-
fonts.GoogleFont("Outfit"), "Arial", "sans-serif",
|
| 38 |
-
),
|
| 39 |
-
font_mono: fonts.Font | str | Iterable[fonts.Font | str] = (
|
| 40 |
-
fonts.GoogleFont("IBM Plex Mono"), "ui-monospace", "monospace",
|
| 41 |
-
),
|
| 42 |
-
):
|
| 43 |
-
super().__init__(
|
| 44 |
-
primary_hue=primary_hue,
|
| 45 |
-
secondary_hue=secondary_hue,
|
| 46 |
-
neutral_hue=neutral_hue,
|
| 47 |
-
text_size=text_size,
|
| 48 |
-
font=font,
|
| 49 |
-
font_mono=font_mono,
|
| 50 |
-
)
|
| 51 |
-
super().set(
|
| 52 |
-
body_background_fill="linear-gradient(135deg, *primary_100, *primary_200)",
|
| 53 |
-
body_background_fill_dark="linear-gradient(135deg, *primary_800, *primary_900)",
|
| 54 |
-
button_primary_background_fill="linear-gradient(90deg, *secondary_500, *secondary_600)",
|
| 55 |
-
button_primary_background_fill_hover="linear-gradient(90deg, *secondary_600, *secondary_700)",
|
| 56 |
-
button_primary_text_color="white",
|
| 57 |
-
slider_color="*secondary_500",
|
| 58 |
-
slider_color_dark="*secondary_600",
|
| 59 |
-
block_title_text_weight="600",
|
| 60 |
-
block_border_width="2px",
|
| 61 |
-
block_shadow="*shadow_drop_lg",
|
| 62 |
-
button_shadow="*shadow_drop_lg",
|
| 63 |
-
button_large_padding="12px",
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
# Instantiate the theme
|
| 67 |
-
steel_blue_theme = SteelBlueTheme()
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
# --- Model and App Setup ---
|
| 71 |
|
| 72 |
# Attempt to install flash-attn
|
| 73 |
try:
|
|
@@ -100,6 +35,13 @@ except Exception as e:
|
|
| 100 |
def describe_image(uploaded_image, model_choice):
|
| 101 |
"""
|
| 102 |
Generates a detailed description of the input image using the selected model.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
"""
|
| 104 |
if uploaded_image is None:
|
| 105 |
return "Please upload an image."
|
|
@@ -147,28 +89,37 @@ if device == "cpu":
|
|
| 147 |
|
| 148 |
# Define examples
|
| 149 |
examples = [
|
| 150 |
-
["images/2.
|
| 151 |
-
["images/1.
|
| 152 |
-
["images/3.
|
| 153 |
-
["images/4.
|
| 154 |
]
|
| 155 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
# Create the Gradio interface with Blocks
|
| 158 |
-
with gr.Blocks(theme=
|
| 159 |
gr.Markdown("# **Florence-2 Models Image Captions**")
|
| 160 |
gr.Markdown(description)
|
| 161 |
with gr.Row():
|
| 162 |
# Left column: Input image and Generate button
|
| 163 |
-
with gr.Column(
|
| 164 |
-
image_input = gr.Image(label="Upload Image", type="pil"
|
| 165 |
-
generate_btn = gr.Button("Generate Caption",
|
| 166 |
gr.Examples(examples=examples, inputs=[image_input])
|
| 167 |
# Right column: Model choice, output, and examples
|
| 168 |
-
with gr.Column(
|
| 169 |
model_choice = gr.Radio(["Florence-2-base", "Florence-2-large"], label="Model Choice", value="Florence-2-base")
|
| 170 |
-
|
| 171 |
-
|
| 172 |
# Connect the button to the function
|
| 173 |
generate_btn.click(fn=describe_image, inputs=[image_input, model_choice], outputs=output)
|
| 174 |
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
import torch
|
| 4 |
from PIL import Image
|
| 5 |
from transformers import AutoProcessor, AutoModelForCausalLM
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# Attempt to install flash-attn
|
| 8 |
try:
|
|
|
|
| 35 |
def describe_image(uploaded_image, model_choice):
|
| 36 |
"""
|
| 37 |
Generates a detailed description of the input image using the selected model.
|
| 38 |
+
|
| 39 |
+
Args:
|
| 40 |
+
uploaded_image (PIL.Image.Image): The image to describe.
|
| 41 |
+
model_choice (str): The model to use, either "Base" or "Large".
|
| 42 |
+
|
| 43 |
+
Returns:
|
| 44 |
+
str: A detailed textual description of the image or an error message.
|
| 45 |
"""
|
| 46 |
if uploaded_image is None:
|
| 47 |
return "Please upload an image."
|
|
|
|
| 89 |
|
| 90 |
# Define examples
|
| 91 |
examples = [
|
| 92 |
+
["images/2.png", "Florence-2-large"],
|
| 93 |
+
["images/1.png", "Florence-2-base"],
|
| 94 |
+
["images/3.png", "Florence-2-large"],
|
| 95 |
+
["images/4.png", "Florence-2-large"]
|
| 96 |
]
|
| 97 |
|
| 98 |
+
css = """
|
| 99 |
+
.submit-btn {
|
| 100 |
+
background-color: #4682B4 !important;
|
| 101 |
+
color: white !important;
|
| 102 |
+
}
|
| 103 |
+
.submit-btn:hover {
|
| 104 |
+
background-color: #87CEEB !important;
|
| 105 |
+
}
|
| 106 |
+
"""
|
| 107 |
|
| 108 |
# Create the Gradio interface with Blocks
|
| 109 |
+
with gr.Blocks(theme="bethecloud/storj_theme", css=css) as demo:
|
| 110 |
gr.Markdown("# **Florence-2 Models Image Captions**")
|
| 111 |
gr.Markdown(description)
|
| 112 |
with gr.Row():
|
| 113 |
# Left column: Input image and Generate button
|
| 114 |
+
with gr.Column():
|
| 115 |
+
image_input = gr.Image(label="Upload Image", type="pil")
|
| 116 |
+
generate_btn = gr.Button("Generate Caption", elem_classes="submit-btn")
|
| 117 |
gr.Examples(examples=examples, inputs=[image_input])
|
| 118 |
# Right column: Model choice, output, and examples
|
| 119 |
+
with gr.Column():
|
| 120 |
model_choice = gr.Radio(["Florence-2-base", "Florence-2-large"], label="Model Choice", value="Florence-2-base")
|
| 121 |
+
with gr.Row():
|
| 122 |
+
output = gr.Textbox(label="Generated Caption", lines=4, show_copy_button=True)
|
| 123 |
# Connect the button to the function
|
| 124 |
generate_btn.click(fn=describe_image, inputs=[image_input, model_choice], outputs=output)
|
| 125 |
|