Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- gradio_demo.py +79 -80
- test_gradio.py +36 -0
gradio_demo.py
CHANGED
|
@@ -5,7 +5,20 @@ This app allows users to upload images or videos and see the AI analysis results
|
|
| 5 |
in a user-friendly interface. It connects to the Modal-deployed Qwen3 Omni model.
|
| 6 |
"""
|
| 7 |
|
| 8 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
import json
|
| 10 |
import uuid
|
| 11 |
import tempfile
|
|
@@ -13,6 +26,8 @@ import os
|
|
| 13 |
from pathlib import Path
|
| 14 |
from typing import Dict, Any, Optional
|
| 15 |
|
|
|
|
|
|
|
| 16 |
# Import the main analysis function
|
| 17 |
DEMO_MODE = False
|
| 18 |
try:
|
|
@@ -301,51 +316,29 @@ def preview_media(media_file):
|
|
| 301 |
return gr.update(visible=False), gr.update(visible=False)
|
| 302 |
|
| 303 |
|
| 304 |
-
def
|
| 305 |
-
"""Show preview of uploaded media."""
|
| 306 |
-
if media_file is None:
|
| 307 |
-
return gr.update(visible=False), gr.update(visible=False)
|
| 308 |
-
|
| 309 |
-
# Detect media type
|
| 310 |
-
file_ext = media_file.split(".")[-1].lower() if isinstance(media_file, str) else ""
|
| 311 |
-
is_video = file_ext in ["mp4", "mov", "avi", "mkv", "webm", "m4v"]
|
| 312 |
-
is_image = file_ext in ["jpg", "jpeg", "png", "webp", "gif", "bmp"]
|
| 313 |
-
|
| 314 |
-
if is_image:
|
| 315 |
-
return gr.update(value=media_file, visible=True), gr.update(visible=False)
|
| 316 |
-
elif is_video:
|
| 317 |
-
return gr.update(visible=False), gr.update(value=media_file, visible=True)
|
| 318 |
-
else:
|
| 319 |
-
return gr.update(visible=False), gr.update(visible=False)
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
def process_media(media_file):
|
| 323 |
"""
|
| 324 |
Main processing function that analyzes the uploaded media.
|
| 325 |
-
Returns
|
| 326 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 327 |
if media_file is None:
|
| 328 |
empty_results = format_analysis_results({"error": "Please upload a media file"})
|
| 329 |
-
return
|
| 330 |
-
|
| 331 |
-
print(f"π¬ Processing media file: {media_file}")
|
| 332 |
-
|
| 333 |
-
# Show preview
|
| 334 |
-
file_ext = media_file.split(".")[-1].lower() if isinstance(media_file, str) else ""
|
| 335 |
-
is_video = file_ext in ["mp4", "mov", "avi", "mkv", "webm", "m4v"]
|
| 336 |
-
is_image = file_ext in ["jpg", "jpeg", "png", "webp", "gif", "bmp"]
|
| 337 |
|
| 338 |
# Run analysis
|
| 339 |
result = analyze_media(media_file)
|
| 340 |
formatted_results = format_analysis_results(result)
|
| 341 |
|
| 342 |
-
|
| 343 |
-
if is_image:
|
| 344 |
-
return (gr.update(value=media_file, visible=True), gr.update(visible=False)) + formatted_results
|
| 345 |
-
elif is_video:
|
| 346 |
-
return (gr.update(visible=False), gr.update(value=media_file, visible=True)) + formatted_results
|
| 347 |
-
else:
|
| 348 |
-
return (gr.update(visible=False), gr.update(visible=False)) + formatted_results
|
| 349 |
|
| 350 |
|
| 351 |
# Custom CSS for better styling
|
|
@@ -374,8 +367,19 @@ custom_css = """
|
|
| 374 |
}
|
| 375 |
"""
|
| 376 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
# Build the Gradio interface
|
| 378 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 379 |
gr.HTML("""
|
| 380 |
<div style="text-align: center; margin-bottom: 20px;">
|
| 381 |
<h1>π¬ Media Optimization AI</h1>
|
|
@@ -396,27 +400,21 @@ with gr.Blocks(css=custom_css, title="Media Optimization AI", theme=gr.themes.So
|
|
| 396 |
|
| 397 |
# Single large upload area at the top
|
| 398 |
with gr.Group():
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
|
|
|
|
|
|
| 402 |
type="filepath",
|
| 403 |
-
height=
|
|
|
|
| 404 |
)
|
| 405 |
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
height=400,
|
| 412 |
-
show_label=False
|
| 413 |
-
)
|
| 414 |
-
media_preview_video = gr.Video(
|
| 415 |
-
label="Preview",
|
| 416 |
-
visible=False,
|
| 417 |
-
height=400,
|
| 418 |
-
show_label=False
|
| 419 |
-
)
|
| 420 |
|
| 421 |
analyze_btn = gr.Button("π Analyze Media", variant="primary", size="lg", scale=1)
|
| 422 |
|
|
@@ -465,31 +463,27 @@ with gr.Blocks(css=custom_css, title="Media Optimization AI", theme=gr.themes.So
|
|
| 465 |
with gr.Tab("π Raw JSON"):
|
| 466 |
json_output = gr.Code(label="Full JSON Response", language="json")
|
| 467 |
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
highlights_output,
|
| 490 |
-
json_output
|
| 491 |
-
]
|
| 492 |
-
)
|
| 493 |
|
| 494 |
gr.Markdown("""
|
| 495 |
---
|
|
@@ -500,6 +494,11 @@ with gr.Blocks(css=custom_css, title="Media Optimization AI", theme=gr.themes.So
|
|
| 500 |
- RAD Score represents the overall viral potential (0-100)
|
| 501 |
""")
|
| 502 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 503 |
|
| 504 |
if __name__ == "__main__":
|
| 505 |
import sys
|
|
|
|
| 5 |
in a user-friendly interface. It connects to the Modal-deployed Qwen3 Omni model.
|
| 6 |
"""
|
| 7 |
|
| 8 |
+
import sys
|
| 9 |
+
import traceback
|
| 10 |
+
|
| 11 |
+
print("=" * 60)
|
| 12 |
+
print("π Starting Media Optimization Gradio App...")
|
| 13 |
+
print("=" * 60)
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
import gradio as gr
|
| 17 |
+
print("β
Gradio imported successfully")
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print(f"β Failed to import Gradio: {e}")
|
| 20 |
+
sys.exit(1)
|
| 21 |
+
|
| 22 |
import json
|
| 23 |
import uuid
|
| 24 |
import tempfile
|
|
|
|
| 26 |
from pathlib import Path
|
| 27 |
from typing import Dict, Any, Optional
|
| 28 |
|
| 29 |
+
print("β
Standard libraries imported")
|
| 30 |
+
|
| 31 |
# Import the main analysis function
|
| 32 |
DEMO_MODE = False
|
| 33 |
try:
|
|
|
|
| 316 |
return gr.update(visible=False), gr.update(visible=False)
|
| 317 |
|
| 318 |
|
| 319 |
+
def process_media(image_file, video_file):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
"""
|
| 321 |
Main processing function that analyzes the uploaded media.
|
| 322 |
+
Returns analysis results with persistent processing time.
|
| 323 |
"""
|
| 324 |
+
# Determine which file was uploaded
|
| 325 |
+
media_file = None
|
| 326 |
+
if image_file is not None:
|
| 327 |
+
media_file = image_file
|
| 328 |
+
print(f"π¬ Processing image: {media_file}")
|
| 329 |
+
elif video_file is not None:
|
| 330 |
+
media_file = video_file
|
| 331 |
+
print(f"π¬ Processing video: {media_file}")
|
| 332 |
+
|
| 333 |
if media_file is None:
|
| 334 |
empty_results = format_analysis_results({"error": "Please upload a media file"})
|
| 335 |
+
return empty_results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 336 |
|
| 337 |
# Run analysis
|
| 338 |
result = analyze_media(media_file)
|
| 339 |
formatted_results = format_analysis_results(result)
|
| 340 |
|
| 341 |
+
return formatted_results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
|
| 344 |
# Custom CSS for better styling
|
|
|
|
| 367 |
}
|
| 368 |
"""
|
| 369 |
|
| 370 |
+
print("=" * 60)
|
| 371 |
+
print("π¨ Building Gradio Interface...")
|
| 372 |
+
print("=" * 60)
|
| 373 |
+
|
| 374 |
# Build the Gradio interface
|
| 375 |
+
try:
|
| 376 |
+
demo = gr.Blocks(css=custom_css, title="Media Optimization AI", theme=gr.themes.Soft())
|
| 377 |
+
except Exception as e:
|
| 378 |
+
print(f"β Failed to create Blocks: {e}")
|
| 379 |
+
traceback.print_exc()
|
| 380 |
+
sys.exit(1)
|
| 381 |
+
|
| 382 |
+
with demo:
|
| 383 |
gr.HTML("""
|
| 384 |
<div style="text-align: center; margin-bottom: 20px;">
|
| 385 |
<h1>π¬ Media Optimization AI</h1>
|
|
|
|
| 400 |
|
| 401 |
# Single large upload area at the top
|
| 402 |
with gr.Group():
|
| 403 |
+
gr.Markdown("### π Upload Image or Video")
|
| 404 |
+
|
| 405 |
+
# Separate upload components for images and videos
|
| 406 |
+
media_image = gr.Image(
|
| 407 |
+
label="Upload Image",
|
| 408 |
type="filepath",
|
| 409 |
+
height=400,
|
| 410 |
+
sources=["upload"]
|
| 411 |
)
|
| 412 |
|
| 413 |
+
media_video = gr.Video(
|
| 414 |
+
label="Upload Video",
|
| 415 |
+
height=400,
|
| 416 |
+
sources=["upload"]
|
| 417 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 418 |
|
| 419 |
analyze_btn = gr.Button("π Analyze Media", variant="primary", size="lg", scale=1)
|
| 420 |
|
|
|
|
| 463 |
with gr.Tab("π Raw JSON"):
|
| 464 |
json_output = gr.Code(label="Full JSON Response", language="json")
|
| 465 |
|
| 466 |
+
try:
|
| 467 |
+
# Connect the analyze button to the processing function
|
| 468 |
+
analyze_btn.click(
|
| 469 |
+
fn=process_media,
|
| 470 |
+
inputs=[media_image, media_video],
|
| 471 |
+
outputs=[
|
| 472 |
+
processing_time_output, # Processing time (persistent)
|
| 473 |
+
summary_output,
|
| 474 |
+
viral_output,
|
| 475 |
+
engagement_output,
|
| 476 |
+
quality_output,
|
| 477 |
+
emotion_output,
|
| 478 |
+
platform_output,
|
| 479 |
+
highlights_output,
|
| 480 |
+
json_output
|
| 481 |
+
]
|
| 482 |
+
)
|
| 483 |
+
print("β
Analyze handler registered")
|
| 484 |
+
except Exception as e:
|
| 485 |
+
print(f"β Failed to register event handlers: {e}")
|
| 486 |
+
traceback.print_exc()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 487 |
|
| 488 |
gr.Markdown("""
|
| 489 |
---
|
|
|
|
| 494 |
- RAD Score represents the overall viral potential (0-100)
|
| 495 |
""")
|
| 496 |
|
| 497 |
+
print("=" * 60)
|
| 498 |
+
print("β
Gradio interface built successfully!")
|
| 499 |
+
print(f" Demo mode: {DEMO_MODE}")
|
| 500 |
+
print("=" * 60)
|
| 501 |
+
|
| 502 |
|
| 503 |
if __name__ == "__main__":
|
| 504 |
import sys
|
test_gradio.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Quick test to verify gradio_demo.py can be imported and has valid structure
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import sys
|
| 6 |
+
print("Testing gradio_demo.py import...")
|
| 7 |
+
|
| 8 |
+
try:
|
| 9 |
+
import gradio_demo
|
| 10 |
+
print("β
gradio_demo.py imported successfully")
|
| 11 |
+
|
| 12 |
+
# Check if demo exists
|
| 13 |
+
if hasattr(gradio_demo, 'demo'):
|
| 14 |
+
print("β
'demo' object found")
|
| 15 |
+
demo = gradio_demo.demo
|
| 16 |
+
|
| 17 |
+
# Try to get API info
|
| 18 |
+
try:
|
| 19 |
+
api_info = demo.get_api_info()
|
| 20 |
+
print(f"β
API info retrieved: {len(api_info)} endpoints found")
|
| 21 |
+
for endpoint_name, endpoint_info in api_info.items():
|
| 22 |
+
print(f" - {endpoint_name}")
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"β Failed to get API info: {e}")
|
| 25 |
+
import traceback
|
| 26 |
+
traceback.print_exc()
|
| 27 |
+
else:
|
| 28 |
+
print("β 'demo' object not found in module")
|
| 29 |
+
|
| 30 |
+
except Exception as e:
|
| 31 |
+
print(f"β Failed to import: {e}")
|
| 32 |
+
import traceback
|
| 33 |
+
traceback.print_exc()
|
| 34 |
+
sys.exit(1)
|
| 35 |
+
|
| 36 |
+
print("\nβ
All tests passed!")
|