import gradio as gr import requests import json import os import time from collections import defaultdict BASE_URL = "https://api.jigsawstack.com/v1" headers = { "x-api-key": os.getenv("JIGSAWSTACK_API_KEY") } # Rate limiting configuration request_times = defaultdict(list) MAX_REQUESTS = 20 # Maximum requests per time window TIME_WINDOW = 3600 # Time window in seconds (1 hour) def get_real_ip(request: gr.Request): """Extract real IP address using x-forwarded-for header or fallback""" if not request: return "unknown" forwarded = request.headers.get("x-forwarded-for") if forwarded: ip = forwarded.split(",")[0].strip() # First IP in the list is the client's else: ip = request.client.host # fallback return ip def check_rate_limit(request: gr.Request): """Check if the current request exceeds rate limits""" if not request: return True, "Rate limit check failed - no request info" ip = get_real_ip(request) now = time.time() # Clean up old timestamps outside the time window request_times[ip] = [t for t in request_times[ip] if now - t < TIME_WINDOW] # Check if rate limit exceeded if len(request_times[ip]) >= MAX_REQUESTS: time_remaining = int(TIME_WINDOW - (now - request_times[ip][0])) time_remaining_minutes = round(time_remaining / 60, 1) time_window_minutes = round(TIME_WINDOW / 60, 1) return False, f"Rate limit exceeded. You can make {MAX_REQUESTS} requests per {time_window_minutes} minutes. Try again in {time_remaining_minutes} minutes." # Add current request timestamp request_times[ip].append(now) return True, "" def text_to_speech(text, accent, voice_clone_id, request: gr.Request): rate_limit_ok, rate_limit_msg = check_rate_limit(request) if not rate_limit_ok: return None, rate_limit_msg if not text or not text.strip(): return None, "Error: Text input is required." payload = {"text": text.strip()} if accent and accent.strip(): payload["accent"] = accent.strip() if voice_clone_id and voice_clone_id.strip(): payload["voice_clone_id"] = voice_clone_id.strip() # Validate text length max_len = 500 if "voice_clone_id" in payload else 1500 if len(payload["text"]) > max_len: mode = "Voice Cloning" if "voice_clone_id" in payload else "Standard" return None, f"Error: Text for {mode} is limited to {max_len} characters." try: response = requests.post(f"{BASE_URL}/ai/tts", headers=headers, json=payload) response.raise_for_status() if response.headers.get("content-type", "").startswith("audio/"): import tempfile with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as tmp_file: tmp_file.write(response.content) return tmp_file.name, "✅ Speech generated successfully!" else: try: error_data = response.json() return None, f"Error: API returned an error - {error_data.get('message', 'Unknown error')}" except: return None, "Error: Received an unexpected response from the API." except requests.exceptions.RequestException as e: return None, f"Request failed: {str(e)}" except Exception as e: return None, f"An unexpected error occurred: {str(e)}" # ----------------- Gradio UI ------------------ with gr.Blocks() as demo: gr.Markdown("""

🧩 Text to Speech

Transform text into natural-sounding human-like AI voices with low latency and exceptional quality.

For more details and API usage, see the documentation.

""") with gr.Row(): with gr.Column(): gr.Markdown("#### Input") tts_text = gr.Textbox( label="Text to Convert", lines=5, placeholder="Enter the text you want to convert to speech here." ) gr.Markdown("#### Quick Examples") gr.Markdown("Click any example below to auto-fill the text:") with gr.Row(): tts_example_btn1 = gr.Button("👋 Greeting", size="sm") tts_example_btn2 = gr.Button("📢 Announcement", size="sm") tts_example_btn3 = gr.Button("📚 Story", size="sm") with gr.Row(): tts_example_btn4 = gr.Button("🎵 Song Lyrics", size="sm") tts_example_btn5 = gr.Button("📖 Poem", size="sm") tts_example_btn6 = gr.Button("💼 Business", size="sm") with gr.Row(): tts_example_btn7 = gr.Button("🎭 Drama", size="sm") tts_example_btn8 = gr.Button("🔔 Notification", size="sm") accent = gr.Textbox( label="Voice Accent (Optional)", placeholder="e.g., en-US-female-27, en-GB-male-2", info="Default voice is used if left blank. Ignored if Voice Clone ID is provided." ) voice_clone_id = gr.Textbox( label="Voice Clone ID (Optional)", placeholder="Enter a voice clone ID to use a custom voice.", info="Overrides the accent if provided." ) with gr.Column(): gr.Markdown("#### Generated Audio") audio_output = gr.Audio(label="Speech Output") tts_status = gr.Textbox(label="Status", interactive=False, placeholder="Ready to generate speech...") tts_btn = gr.Button("Generate Speech", variant="primary") # Example functions to auto-fill text field def fill_tts_example_1(): return "Hello! Welcome to our AI-powered text-to-speech system. This technology can convert any written text into natural-sounding speech." def fill_tts_example_2(): return "Attention all passengers. Flight 1234 to New York is now boarding at gate 15. Please have your boarding pass ready." def fill_tts_example_3(): return "Once upon a time, in a magical forest, there lived a wise old owl who loved to share stories with all the woodland creatures." def fill_tts_example_4(): return "Twinkle twinkle little star, how I wonder what you are. Up above the world so high, like a diamond in the sky." def fill_tts_example_5(): return "The road not taken by Robert Frost. Two roads diverged in a yellow wood, and sorry I could not travel both." def fill_tts_example_6(): return "Thank you for your business. Your order has been confirmed and will be shipped within 2-3 business days." def fill_tts_example_7(): return "To be or not to be, that is the question. Whether tis nobler in the mind to suffer the slings and arrows of outrageous fortune." def fill_tts_example_8(): return "You have a new message. Please check your inbox for important updates regarding your account." # Connect example buttons to auto-fill functions tts_example_btn1.click(fill_tts_example_1, outputs=[tts_text]) tts_example_btn2.click(fill_tts_example_2, outputs=[tts_text]) tts_example_btn3.click(fill_tts_example_3, outputs=[tts_text]) tts_example_btn4.click(fill_tts_example_4, outputs=[tts_text]) tts_example_btn5.click(fill_tts_example_5, outputs=[tts_text]) tts_example_btn6.click(fill_tts_example_6, outputs=[tts_text]) tts_example_btn7.click(fill_tts_example_7, outputs=[tts_text]) tts_example_btn8.click(fill_tts_example_8, outputs=[tts_text]) tts_btn.click( text_to_speech, inputs=[tts_text, accent, voice_clone_id], outputs=[audio_output, tts_status] ) demo.launch()