Untitled

 avatar
unknown
plain_text
9 months ago
1.5 kB
2
Indexable
from fastapi import APIRouter
from google.cloud import texttospeech, storage
import uuid

router = APIRouter()

@router.post("/generate-audio/")
async def generate_audio(script: str):
    cleaned_script = script.replace('*', '').replace('\\n', '').replace('\n', '').replace('Locutor:', '').replace('Locutora:', '')

    # Cliente de Text to Speech
    client = texttospeech.TextToSpeechClient()
    input_text = texttospeech.SynthesisInput(text=cleaned_script)
    voice = texttospeech.VoiceSelectionParams(
        language_code="es-ES",
        name="es-ES-Wavenet-B",
    )
    audio_config = texttospeech.AudioConfig(
        audio_encoding=texttospeech.AudioEncoding.MP3
    )

    response = client.synthesize_speech(
        request={"input": input_text, "voice": voice, "audio_config": audio_config}
    )

    # Genera un nombre de archivo único usando UUID
    file_name = f"{uuid.uuid4()}.mp3"
    bucket_name = "your-bucket-name"  # Reemplazar con el nombre de tu bucket

    # Cliente de Google Cloud Storage
    storage_client = storage.Client()
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(file_name)

    # Subir el contenido del audio al bucket
    blob.upload_from_string(
        response.audio_content,
        content_type="audio/mp3"
    )
    print(f'Audio content uploaded to GCP bucket "{bucket_name}" with file name "{file_name}"')

    return {
        "message": "Audio generated and uploaded successfully!",
        "bucket": bucket_name,
        "file_name": file_name
    }
Editor is loading...
Leave a Comment