sheikhed commited on
Commit
cbce214
·
verified ·
1 Parent(s): b5dfd79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -26
app.py CHANGED
@@ -12,10 +12,10 @@ load_dotenv()
12
 
13
  # API Keys
14
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
15
- B_KEY = os.getenv("B_KEY")
16
 
17
  # URLs
18
- API_URL = os.getenv("API_URL")
19
  UPLOAD_URL = os.getenv("UPLOAD_URL")
20
 
21
  def get_voices():
@@ -65,34 +65,33 @@ def upload_file(file_path):
65
 
66
  def lipsync_api_call(video_url, audio_url):
67
  headers = {
 
68
  "Content-Type": "application/json",
69
- "x-api-key": B_KEY
70
  }
71
 
72
  data = {
73
- "audioUrl": audio_url,
74
- "videoUrl": video_url,
75
- "maxCredits": 1000,
76
- "model": "sync-1.7.1-beta",
77
- "synergize": True,
78
- "pads": [0, 5, 0, 0],
79
- "synergizerStrength": 1
80
  }
81
 
82
- response = requests.post(API_URL, headers=headers, data=json.dumps(data))
83
  return response.json()
84
 
85
- def check_job_status(job_id):
86
- headers = {"x-api-key": B_KEY}
87
  max_attempts = 30 # Limit the number of attempts
88
 
89
  for _ in range(max_attempts):
90
- response = requests.get(f"{API_URL}/{job_id}", headers=headers)
91
  data = response.json()
92
 
93
- if data["status"] == "COMPLETED":
94
- return data["videoUrl"]
95
- elif data["status"] == "FAILED":
96
  return None
97
 
98
  time.sleep(10)
@@ -183,13 +182,13 @@ def process_video(voice, url, text, progress=gr.Progress()):
183
  progress(0.5, desc="Initiating lipsync...")
184
  job_data = lipsync_api_call(video_url, audio_url)
185
 
186
- if "error" in job_data or "message" in job_data:
187
- raise Exception(job_data.get("error", job_data.get("message", "Unknown error")))
188
 
189
- job_id = job_data["id"]
190
 
191
  progress(0.6, desc="Processing lipsync...")
192
- result_url = check_job_status(job_id)
193
 
194
  if result_url:
195
  progress(0.9, desc="Downloading result...")
@@ -227,11 +226,8 @@ def process_video(voice, url, text, progress=gr.Progress()):
227
 
228
  def create_interface():
229
  voices = get_voices()
230
- css = """
231
- #component-0 > :not(.prose) {display: none !important;}
232
- footer {display: none !important;}
233
- """
234
- with gr.Blocks(css=css) as app:
235
  gr.Markdown("# Generator")
236
  with gr.Row():
237
  with gr.Column():
 
12
 
13
  # API Keys
14
  OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
15
+ REPLICATE_API_TOKEN = os.getenv("REPLICATE_API_TOKEN")
16
 
17
  # URLs
18
+ REPLICATE_API_URL = "https://api.replicate.com/v1/predictions"
19
  UPLOAD_URL = os.getenv("UPLOAD_URL")
20
 
21
  def get_voices():
 
65
 
66
  def lipsync_api_call(video_url, audio_url):
67
  headers = {
68
+ "Authorization": f"Bearer {REPLICATE_API_TOKEN}",
69
  "Content-Type": "application/json",
70
+ "Prefer": "wait"
71
  }
72
 
73
  data = {
74
+ "version": "db5a650c807b007dc5f9e5abe27c53e1b62880d1f94d218d27ce7fa802711d67",
75
+ "input": {
76
+ "face": video_url,
77
+ "input_audio": audio_url
78
+ }
 
 
79
  }
80
 
81
+ response = requests.post(REPLICATE_API_URL, headers=headers, json=data)
82
  return response.json()
83
 
84
+ def check_job_status(prediction_id):
85
+ headers = {"Authorization": f"Bearer {REPLICATE_API_TOKEN}"}
86
  max_attempts = 30 # Limit the number of attempts
87
 
88
  for _ in range(max_attempts):
89
+ response = requests.get(f"{REPLICATE_API_URL}/{prediction_id}", headers=headers)
90
  data = response.json()
91
 
92
+ if data["status"] == "succeeded":
93
+ return data["output"]
94
+ elif data["status"] == "failed":
95
  return None
96
 
97
  time.sleep(10)
 
182
  progress(0.5, desc="Initiating lipsync...")
183
  job_data = lipsync_api_call(video_url, audio_url)
184
 
185
+ if "error" in job_data:
186
+ raise Exception(job_data.get("error", "Unknown error"))
187
 
188
+ prediction_id = job_data["id"]
189
 
190
  progress(0.6, desc="Processing lipsync...")
191
+ result_url = check_job_status(prediction_id)
192
 
193
  if result_url:
194
  progress(0.9, desc="Downloading result...")
 
226
 
227
  def create_interface():
228
  voices = get_voices()
229
+
230
+ with gr.Blocks() as app:
 
 
 
231
  gr.Markdown("# Generator")
232
  with gr.Row():
233
  with gr.Column():