|
import gradio as gr |
|
import requests |
|
import os |
|
from dotenv import load_dotenv |
|
from datetime import datetime, timedelta |
|
from flask import Flask, jsonify |
|
|
|
|
|
load_dotenv() |
|
|
|
|
|
CHUNK_SIZE = 1024 |
|
XI_API_KEY = os.getenv("XI_API_KEY") |
|
VOICE_ID = os.getenv("VOICE_ID") |
|
|
|
|
|
usage_data = { |
|
'message_count': 0, |
|
'last_reset': datetime.now() |
|
} |
|
|
|
|
|
MESSAGE_LIMIT = 45 |
|
TIME_LIMIT = timedelta(hours=2) |
|
|
|
|
|
app = Flask(__name__) |
|
|
|
@app.route('/reset_usage', methods=['POST']) |
|
def reset_usage(): |
|
global usage_data |
|
usage_data = { |
|
'message_count': 0, |
|
'last_reset': datetime.now() |
|
} |
|
return jsonify({"success": "Usage reset."}), 200 |
|
|
|
def text_to_speech(text): |
|
global usage_data |
|
current_time = datetime.now() |
|
|
|
|
|
if current_time - usage_data['last_reset'] > TIME_LIMIT: |
|
usage_data = { |
|
'message_count': 0, |
|
'last_reset': current_time |
|
} |
|
|
|
|
|
if usage_data['message_count'] >= MESSAGE_LIMIT: |
|
return "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) |
|
|
|
usage_data['message_count'] += 1 |
|
return output_path |
|
else: |
|
return f"Error: {response.text}" |
|
|
|
|
|
iface = gr.Interface( |
|
fn=text_to_speech, |
|
inputs="text", |
|
outputs="audio", |
|
title="", |
|
description="texto" |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
iface.launch() |
|
|