Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, HTTPException, UploadFile, File | |
| from fastapi.responses import FileResponse | |
| from kokoro import KPipeline | |
| import soundfile as sf | |
| import os | |
| import tempfile | |
| app = FastAPI(title="Text-to-Speech Converter") | |
| # Initialize pipeline once at startup | |
| pipeline = KPipeline(lang_code='a') | |
| async def generate_audio(text: str = None): | |
| if not text: | |
| raise HTTPException(status_code=400, detail="No text provided") | |
| try: | |
| # Create temporary directory | |
| with tempfile.TemporaryDirectory() as tmpdir: | |
| # Generate audio | |
| generator = pipeline( | |
| text, | |
| voice='af_heart', | |
| speed=1, | |
| split_pattern=r'\n+' | |
| ) | |
| # Process first audio segment only (modify as needed) | |
| i, (gs, ps, audio) = next(enumerate(generator)) | |
| # Save to temporary file | |
| output_path = f"{tmpdir}/output.wav" | |
| sf.write(output_path, audio, 24000) | |
| return FileResponse( | |
| output_path, | |
| media_type='audio/wav', | |
| filename="generated_audio.wav" | |
| ) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) |