File size: 2,602 Bytes
bf679e1
 
42d241e
bf679e1
42d241e
bf679e1
b485fef
bf679e1
 
 
cc22598
b485fef
bf679e1
cc22598
bf679e1
b485fef
 
bf679e1
b485fef
 
bf679e1
 
b485fef
 
bf679e1
b485fef
 
 
 
 
 
bf679e1
 
 
b485fef
 
bf679e1
 
b485fef
 
bf679e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b485fef
bf679e1
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from transformers import AutoTokenizer, VitsModel
import torch
import numpy as np
import os
import noisereduce as nr
import base64
import io
import soundfile as sf

# قراءة التوكن من Secrets
token = os.getenv("acees-token")

# تخزين النماذج
models = {}

# اختيار الجهاز
device = "cuda" if torch.cuda.is_available() else "cpu"

# إزالة الضوضاء
def remove_noise(audio_data, sr=16000):
    return nr.reduce_noise(y=audio_data, hop_length=256, sr=sr)

# تحميل النموذج
def get_model(name_model):
    global models
    if name_model in models:
        tokenizer = AutoTokenizer.from_pretrained(name_model, token=token)
        return models[name_model], tokenizer

    model = VitsModel.from_pretrained(name_model, token=token)
    model.decoder.apply_weight_norm()
    for flow in model.flow.flows:
        torch.nn.utils.weight_norm(flow.conv_pre)
        torch.nn.utils.weight_norm(flow.conv_post)
    model.to(device)
    models[name_model] = model

    tokenizer = AutoTokenizer.from_pretrained(name_model, token=token)
    return model, tokenizer

# نموذج البيانات للـ POST
class TTSRequest(BaseModel):
    text: str
    name_model: str = "wasmdashai/vits-ar-sa-huba-v2"
    speaking_rate: float = 16000.0

# إنشاء التطبيق
app = FastAPI(title="VITS TTS API", description="Convert Arabic/English text to speech using VITS models")

@app.get("/", summary="Health check")
def home():
    return {"message": "FastAPI VITS TTS service is running"}

@app.post("/predict/", summary="Text-to-Speech", description="Convert text to audio (WAV, Base64)")
def modelspeech(req: TTSRequest):
    try:
        model, tokenizer = get_model(req.name_model)
        inputs = tokenizer(req.text, return_tensors="pt").to(device)
        model.speaking_rate = req.speaking_rate

        with torch.no_grad():
            outputs = model(**inputs)
            waveform = outputs.waveform[0].cpu().numpy()

        # إزالة الضوضاء
        waveform = remove_noise(waveform)

        # تحويل الصوت إلى Base64 WAV
        buffer = io.BytesIO()
        sf.write(buffer, waveform, samplerate=model.config.sampling_rate, format="WAV")
        buffer.seek(0)
        audio_base64 = base64.b64encode(buffer.read()).decode("utf-8")

        return {
            "sampling_rate": model.config.sampling_rate,
            "audio_base64": audio_base64
        }

    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))