Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ import subprocess
|
|
6 |
import gradio as gr
|
7 |
import uuid
|
8 |
from dotenv import load_dotenv
|
|
|
9 |
|
10 |
# Load environment variables
|
11 |
load_dotenv()
|
@@ -15,6 +16,7 @@ B_KEY = os.getenv("B_KEY")
|
|
15 |
|
16 |
# URLs
|
17 |
API_URL = os.getenv("API_URL")
|
|
|
18 |
|
19 |
def lipsync_api_call(video_url, audio_url):
|
20 |
headers = {
|
@@ -80,6 +82,45 @@ def combine_audio_video(video_path, audio_path, output_path):
|
|
80 |
|
81 |
subprocess.run(cmd, check=True)
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
def process_video(video_url, audio_url, progress=gr.Progress()):
|
84 |
if not audio_url:
|
85 |
return None, "No audio URL provided"
|
@@ -88,10 +129,20 @@ def process_video(video_url, audio_url, progress=gr.Progress()):
|
|
88 |
|
89 |
session_id = str(uuid.uuid4())
|
90 |
|
91 |
-
progress(0.2, desc="Processing
|
92 |
|
93 |
try:
|
94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
95 |
job_data = lipsync_api_call(video_url, audio_url)
|
96 |
|
97 |
if "error" in job_data or "message" in job_data:
|
@@ -99,7 +150,7 @@ def process_video(video_url, audio_url, progress=gr.Progress()):
|
|
99 |
|
100 |
job_id = job_data["id"]
|
101 |
|
102 |
-
progress(0.
|
103 |
result_url = check_job_status(job_id)
|
104 |
|
105 |
if result_url:
|
@@ -142,7 +193,7 @@ def create_interface():
|
|
142 |
gr.Markdown("# JSON 2")
|
143 |
with gr.Row():
|
144 |
with gr.Column():
|
145 |
-
video_url_input = gr.Textbox(label="Video URL")
|
146 |
audio_url_input = gr.Textbox(label="Audio URL")
|
147 |
generate_btn = gr.Button("Generate Video")
|
148 |
with gr.Column():
|
|
|
6 |
import gradio as gr
|
7 |
import uuid
|
8 |
from dotenv import load_dotenv
|
9 |
+
from urllib.parse import urlparse
|
10 |
|
11 |
# Load environment variables
|
12 |
load_dotenv()
|
|
|
16 |
|
17 |
# URLs
|
18 |
API_URL = os.getenv("API_URL")
|
19 |
+
UPLOAD_URL = os.getenv("UPLOAD_URL") # Make sure to add this to your .env file
|
20 |
|
21 |
def lipsync_api_call(video_url, audio_url):
|
22 |
headers = {
|
|
|
82 |
|
83 |
subprocess.run(cmd, check=True)
|
84 |
|
85 |
+
def is_image_url(url):
|
86 |
+
parsed = urlparse(url)
|
87 |
+
path = parsed.path.lower()
|
88 |
+
return path.endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))
|
89 |
+
|
90 |
+
def create_video_from_image(image_url, output_path, duration=10):
|
91 |
+
# Download the image
|
92 |
+
response = requests.get(image_url)
|
93 |
+
if response.status_code != 200:
|
94 |
+
raise Exception("Failed to download the image")
|
95 |
+
|
96 |
+
temp_image_path = f"temp_image_{uuid.uuid4()}.jpg"
|
97 |
+
with open(temp_image_path, 'wb') as f:
|
98 |
+
f.write(response.content)
|
99 |
+
|
100 |
+
# Create a 10-second video from the image
|
101 |
+
cmd = [
|
102 |
+
'ffmpeg', '-loop', '1', '-i', temp_image_path,
|
103 |
+
'-c:v', 'libx264', '-t', str(duration), '-pix_fmt', 'yuv420p',
|
104 |
+
'-vf', 'scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2',
|
105 |
+
'-y', output_path
|
106 |
+
]
|
107 |
+
subprocess.run(cmd, check=True)
|
108 |
+
|
109 |
+
# Clean up the temporary image file
|
110 |
+
os.remove(temp_image_path)
|
111 |
+
|
112 |
+
return output_path
|
113 |
+
|
114 |
+
def upload_file(file_path):
|
115 |
+
with open(file_path, 'rb') as file:
|
116 |
+
files = {'fileToUpload': (os.path.basename(file_path), file)}
|
117 |
+
data = {'reqtype': 'fileupload'}
|
118 |
+
response = requests.post(UPLOAD_URL, files=files, data=data)
|
119 |
+
|
120 |
+
if response.status_code == 200:
|
121 |
+
return response.text.strip()
|
122 |
+
return None
|
123 |
+
|
124 |
def process_video(video_url, audio_url, progress=gr.Progress()):
|
125 |
if not audio_url:
|
126 |
return None, "No audio URL provided"
|
|
|
129 |
|
130 |
session_id = str(uuid.uuid4())
|
131 |
|
132 |
+
progress(0.2, desc="Processing media...")
|
133 |
|
134 |
try:
|
135 |
+
if is_image_url(video_url):
|
136 |
+
progress(0.3, desc="Converting image to video...")
|
137 |
+
video_path = f"temp_video_{session_id}.mp4"
|
138 |
+
create_video_from_image(video_url, video_path)
|
139 |
+
progress(0.4, desc="Uploading converted video...")
|
140 |
+
video_url = upload_file(video_path)
|
141 |
+
if not video_url:
|
142 |
+
raise Exception("Failed to upload converted video")
|
143 |
+
os.remove(video_path)
|
144 |
+
|
145 |
+
progress(0.5, desc="Initiating lipsync...")
|
146 |
job_data = lipsync_api_call(video_url, audio_url)
|
147 |
|
148 |
if "error" in job_data or "message" in job_data:
|
|
|
150 |
|
151 |
job_id = job_data["id"]
|
152 |
|
153 |
+
progress(0.6, desc="Processing lipsync...")
|
154 |
result_url = check_job_status(job_id)
|
155 |
|
156 |
if result_url:
|
|
|
193 |
gr.Markdown("# JSON 2")
|
194 |
with gr.Row():
|
195 |
with gr.Column():
|
196 |
+
video_url_input = gr.Textbox(label="Video or Image URL")
|
197 |
audio_url_input = gr.Textbox(label="Audio URL")
|
198 |
generate_btn = gr.Button("Generate Video")
|
199 |
with gr.Column():
|