Update app.py
Browse files
app.py
CHANGED
@@ -16,6 +16,7 @@ B_KEY = os.getenv("B_KEY")
|
|
16 |
|
17 |
# URLs
|
18 |
API_URL = os.getenv("API_URL")
|
|
|
19 |
|
20 |
def get_voices():
|
21 |
# OpenAI TTS voices
|
@@ -48,18 +49,23 @@ def text_to_speech(voice, text):
|
|
48 |
|
49 |
return response.content
|
50 |
|
51 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
headers = {
|
53 |
"Content-Type": "application/json",
|
54 |
"x-api-key": B_KEY
|
55 |
}
|
56 |
|
57 |
-
# Create a multipart form-data request
|
58 |
-
files = {
|
59 |
-
'audio': ('audio.mp3', audio_content, 'audio/mpeg')
|
60 |
-
}
|
61 |
-
|
62 |
data = {
|
|
|
63 |
"videoUrl": video_url,
|
64 |
"maxCredits": 1000,
|
65 |
"model": "sync-1.7.1-beta",
|
@@ -68,7 +74,7 @@ def lipsync_api_call(video_url, audio_content):
|
|
68 |
"synergizerStrength": 1
|
69 |
}
|
70 |
|
71 |
-
response = requests.post(API_URL, headers=headers,
|
72 |
return response.json()
|
73 |
|
74 |
def check_job_status(job_id):
|
@@ -88,7 +94,6 @@ def check_job_status(job_id):
|
|
88 |
return None
|
89 |
|
90 |
def get_media_duration(file_path):
|
91 |
-
# Fetch media duration using ffprobe
|
92 |
cmd = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', file_path]
|
93 |
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
94 |
return float(result.stdout.strip())
|
@@ -130,16 +135,20 @@ def combine_audio_video(video_path, audio_content, output_path):
|
|
130 |
|
131 |
def process_video(voice, video_url, text, progress=gr.Progress()):
|
132 |
session_id = str(uuid.uuid4()) # Generate a unique session ID
|
|
|
133 |
progress(0, desc="Generating speech...")
|
134 |
audio_content = text_to_speech(voice, text)
|
135 |
if not audio_content:
|
136 |
return None, "Failed to generate speech audio."
|
137 |
|
138 |
-
progress(0.2, desc="
|
|
|
|
|
|
|
139 |
|
140 |
try:
|
141 |
progress(0.4, desc="Initiating lipsync...")
|
142 |
-
job_data = lipsync_api_call(video_url,
|
143 |
|
144 |
if "error" in job_data or "message" in job_data:
|
145 |
raise Exception(job_data.get("error", job_data.get("message", "Unknown error")))
|
|
|
16 |
|
17 |
# URLs
|
18 |
API_URL = os.getenv("API_URL")
|
19 |
+
UPLOAD_URL = os.getenv("UPLOAD_URL")
|
20 |
|
21 |
def get_voices():
|
22 |
# OpenAI TTS voices
|
|
|
49 |
|
50 |
return response.content
|
51 |
|
52 |
+
def upload_file(file_content, file_name):
|
53 |
+
files = {'fileToUpload': (file_name, file_content)}
|
54 |
+
data = {'reqtype': 'fileupload'}
|
55 |
+
response = requests.post(UPLOAD_URL, files=files, data=data)
|
56 |
+
|
57 |
+
if response.status_code == 200:
|
58 |
+
return response.text.strip()
|
59 |
+
return None
|
60 |
+
|
61 |
+
def lipsync_api_call(video_url, audio_url):
|
62 |
headers = {
|
63 |
"Content-Type": "application/json",
|
64 |
"x-api-key": B_KEY
|
65 |
}
|
66 |
|
|
|
|
|
|
|
|
|
|
|
67 |
data = {
|
68 |
+
"audioUrl": audio_url,
|
69 |
"videoUrl": video_url,
|
70 |
"maxCredits": 1000,
|
71 |
"model": "sync-1.7.1-beta",
|
|
|
74 |
"synergizerStrength": 1
|
75 |
}
|
76 |
|
77 |
+
response = requests.post(API_URL, headers=headers, json=data)
|
78 |
return response.json()
|
79 |
|
80 |
def check_job_status(job_id):
|
|
|
94 |
return None
|
95 |
|
96 |
def get_media_duration(file_path):
|
|
|
97 |
cmd = ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', file_path]
|
98 |
result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
99 |
return float(result.stdout.strip())
|
|
|
135 |
|
136 |
def process_video(voice, video_url, text, progress=gr.Progress()):
|
137 |
session_id = str(uuid.uuid4()) # Generate a unique session ID
|
138 |
+
|
139 |
progress(0, desc="Generating speech...")
|
140 |
audio_content = text_to_speech(voice, text)
|
141 |
if not audio_content:
|
142 |
return None, "Failed to generate speech audio."
|
143 |
|
144 |
+
progress(0.2, desc="Uploading audio...")
|
145 |
+
audio_url = upload_file(audio_content, f"audio_{session_id}.mp3")
|
146 |
+
if not audio_url:
|
147 |
+
return None, "Failed to upload audio file."
|
148 |
|
149 |
try:
|
150 |
progress(0.4, desc="Initiating lipsync...")
|
151 |
+
job_data = lipsync_api_call(video_url, audio_url)
|
152 |
|
153 |
if "error" in job_data or "message" in job_data:
|
154 |
raise Exception(job_data.get("error", job_data.get("message", "Unknown error")))
|