Update app.py
Browse files
app.py
CHANGED
@@ -17,16 +17,6 @@ B_KEY = os.getenv("B_KEY")
|
|
17 |
API_URL = os.getenv("API_URL")
|
18 |
UPLOAD_URL = os.getenv("UPLOAD_URL")
|
19 |
|
20 |
-
def upload_file(file_path):
|
21 |
-
with open(file_path, 'rb') as file:
|
22 |
-
files = {'fileToUpload': (os.path.basename(file_path), file)}
|
23 |
-
data = {'reqtype': 'fileupload'}
|
24 |
-
response = requests.post(UPLOAD_URL, files=files, data=data)
|
25 |
-
|
26 |
-
if response.status_code == 200:
|
27 |
-
return response.text.strip()
|
28 |
-
return None
|
29 |
-
|
30 |
def lipsync_api_call(video_url, audio_url):
|
31 |
headers = {
|
32 |
"Content-Type": "application/json",
|
@@ -95,9 +85,9 @@ def combine_audio_video(video_path, audio_path, output_path):
|
|
95 |
|
96 |
subprocess.run(cmd, check=True)
|
97 |
|
98 |
-
def process_video(video_url,
|
99 |
-
if not
|
100 |
-
return None, "No audio
|
101 |
if not video_url:
|
102 |
return None, "No video URL provided"
|
103 |
|
@@ -106,12 +96,6 @@ def process_video(video_url, audio_path, progress=gr.Progress()):
|
|
106 |
progress(0.2, desc="Processing video...")
|
107 |
|
108 |
try:
|
109 |
-
progress(0.3, desc="Uploading audio file...")
|
110 |
-
audio_url = upload_file(audio_path)
|
111 |
-
|
112 |
-
if not audio_url:
|
113 |
-
raise Exception("Failed to upload audio file")
|
114 |
-
|
115 |
progress(0.4, desc="Initiating lipsync...")
|
116 |
job_data = lipsync_api_call(video_url, audio_url)
|
117 |
|
@@ -143,11 +127,18 @@ def process_video(video_url, audio_path, progress=gr.Progress()):
|
|
143 |
with open(temp_video_path, "wb") as f:
|
144 |
f.write(video_response.content)
|
145 |
|
|
|
|
|
|
|
|
|
|
|
|
|
146 |
output_path = f"output_{session_id}.mp4"
|
147 |
-
combine_audio_video(temp_video_path,
|
148 |
|
149 |
-
# Clean up temporary
|
150 |
os.remove(temp_video_path)
|
|
|
151 |
|
152 |
progress(1.0, desc="Complete!")
|
153 |
return output_path, f"Used fallback method. Original error: {str(e)}"
|
@@ -160,22 +151,22 @@ def create_interface():
|
|
160 |
with gr.Row():
|
161 |
with gr.Column():
|
162 |
video_url_input = gr.Textbox(label="Video URL")
|
163 |
-
|
164 |
generate_btn = gr.Button("Generate Video")
|
165 |
with gr.Column():
|
166 |
video_output = gr.Video(label="Generated Video")
|
167 |
status_output = gr.Textbox(label="Status", interactive=False)
|
168 |
|
169 |
-
def on_generate(video_url,
|
170 |
-
if not
|
171 |
-
return None, "Please
|
172 |
if not video_url:
|
173 |
return None, "Please provide a video URL."
|
174 |
-
return process_video(video_url,
|
175 |
|
176 |
generate_btn.click(
|
177 |
fn=on_generate,
|
178 |
-
inputs=[video_url_input,
|
179 |
outputs=[video_output, status_output]
|
180 |
)
|
181 |
|
|
|
17 |
API_URL = os.getenv("API_URL")
|
18 |
UPLOAD_URL = os.getenv("UPLOAD_URL")
|
19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
def lipsync_api_call(video_url, audio_url):
|
21 |
headers = {
|
22 |
"Content-Type": "application/json",
|
|
|
85 |
|
86 |
subprocess.run(cmd, check=True)
|
87 |
|
88 |
+
def process_video(video_url, audio_url, progress=gr.Progress()):
|
89 |
+
if not audio_url:
|
90 |
+
return None, "No audio URL provided"
|
91 |
if not video_url:
|
92 |
return None, "No video URL provided"
|
93 |
|
|
|
96 |
progress(0.2, desc="Processing video...")
|
97 |
|
98 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
progress(0.4, desc="Initiating lipsync...")
|
100 |
job_data = lipsync_api_call(video_url, audio_url)
|
101 |
|
|
|
127 |
with open(temp_video_path, "wb") as f:
|
128 |
f.write(video_response.content)
|
129 |
|
130 |
+
# Download the audio from the URL
|
131 |
+
audio_response = requests.get(audio_url)
|
132 |
+
temp_audio_path = f"temp_audio_{session_id}.mp3"
|
133 |
+
with open(temp_audio_path, "wb") as f:
|
134 |
+
f.write(audio_response.content)
|
135 |
+
|
136 |
output_path = f"output_{session_id}.mp4"
|
137 |
+
combine_audio_video(temp_video_path, temp_audio_path, output_path)
|
138 |
|
139 |
+
# Clean up temporary files
|
140 |
os.remove(temp_video_path)
|
141 |
+
os.remove(temp_audio_path)
|
142 |
|
143 |
progress(1.0, desc="Complete!")
|
144 |
return output_path, f"Used fallback method. Original error: {str(e)}"
|
|
|
151 |
with gr.Row():
|
152 |
with gr.Column():
|
153 |
video_url_input = gr.Textbox(label="Video URL")
|
154 |
+
audio_url_input = gr.Textbox(label="Audio URL")
|
155 |
generate_btn = gr.Button("Generate Video")
|
156 |
with gr.Column():
|
157 |
video_output = gr.Video(label="Generated Video")
|
158 |
status_output = gr.Textbox(label="Status", interactive=False)
|
159 |
|
160 |
+
def on_generate(video_url, audio_url):
|
161 |
+
if not audio_url:
|
162 |
+
return None, "Please provide an audio URL."
|
163 |
if not video_url:
|
164 |
return None, "Please provide a video URL."
|
165 |
+
return process_video(video_url, audio_url)
|
166 |
|
167 |
generate_btn.click(
|
168 |
fn=on_generate,
|
169 |
+
inputs=[video_url_input, audio_url_input],
|
170 |
outputs=[video_output, status_output]
|
171 |
)
|
172 |
|