Spaces:
Runtime error
Runtime error
Update app.py from anycoder
Browse files
app.py
CHANGED
|
@@ -1,111 +1,161 @@
|
|
| 1 |
-
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import time
|
| 3 |
-
from utils import calculate_similarity_mock
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
def
|
| 7 |
"""
|
| 8 |
-
|
| 9 |
-
|
| 10 |
"""
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
# Create the Gradio
|
| 27 |
-
# 🚨 NOTE: gr.Blocks() takes NO arguments in Gradio 6.
|
| 28 |
-
# Theme and title go in demo.launch()
|
| 29 |
with gr.Blocks() as demo:
|
|
|
|
|
|
|
| 30 |
|
| 31 |
-
# Header Section
|
| 32 |
with gr.Row():
|
| 33 |
with gr.Column():
|
| 34 |
-
gr.Markdown("
|
| 35 |
-
gr.
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
)
|
| 39 |
-
|
| 40 |
-
# Main Content
|
| 41 |
-
with gr.Row():
|
| 42 |
-
# Left Column: Inputs
|
| 43 |
-
with gr.Column(scale=1):
|
| 44 |
-
gr.Markdown("### 1. Target Audio")
|
| 45 |
-
target_input = gr.Audio(
|
| 46 |
-
sources=["upload", "microphone"],
|
| 47 |
-
type="filepath",
|
| 48 |
-
label="Upload Target Sound",
|
| 49 |
-
render=True
|
| 50 |
-
)
|
| 51 |
|
| 52 |
-
gr.
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
url_input_2 = gr.Textbox(
|
| 59 |
-
label="Source Audio URL #2",
|
| 60 |
-
placeholder="https://example.com/audio2.mp3",
|
| 61 |
-
lines=1
|
| 62 |
)
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
# Right Column: Outputs
|
| 67 |
-
with gr.Column(scale=1):
|
| 68 |
-
gr.Markdown("### Results")
|
| 69 |
-
# Label component is great for classification/similarity scores
|
| 70 |
-
similarity_output = gr.Label(
|
| 71 |
-
label="Similarity Score",
|
| 72 |
-
num_top_classes=2
|
| 73 |
-
)
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
api_visibility="public"
|
| 88 |
)
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
),
|
| 108 |
-
footer_links=[
|
| 109 |
-
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
|
| 110 |
-
]
|
| 111 |
-
)
|
|
|
|
| 1 |
+
<|begin_of_box|>import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import cv2
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
from PIL import Image
|
| 7 |
import time
|
|
|
|
| 8 |
|
| 9 |
+
# Simulated outpainting function (in a real application, this would use a proper model)
|
| 10 |
+
def outpaint_video_with_reference(input_video, reference_image, outpaint_ratio=0.5):
|
| 11 |
"""
|
| 12 |
+
Simulate video outpainting to match reference image height.
|
| 13 |
+
In a real implementation, this would use a diffusion model or similar.
|
| 14 |
"""
|
| 15 |
+
try:
|
| 16 |
+
# Create temporary files for processing
|
| 17 |
+
with tempfile.NamedTemporaryFile(suffix='.mp4', delete=False) as temp_video:
|
| 18 |
+
temp_video_path = temp_video.name
|
| 19 |
+
|
| 20 |
+
with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp_ref:
|
| 21 |
+
temp_ref_path = temp_ref.name
|
| 22 |
+
|
| 23 |
+
# Save uploaded files
|
| 24 |
+
with open(input_video, 'wb') as f:
|
| 25 |
+
f.write(open(input_video, 'rb').read())
|
| 26 |
+
|
| 27 |
+
with open(reference_image, 'wb') as f:
|
| 28 |
+
f.write(open(reference_image, 'rb').read())
|
| 29 |
+
|
| 30 |
+
# Get video and reference image dimensions
|
| 31 |
+
cap = cv2.VideoCapture(input_video)
|
| 32 |
+
ref_img = Image.open(reference_image)
|
| 33 |
+
ref_height = ref_img.height
|
| 34 |
+
ref_width = ref_img.width
|
| 35 |
+
|
| 36 |
+
# Get original video dimensions
|
| 37 |
+
original_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
| 38 |
+
original_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
| 39 |
+
|
| 40 |
+
# Calculate new dimensions to match reference height
|
| 41 |
+
new_height = ref_height
|
| 42 |
+
scale_factor = new_height / original_height
|
| 43 |
+
new_width = int(original_width * scale_factor)
|
| 44 |
+
|
| 45 |
+
# Create output video writer
|
| 46 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 47 |
+
out = cv2.VideoWriter(temp_video_path, fourcc, 30.0, (new_width, new_height))
|
| 48 |
+
|
| 49 |
+
frame_count = 0
|
| 50 |
+
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
|
| 51 |
+
|
| 52 |
+
while cap.isOpened():
|
| 53 |
+
ret, frame = cap.read()
|
| 54 |
+
if not ret:
|
| 55 |
+
break
|
| 56 |
+
|
| 57 |
+
# Resize frame to match new dimensions
|
| 58 |
+
resized_frame = cv2.resize(frame, (new_width, new_height))
|
| 59 |
+
|
| 60 |
+
# Simulate outpainting by adding a border that matches reference style
|
| 61 |
+
# In a real implementation, this would use AI to generate content
|
| 62 |
+
border_size = int(new_width * outpaint_ratio)
|
| 63 |
+
if border_size > 0:
|
| 64 |
+
# Create a border that tries to match the reference image style
|
| 65 |
+
border_color = np.mean(resized_frame, axis=(0, 1))
|
| 66 |
+
bordered_frame = cv2.copyMakeBorder(
|
| 67 |
+
resized_frame,
|
| 68 |
+
0, 0, border_size, border_size,
|
| 69 |
+
cv2.BORDER_CONSTANT,
|
| 70 |
+
value=border_color
|
| 71 |
+
)
|
| 72 |
+
out.write(bordered_frame)
|
| 73 |
+
else:
|
| 74 |
+
out.write(resized_frame)
|
| 75 |
+
|
| 76 |
+
frame_count += 1
|
| 77 |
+
if frame_count % 10 == 0:
|
| 78 |
+
yield f"Processing frame {frame_count}/{total_frames}..."
|
| 79 |
+
|
| 80 |
+
cap.release()
|
| 81 |
+
out.release()
|
| 82 |
+
|
| 83 |
+
# Return the processed video
|
| 84 |
+
yield temp_video_path
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
yield f"Error: {str(e)}"
|
| 88 |
+
finally:
|
| 89 |
+
# Clean up temporary files
|
| 90 |
+
try:
|
| 91 |
+
if 'temp_video_path' in locals() and os.path.exists(temp_video_path):
|
| 92 |
+
os.unlink(temp_video_path)
|
| 93 |
+
if 'temp_ref_path' in locals() and os.path.exists(temp_ref_path):
|
| 94 |
+
os.unlink(temp_ref_path)
|
| 95 |
+
except:
|
| 96 |
+
pass
|
| 97 |
|
| 98 |
+
# Create the Gradio interface
|
|
|
|
|
|
|
| 99 |
with gr.Blocks() as demo:
|
| 100 |
+
gr.Markdown("# 🎬 Video Outpainting with Reference Height Matching")
|
| 101 |
+
gr.Markdown("Upload a video and a reference image. The video will be outpainted to match the height of the reference image.")
|
| 102 |
|
|
|
|
| 103 |
with gr.Row():
|
| 104 |
with gr.Column():
|
| 105 |
+
gr.Markdown("### Input Video")
|
| 106 |
+
video_input = gr.Video(label="Upload Video", type="filepath")
|
| 107 |
+
gr.Markdown("### Reference Image")
|
| 108 |
+
reference_input = gr.Image(label="Upload Reference Image", type="filepath")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
+
outpaint_ratio = gr.Slider(
|
| 111 |
+
minimum=0.1,
|
| 112 |
+
maximum=1.0,
|
| 113 |
+
value=0.3,
|
| 114 |
+
step=0.1,
|
| 115 |
+
label="Outpaint Ratio (percentage of new width)"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
)
|
| 117 |
|
| 118 |
+
process_button = gr.Button("Process Video", variant="primary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
+
with gr.Column():
|
| 121 |
+
gr.Markdown("### Output")
|
| 122 |
+
output_video = gr.Video(label="Processed Video")
|
| 123 |
+
status_text = gr.Textbox(label="Status", interactive=False)
|
| 124 |
+
|
| 125 |
+
# Process function
|
| 126 |
+
def process_video(video, reference, ratio):
|
| 127 |
+
if not video or not reference:
|
| 128 |
+
return None, "Please upload both video and reference image"
|
| 129 |
+
|
| 130 |
+
status_text = ""
|
| 131 |
+
for status in outpaint_video_with_reference(video, reference, ratio):
|
| 132 |
+
if isinstance(status, str):
|
| 133 |
+
status_text = status
|
| 134 |
+
yield None, status_text
|
| 135 |
+
else:
|
| 136 |
+
yield status, "Processing complete!"
|
| 137 |
+
|
| 138 |
+
process_button.click(
|
| 139 |
+
fn=process_video,
|
| 140 |
+
inputs=[video_input, reference_input, outpaint_ratio],
|
| 141 |
+
outputs=[output_video, status_text],
|
| 142 |
api_visibility="public"
|
| 143 |
)
|
| 144 |
|
| 145 |
+
# Launch the app with modern theme
|
| 146 |
+
demo.launch(
|
| 147 |
+
theme=gr.themes.Soft(
|
| 148 |
+
primary_hue="blue",
|
| 149 |
+
secondary_hue="indigo",
|
| 150 |
+
neutral_hue="slate",
|
| 151 |
+
font=gr.themes.GoogleFont("Inter"),
|
| 152 |
+
text_size="lg",
|
| 153 |
+
spacing_size="lg",
|
| 154 |
+
radius_size="md"
|
| 155 |
+
).set(
|
| 156 |
+
button_primary_background_fill="*primary_600",
|
| 157 |
+
button_primary_background_fill_hover="*primary_700",
|
| 158 |
+
block_title_text_weight="600",
|
| 159 |
+
),
|
| 160 |
+
footer_links=[{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}]
|
| 161 |
+
)<|end_of_box|>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|