Revert TTS generation to backend with fixed local proxy 127.0.0.1:7890
This commit is contained in:
@@ -858,44 +858,24 @@
|
|||||||
|
|
||||||
async function _generateSingleTTS(text, reference_id) {
|
async function _generateSingleTTS(text, reference_id) {
|
||||||
try {
|
try {
|
||||||
// Fetch directly from frontend to use the local browser's network
|
const res = await fetch('/api/tts/generate', {
|
||||||
const fishRes = await fetch('https://api.fish.audio/v1/tts', {
|
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: { 'Content-Type': 'application/json' },
|
||||||
'Authorization': 'Bearer 233f3a9bcc204b20a1979d59d33cc649',
|
body: JSON.stringify({ text, reference_id, model: TTS_MODEL })
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
text: text,
|
|
||||||
reference_id: reference_id,
|
|
||||||
model: TTS_MODEL,
|
|
||||||
format: "wav"
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!fishRes.ok) {
|
if (!res.ok) {
|
||||||
let msg = 'Fish Audio API Error';
|
let msg = '服务器返回错误';
|
||||||
try { msg = await fishRes.text(); } catch(e){}
|
try { const errData = await res.json(); msg = errData.message || msg; } catch(e){}
|
||||||
throw new Error(`[${fishRes.status}] ${msg}`);
|
throw new Error(`[${res.status}] ${msg}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const audioBlob = await fishRes.blob();
|
const data = await res.json();
|
||||||
|
|
||||||
// Upload the blob to the server as a temporary file
|
|
||||||
const formData = new FormData();
|
|
||||||
formData.append('audio', audioBlob, 'temp.wav');
|
|
||||||
|
|
||||||
const uploadRes = await fetch('/api/tts/upload_temp', {
|
|
||||||
method: 'POST',
|
|
||||||
body: formData
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = await uploadRes.json();
|
|
||||||
if (!data.success) throw new Error(data.message);
|
if (!data.success) throw new Error(data.message);
|
||||||
return data;
|
return data;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.name === 'TypeError' && err.message === 'Failed to fetch') {
|
if (err.name === 'TypeError' && err.message === 'Failed to fetch') {
|
||||||
throw new Error('网络跨域拦截(CORS)或连接失败。请确保你的浏览器已安装并开启 CORS Unblock 插件!');
|
throw new Error('网络连接失败。请确保你的服务器代理正常!');
|
||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -324,44 +324,24 @@
|
|||||||
|
|
||||||
async function _generateTTSLocal(text, reference_id) {
|
async function _generateTTSLocal(text, reference_id) {
|
||||||
try {
|
try {
|
||||||
// Client-side fetch to use local browser's network
|
const res = await fetch('/api/tts/generate', {
|
||||||
const fishRes = await fetch('https://api.fish.audio/v1/tts', {
|
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: { 'Content-Type': 'application/json' },
|
||||||
'Authorization': 'Bearer 233f3a9bcc204b20a1979d59d33cc649',
|
body: JSON.stringify({ text, reference_id, model: TTS_MODEL })
|
||||||
'Content-Type': 'application/json'
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
text: text,
|
|
||||||
reference_id: reference_id,
|
|
||||||
model: TTS_MODEL,
|
|
||||||
format: "wav"
|
|
||||||
})
|
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!fishRes.ok) {
|
if (!res.ok) {
|
||||||
let msg = 'Fish Audio API Error';
|
let msg = '服务器返回错误';
|
||||||
try { msg = await fishRes.text(); } catch(e){}
|
try { const errData = await res.json(); msg = errData.message || msg; } catch(e){}
|
||||||
throw new Error(`[${fishRes.status}] ${msg}`);
|
throw new Error(`[${res.status}] ${msg}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const audioBlob = await fishRes.blob();
|
const data = await res.json();
|
||||||
|
|
||||||
// Upload the blob to the server as a temporary file
|
|
||||||
const formData = new FormData();
|
|
||||||
formData.append('audio', audioBlob, 'temp.wav');
|
|
||||||
|
|
||||||
const uploadRes = await fetch('/api/tts/upload_temp', {
|
|
||||||
method: 'POST',
|
|
||||||
body: formData
|
|
||||||
});
|
|
||||||
|
|
||||||
const data = await uploadRes.json();
|
|
||||||
if (!data.success) throw new Error(data.message);
|
if (!data.success) throw new Error(data.message);
|
||||||
return data;
|
return data;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.name === 'TypeError' && err.message === 'Failed to fetch') {
|
if (err.name === 'TypeError' && err.message === 'Failed to fetch') {
|
||||||
throw new Error('网络跨域拦截(CORS)或连接失败。请确保你的浏览器已安装并开启 CORS Unblock 插件!');
|
throw new Error('网络连接失败。请确保你的服务器代理正常!');
|
||||||
}
|
}
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2423,7 +2423,7 @@ function requestDirect(urlStr, options) {
|
|||||||
|
|
||||||
async function generateTtsWavToTemp(text, reference_id, model = DEFAULT_TTS_MODEL) {
|
async function generateTtsWavToTemp(text, reference_id, model = DEFAULT_TTS_MODEL) {
|
||||||
let proxyUrl = process.env.HTTP_PROXY || process.env.http_proxy || process.env.HTTPS_PROXY || process.env.https_proxy || process.env.ALL_PROXY || process.env.all_proxy;
|
let proxyUrl = process.env.HTTP_PROXY || process.env.http_proxy || process.env.HTTPS_PROXY || process.env.https_proxy || process.env.ALL_PROXY || process.env.all_proxy;
|
||||||
if (!proxyUrl && os.platform() === 'win32') {
|
if (!proxyUrl) {
|
||||||
proxyUrl = 'http://127.0.0.1:7890';
|
proxyUrl = 'http://127.0.0.1:7890';
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2512,21 +2512,21 @@ app.post('/api/tts/generate', express.json(), async (req, res) => {
|
|||||||
body: requestBody
|
body: requestBody
|
||||||
};
|
};
|
||||||
|
|
||||||
// Always try to use local proxy first if we are on a Windows machine
|
// Always try to use proxy
|
||||||
if (!proxyUrl && os.platform() === 'win32') {
|
if (!proxyUrl) {
|
||||||
proxyUrl = 'http://127.0.0.1:7890';
|
proxyUrl = 'http://127.0.0.1:7890';
|
||||||
}
|
}
|
||||||
|
|
||||||
let response;
|
let response;
|
||||||
try {
|
try {
|
||||||
response = proxyUrl
|
response = proxyUrl
|
||||||
? await requestViaProxy('https://fishaudio.fse-media.group/v1/tts', options, proxyUrl)
|
? await requestViaProxy('https://api.fish.audio/v1/tts', options, proxyUrl)
|
||||||
: await requestDirect('https://fishaudio.fse-media.group/v1/tts', options);
|
: await requestDirect('https://api.fish.audio/v1/tts', options);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// Fallback for Windows if local proxy fails
|
// Fallback if proxy fails
|
||||||
if (os.platform() === 'win32' && proxyUrl === 'http://127.0.0.1:7890') {
|
if (proxyUrl === 'http://127.0.0.1:7890') {
|
||||||
console.log(`[TTS] Local proxy failed, attempting direct connection...`);
|
console.log(`[TTS] Local proxy failed, attempting direct connection...`);
|
||||||
response = await requestDirect('https://fishaudio.fse-media.group/v1/tts', options);
|
response = await requestDirect('https://api.fish.audio/v1/tts', options);
|
||||||
} else {
|
} else {
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user