sheikhed commited on
Commit
eec2aff
·
verified ·
1 Parent(s): 4aa1838

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -16
app.py CHANGED
@@ -17,9 +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 get_video_models():
21
- return [f for f in os.listdir("models") if f.endswith((".mp4", ".avi", ".mov"))]
22
-
23
  def upload_file(file_path):
24
  with open(file_path, 'rb') as file:
25
  files = {'fileToUpload': (os.path.basename(file_path), file)}
@@ -98,22 +95,22 @@ def combine_audio_video(video_path, audio_path, output_path):
98
 
99
  subprocess.run(cmd, check=True)
100
 
101
- def process_video(model, audio_path, progress=gr.Progress()):
102
  if not audio_path:
103
  return None, "No audio file provided"
 
 
104
 
105
  session_id = str(uuid.uuid4()) # Generate a unique session ID
106
 
107
  progress(0.2, desc="Processing video...")
108
- video_path = os.path.join("models", model)
109
 
110
  try:
111
- progress(0.3, desc="Uploading files...")
112
- video_url = upload_file(video_path)
113
  audio_url = upload_file(audio_path)
114
 
115
- if not video_url or not audio_url:
116
- raise Exception("Failed to upload files")
117
 
118
  progress(0.4, desc="Initiating lipsync...")
119
  job_data = lipsync_api_call(video_url, audio_url)
@@ -140,35 +137,45 @@ def process_video(model, audio_path, progress=gr.Progress()):
140
  except Exception as e:
141
  progress(0.8, desc="Falling back to simple combination...")
142
  try:
 
 
 
 
 
 
143
  output_path = f"output_{session_id}.mp4"
144
- combine_audio_video(video_path, audio_path, output_path)
 
 
 
 
145
  progress(1.0, desc="Complete!")
146
  return output_path, f"Used fallback method. Original error: {str(e)}"
147
  except Exception as fallback_error:
148
  return None, f"All methods failed. Error: {str(fallback_error)}"
149
 
150
  def create_interface():
151
- models = get_video_models()
152
-
153
  with gr.Blocks() as app:
154
  gr.Markdown("# Video Lip Sync")
155
  with gr.Row():
156
  with gr.Column():
157
- model_dropdown = gr.Dropdown(choices=models, label="Select Video Model", value=models[0] if models else None)
158
  audio_input = gr.Audio(label="Upload Audio File", type="filepath")
159
  generate_btn = gr.Button("Generate Video")
160
  with gr.Column():
161
  video_output = gr.Video(label="Generated Video")
162
  status_output = gr.Textbox(label="Status", interactive=False)
163
 
164
- def on_generate(model_name, audio_file):
165
  if not audio_file:
166
  return None, "Please upload an audio file."
167
- return process_video(model_name, audio_file)
 
 
168
 
169
  generate_btn.click(
170
  fn=on_generate,
171
- inputs=[model_dropdown, audio_input],
172
  outputs=[video_output, status_output]
173
  )
174
 
 
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)}
 
95
 
96
  subprocess.run(cmd, check=True)
97
 
98
+ def process_video(video_url, audio_path, progress=gr.Progress()):
99
  if not audio_path:
100
  return None, "No audio file provided"
101
+ if not video_url:
102
+ return None, "No video URL provided"
103
 
104
  session_id = str(uuid.uuid4()) # Generate a unique session ID
105
 
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)
 
137
  except Exception as e:
138
  progress(0.8, desc="Falling back to simple combination...")
139
  try:
140
+ # Download the video from the URL
141
+ video_response = requests.get(video_url)
142
+ temp_video_path = f"temp_video_{session_id}.mp4"
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, audio_path, output_path)
148
+
149
+ # Clean up temporary video file
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)}"
154
  except Exception as fallback_error:
155
  return None, f"All methods failed. Error: {str(fallback_error)}"
156
 
157
  def create_interface():
 
 
158
  with gr.Blocks() as app:
159
  gr.Markdown("# Video Lip Sync")
160
  with gr.Row():
161
  with gr.Column():
162
+ video_url_input = gr.Textbox(label="Video URL")
163
  audio_input = gr.Audio(label="Upload Audio File", type="filepath")
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, audio_file):
170
  if not audio_file:
171
  return None, "Please upload an audio file."
172
+ if not video_url:
173
+ return None, "Please provide a video URL."
174
+ return process_video(video_url, audio_file)
175
 
176
  generate_btn.click(
177
  fn=on_generate,
178
+ inputs=[video_url_input, audio_input],
179
  outputs=[video_output, status_output]
180
  )
181