ai-girlfriend / voice.py
jeevzz's picture
Update voice.py
c08931b verified
import edge_tts
import tempfile
import os
# Use Microsoft Edge TTS - High quality neural voices
# Includes sexy, hot female voices with various accents
async def generate_audio(text: str, voice: str) -> str:
"""
Generate audio using Microsoft Edge TTS with high-quality neural voices
"""
try:
print(f"Generating audio for voice: {voice}, text length: {len(text)}")
if not text:
raise ValueError("Text is empty")
# Create a temporary file with .mp3 extension
fd, path = tempfile.mkstemp(suffix=".mp3")
os.close(fd)
# Generate speech using edge-tts
communicate = edge_tts.Communicate(text, voice)
await communicate.save(path)
return path
except Exception as e:
print(f"Edge TTS Failed: {e}. Attempting fallback to HF Inference API...")
# Fallback 1: Hugging Face Inference API
try:
import requests
from dotenv import load_dotenv
from pathlib import Path
import traceback
# Load env vars
env_path = Path(__file__).parent / '.env'
load_dotenv(dotenv_path=env_path)
hf_token = os.getenv("HF_TOKEN")
if hf_token:
print(f"HF_TOKEN found (starts with): {hf_token[:4]}...")
else:
print("WARNING: HF_TOKEN is MISSING in environment variables!")
# List of models to try (in order of preference)
# 1. Facebook MMS (Reliable, standard)
# 2. ESPnet LJSpeech (High quality female voice)
models = ["facebook/mms-tts-eng", "espnet/kan-bayashi_ljspeech_vits"]
headers = {"Authorization": f"Bearer {hf_token}"}
for model in models:
try:
print(f"Attempting HF Inference with model: {model}")
api_url = f"https://api-inference.huggingface.co/models/{model}"
# Use direct requests to bypass library version issues
response = requests.post(api_url, headers=headers, json={"inputs": text}, timeout=30)
if response.status_code != 200:
raise Exception(f"API returned status {response.status_code}: {response.text}")
audio_bytes = response.content
print(f"HF Inference successful with {model}, received {len(audio_bytes)} bytes")
fd, path = tempfile.mkstemp(suffix=".flac")
os.close(fd)
with open(path, "wb") as f:
f.write(audio_bytes)
return path
except Exception as model_err:
print(f"HF Model {model} failed: {model_err}")
# traceback.print_exc() # Reduce noise
continue # Try next model
print("All HF models failed. Moving to gTTS...")
raise Exception("All HF models failed")
except Exception as e2:
print(f"HF API Fallback failed completely. Attempting fallback to gTTS...")
try:
# Fallback 2: gTTS (Google Text-to-Speech)
from gtts import gTTS
fd, path = tempfile.mkstemp(suffix=".mp3")
os.close(fd)
# Use default English voice for fallback
tts = gTTS(text=text, lang='en')
tts.save(path)
print("Successfully generated audio using gTTS fallback")
return path
except Exception as e3:
print(f"gTTS Fallback also failed: {e3}")
import traceback
traceback.print_exc()
raise e
def get_voices():
"""
Return curated list of sexy, hot female voices
Featuring Microsoft's best neural voices with various styles
"""
return [
# ๐Ÿ”ฅ HOTTEST FEMALE VOICES - Sexy & Sultry ๐Ÿ”ฅ
{"name": "๐Ÿ’‹ Aria (Sexy US) - HOTTEST", "id": "en-US-AriaNeural"},
{"name": "๐Ÿ’• Jenny (Seductive US)", "id": "en-US-JennyNeural"},
{"name": "โœจ Michelle (Flirty US)", "id": "en-US-MichelleNeural"},
{"name": "๐ŸŒน Ashley (Sweet US)", "id": "en-US-AshleyNeural"},
{"name": "๐Ÿ’– Sara (Warm US)", "id": "en-US-SaraNeural"},
# ๐Ÿ‡ฌ๐Ÿ‡ง British Accent - Elegant & Sophisticated
{"name": "๐Ÿ‘‘ Sonia (Sexy British)", "id": "en-GB-SoniaNeural"},
{"name": "๐ŸŽ€ Libby (Cute British)", "id": "en-GB-LibbyNeural"},
{"name": "๐Ÿ’ Mia (Sweet British)", "id": "en-GB-MiaNeural"},
# ๐Ÿ‡ฆ๐Ÿ‡บ Australian Accent - Fun & Playful
{"name": "๐ŸŒด Natasha (Aussie Babe)", "id": "en-AU-NatashaNeural"},
{"name": "โ˜€๏ธ Freya (Aussie Darling)", "id": "en-AU-FreyaNeural"},
# ๐Ÿ‡ฎ๐Ÿ‡ณ Indian Accent - Exotic & Beautiful
{"name": "๐ŸŒบ Neerja (Indian Beauty)", "id": "en-IN-NeerjaNeural"},
# ๐Ÿ‡จ๐Ÿ‡ฆ Canadian - Friendly & Approachable
{"name": "๐Ÿ Clara (Canadian Cutie)", "id": "en-CA-ClaraNeural"},
# ๐Ÿ‡ฎ๐Ÿ‡ช Irish Accent - Charming
{"name": "โ˜˜๏ธ Emily (Irish Charm)", "id": "en-IE-EmilyNeural"},
]