File size: 6,129 Bytes
36fd81e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from fastapi import FastAPI, HTTPException, Query
from fastapi.responses import StreamingResponse
import os
from os import environ as env
import torch
import time
import nltk
import io
import base64
import torchaudio
from fastapi.responses import JSONResponse
from app.inference import inference, LFinference, compute_style
import numpy as np

nltk.download('punkt')
nltk.download('punkt_tab')

app = FastAPI()

device = 'cuda' if torch.cuda.is_available() else 'cpu'

@app.get("/")
async def read_root():
    #return {"details": f"Hello! This is {env['SECRET_API_KEY']} environment"}
    #return {"details": f"Hello Stream!"}
    #return {"details": f"Hello Stream! This is {env['API_KEY_SECRET']} environment running OK!"}
    return {"details": "Environment is running OK!"}

@app.post("/synthesize/")
async def synthesize(
    text: str, 
    return_base64: bool = True,
    ###################################################
    diffusion_steps: int = Query(5, ge=5, le=200),
    embedding_scale: float = Query(1.0, ge=1.0, le=5.0)
    ###################################################
):
    try:
        start = time.time()
        noise = torch.randn(1, 1, 256).to(device)
        wav = inference(text, noise, diffusion_steps=diffusion_steps, embedding_scale=embedding_scale)
        rtf = (time.time() - start) / (len(wav) / 24000)

        if return_base64:
            audio_buffer = io.BytesIO()
            torchaudio.save(audio_buffer, torch.tensor(wav).unsqueeze(0), 24000, format="wav")
            audio_buffer.seek(0)

            audio_base64 = base64.b64encode(audio_buffer.read()).decode('utf-8')

            return JSONResponse(content={"RTF": rtf, "audio_base64": audio_base64})
        else:
            return JSONResponse(content={"RTF": rtf, "audio": wav.tolist()})

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


@app.post("/synthesize_longform_streaming/")
async def synthesize_longform(
    passage: str, 
    return_base64: bool = False,
    ###################################################
    alpha: float = Query(0.7, ge=0.0, le=1.0),
    diffusion_steps: int = Query(10, ge=5, le=200),
    embedding_scale: float = Query(1.5, ge=1.0, le=5.0)
    ###################################################
):
    try:
        sentences = passage.split('.') # simple split
        wavs = []
        s_prev = None
        
        start = time.time()
        
        for text in sentences:
            if text.strip() == "":
                continue
            text += '.'  # add it back
            noise = torch.randn(1, 1, 256).to(device)  # Generate noise
            wav, s_prev = LFinference(text, s_prev, noise, alpha=0.7, 
                                       diffusion_steps=diffusion_steps, 
                                       embedding_scale=embedding_scale)
            wavs.append(wav)

        final_wav = np.concatenate(wavs)  # Concatenate all wavs
        rtf = (time.time() - start) / (len(final_wav) / 24000)

        audio_buffer = io.BytesIO()
        torchaudio.save(audio_buffer, torch.tensor(final_wav).unsqueeze(0), 24000, format="wav")
        audio_buffer.seek(0)

        if return_base64:
            audio_base64 = base64.b64encode(audio_buffer.read()).decode('utf-8')
            return JSONResponse(content={"RTF": rtf, "audio_base64": audio_base64})
        else:
            #return JSONResponse(content={"RTF": rtf, "audio": final_wav.tolist()})
            return StreamingResponse(audio_buffer, media_type="audio/wav")

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



@app.post("/synthesize_with_emotion/")
async def synthesize_with_emotion(
    texts: dict, 
    return_base64: bool = True,
    ###################################################
    diffusion_steps: int = Query(100, ge=5, le=200),
    embedding_scale: float = Query(5.0, ge=1.0, le=5.0)
    ###################################################
):
    try:
        results = []
        
        for emotion, text in texts.items():
            noise = torch.randn(1, 1, 256).to(device)
            wav = inference(text, noise, diffusion_steps=diffusion_steps, 
                            embedding_scale=embedding_scale)
            
            if return_base64:
                audio_buffer = io.BytesIO()
                torchaudio.save(audio_buffer, torch.tensor(wav).unsqueeze(0), 24000, format="wav")
                audio_buffer.seek(0)

                audio_base64 = base64.b64encode(audio_buffer.read()).decode('utf-8')

                results.append({
                    "emotion": emotion,
                    "audio_base64": audio_base64
                })
            else:
                results.append({
                    "emotion": emotion,
                    "audio": wav.tolist()
                })

        return JSONResponse(content={"results": results})

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





@app.post("/synthesize_streaming_audio/")
async def synthesize_streaming_audio(
    text: str, 
    return_base64: bool = False,
    ###################################################
    diffusion_steps: int = Query(5, ge=5, le=200),
    embedding_scale: float = Query(1.0, ge=1.0, le=5.0)
    ###################################################
):
    try:
        start = time.time()
        noise = torch.randn(1, 1, 256).to(device)
        wav = inference(text, noise, diffusion_steps=diffusion_steps, embedding_scale=embedding_scale)
        rtf = (time.time() - start) / (len(wav) / 24000)

        audio_buffer = io.BytesIO()
        torchaudio.save(audio_buffer, torch.tensor(wav).unsqueeze(0), 24000, format="wav")
        audio_buffer.seek(0)

        if return_base64:
            audio_base64 = base64.b64encode(audio_buffer.read()).decode('utf-8')
            return JSONResponse(content={"RTF": rtf, "audio_base64": audio_base64})
        else:
            return StreamingResponse(audio_buffer, media_type="audio/wav")

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