Download app.py from NeoPy/SAPI4: direct link, hf CLI and curl.
- Browser
- Download file 3.32 kB
-
https://huggingface.co/spaces/NeoPy/SAPI4/resolve/main/app.py
- Command line
-
hf download hf://spaces/NeoPy/SAPI4/app.py
-
curl -L -o app.py https://huggingface.co/spaces/NeoPy/SAPI4/resolve/main/app.py
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 | |
| 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() |