File size: 6,962 Bytes
fff6648
 
a4d2895
fff6648
 
 
 
 
 
 
 
4ce7dc8
a4d2895
 
5e84b34
a4d2895
 
fff6648
 
5c78a42
fff6648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17043fb
 
cabb3d1
17043fb
 
 
 
cabb3d1
17043fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fff6648
 
 
 
 
 
 
 
 
 
 
 
 
 
a4d2895
fff6648
 
 
 
 
fa3c9b4
fff6648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17043fb
fff6648
 
17043fb
 
 
 
 
 
 
 
fff6648
 
699a7a1
fff6648
 
17043fb
fff6648
17043fb
 
 
 
 
fff6648
17043fb
fff6648
 
17043fb
fff6648
 
 
 
17043fb
 
fff6648
 
17043fb
fff6648
17043fb
 
fff6648
17043fb
 
fff6648
17043fb
fff6648
cabb3d1
fff6648
 
17043fb
cabb3d1
fff6648
 
 
 
a4d2895
 
5e84b34
 
 
a4d2895
 
 
 
 
 
 
 
 
 
5e84b34
 
a4d2895
5e84b34
fff6648
a4d2895
3c5d4a8
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import os
import torch
import argparse
import openai
from zipfile import ZipFile
import requests
import se_extractor
from api import BaseSpeakerTTS, ToneColorConverter
import langid
import traceback
from dotenv import load_dotenv

from fastapi import FastAPI, File, UploadFile, Form
from fastapi.responses import JSONResponse
from fastapi.staticfiles import StaticFiles
import uvicorn

# Load environment variables
load_dotenv()

def download_and_extract_checkpoints():
    zip_url = "https://huggingface.co/camenduru/OpenVoice/resolve/main/checkpoints_1226.zip"
    zip_path = "checkpoints.zip"

    if not os.path.exists("checkpoints"):
        print("Downloading checkpoints...")
        response = requests.get(zip_url, stream=True)
        with open(zip_path, "wb") as zip_file:
            for chunk in response.iter_content(chunk_size=8192):
                if chunk:
                    zip_file.write(chunk)
        print("Extracting checkpoints...")
        with ZipFile(zip_path, "r") as zip_ref:
            zip_ref.extractall(".")
        os.remove(zip_path)
        print("Checkpoints are ready.")

# Call the function to ensure checkpoints are available
download_and_extract_checkpoints()

# Initialize OpenAI API key
openai.api_key = os.getenv("OPENAI_API_KEY")
if not openai.api_key:
    raise ValueError("Please set the OPENAI_API_KEY environment variable.")

en_ckpt_base = 'checkpoints/base_speakers/EN'
zh_ckpt_base = 'checkpoints/base_speakers/ZH'
ckpt_converter = 'checkpoints/converter'
device = 'cuda' if torch.cuda.is_available() else 'cpu'
output_dir = 'outputs'
os.makedirs(output_dir, exist_ok=True)

en_base_speaker_tts = BaseSpeakerTTS(f'{en_ckpt_base}/config.json', device=device)
en_base_speaker_tts.load_ckpt(f'{en_ckpt_base}/checkpoint.pth')
zh_base_speaker_tts = BaseSpeakerTTS(f'{zh_ckpt_base}/config.json', device=device)
zh_base_speaker_tts.load_ckpt(f'{zh_ckpt_base}/checkpoint.pth')

tone_color_converter = ToneColorConverter(f'{ckpt_converter}/config.json', device=device)
tone_color_converter.load_ckpt(f'{ckpt_converter}/checkpoint.pth')

en_source_default_se = torch.load(f'{en_ckpt_base}/en_default_se.pth').to(device)
en_source_style_se = torch.load(f'{en_ckpt_base}/en_style_se.pth').to(device)
zh_source_se = torch.load(f'{zh_ckpt_base}/zh_default_se.pth').to(device)

default_speaker_audio = "resources/output.wav"
try:
    target_se, _ = se_extractor.get_se(
        default_speaker_audio,
        tone_color_converter,
        target_dir='processed',
        vad=True
    )
    print("Speaker embedding extracted successfully.")
