Spaces:
Paused
Paused
File size: 27,347 Bytes
20cd47a d8a6eb5 20cd47a c3727de 7d6f952 b8b3af8 3845d1c 7e5f34c 20cd47a 7eb654c 20cd47a a42f5f3 20cd47a 7dac13b da97815 20cd47a 4978fe4 20cd47a 1aba95f a42f5f3 20cd47a 7eb654c 20cd47a 7d6f952 20cd47a a42f5f3 20cd47a 8ad7dd5 20cd47a 7bc4297 372a492 a42f5f3 3845d1c a42f5f3 3ae3408 a42f5f3 eafd344 3ae3408 eafd344 3ae3408 eafd344 3ae3408 a42f5f3 eafd344 3ae3408 7bc4297 3114eb3 7bc4297 3114eb3 7bc4297 3114eb3 7bc4297 3114eb3 7bc4297 28d1815 7bc4297 7e5f34c 20cd47a 7e5f34c a82704b 7e5f34c a82704b 7e5f34c a82704b 7e5f34c 7b303d5 7e5f34c 7b303d5 0eb5747 7d6f952 7e5f34c 7bc4297 7b303d5 7e5f34c 20cd47a 2aea41f 20cd47a b07f9ec 20cd47a c381ea3 20cd47a c3866eb c3727de 0664438 c3727de c3866eb 20cd47a a42f5f3 20cd47a 2dc6197 7eb654c 20cd47a f6138ad 44dcd57 c3727de b0a721c c3727de 44dcd57 d480876 44dcd57 e603987 d480876 44dcd57 a42f5f3 c3866eb 44dcd57 d0efd99 44dcd57 95397b0 44dcd57 c3727de c3866eb 2b9bd47 c3866eb c81c4cf c3866eb 44dcd57 2aea41f 44dcd57 1600e93 2608466 44dcd57 2608466 70f18e6 2608466 70f18e6 2608466 44dcd57 14622e1 2608466 0ec9471 2608466 7d797d7 44dcd57 7d797d7 44dcd57 60da68b cd9bf20 20cd47a cd9bf20 60da68b cd9bf20 60da68b cd9bf20 124c929 2efc33b 124c929 2efc33b cd9bf20 2efc33b cd9bf20 a42f5f3 cd9bf20 20cd47a 6764da3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
from flask import Flask, request, jsonify
import torch
import shutil
import os
import sys
from argparse import ArgumentParser
from time import strftime
from argparse import Namespace
from src.utils.preprocess import CropAndExtract
from src.test_audio2coeff import Audio2Coeff
from src.facerender.animate import AnimateFromCoeff
from src.generate_batch import get_data
from src.generate_facerender_batch import get_facerender_data
# from src.utils.init_path import init_path
import tempfile
from openai import OpenAI
import threading
import elevenlabs
from elevenlabs import set_api_key, generate, play, clone, Voice, VoiceSettings
from flask_cors import CORS, cross_origin
# from flask_swagger_ui import get_swaggerui_blueprint
import uuid
import time
from PIL import Image
import moviepy.editor as mp
import requests
import json
import pickle
# from videoretalking import inference_function
# import base64
# import gfpgan_enhancer
class AnimationConfig:
def __init__(self, driven_audio_path, source_image_path, result_folder,pose_style,expression_scale,enhancer,still,preprocess,ref_pose_video_path, image_hardcoded):
self.driven_audio = driven_audio_path
self.source_image = source_image_path
self.ref_eyeblink = None
self.ref_pose = ref_pose_video_path
self.checkpoint_dir = './checkpoints'
self.result_dir = result_folder
self.pose_style = pose_style
self.batch_size = 2
self.expression_scale = expression_scale
self.input_yaw = None
self.input_pitch = None
self.input_roll = None
self.enhancer = enhancer
self.background_enhancer = None
self.cpu = False
self.face3dvis = False
self.still = still
self.preprocess = preprocess
self.verbose = False
self.old_version = False
self.net_recon = 'resnet50'
self.init_path = None
self.use_last_fc = False
self.bfm_folder = './checkpoints/BFM_Fitting/'
self.bfm_model = 'BFM_model_front.mat'
self.focal = 1015.
self.center = 112.
self.camera_d = 10.
self.z_near = 5.
self.z_far = 15.
self.device = 'cuda'
self.image_hardcoded = image_hardcoded
app = Flask(__name__)
CORS(app)
TEMP_DIR = None
start_time = None
app.config['temp_response'] = None
app.config['generation_thread'] = None
app.config['text_prompt'] = None
app.config['final_video_path'] = None
app.config['final_video_duration'] = None
def main(args):
pic_path = args.source_image
audio_path = args.driven_audio
save_dir = args.result_dir
pose_style = args.pose_style
device = args.device
batch_size = args.batch_size
input_yaw_list = args.input_yaw
input_pitch_list = args.input_pitch
input_roll_list = args.input_roll
ref_eyeblink = args.ref_eyeblink
ref_pose = args.ref_pose
preprocess = args.preprocess
image_hardcoded = args.image_hardcoded
dir_path = os.path.dirname(os.path.realpath(__file__))
current_root_path = dir_path
print('current_root_path ',current_root_path)
# sadtalker_paths = init_path(args.checkpoint_dir, os.path.join(current_root_path, 'src/config'), args.size, args.old_version, args.preprocess)
path_of_lm_croper = os.path.join(current_root_path, args.checkpoint_dir, 'shape_predictor_68_face_landmarks.dat')
path_of_net_recon_model = os.path.join(current_root_path, args.checkpoint_dir, 'epoch_20.pth')
dir_of_BFM_fitting = os.path.join(current_root_path, args.checkpoint_dir, 'BFM_Fitting')
wav2lip_checkpoint = os.path.join(current_root_path, args.checkpoint_dir, 'wav2lip.pth')
audio2pose_checkpoint = os.path.join(current_root_path, args.checkpoint_dir, 'auido2pose_00140-model.pth')
audio2pose_yaml_path = os.path.join(current_root_path, 'src', 'config', 'auido2pose.yaml')
audio2exp_checkpoint = os.path.join(current_root_path, args.checkpoint_dir, 'auido2exp_00300-model.pth')
audio2exp_yaml_path = os.path.join(current_root_path, 'src', 'config', 'auido2exp.yaml')
free_view_checkpoint = os.path.join(current_root_path, args.checkpoint_dir, 'facevid2vid_00189-model.pth.tar')
if preprocess == 'full':
mapping_checkpoint = os.path.join(current_root_path, args.checkpoint_dir, 'mapping_00109-model.pth.tar')
facerender_yaml_path = os.path.join(current_root_path, 'src', 'config', 'facerender_still.yaml')
else:
mapping_checkpoint = os.path.join(current_root_path, args.checkpoint_dir, 'mapping_00229-model.pth.tar')
facerender_yaml_path = os.path.join(current_root_path, 'src', 'config', 'facerender.yaml')
# preprocess_model = CropAndExtract(sadtalker_paths, device)
#init model
print(path_of_net_recon_model)
preprocess_model = CropAndExtract(path_of_lm_croper, path_of_net_recon_model, dir_of_BFM_fitting, device)
# audio_to_coeff = Audio2Coeff(sadtalker_paths, device)
audio_to_coeff = Audio2Coeff(audio2pose_checkpoint, audio2pose_yaml_path,
audio2exp_checkpoint, audio2exp_yaml_path,
wav2lip_checkpoint, device)
# animate_from_coeff = AnimateFromCoeff(sadtalker_paths, device)
animate_from_coeff = AnimateFromCoeff(free_view_checkpoint, mapping_checkpoint,
facerender_yaml_path, device)
first_frame_dir = os.path.join(save_dir, 'first_frame_dir')
os.makedirs(first_frame_dir, exist_ok=True)
# first_coeff_path, crop_pic_path, crop_info = preprocess_model.generate(pic_path, first_frame_dir, args.preprocess,\
# source_image_flag=True, pic_size=args.size)
fixed_temp_dir = "/tmp/preprocess_data"
os.makedirs(fixed_temp_dir, exist_ok=True)
preprocessed_data_path = os.path.join(fixed_temp_dir, "preprocessed_data.pkl")
if os.path.exists(preprocessed_data_path) and image_hardcoded == "yes":
print("Loading preprocessed data...")
with open(preprocessed_data_path, "rb") as f:
preprocessed_data = pickle.load(f)
first_coeff_new_path = preprocessed_data["first_coeff_path"]
crop_pic_new_path = preprocessed_data["crop_pic_path"]
crop_info_path = preprocessed_data["crop_info_path"]
with open(crop_info_path, "rb") as f:
crop_info = pickle.load(f)
print(f"Loaded existing preprocessed data from: {preprocessed_data_path}")
else:
print("Running preprocessing...")
first_coeff_path, crop_pic_path, crop_info = preprocess_model.generate(pic_path, first_frame_dir, args.preprocess, source_image_flag=True)
first_coeff_new_path = os.path.join(fixed_temp_dir, os.path.basename(first_coeff_path))
crop_pic_new_path = os.path.join(fixed_temp_dir, os.path.basename(crop_pic_path))
crop_info_new_path = os.path.join(fixed_temp_dir, "crop_info.pkl")
shutil.move(first_coeff_path, first_coeff_new_path)
shutil.move(crop_pic_path, crop_pic_new_path)
with open(crop_info_new_path, "wb") as f:
pickle.dump(crop_info, f)
preprocessed_data = {"first_coeff_path": first_coeff_new_path,
"crop_pic_path": crop_pic_new_path,
"crop_info_path": crop_info_new_path}
with open(preprocessed_data_path, "wb") as f:
pickle.dump(preprocessed_data, f)
print(f"Preprocessed data saved to: {preprocessed_data_path}")
print('first_coeff_path ',first_coeff_new_path)
print('crop_pic_path ',crop_pic_new_path)
print('crop_info ',crop_info)
if first_coeff_new_path is None:
print("Can't get the coeffs of the input")
return
if ref_eyeblink is not None:
ref_eyeblink_videoname = os.path.splitext(os.path.split(ref_eyeblink)[-1])[0]
ref_eyeblink_frame_dir = os.path.join(save_dir, ref_eyeblink_videoname)
os.makedirs(ref_eyeblink_frame_dir, exist_ok=True)
# ref_eyeblink_coeff_path, _, _ = preprocess_model.generate(ref_eyeblink, ref_eyeblink_frame_dir, args.preprocess, source_image_flag=False)
ref_eyeblink_coeff_path, _, _ = preprocess_model.generate(ref_eyeblink, ref_eyeblink_frame_dir)
else:
ref_eyeblink_coeff_path=None
print('ref_eyeblink_coeff_path',ref_eyeblink_coeff_path)
if ref_pose is not None:
if ref_pose == ref_eyeblink:
ref_pose_coeff_path = ref_eyeblink_coeff_path
else:
ref_pose_videoname = os.path.splitext(os.path.split(ref_pose)[-1])[0]
ref_pose_frame_dir = os.path.join(save_dir, ref_pose_videoname)
os.makedirs(ref_pose_frame_dir, exist_ok=True)
# ref_pose_coeff_path, _, _ = preprocess_model.generate(ref_pose, ref_pose_frame_dir, args.preprocess, source_image_flag=False)
ref_pose_coeff_path, _, _ = preprocess_model.generate(ref_pose, ref_pose_frame_dir)
else:
ref_pose_coeff_path=None
print('ref_eyeblink_coeff_path',ref_pose_coeff_path)
batch = get_data(first_coeff_new_path, audio_path, device, ref_eyeblink_coeff_path, still=args.still)
coeff_path = audio_to_coeff.generate(batch, save_dir, pose_style, ref_pose_coeff_path)
if args.face3dvis:
from src.face3d.visualize import gen_composed_video
gen_composed_video(args, device, first_coeff_new_path, coeff_path, audio_path, os.path.join(save_dir, '3dface.mp4'))
# data = get_facerender_data(coeff_path, crop_pic_path, first_coeff_path, audio_path,
# batch_size, input_yaw_list, input_pitch_list, input_roll_list,
# expression_scale=args.expression_scale, still_mode=args.still, preprocess=args.preprocess, size=args.size)
data = get_facerender_data(coeff_path, crop_pic_new_path, first_coeff_new_path, audio_path,
batch_size, input_yaw_list, input_pitch_list, input_roll_list,
expression_scale=args.expression_scale, still_mode=args.still, preprocess=args.preprocess)
# result, base64_video,temp_file_path= animate_from_coeff.generate(data, save_dir, pic_path, crop_info, \
# enhancer=args.enhancer, background_enhancer=args.background_enhancer, preprocess=args.preprocess, img_size=args.size)
result, base64_video,temp_file_path,new_audio_path = animate_from_coeff.generate(data, save_dir, pic_path, crop_info, \
enhancer=args.enhancer, background_enhancer=args.background_enhancer, preprocess=args.preprocess)
# face_path = temp_file_path
# audio_path = new_audio_path
# temp_file = tempfile.NamedTemporaryFile(delete=False, dir=TEMP_DIR.name, suffix='.mp4')
# video_lipsync_file_path = temp_file.name
# output_path = video_lipsync_file_path
# # Call the function
# inference_function.video_lipsync_correctness(
# face=face_path,
# audio_path=audio_path,
# face3d_net_path = path_of_net_recon_model,
# outfile=output_path,
# tmp_dir="temp",
# crop=[0, -1, 0, -1],
# re_preprocess=True, # Set to True if you want to reprocess; False otherwise
# exp_img="neutral", # Can be 'smile', 'neutral', or path to an expression image
# one_shot=False,
# up_face="original", # Options: 'original', 'sad', 'angry', 'surprise'
# LNet_batch_size=16,
# without_rl1=False
# )
# print('The video with lip sync is generated')
# print("GFPGAN Activated")
# gfpgan_enhancer.process_video_with_gfpgan(output_path, output_path)
# audio_clip = mp.AudioFileClip(new_audio_path)
# video_clip = mp.VideoFileClip(output_path)
# # Combine audio and video
# final_clip = video_clip.set_audio(audio_clip)
# temp_file = tempfile.NamedTemporaryFile(suffix='.mp4', dir=TEMP_DIR.name, delete=False)
# temp_file.close()
# final_video_path = temp_file.name
# final_clip.write_videofile(final_video_path)
# with open(final_video_path, 'rb') as f:
# video_content = f.read()
# base64_lipsync_video = base64.b64encode(video_content).decode('utf-8')
video_clip = mp.VideoFileClip(temp_file_path)
duration = video_clip.duration
app.config['temp_response'] = base64_video
app.config['final_video_path'] = temp_file_path
app.config['final_video_duration'] = duration
return base64_video, temp_file_path, duration
# shutil.move(result, save_dir+'.mp4')
if not args.verbose:
shutil.rmtree(save_dir)
def create_temp_dir():
return tempfile.TemporaryDirectory()
def save_uploaded_file(file, filename,TEMP_DIR):
unique_filename = str(uuid.uuid4()) + "_" + filename
file_path = os.path.join(TEMP_DIR.name, unique_filename)
file.save(file_path)
return file_path
client = OpenAI(api_key="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")
def translate_text(text_prompt, target_language):
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": "You are a helpful language translator assistant."},
{"role": "user", "content": f"Translate completely without hallucination, end to end and the ouput should just be the translation of the text prompt and nothing else, and give the following text to {target_language} language and the text is: {text_prompt}"},
],
max_tokens = len(text_prompt) + 200 # Use the length of the input text
# temperature=0.3,
# stop=["Translate:", "Text:"]
)
return response
def openai_chat_avatar(text_prompt):
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "system", "content": "Answer in English language always using the minimum words you can ever use."},
{"role": "user", "content": f"Hi! I need help with something. Can you assist me with the following: {text_prompt}"},
],
max_tokens = len(text_prompt) + 300 # Use the length of the input text
# temperature=0.3,
# stop=["Translate:", "Text:"]
)
return response
def ryzedb_chat_avatar(question):
url = "https://inference.dev.ryzeai.ai/chat/stream"
question = question + ". Summarize and Answer using the minimum words you can ever use."
payload = json.dumps({
"input": {
"chat_history": [],
"app_id": "af6b2bc719a14478adcff1d71c19dc00",
"question": question
},
"config": {}
})
headers = {
'Content-Type': 'application/json'
}
try:
# Send the POST request
response = requests.request("POST", url, headers=headers, data=payload)
# Check for successful request
response.raise_for_status()
# Return the response JSON
return response.text
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return None
def custom_cleanup(temp_dir, exclude_dir):
# Iterate over the files and directories in TEMP_DIR
for filename in os.listdir(temp_dir):
file_path = os.path.join(temp_dir, filename)
# Skip the directory we want to exclude
if file_path != exclude_dir:
try:
if os.path.isdir(file_path):
shutil.rmtree(file_path)
else:
os.remove(file_path)
print(f"Deleted: {file_path}")
except Exception as e:
print(f"Failed to delete {file_path}. Reason: {e}")
@app.route("/run", methods=['POST'])
def generate_video():
global start_time
start_time = time.time()
global TEMP_DIR
TEMP_DIR = create_temp_dir()
print('request:',request.method)
try:
if request.method == 'POST':
# source_image = request.files['source_image']
image_path = '/home/user/app/images/marc_smile_enhanced.jpg'
source_image = Image.open(image_path)
text_prompt = request.form['text_prompt']
print('Input text prompt: ',text_prompt)
text_prompt = text_prompt.strip()
if not text_prompt:
return jsonify({'error': 'Input text prompt cannot be blank'}), 400
voice_cloning = request.form.get('voice_cloning', 'no')
image_hardcoded = request.form.get('image_hardcoded', 'yes')
chat_model_used = request.form.get('chat_model_used', 'openai')
target_language = request.form.get('target_language', 'original_text')
print('target_language',target_language)
pose_style = int(request.form.get('pose_style', 1))
expression_scale = float(request.form.get('expression_scale', 1))
enhancer = request.form.get('enhancer', None)
voice_gender = request.form.get('voice_gender', 'male')
still_str = request.form.get('still', 'False')
still = still_str.lower() == 'false'
print('still', still)
preprocess = request.form.get('preprocess', 'crop')
print('preprocess selected: ',preprocess)
ref_pose_video = request.files.get('ref_pose', None)
# if target_language != 'original_text':
# response = translate_text(text_prompt, target_language)
# # response = await translate_text_async(text_prompt, target_language)
# text_prompt = response.choices[0].message.content.strip()
if chat_model_used == 'ryzedb':
response = ryzedb_chat_avatar(text_prompt)
events = response.split('\r\n\r\n')
content = None
for event in events:
# Split each event block by "\r\n" to get the lines
lines = event.split('\r\n')
if len(lines) > 1 and lines[0] == 'event: data':
# Extract the JSON part from the second line and parse it
json_data = lines[1].replace('data: ', '')
try:
data = json.loads(json_data)
text_prompt = data.get('content')
app.config['text_prompt'] = text_prompt
print('Final output text prompt using ryzedb: ',text_prompt)
break # Exit the loop once content is found
except json.JSONDecodeError:
continue
else:
# response = openai_chat_avatar(text_prompt)
# text_prompt = response.choices[0].message.content.strip()
app.config['text_prompt'] = text_prompt
print('Final output text prompt using openai: ',text_prompt)
source_image_path = save_uploaded_file(source_image, 'source_image.png',TEMP_DIR)
print(source_image_path)
# driven_audio_path = await voice_cloning_async(voice_cloning, voice_gender, text_prompt, user_voice)
if voice_cloning == 'no':
if voice_gender == 'male':
voice = 'echo'
print('Entering Audio creation using elevenlabs')
set_api_key("92e149985ea2732b4359c74346c3daee")
audio = generate(text = text_prompt, voice = "Daniel", model = "eleven_multilingual_v2",stream=True, latency=4)
with tempfile.NamedTemporaryFile(suffix=".mp3", prefix="text_to_speech_",dir=TEMP_DIR.name, delete=False) as temp_file:
for chunk in audio:
temp_file.write(chunk)
driven_audio_path = temp_file.name
print('driven_audio_path',driven_audio_path)
print('Audio file saved using elevenlabs')
else:
voice = 'nova'
print('Entering Audio creation using whisper')
response = client.audio.speech.create(model="tts-1-hd",
voice=voice,
input = text_prompt)
print('Audio created using whisper')
with tempfile.NamedTemporaryFile(suffix=".wav", prefix="text_to_speech_",dir=TEMP_DIR.name, delete=False) as temp_file:
driven_audio_path = temp_file.name
response.write_to_file(driven_audio_path)
print('Audio file saved using whisper')
elif voice_cloning == 'yes':
# user_voice = request.files['user_voice']
# user_voice = '/home/user/app/images/marc_voice.mp3'
# with tempfile.NamedTemporaryFile(suffix=".wav", prefix="user_voice_",dir=TEMP_DIR.name, delete=False) as temp_file:
# with open(user_voice, 'rb') as source_file:
# file_contents = source_file.read()
# temp_file.write(file_contents)
# temp_file.flush()
# user_voice_path = temp_file.name
# user_voice.save(user_voice_path)
# print('user_voice_path',user_voice_path)
set_api_key("92e149985ea2732b4359c74346c3daee")
# voice = clone(name = "User Cloned Voice",
# files = [user_voice_path] )
voice = Voice(voice_id="DeZH4ash9IU9gUcNjVXh",name="Marc",settings=VoiceSettings(
stability=0.71, similarity_boost=0.5, style=0.0, use_speaker_boost=True),)
audio = generate(text = text_prompt, voice = voice, model = "eleven_multilingual_v2",stream=True, latency=4)
with tempfile.NamedTemporaryFile(suffix=".mp3", prefix="cloned_audio_",dir=TEMP_DIR.name, delete=False) as temp_file:
for chunk in audio:
temp_file.write(chunk)
driven_audio_path = temp_file.name
print('driven_audio_path',driven_audio_path)
# elevenlabs.save(audio, driven_audio_path)
save_dir = tempfile.mkdtemp(dir=TEMP_DIR.name)
result_folder = os.path.join(save_dir, "results")
os.makedirs(result_folder, exist_ok=True)
ref_pose_video_path = None
if ref_pose_video:
with tempfile.NamedTemporaryFile(suffix=".mp4", prefix="ref_pose_",dir=TEMP_DIR.name, delete=False) as temp_file:
ref_pose_video_path = temp_file.name
ref_pose_video.save(ref_pose_video_path)
print('ref_pose_video_path',ref_pose_video_path)
except Exception as e:
app.logger.error(f"An error occurred: {e}")
return "An error occurred", 500
# Example of using the class with some hypothetical paths
args = AnimationConfig(driven_audio_path=driven_audio_path, source_image_path=source_image_path, result_folder=result_folder, pose_style=pose_style, expression_scale=expression_scale,enhancer=enhancer,still=still,preprocess=preprocess,ref_pose_video_path=ref_pose_video_path, image_hardcoded=image_hardcoded)
if torch.cuda.is_available() and not args.cpu:
args.device = "cuda"
else:
args.device = "cpu"
# generation_thread = threading.Thread(target=main, args=(args,))
# app.config['generation_thread'] = generation_thread
# generation_thread.start()
# response_data = {"message": "Video generation started",
# "process_id": generation_thread.ident}
try:
base64_video, temp_file_path, duration = main(args)
final_video_path = app.config['final_video_path']
print('final_video_path',final_video_path)
if final_video_path and os.path.exists(final_video_path):
os.remove(final_video_path)
print("Deleted video file:", final_video_path)
preprocess_dir = os.path.join("/tmp", "preprocess_data")
custom_cleanup(TEMP_DIR.name, preprocess_dir)
print("Temporary files cleaned up, but preprocess_data is retained.")
end_time = time.time()
time_taken = end_time - start_time
print(f"Time taken for endpoint: {time_taken:.2f} seconds")
return jsonify({
'base64_video': base64_video,
'text_prompt': text_prompt,
'duration': duration,
'time_taken':time_taken,
'status': 'completed'
})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)}), 500
# return jsonify(response_data)
# @app.route("/status", methods=["GET"])
# def check_generation_status():
# global TEMP_DIR
# global start_time
# response = {"base64_video": "","text_prompt":"", "status": ""}
# process_id = request.args.get('process_id', None)
# # process_id is required to check the status for that specific process
# if process_id:
# generation_thread = app.config.get('generation_thread')
# if generation_thread and generation_thread.ident == int(process_id) and generation_thread.is_alive():
# return jsonify({"status": "in_progress"}), 200
# elif app.config.get('temp_response'):
# # app.config['temp_response']['status'] = 'completed'
# final_response = app.config['temp_response']
# response["base64_video"] = final_response
# response["text_prompt"] = app.config.get('text_prompt')
# response["duration"] = app.config.get('final_video_duration')
# response["status"] = "completed"
# final_video_path = app.config['final_video_path']
# print('final_video_path',final_video_path)
# if final_video_path and os.path.exists(final_video_path):
# os.remove(final_video_path)
# print("Deleted video file:", final_video_path)
# # TEMP_DIR.cleanup()
# preprocess_dir = os.path.join("/tmp", "preprocess_data")
# custom_cleanup(TEMP_DIR.name, preprocess_dir)
# print("Temporary files cleaned up, but preprocess_data is retained.")
# end_time = time.time()
# total_time = round(end_time - start_time, 2)
# print("Total time taken for execution:", total_time, " seconds")
# response["time_taken"] = total_time
# return jsonify(response)
# return jsonify({"error":"No process id provided"})
@app.route("/health", methods=["GET"])
def health_status():
response = {"online": "true"}
return jsonify(response)
if __name__ == '__main__':
app.run(debug=True) |