| 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") |
|
|
| |
| usage_data = { |
| 'message_count': 0, |
| 'last_reset': datetime.now() |
| } |
|
|
| |
| MESSAGE_LIMIT = 145 |
| TIME_LIMIT = timedelta(hours=2) |
|
|
| def text_to_speech(text, style, speed): |
| 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 None |
|
|
| |
| 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": style, |
| "speed": speed, |
| "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 None |
|
|
| iface = gr.Interface( |
| fn=text_to_speech, |
| inputs=[ |
| gr.Textbox(label="Texto"), |
| gr.Slider(minimum=0, maximum=1, step=0.1, label="Style", value=0.5), |
| gr.Slider(minimum=0.7, maximum=1.2, step=0.01, label="Velocidad", value=1) |
| ], |
| outputs=gr.Audio(type="filepath"), |
| title="", |
| description="" |
| ) |
|
|
| if __name__ == "__main__": |
| iface.launch(share=True) |