Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Form, HTTPException
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
+
from kokoro import KPipeline
|
| 4 |
+
import torch
|
| 5 |
+
import soundfile as sf
|
| 6 |
+
import numpy as np
|
| 7 |
+
import io
|
| 8 |
+
import base64
|
| 9 |
+
|
| 10 |
+
app = FastAPI(title="Kokoro TTS API", description="API لتحويل النص إلى كلام باستخدام Kokoro TTS")
|
| 11 |
+
|
| 12 |
+
# Automatically use GPU if available, otherwise use CPU
|
| 13 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 14 |
+
print(f"Using device: {device}")
|
| 15 |
+
|
| 16 |
+
# Initialize KPipeline globally when the app starts
|
| 17 |
+
try:
|
| 18 |
+
pipeline = KPipeline(lang_code='a', device=device) # Assuming 'a' is still the correct lang_code
|
| 19 |
+
except Exception as e:
|
| 20 |
+
print(f"Error initializing KPipeline: {e}")
|
| 21 |
+
pipeline = None # Handle initialization failure
|
| 22 |
+
|
| 23 |
+
@app.post("/tts/")
|
| 24 |
+
async def text_to_speech(text: str = Form(...)):
|
| 25 |
+
"""
|
| 26 |
+
تحويل النص إلى كلام باستخدام نموذج Kokoro TTS.
|
| 27 |
+
|
| 28 |
+
- **text**: النص المراد تحويله إلى كلام (إلزامي).
|
| 29 |
+
"""
|
| 30 |
+
if pipeline is None:
|
| 31 |
+
raise HTTPException(status_code=500, detail="Kokoro TTS Pipeline لم يتم تهيئته بشكل صحيح.")
|
| 32 |
+
|
| 33 |
+
if not text:
|
| 34 |
+
raise HTTPException(status_code=400, detail="يجب توفير نص لتحويله إلى كلام.")
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
generator = pipeline(text, voice='af_heart', speed=1, split_pattern=None)
|
| 38 |
+
audio_segments = []
|
| 39 |
+
for _, _, audio in generator:
|
| 40 |
+
audio_segments.append(audio)
|
| 41 |
+
full_audio = np.concatenate(audio_segments, axis=0)
|
| 42 |
+
|
| 43 |
+
# Save to BytesIO buffer in WAV format
|
| 44 |
+
wav_buffer = io.BytesIO()
|
| 45 |
+
sf.write(wav_buffer, full_audio, 24000, format='WAV')
|
| 46 |
+
wav_bytes = wav_buffer.getvalue()
|
| 47 |
+
|
| 48 |
+
# Encode to base64 for embedding in JSON response (optional, can also return FileResponse)
|
| 49 |
+
# audio_base64 = base64.b64encode(wav_bytes).decode('utf-8')
|
| 50 |
+
# return {"audio_base64": audio_base64}
|
| 51 |
+
|
| 52 |
+
# Save to a temporary file and return FileResponse (more efficient for larger audio files)
|
| 53 |
+
with open("output.wav", "wb") as wav_file:
|
| 54 |
+
wav_file.write(wav_bytes)
|
| 55 |
+
return FileResponse("output.wav", media_type="audio/wav", filename="output.wav")
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
except Exception as e:
|
| 59 |
+
print(f"Error during TTS processing: {e}")
|
| 60 |
+
raise HTTPException(status_code=500, detail=f"حدث خطأ أثناء معالجة النص إلى كلام: {e}")
|