oraculo / app.py
salomonsky's picture
Update app.py
84c807e
raw
history blame
2.31 kB
import os
import openai
import subprocess
from gtts import gTTS
from pydub import AudioSegment
from flask import Flask, render_template, request
openai.api_key = os.environ.get("openai_api_key")
app = Flask(__name__)
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
name = request.form["name"]
horoscope_type = request.form["horoscope_type"]
video_path, error_message = generate_output(name, horoscope_type)
if error_message:
return render_template("index.html", error_message=error_message)
else:
return render_template("index.html", video_path=video_path)
return render_template("index.html")
def generate_output(name, horoscope_type):
prompt = f"{name}, tu hor贸scopo {horoscope_type} y or谩culo 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."
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)
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):
return output_video_path, None
return None, "No se pudo generar el video"
if __name__ == "__main__":
app.run()