ai-girlfriend / voice.py
jeevzz's picture
Update voice.py
ea7c4ee verified
raw
history blame
4.29 kB
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...")
try:
# Fallback 1: Hugging Face Inference API (Realistic Female Voice)
# using espnet/kan-bayashi_ljspeech_vits (High quality single speaker)
from huggingface_hub import InferenceClient
from dotenv import load_dotenv
from pathlib import Path
# Load env vars if not already loaded
env_path = Path(__file__).parent / '.env'
load_dotenv(dotenv_path=env_path)
hf_token = os.getenv("HF_TOKEN")
client = InferenceClient(token=hf_token)
# Generate audio using HF API
# This model is a high quality female voice
audio_bytes = client.text_to_speech(text, model="espnet/kan-bayashi_ljspeech_vits")
fd, path = tempfile.mkstemp(suffix=".flac") # Model returns flac usually
os.close(fd)
with open(path, "wb") as f:
f.write(audio_bytes)
print("Successfully generated audio using HF Inference API")
return path
except Exception as e2:
print(f"HF API Fallback failed: {e2}. 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"},
]