Spaces:
Running
Running
import os | |
import openai | |
import gradio as gr | |
import subprocess | |
from gtts import gTTS | |
from pydub import AudioSegment | |
import ipywidgets as widgets | |
from IPython.display import display | |
openai.api_key = os.environ.get("openai_api_key") | |
def generate_output(name, date_of_birth, progress=gr.Progress(), image_widget=None): | |
prompt = f"{name}, tu horóscopo y oráculo astrológico de hoy es:" | |
response = openai.Completion.create( | |
engine="text-davinci-003", | |
prompt=prompt, | |
max_tokens=120, | |
temperature=0.6, | |
n=1, | |
stop=None, | |
) | |
gpt3_output = response.choices[0].text.strip() | |
generated_text = gpt3_output.replace(prompt, "").strip() | |
if len(response.choices) == 0 or 'text' not in response.choices[0]: | |
return None, "No se pudo generar el texto." | |
def _progress(generated, to_generate): | |
progress((generated, to_generate)) | |
_progress(0, 1) | |
try: | |
tts = gTTS(generated_text, lang='es') | |
temp_audio_path = "temp_audio.mp3" | |
tts.save(temp_audio_path) | |
audio_path = "audio.wav" | |
audio = AudioSegment.from_mp3(temp_audio_path) | |
audio.export(audio_path, format="wav") | |
print("Archivo de audio generado:", audio_path) | |
_progress(1, 2) | |
except Exception as e: | |
return None, f"No se pudo generar el audio: {str(e)}" | |
command = f"python3 inference.py --checkpoint_path checkpoints/wav2lip_gan.pth --face oraculo.jpg --audio audio.wav --outfile video.mp4 --nosmooth" | |
process = subprocess.run(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
if process.returncode != 0: | |
error_message = process.stderr.decode("utf-8") | |
return None, f"No se pudo generar el video: {error_message}" | |
output_video_path = "video.mp4" | |
os.remove(temp_audio_path) | |
if os.path.isfile(output_video_path): | |
_progress(2, 2) | |
return output_video_path, None | |
return None, "No se pudo generar el video" | |
name_input = gr.inputs.Textbox(lines=1, label="Nombre", placeholder="Ingresa tu nombre") | |
dob_input = gr.inputs.Textbox(lines=1, label="Fecha y Hora de Nacimiento (opcional)", placeholder="DD/MM/AAAA y HH:MM") | |
progress_widget = gr.Progress() | |
image_widget = widgets.Image(value=open("oraculo.jpg", "rb").read()) | |
video_widget = gr.outputs.Video(label="Respuesta de Andrea (dos minutos aproximadamente)").style(width=590) | |
output_widget = widgets.Output() | |
def generate_and_display_output(sender): | |
with output_widget: | |
output_widget.clear_output() | |
video_path, error_message = generate_output(name_input.value, dob_input.value, progress_widget, image_widget) | |
if error_message: | |
print(f"Error: {error_message}") | |
else: | |
display(video_widget.from_url(video_path)) | |
submit_button = widgets.Button(description="Enviar") | |
submit_button.on_click(generate_and_display_output) | |
display(name_input) | |
display(dob_input) | |
display(progress_widget) | |
display(image_widget) | |
display(video_widget) | |
display(submit_button) | |
display(output_widget) |