except Exception as e:
    raise RuntimeError(f"Failed to extract speaker embedding from {default_speaker_audio}: {str(e)}")

supported_languages = ['zh', 'en']

def predict(audio_file_pth, agree):
    text_hint = ''
    synthesized_audio_path = None

    if not agree:
        text_hint += '[ERROR] Please accept the Terms & Conditions!\n'
        return (text_hint, None)

    if audio_file_pth is not None:
        speaker_wav = audio_file_pth
    else:
        text_hint += "[ERROR] Please provide your voice as an audio file.\n"
        return (text_hint, None)

    # Transcribe audio to text using OpenAI Whisper
    try:
        with open(speaker_wav, 'rb') as audio_file:
            transcription_response = openai.audio.transcriptions.create(
                model="whisper-1",
                file=audio_file,
                response_format='text'
            )
        input_text = transcription_response.strip()
        print(f"Transcribed Text: {input_text}")
    except Exception as e:
        text_hint += f"[ERROR] Transcription failed: {str(e)}\n"
        return (text_hint, None)

    if len(input_text) == 0:
        text_hint += "[ERROR] No speech detected in the audio.\n"
        return (text_hint, None)

    language_predicted = langid.classify(input_text)[0].strip()
    print(f"Detected language: {language_predicted}")

    if language_predicted not in supported_languages:
        text_hint += f"[ERROR] The detected language '{language_predicted}' is not supported. Supported languages are: {supported_languages}\n"
        return (text_hint, None)

    if language_predicted == "zh":
        tts_model = zh_base_speaker_tts
        language = 'Chinese'
        speaker_style = 'default'
    else:
        tts_model = en_base_speaker_tts
        language = 'English'
        speaker_style = 'default'

    try:
        response = openai.chat.completions.create(
            model="gpt-4o-mini",
            messages=[
                {"role": "system", "content": "You are Mickey Mouse, a friendly and cheerful character who responds to children's queries in a simple and engaging manner. Please keep your response up to 200 characters."},
                {"role": "user", "content": input_text}
            ],
            max_tokens=200,
            n=1,
            stop=None,
            temperature=0.7,
        )
        reply_text = response.choices[0].message.content.strip()
        print(f"GPT-4 Reply: {reply_text}")
    except Exception as e:
        text_hint += f"[ERROR] Failed to get response from OpenAI GPT-4: {str(e)}\n"
        return (text_hint, None)

    try:
        src_path = os.path.join(output_dir, 'tmp_reply.wav')
        tts_model.tts(reply_text, src_path, speaker=speaker_style, language=language)
        print(f"Audio synthesized and saved to {src_path}")

        save_path = os.path.join(output_dir, 'output_reply.wav')

        tone_color_converter.convert(
            audio_src_path=src_path, 
            src_se=en_source_default_se if language == 'English' else zh_source_se,
            tgt_se=target_se,
            output_path=save_path,
            message="@MickeyMouse"
        )
        print(f"Tone color conversion completed and saved to {save_path}")

        text_hint += "Response generated successfully.\n"
        synthesized_audio_path = save_path
    except Exception as e:
        text_hint += f"[ERROR] Failed to synthesize audio: {str(e)}\n"
        traceback.print_exc()
        return (text_hint, None)

    return (text_hint, synthesized_audio_path)

app = FastAPI()

# Mount the 'outputs' directory to serve static files
app.mount("/outputs", StaticFiles(directory="outputs"), name="outputs")

@app.post("/predict")
async def predict_endpoint(agree: bool = Form(...), audio_file: UploadFile = File(...)):
    temp_dir = "temp"
    os.makedirs(temp_dir, exist_ok=True)
    audio_path = os.path.join(temp_dir, audio_file.filename)
    with open(audio_path, "wb") as f:
        f.write(await audio_file.read())

    info, audio_output_path = predict(audio_path, agree)
    if audio_output_path:
        audio_url = f"/outputs/{os.path.basename(audio_output_path)}"
        return {"info": info, "audio_path": audio_url}
    else:
        return {"info": info, "audio_path": None}, 400

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=int(os.environ.get("PORT", 7860)))