Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -147,108 +147,277 @@ def generate_description(image, prompt, max_length):
|
|
| 147 |
|
| 148 |
import gradio as gr
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
padding: 30px;
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
}
|
| 165 |
-
|
| 166 |
-
color: #0f766e;
|
| 167 |
text-align: center;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
font-size: 2.2em;
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
background: #ffffff;
|
| 173 |
-
border:
|
| 174 |
-
border-radius: 10px;
|
| 175 |
padding: 20px;
|
| 176 |
margin-bottom: 20px;
|
| 177 |
-
|
| 178 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
font-size: 1.05em;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
line-height: 1.6;
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
| 185 |
color: white !important;
|
| 186 |
border-radius: 8px !important;
|
| 187 |
padding: 12px 24px !important;
|
| 188 |
font-weight: 600 !important;
|
| 189 |
-
font-size:
|
| 190 |
border: none !important;
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
}
|
| 196 |
-
.
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
"""
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
with gr.Row():
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 214 |
)
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
label="🎤 Record Voice (optional)",
|
| 227 |
-
elem_classes="input-label"
|
| 228 |
-
)
|
| 229 |
-
model_choice = gr.Dropdown(
|
| 230 |
-
choices=["BLIP", "OmniPhi"],
|
| 231 |
-
label="Select Model",
|
| 232 |
-
value="BLIP",
|
| 233 |
-
elem_classes="input-label"
|
| 234 |
-
)
|
| 235 |
-
submit_btn = gr.Button("✨ Generate Description")
|
| 236 |
-
|
| 237 |
-
with gr.Group(elem_classes="output-container"):
|
| 238 |
-
output = gr.Textbox(
|
| 239 |
-
label="Generated Description",
|
| 240 |
-
show_label=True,
|
| 241 |
-
lines=5
|
| 242 |
-
)
|
| 243 |
-
|
| 244 |
submit_btn.click(
|
| 245 |
fn=generate_fn,
|
| 246 |
inputs=[image_input, text_input, audio_input, model_choice],
|
| 247 |
outputs=output
|
| 248 |
)
|
| 249 |
-
|
| 250 |
return iface
|
| 251 |
|
|
|
|
| 252 |
if __name__ == "__main__":
|
| 253 |
-
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
import gradio as gr
|
| 149 |
|
| 150 |
+
# Gradio interface
|
| 151 |
+
def create_gradio_interface(generate_fn, color_theme=COLOR_THEME):
|
| 152 |
+
# Set color variables based on theme
|
| 153 |
+
if color_theme == "blue":
|
| 154 |
+
primary_gradient = "linear-gradient(145deg, #e0f2fe, #dbeafe)"
|
| 155 |
+
header_gradient = "linear-gradient(135deg, #bfdbfe, #93c5fd)" # Blue gradient for header
|
| 156 |
+
button_gradient = "linear-gradient(135deg, #3b82f6, #1d4ed8)"
|
| 157 |
+
button_hover_gradient = "linear-gradient(135deg, #2563eb, #1e40af)"
|
| 158 |
+
primary_color = "#1e40af"
|
| 159 |
+
icon_color = "#2563eb"
|
| 160 |
+
shadow_color = "rgba(59, 130, 246, 0.15)"
|
| 161 |
+
button_shadow = "rgba(29, 78, 216, 0.25)"
|
| 162 |
+
else:
|
| 163 |
+
primary_gradient = "linear-gradient(145deg, #fff7ed, #ffedd5)"
|
| 164 |
+
header_gradient = "linear-gradient(135deg, #fed7aa, #fdba74)" # Orange gradient for header
|
| 165 |
+
button_gradient = "linear-gradient(135deg, #f97316, #ea580c)"
|
| 166 |
+
button_hover_gradient = "linear-gradient(135deg, #ea580c, #c2410c)"
|
| 167 |
+
primary_color = "#9a3412"
|
| 168 |
+
icon_color = "#ea580c"
|
| 169 |
+
shadow_color = "rgba(249, 115, 22, 0.15)"
|
| 170 |
+
button_shadow = "rgba(234, 88, 12, 0.25)"
|
| 171 |
+
|
| 172 |
+
|
| 173 |
+
# Gradio interface
|
| 174 |
+
def create_gradio_interface(generate_fn, color_theme=COLOR_THEME):
|
| 175 |
+
# Set color variables based on theme
|
| 176 |
+
if color_theme == "blue":
|
| 177 |
+
primary_gradient = "linear-gradient(145deg, #e0f2fe, #dbeafe)"
|
| 178 |
+
header_gradient = "linear-gradient(135deg, #bfdbfe, #93c5fd)"
|
| 179 |
+
header_background = "#dbeafe" # Light blue for section headers
|
| 180 |
+
button_gradient = "linear-gradient(135deg, #3b82f6, #1d4ed8)"
|
| 181 |
+
button_hover_gradient = "linear-gradient(135deg, #2563eb, #1e40af)"
|
| 182 |
+
primary_color = "#1e40af"
|
| 183 |
+
icon_color = "#2563eb"
|
| 184 |
+
shadow_color = "rgba(59, 130, 246, 0.15)"
|
| 185 |
+
button_shadow = "rgba(29, 78, 216, 0.25)"
|
| 186 |
+
else:
|
| 187 |
+
primary_gradient = "linear-gradient(145deg, #fff7ed, #ffedd5)"
|
| 188 |
+
header_gradient = "linear-gradient(135deg, #fed7aa, #fdba74)"
|
| 189 |
+
header_background = "#ffedd5" # Light orange for section headers
|
| 190 |
+
button_gradient = "linear-gradient(135deg, #f97316, #ea580c)"
|
| 191 |
+
button_hover_gradient = "linear-gradient(135deg, #ea580c, #c2410c)"
|
| 192 |
+
primary_color = "#9a3412"
|
| 193 |
+
icon_color = "#ea580c"
|
| 194 |
+
shadow_color = "rgba(249, 115, 22, 0.15)"
|
| 195 |
+
button_shadow = "rgba(234, 88, 12, 0.25)"
|
| 196 |
+
|
| 197 |
+
# Custom CSS with dynamic color variables
|
| 198 |
+
custom_css = f"""
|
| 199 |
+
body {{
|
| 200 |
+
font-family: 'Inter', 'Segoe UI', sans-serif;
|
| 201 |
+
background-color: #f8fafc;
|
| 202 |
+
}}
|
| 203 |
+
.container {{
|
| 204 |
+
background: {primary_gradient};
|
| 205 |
+
border-radius: 16px;
|
| 206 |
padding: 30px;
|
| 207 |
+
max-width: 1200px;
|
| 208 |
+
margin: 0 auto;
|
| 209 |
+
box-shadow: 0 10px 25px {shadow_color};
|
| 210 |
+
}}
|
| 211 |
+
.app-header {{
|
|
|
|
| 212 |
text-align: center;
|
| 213 |
+
margin-bottom: 30px;
|
| 214 |
+
background: {header_gradient};
|
| 215 |
+
border-radius: 12px;
|
| 216 |
+
padding: 20px;
|
| 217 |
+
box-shadow: 0 4px 12px {shadow_color};
|
| 218 |
+
}}
|
| 219 |
+
.app-title {{
|
| 220 |
+
color: {primary_color};
|
| 221 |
font-size: 2.2em;
|
| 222 |
+
font-weight: 700;
|
| 223 |
+
margin-bottom: 10px;
|
| 224 |
+
}}
|
| 225 |
+
.app-description {{
|
| 226 |
+
color: #334155;
|
| 227 |
+
font-size: 1.1em;
|
| 228 |
+
line-height: 1.5;
|
| 229 |
+
max-width: 700px;
|
| 230 |
+
margin: 0 auto;
|
| 231 |
+
}}
|
| 232 |
+
.card {{
|
| 233 |
background: #ffffff;
|
| 234 |
+
border-radius: 12px;
|
|
|
|
| 235 |
padding: 20px;
|
| 236 |
margin-bottom: 20px;
|
| 237 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.05);
|
| 238 |
+
border: 1px solid rgba(226, 232, 240, 0.8);
|
| 239 |
+
transition: transform 0.2s, box-shadow 0.2s;
|
| 240 |
+
height: 100%;
|
| 241 |
+
}}
|
| 242 |
+
.card:hover {{
|
| 243 |
+
transform: translateY(-2px);
|
| 244 |
+
box-shadow: 0 6px 16px rgba(0,0,0,0.08);
|
| 245 |
+
}}
|
| 246 |
+
.input-label {{
|
| 247 |
+
color: {primary_color};
|
| 248 |
+
font-weight: 600;
|
| 249 |
+
margin-bottom: 8px;
|
| 250 |
font-size: 1.05em;
|
| 251 |
+
background: {header_background}; /* Add background to section headers */
|
| 252 |
+
padding: 5px 10px;
|
| 253 |
+
border-radius: 6px;
|
| 254 |
+
display: inline-block;
|
| 255 |
+
}}
|
| 256 |
+
.output-card {{
|
| 257 |
+
background: #ffffff;
|
| 258 |
+
border-radius: 12px;
|
| 259 |
+
padding: 25px;
|
| 260 |
+
border: 1px solid rgba(226, 232, 240, 0.8);
|
| 261 |
+
box-shadow: 0 4px 15px rgba(0,0,0,0.05);
|
| 262 |
+
height: 100%;
|
| 263 |
+
display: flex;
|
| 264 |
+
flex-direction: column;
|
| 265 |
+
}}
|
| 266 |
+
.output-content {{
|
| 267 |
+
font-size: 1.1em;
|
| 268 |
line-height: 1.6;
|
| 269 |
+
color: #1e293b;
|
| 270 |
+
flex-grow: 1;
|
| 271 |
+
}}
|
| 272 |
+
.btn-generate {{
|
| 273 |
+
background: {button_gradient} !important;
|
| 274 |
color: white !important;
|
| 275 |
border-radius: 8px !important;
|
| 276 |
padding: 12px 24px !important;
|
| 277 |
font-weight: 600 !important;
|
| 278 |
+
font-size: 1.05em !important;
|
| 279 |
border: none !important;
|
| 280 |
+
box-shadow: 0 4px 12px {button_shadow} !important;
|
| 281 |
+
transition: all 0.3s ease !important;
|
| 282 |
+
width: 100% !important;
|
| 283 |
+
margin-top: 15px;
|
| 284 |
+
}}
|
| 285 |
+
.btn-generate:hover {{
|
| 286 |
+
background: {button_hover_gradient} !important;
|
| 287 |
+
box-shadow: 0 6px 16px {button_shadow} !important;
|
| 288 |
+
transform: translateY(-2px) !important;
|
| 289 |
+
}}
|
| 290 |
+
.footer {{
|
| 291 |
+
text-align: center;
|
| 292 |
+
margin-top: 30px;
|
| 293 |
+
color: #64748b;
|
| 294 |
+
font-size: 0.9em;
|
| 295 |
+
}}
|
| 296 |
+
.model-selector {{
|
| 297 |
+
margin-bottom: 15px;
|
| 298 |
+
}}
|
| 299 |
+
.input-icon {{
|
| 300 |
+
font-size: 1.5em;
|
| 301 |
+
margin-right: 8px;
|
| 302 |
+
color: {icon_color};
|
| 303 |
+
}}
|
| 304 |
+
.divider {{
|
| 305 |
+
border-top: 1px solid #e2e8f0;
|
| 306 |
+
margin: 15px 0;
|
| 307 |
+
}}
|
| 308 |
+
.input-section {{
|
| 309 |
+
height: 100%;
|
| 310 |
+
}}
|
| 311 |
+
.result-heading {{
|
| 312 |
+
margin-bottom: 15px;
|
| 313 |
+
color: {primary_color};
|
| 314 |
+
background: {header_background}; /* Add background to result header */
|
| 315 |
+
padding: 5px 10px;
|
| 316 |
+
border-radius: 6px;
|
| 317 |
+
display: inline-block;
|
| 318 |
+
}}
|
| 319 |
"""
|
| 320 |
|
| 321 |
+
# Create Blocks interface with improved structure and parallel layout
|
| 322 |
+
with gr.Blocks(css=custom_css) as iface:
|
| 323 |
+
with gr.Group():
|
| 324 |
+
icon = "🔷" if color_theme == "blue" else "🔶"
|
| 325 |
+
app_name = "OmniPhi Blue" if color_theme == "blue" else "OmniPhi Orange"
|
| 326 |
+
|
| 327 |
+
gr.Markdown(
|
| 328 |
+
f"""
|
| 329 |
+
<div class="app-header">
|
| 330 |
+
<div class="app-title">{icon} {app_name}</div>
|
| 331 |
+
<div class="app-description">Advanced Multi-Modal AI with BLIP or Custom Model Integration. Upload an image and provide instructions through text or voice to generate detailed descriptions.</div>
|
| 332 |
+
</div>
|
| 333 |
+
"""
|
| 334 |
+
)
|
| 335 |
+
|
| 336 |
+
# Main content in a 2-column layout (inputs and output side by side)
|
| 337 |
with gr.Row():
|
| 338 |
+
# Left column for all inputs
|
| 339 |
+
with gr.Column(scale=3):
|
| 340 |
+
with gr.Group():
|
| 341 |
+
# Image upload card
|
| 342 |
+
with gr.Group():
|
| 343 |
+
gr.Markdown('<span class="input-icon">🖼️</span><span class="input-label">Upload Image</span>')
|
| 344 |
+
image_input = gr.Image(
|
| 345 |
+
type="pil",
|
| 346 |
+
label=None
|
| 347 |
+
)
|
| 348 |
+
|
| 349 |
+
# Text and voice input card
|
| 350 |
+
with gr.Group():
|
| 351 |
+
gr.Markdown('<span class="input-icon">💬</span><span class="input-label">Text Instruction</span>')
|
| 352 |
+
text_input = gr.Textbox(
|
| 353 |
+
label=None,
|
| 354 |
+
placeholder="e.g., Describe this image in detail, focusing on the environment...",
|
| 355 |
+
lines=3
|
| 356 |
+
)
|
| 357 |
+
|
| 358 |
+
gr.Markdown('<div class="divider"></div>')
|
| 359 |
+
|
| 360 |
+
gr.Markdown('<span class="input-icon">🎙️</span><span class="input-label">Voice Instruction (optional)</span>')
|
| 361 |
+
audio_input = gr.Audio(
|
| 362 |
+
type="microphone",
|
| 363 |
+
label=None
|
| 364 |
+
)
|
| 365 |
+
|
| 366 |
+
gr.Markdown('<div class="divider"></div>')
|
| 367 |
+
|
| 368 |
+
gr.Markdown('<span class="input-icon">⚙️</span><span class="input-label">Model Selection</span>')
|
| 369 |
+
with gr.Group():
|
| 370 |
+
model_choice = gr.Radio(
|
| 371 |
+
choices=["BLIP", "OmniPhi"],
|
| 372 |
+
value="BLIP",
|
| 373 |
+
label=None,
|
| 374 |
+
interactive=True
|
| 375 |
+
)
|
| 376 |
+
|
| 377 |
+
submit_btn = gr.Button("Generate Description")
|
| 378 |
+
|
| 379 |
+
# Right column for output
|
| 380 |
+
with gr.Column(scale=2):
|
| 381 |
+
with gr.Group():
|
| 382 |
+
gr.Markdown('<span class="input-icon">✨</span><span class="input-label result-heading">Generated Description</span>')
|
| 383 |
+
output = gr.Textbox(
|
| 384 |
+
label=None,
|
| 385 |
+
lines=12,
|
| 386 |
+
placeholder="Your description will appear here after generation..."
|
| 387 |
)
|
| 388 |
+
|
| 389 |
+
# Footer
|
| 390 |
+
gr.Markdown(
|
| 391 |
+
f"""
|
| 392 |
+
<div class="footer">
|
| 393 |
+
Powered by OmniPhi Technology • Upload your image and provide instructions through text or voice
|
| 394 |
+
</div>
|
| 395 |
+
"""
|
| 396 |
+
)
|
| 397 |
+
|
| 398 |
+
# Connect the button to the function
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 399 |
submit_btn.click(
|
| 400 |
fn=generate_fn,
|
| 401 |
inputs=[image_input, text_input, audio_input, model_choice],
|
| 402 |
outputs=output
|
| 403 |
)
|
| 404 |
+
|
| 405 |
return iface
|
| 406 |
|
| 407 |
+
# Main execution
|
| 408 |
if __name__ == "__main__":
|
| 409 |
+
# Load models
|
| 410 |
+
transcriber = initialize_transcriber(WHISPER_MODEL)
|
| 411 |
+
blip_model, blip_processor = load_blip(BLIP_MODEL, DEVICE, TORCH_DTYPE)
|
| 412 |
+
clip_model, clip_processor = load_clip(CLIP_MODEL, DEVICE, TORCH_DTYPE)
|
| 413 |
+
omniphi_model, omniphi_tokenizer = load_omniphi(CHECKPOINT_DIR, PHI_MODEL, CLIP_MODEL, DEVICE)
|
| 414 |
+
|
| 415 |
+
# Define generate function
|
| 416 |
+
generate_fn = lambda image, text_prompt, audio, model_choice: generate_description(
|
| 417 |
+
image, text_prompt, audio, model_choice, transcriber, blip_model, blip_processor,
|
| 418 |
+
clip_model, clip_processor, omniphi_model, omniphi_tokenizer, DEVICE
|
| 419 |
+
)
|
| 420 |
+
|
| 421 |
+
# Launch Gradio interface
|
| 422 |
+
iface = create_gradio_interface(generate_fn, color_theme=COLOR_THEME)
|
| 423 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|