Mudrock10 commited on
Commit
92d2cce
·
verified ·
1 Parent(s): c070575

Update app.py from anycoder

Browse files
Files changed (1) hide show
  1. app.py +145 -95
app.py CHANGED
@@ -1,111 +1,161 @@
1
- import gradio as gr
 
 
 
 
 
2
  import time
3
- from utils import calculate_similarity_mock
4
 
5
- # Define the main processing function
6
- def compare_audio(target_audio, url_1, url_2):
7
  """
8
- Takes a target audio file path and two URLs.
9
- Returns a dictionary of similarity scores.
10
  """
11
- if target_audio is None:
12
- raise gr.Error("Please upload a target audio file first.")
13
-
14
- if not url_1 or not url_2:
15
- raise gr.Warning("Please provide both source URLs for comparison.")
16
-
17
- # In a real app, you would download the audio from the URLs here
18
- # and run them through a model like CLAP or Wav2Vec.
19
- # We are using a simulated backend for this demo.
20
-
21
- results = calculate_similarity_mock(target_audio, url_1, url_2)
22
-
23
- # Return the results for the Label component
24
- return results
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- # Create the Gradio 6 Application
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("# 🎵 Audio Similarity Matcher")
35
- gr.Markdown(
36
- "Upload a target sound and compare it against two source audio URLs to find the best match. "
37
- "[Built with anycoder](https://huggingface.co/spaces/akhaliq/anycoder)"
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.Markdown("### 2. Compare Against Source URLs")
53
- url_input_1 = gr.Textbox(
54
- label="Source Audio URL #1",
55
- placeholder="https://example.com/audio1.mp3",
56
- lines=1
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
- submit_btn = gr.Button("Analyze Similarity 🚀", variant="primary", size="lg")
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
- # JSON output for detailed debugging data
76
- json_output = gr.JSON(
77
- label="Detailed Analysis Data",
78
- visible=False
79
- )
80
-
81
- # Event Listeners
82
- # 🚨 Gradio 6 uses 'api_visibility' instead of just api_name
83
- submit_btn.click(
84
- fn=compare_audio,
85
- inputs=[target_input, url_input_1, url_input_2],
86
- outputs=[similarity_output],
 
 
 
 
 
 
 
 
 
 
87
  api_visibility="public"
88
  )
89
 
90
- # Add example data to help users test quickly
91
- gr.Examples(
92
- examples=[
93
- [None, "https://www2.cs.uic.edu/~i101/SoundFiles/BabyElephantWalk60.wav", "https://www2.cs.uic.edu/~i101/SoundFiles/CantinaBand3.wav"]
94
- ],
95
- inputs=[target_input, url_input_1, url_input_2]
96
- )
97
-
98
- # Launch the application
99
- # 🚨 CRITICAL: Theme, CSS, and app configs go here in Gradio 6
100
- if __name__ == "__main__":
101
- demo.launch(
102
- theme=gr.themes.Soft(
103
- primary_hue="indigo",
104
- secondary_hue="blue",
105
- neutral_hue="slate",
106
- radius_size="lg"
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|>