Learn how to integrate SM-AI-MODELS APIs into your Python applications.
Installation
Codepip install requests
Text-to-Speech
Codeimport requests def generate_speech(text, voice="Yara", format="mp3", speed=1.0): """Generate speech from text.""" response = requests.post( "http://localhost:9999/v1/audio/speech", json={ "input": text, "voice": voice, "response_format": format, "speed": speed } ) response.raise_for_status() return response.content # Example usage audio = generate_speech("مرحباً بكم في يونيكود سولوشنز", voice="Yara") with open("output.mp3", "wb") as f: f.write(audio) print("Audio saved to output.mp3")
Speech Recognition
Codeimport requests def transcribe_audio(file_path): """Transcribe audio file to text.""" with open(file_path, "rb") as f: response = requests.post( "http://localhost:8088/v1/audio/transcriptions", files={"file": f} ) response.raise_for_status() return response.json()["text"] # Example usage text = transcribe_audio("recording.wav") print(f"Transcribed: {text}")
Health Check
Codeimport requests def check_health(service="tts"): """Check service health.""" port = 9999 if service == "tts" else 8088 response = requests.get(f"http://localhost:{port}/health") return response.json() # Example usage tts_status = check_health("tts") asr_status = check_health("asr") print(f"TTS: {tts_status['status']}") print(f"ASR: {asr_status['status']}")
Complete Example
Codeimport requests from pathlib import Path class SMAIModels: def __init__(self, tts_url="http://localhost:9999", asr_url="http://localhost:8088"): self.tts_url = tts_url self.asr_url = asr_url def speak(self, text, voice="Yara", format="mp3", speed=1.0): """Convert text to speech.""" response = requests.post( f"{self.tts_url}/v1/audio/speech", json={ "input": text, "voice": voice, "response_format": format, "speed": speed } ) response.raise_for_status() return response.content def transcribe(self, audio_path): """Transcribe audio to text.""" with open(audio_path, "rb") as f: response = requests.post( f"{self.asr_url}/v1/audio/transcriptions", files={"file": f} ) response.raise_for_status() return response.json()["text"] def is_healthy(self): """Check if services are healthy.""" try: tts = requests.get(f"{self.tts_url}/health").json() asr = requests.get(f"{self.asr_url}/health").json() return tts.get("status") == "healthy" and asr.get("status") == "healthy" except: return False # Usage client = SMAIModels() if client.is_healthy(): # Generate speech audio = client.speak("مرحباً بكم", voice="Yara") Path("greeting.mp3").write_bytes(audio) # Transcribe audio text = client.transcribe("greeting.mp3") print(f"Transcribed: {text}") else: print("Services not available")
Error Handling
Codeimport requests from requests.exceptions import RequestException def safe_generate_speech(text, voice="Yara"): """Generate speech with error handling.""" try: response = requests.post( "http://localhost:9999/v1/audio/speech", json={"input": text, "voice": voice}, timeout=30 ) response.raise_for_status() return response.content except requests.Timeout: print("Request timed out") return None except requests.HTTPError as e: if e.response.status_code == 400: print(f"Bad request: {e.response.json()}") elif e.response.status_code == 500: print("Server error") return None except RequestException as e: print(f"Connection error: {e}") return None
Last modified on
