SAPI4 / app.py
NeoPy's picture
Update app.py
31bcf8c verified
Raw
History Blame Contribute Delete
3.32 kB
import requests
import urllib.parse
from typing import Optional, Dict, Any
from gradio import Server
app = Server()
def generate_tts_audio(
text: str,
voice: str = "Sam",
pitch: int = 100,
speed: int = 100,
base_url: str = "https://tetyys.com/SAPI4"
) -> bytes:
"""
Generate TTS audio using the Tetyys SAPI4 web interface.
Args:
text: Text to synthesize (required)
voice: Voice name (optional, default: "Sam")
pitch: Pitch value (optional, default: 100)
speed: Speed value (optional, default: 100)
base_url: Base URL for the API (optional)
Returns:
bytes: WAV audio file content
Raises:
requests.RequestException: If the HTTP request fails
ValueError: If the response is not valid audio
"""
# URL encode the text to handle special characters
encoded_text = urllib.parse.quote(text)
# Build the endpoint URL with query parameters
endpoint = f"{base_url}/SAPI4?text={encoded_text}"
# Add optional parameters if provided
params = []
if voice:
params.append(f"voice={urllib.parse.quote(voice)}")
if pitch is not None:
params.append(f"pitch={pitch}")
if speed is not None:
params.append(f"speed={speed}")
# Combine base endpoint with optional parameters
if params:
endpoint += "&" + "&".join(params)
# Make the request
response = requests.get(endpoint, timeout=30)
response.raise_for_status()
# Validate that we received audio content
content_type = response.headers.get('Content-Type', '')
if 'audio' not in content_type.lower():
raise ValueError(
f"Expected audio response, got: {content_type}. "
f"Response preview: {response.text[:100]}"
)
return response.content
@app.api(name="tts")
def save_tts_audio(
text: str,
output_path: str,
voice: str = "Sam",
pitch: int = 100,
speed: int = 100,
base_url: str = "https://tetyys.com/SAPI4"
) -> None:
"""
Generate TTS audio and save it to a file.
Args:
text: Text to synthesize
output_path: Path where the WAV file will be saved
voice: Voice name (optional, default: "Sam")
pitch: Pitch value (optional, default: 100)
speed: Speed value (optional, default: 100)
base_url: Base URL for the API (optional)
"""
audio_data = generate_tts_audio(text, voice, pitch, speed, base_url)
with open(output_path, 'wb') as f:
f.write(audio_data)
def get_voice_limits(
voice: str,
base_url: str = "https://tetyys.com/SAPI4"
) -> Dict[str, Any]:
"""
Get the pitch and speed limitations for a specific voice.
Args:
voice: Voice name to query
base_url: Base URL for the API (optional)
Returns:
dict: Voice limitations data
"""
encoded_voice = urllib.parse.quote(voice)
endpoint = f"{base_url}/VoiceLimitations?voice={encoded_voice}"
response = requests.get(endpoint, timeout=30)
response.raise_for_status()
return response.json()
# ─────────────────────────────────────────────
app.launch()