|
import gradio as gr |
|
import requests |
|
import os |
|
from dotenv import load_dotenv |
|
from datetime import datetime, timedelta |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
CHUNK_SIZE = 1024 |
|
XI_API_KEY = os.getenv("XI_API_KEY") |
|
VOICE_ID = os.getenv("VOICE_ID") |
|
|
|
|
|
user_data = {} |
|
|
|
|
|
MESSAGE_LIMIT = 2 |
|
TIME_LIMIT = timedelta(hours=2) |
|
|
|
def text_to_speech(text, user_id): |
|
current_time = datetime.now() |
|
|
|
if user_id not in user_data: |
|
user_data[user_id] = {'messages': [], 'last_reset': current_time} |
|
|
|
user_info = user_data[user_id] |
|
|
|
|
|
if current_time - user_info['last_reset'] > TIME_LIMIT: |
|
user_info['messages'] = [] |
|
user_info['last_reset'] = current_time |
|
|
|
|
|
if len(user_info['messages']) >= MESSAGE_LIMIT: |
|
return f"Error: L铆mite de mensajes alcanzado. Intenta nuevamente en 2 horas." |
|
|
|
|
|
tts_url = f"https://api.elevenlabs.io/v1/text-to-speech/{VOICE_ID}/stream" |
|
|
|
|
|
headers = { |
|
"Accept": "application/json", |
|
"xi-api-key": XI_API_KEY |
|
} |
|
|
|
|
|
data = { |
|
"text": text, |
|
"model_id": "eleven_multilingual_v2", |
|
"voice_settings": { |
|
"stability": 0.5, |
|
"similarity_boost": 0.8, |
|
"style": 0.0, |
|
"use_speaker_boost": True |
|
} |
|
} |
|
|
|
|
|
response = requests.post(tts_url, headers=headers, json=data, stream=True) |
|
|
|
|
|
if response.ok: |
|
|
|
output_path = "output.mp3" |
|
with open(output_path, "wb") as f: |
|
|
|
for chunk in response.iter_content(chunk_size=CHUNK_SIZE): |
|
f.write(chunk) |
|
|
|
user_info['messages'].append({'message': text, 'time': current_time}) |
|
return output_path |
|
else: |
|
return f"Error: {response.text}" |
|
|
|
|
|
iface = gr.Interface( |
|
fn=text_to_speech, |
|
inputs=["text", "text"], |
|
outputs="audio", |
|
title="", |
|
description="texto" |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|