Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
from datetime import datetime, timedelta
|
| 6 |
+
|
| 7 |
+
# Cargar variables de entorno desde el archivo .env
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
# Constantes para el script
|
| 11 |
+
CHUNK_SIZE = 1024 # Tama帽o de los chunks para leer/escribir a la vez
|
| 12 |
+
XI_API_KEY = os.getenv("XI_API_KEY") # Tu clave API para autenticaci贸n
|
| 13 |
+
VOICE_ID = os.getenv("VOICE_ID") # ID del modelo de voz a utilizar
|
| 14 |
+
|
| 15 |
+
# Diccionario para almacenar la informaci贸n de uso
|
| 16 |
+
usage_data = {
|
| 17 |
+
'message_count': 0,
|
| 18 |
+
'last_reset': datetime.now()
|
| 19 |
+
}
|
| 20 |
+
|
| 21 |
+
# Configuraci贸n de l铆mites
|
| 22 |
+
MESSAGE_LIMIT = 145
|
| 23 |
+
TIME_LIMIT = timedelta(hours=2)
|
| 24 |
+
|
| 25 |
+
def text_to_speech(text, style):
|
| 26 |
+
global usage_data
|
| 27 |
+
current_time = datetime.now()
|
| 28 |
+
|
| 29 |
+
# Resetear el contador si ha pasado el tiempo l铆mite
|
| 30 |
+
if current_time - usage_data['last_reset'] > TIME_LIMIT:
|
| 31 |
+
usage_data = {
|
| 32 |
+
'message_count': 0,
|
| 33 |
+
'last_reset': current_time
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
# Verificar si se ha alcanzado el l铆mite de mensajes
|
| 37 |
+
if usage_data['message_count'] >= MESSAGE_LIMIT:
|
| 38 |
+
return None # Limite alcanzado, no se produce audio
|
| 39 |
+
|
| 40 |
+
# URL para la solicitud de la API de Text-to-Speech
|
| 41 |
+
tts_url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream"
|
| 42 |
+
|
| 43 |
+
# Encabezados para la solicitud de la API
|
| 44 |
+
headers = {
|
| 45 |
+
"Accept": "application/json",
|
| 46 |
+
"xi-api-key": XI_API_KEY
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
# Datos de carga 煤til para la solicitud de la API
|
| 50 |
+
data = {
|
| 51 |
+
"text": text,
|
| 52 |
+
"model_id": "eleven_multilingual_v2",
|
| 53 |
+
"voice_settings": {
|
| 54 |
+
"stability": 0.5,
|
| 55 |
+
"similarity_boost": 0.8,
|
| 56 |
+
"style": style,
|
| 57 |
+
"use_speaker_boost": True
|
| 58 |
+
}
|
| 59 |
+
}
|
| 60 |
+
|
| 61 |
+
# Realizar la solicitud POST
|
| 62 |
+
response = requests.post(tts_url, headers=headers, json=data, stream=True)
|
| 63 |
+
|
| 64 |
+
# Verificar si la solicitud fue exitosa
|
| 65 |
+
if response.ok:
|
| 66 |
+
output_path = "output.mp3"
|
| 67 |
+
with open(output_path, "wb") as f:
|
| 68 |
+
for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
|
| 69 |
+
f.write(chunk)
|
| 70 |
+
usage_data['message_count'] += 1
|
| 71 |
+
return output_path
|
| 72 |
+
else:
|
| 73 |
+
return None # Si falla, tampoco generes audio
|
| 74 |
+
|
| 75 |
+
iface = gr.Interface(
|
| 76 |
+
fn=text_to_speech,
|
| 77 |
+
inputs=[
|
| 78 |
+
gr.Textbox(label="Texto"),
|
| 79 |
+
gr.Slider(minimum=0, maximum=1, step=0.1, label="Style")
|
| 80 |
+
],
|
| 81 |
+
outputs=gr.Audio(type="filepath"),
|
| 82 |
+
title="Text to Speech ElevenLabs",
|
| 83 |
+
description="Convierte texto a voz usando ElevenLabs. Elige el 'Style' con el slider."
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
if __name__ == "__main__":
|
| 87 |
+
iface.launch(share=True)
|