Spaces:
Sleeping
Sleeping
Commit
路
4caa659
1
Parent(s):
609e485
Update app.py
Browse files
app.py
CHANGED
@@ -15,35 +15,61 @@ def generate_output(text):
|
|
15 |
input_tokens = tokenizer.encode(text, add_special_tokens=False)
|
16 |
input_text = tokenizer.decode(input_tokens)
|
17 |
gpt2_output = generator(input_text, max_length=20, do_sample=True, temperature=0.9)
|
|
|
|
|
|
|
18 |
generated_text = gpt2_output[0]['generated_text']
|
19 |
generated_text = generated_text.replace(input_text, "").strip()
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
-
|
|
|
|
|
29 |
process = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
30 |
if process.returncode != 0:
|
31 |
-
|
|
|
32 |
|
33 |
output_video_path = "video.mp4"
|
|
|
|
|
|
|
34 |
|
35 |
if os.path.isfile(output_video_path):
|
36 |
-
return output_video_path
|
|
|
|
|
37 |
|
38 |
-
|
|
|
|
|
|
|
|
|
39 |
|
40 |
iface = gr.Interface(
|
41 |
fn=generate_output,
|
42 |
inputs=gr.inputs.Textbox(lines=1, placeholder='Escribe tu nombre para presentarte con Andrea...'),
|
43 |
outputs=[
|
44 |
-
gr.outputs.Video(label="Respuesta de Andrea (un minuto aproximadamente)")
|
|
|
45 |
],
|
46 |
title="Andrea - Humanoid Chatbot IA 2023(c)",
|
|
|
|
|
47 |
)
|
48 |
|
49 |
iface.launch()
|
|
|
15 |
input_tokens = tokenizer.encode(text, add_special_tokens=False)
|
16 |
input_text = tokenizer.decode(input_tokens)
|
17 |
gpt2_output = generator(input_text, max_length=20, do_sample=True, temperature=0.9)
|
18 |
+
if len(gpt2_output) == 0 or 'generated_text' not in gpt2_output[0]:
|
19 |
+
return None, "No se pudo generar el texto."
|
20 |
+
|
21 |
generated_text = gpt2_output[0]['generated_text']
|
22 |
generated_text = generated_text.replace(input_text, "").strip()
|
23 |
|
24 |
+
try:
|
25 |
+
tts = gTTS(generated_text, lang='es')
|
26 |
+
temp_audio_path = "temp_audio.mp3"
|
27 |
+
tts.save(temp_audio_path)
|
28 |
+
audio_path = "audio.wav"
|
29 |
+
audio = AudioSegment.from_mp3(temp_audio_path)
|
30 |
+
audio.export(audio_path, format="wav")
|
31 |
+
print("Archivo de audio generado:", audio_path)
|
32 |
+
except Exception as e:
|
33 |
+
return None, f"No se pudo generar el audio: {str(e)}"
|
34 |
+
|
35 |
+
face_image_path = "face.jpg"
|
36 |
+
if not os.path.isfile(face_image_path):
|
37 |
+
return None, "No se encontr贸 el archivo de imagen de cara."
|
38 |
|
39 |
+
print("Archivo de imagen de cara:", face_image_path)
|
40 |
+
|
41 |
+
command = f"python3 inference.py --checkpoint_path checkpoints/wav2lip_gan.pth --face {face_image_path} --audio {audio_path} --outfile video.mp4 --nosmooth"
|
42 |
process = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
43 |
if process.returncode != 0:
|
44 |
+
error_message = process.stderr.decode()
|
45 |
+
return None, f"No se pudo generar el video: {error_message}"
|
46 |
|
47 |
output_video_path = "video.mp4"
|
48 |
+
print("Archivo de video generado:", output_video_path)
|
49 |
+
|
50 |
+
os.remove(temp_audio_path)
|
51 |
|
52 |
if os.path.isfile(output_video_path):
|
53 |
+
return output_video_path, None
|
54 |
+
|
55 |
+
return None, "No se pudo generar el video."
|
56 |
|
57 |
+
def error_message_fn(error_message):
|
58 |
+
if error_message is not None:
|
59 |
+
return gr.outputs.Textbox(text=error_message, placeholder="Error")
|
60 |
+
else:
|
61 |
+
return None
|
62 |
|
63 |
iface = gr.Interface(
|
64 |
fn=generate_output,
|
65 |
inputs=gr.inputs.Textbox(lines=1, placeholder='Escribe tu nombre para presentarte con Andrea...'),
|
66 |
outputs=[
|
67 |
+
gr.outputs.Video(label="Respuesta de Andrea (un minuto aproximadamente)"),
|
68 |
+
gr.outputs.Textbox(label="Mensaje de error", type="auto", output_name="error_message")
|
69 |
],
|
70 |
title="Andrea - Humanoid Chatbot IA 2023(c)",
|
71 |
+
error="No se pudo generar la salida.",
|
72 |
+
error_message=error_message_fn
|
73 |
)
|
74 |
|
75 |
iface.launch()
|