Spaces:
Sleeping
Sleeping
Download server.js from Alejandrott24/piper-tts-server: direct link, hf CLI and curl.
- Browser
- Download file 10.5 kB
-
https://huggingface.co/spaces/Alejandrott24/piper-tts-server/resolve/main/server.js
- Command line
-
hf download hf://spaces/Alejandrott24/piper-tts-server/server.js
-
curl -L -o server.js https://huggingface.co/spaces/Alejandrott24/piper-tts-server/resolve/main/server.js
10.5 kB
| const express = require('express'); | |
| const { exec } = require('child_process'); | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const cors = require('cors'); | |
| const app = express(); | |
| app.use(cors()); | |
| app.use(express.json()); | |
| // 🐧 Ruta para Linux (Hugging Face usa /home/user por defecto) | |
| const PIPER_PATH = process.env.PIPER_PATH || '/home/user/piper'; | |
| const PIPER_BINARY = path.join(PIPER_PATH, 'piper'); // Sin .exe en Linux | |
| function normalizarTexto(texto) { | |
| const onomatopeyas = { | |
| // ========== RISAS (mejoradas) ========== | |
| 'jajaja+': 'ja. ja. ja. ja.', | |
| 'jajaja': 'ja. ja. ja.', | |
| 'jaja': 'ja. ja.', | |
| 'ja': 'ja.', | |
| 'jejeje+': 'je. je. je. je.', | |
| 'jejeje': 'je. je. je.', | |
| 'jeje': 'je. je.', | |
| 'je': 'je.', | |
| 'jijiji+': 'ji. ji. ji. ji.', | |
| 'jijiji': 'ji. ji. ji.', | |
| 'jiji': 'ji. ji.', | |
| 'ji': 'ji.', | |
| 'jojojo': 'jo. jo. jo.', | |
| 'juajua': 'jua. jua.', | |
| // ========== COMIDA Y BEBIDA ========== | |
| 'ñam+': 'ñam. ñam. ñam.', | |
| 'ñam': 'ñam.', | |
| 'nam+': 'ñam. ñam. ñam.', // Por si viene sin ñ | |
| 'nam': 'ñam.', | |
| 'slurp+': 'slurp. slurp.', | |
| 'slurp': 'slurp.', | |
| 'glup+': 'glup. glup.', | |
| 'glup': 'glup.', | |
| 'gulp': 'glup.', | |
| 'sorber': 'slurp.', | |
| 'tragar': 'glup.', | |
| 'munch': 'ñam. ñam.', | |
| 'chomp': 'ñam.', | |
| // ========== RESPIRACIÓN Y SUSPIROS ========== | |
| 'uff+': 'ufff.', | |
| 'uff': 'uff.', | |
| 'uf': 'uf.', | |
| 'huff': 'uff.', | |
| 'puff': 'puff.', | |
| 'suspiro': 'ahhh.', | |
| 'exhalar': 'fuu.', | |
| 'inhalar': 'ahhh,', | |
| // ========== SILENCIO Y SUSURROS ========== | |
| 'shhh+': 'shhhhh.', | |
| 'shhh': 'shhh.', | |
| 'shh': 'shh.', | |
| 'shush': 'shhh.', | |
| 'psss+': 'psss.', | |
| 'psss': 'psss.', | |
| 'pst': 'pst.', | |
| // ========== EXPRESIONES VOCALES ========== | |
| 'ahhh+': 'ahhhhh.', | |
| 'ahhh': 'ahhh.', | |
| 'ahh': 'ahh.', | |
| 'ah': 'ah.', | |
| 'ohhh+': 'ohhhhh.', | |
| 'ohhh': 'ohhh.', | |
| 'ohh': 'ohh.', | |
| 'oh': 'oh.', | |
| 'uhhh+': 'uhhh.', | |
| 'uhh': 'uhh.', | |
| 'uh': 'uh.', | |
| 'ehhh': 'ehhh.', | |
| 'eh': 'eh.', | |
| 'mmmm+': 'mmmmm.', | |
| 'mmmm': 'mmmm.', | |
| 'mmm': 'mmm.', | |
| 'mm': 'mm.', | |
| 'hmmm+': 'hmmmm.', | |
| 'hmmm': 'hmmm.', | |
| 'hmm': 'hmm.', | |
| 'hm': 'hm.', | |
| // ========== DOLOR Y SORPRESA ========== | |
| 'ayy+': 'ay, ay, ay.', | |
| 'ayy': 'ay, ay.', | |
| 'ay': 'ay.', | |
| 'auch+': 'auch, auch.', | |
| 'auch': 'auch.', | |
| 'ouch': 'auch.', | |
| 'ow': 'au.', | |
| 'agh': 'agh.', | |
| 'argh': 'argh.', | |
| 'uy': 'uy.', | |
| 'ups': 'ups.', | |
| 'oops': 'ups.', | |
| 'opa': 'opa.', | |
| // ========== ASCO Y DESAGRADO ========== | |
| 'puaj': 'puaj.', | |
| 'guácala': 'guácala.', | |
| 'guacala': 'guácala.', | |
| 'ecs': 'ecs.', | |
| 'aj': 'aj.', | |
| 'blergh': 'blergh.', | |
| 'ugh': 'agh.', | |
| 'eww': 'iu.', | |
| // ========== ESTORNUDOS Y TOS ========== | |
| 'achís+': 'achú, achú.', | |
| 'achís': 'achú.', | |
| 'atchís': 'achú.', | |
| 'achú': 'achú.', | |
| 'achoo': 'achú.', | |
| 'cof cof': 'cof, cof.', | |
| 'ejem': 'ejem.', | |
| 'ahem': 'ejem.', | |
| // ========== GOLPES Y ACCIONES ========== | |
| 'pum': 'pum.', | |
| 'bam': 'bam.', | |
| 'pow': 'pau.', | |
| 'bum': 'bum.', | |
| 'boom': 'bum.', | |
| 'bang': 'bang.', | |
| 'paf': 'paf.', | |
| 'zas': 'zas.', | |
| 'plas': 'plas.', | |
| 'clap': 'clap.', | |
| 'crac': 'crac.', | |
| 'crack': 'crac.', | |
| 'crash': 'crash.', | |
| 'smash': 'smash.', | |
| 'splash': 'splash.', | |
| 'plaf': 'plaf.', | |
| 'plop': 'plop.', | |
| 'thud': 'thud.', | |
| 'wham': 'guam.', | |
| // ========== SONIDOS MECÁNICOS ========== | |
| 'clic': 'clic.', | |
| 'click': 'clic.', | |
| 'clac': 'clac.', | |
| 'tic tac': 'tic, tac.', | |
| 'tictac': 'tic, tac.', | |
| 'ring+': 'ring, ring, ring.', | |
| 'ring': 'ring.', | |
| 'beep+': 'bip, bip, bip.', | |
| 'beep': 'bip.', | |
| 'bip': 'bip.', | |
| 'buzz': 'bzzz.', | |
| 'bzzz': 'bzzz.', | |
| 'vrum': 'vrum.', | |
| 'brum': 'brum.', | |
| 'piii': 'piii.', | |
| 'tuuut': 'tuuut.', | |
| // ========== ANIMALES ========== | |
| 'guau+': 'guau, guau.', | |
| 'guau': 'guau.', | |
| 'woof': 'guau.', | |
| 'miau+': 'miau, miau.', | |
| 'miau': 'miau.', | |
| 'meow': 'miau.', | |
| 'muuu': 'muuu.', | |
| 'moo': 'muuu.', | |
| 'cuac': 'cuac.', | |
| 'quack': 'cuac.', | |
| 'pío': 'pío.', | |
| 'tweet': 'pío.', | |
| 'cock-a-doodle-doo': 'quiquiriquí.', | |
| 'kikiriki': 'quiquiriquí.', | |
| 'croac': 'croac.', | |
| 'ribbit': 'croac.', | |
| 'oinc': 'oinc.', | |
| 'oink': 'oinc.', | |
| 'hiiiiii': 'hiiii.', | |
| 'neigh': 'hiiii.', | |
| 'ssss': 'ssss.', | |
| 'hiss': 'ssss.', | |
| // ========== EXPRESIONES EN INGLÉS ========== | |
| 'wow': 'uau.', | |
| 'woah': 'uoou.', | |
| 'whoa': 'uoou.', | |
| 'yay': 'yei.', | |
| 'yey': 'yei.', | |
| 'yeah': 'ye.', | |
| 'yup': 'yep.', | |
| 'nope': 'nop.', | |
| 'uh-oh': 'oh, oh.', | |
| 'uh oh': 'oh, oh.', | |
| 'duh': 'dah.', | |
| 'meh': 'me.', | |
| 'pfft': 'pfff.', | |
| 'tsk': 'tsc.', | |
| // ========== CELEBRACIÓN ========== | |
| 'hurra': 'hurra.', | |
| 'yujuu': 'yuhu.', | |
| 'yupi': 'yupi.', | |
| 'olé': 'olé.', | |
| 'bravo': 'bravo.', | |
| 'weee': 'uiii.', | |
| // ========== SUSTOS Y MIEDO ========== | |
| 'buu': 'buu.', | |
| 'boo': 'buu.', | |
| 'ahhh': 'ahhh.', | |
| 'eek': 'iii.', | |
| 'gulp': 'glup.', | |
| // ========== OTROS ========== | |
| 'ta-da': 'tachán.', | |
| 'tachán': 'tachán.', | |
| 'voilà': 'voalá.', | |
| 'zzzz': 'zzzz.', | |
| 'snore': 'zzzz.', | |
| 'yawn': 'ahhh.', | |
| 'burp': 'burp.', | |
| 'hiccup': 'hip.', | |
| 'sniff': 'snif.', | |
| 'sob': 'buaa.', | |
| 'boo-hoo': 'buaa.', | |
| 'hic': 'hip.', | |
| 'achoo': 'achú.', | |
| }; | |
| let textoLimpio = texto | |
| .replace(/¡/g, '') | |
| .replace(/¿/g, ''); | |
| // Reemplazar onomatopeyas (primero las más largas) | |
| const ordenadas = Object.entries(onomatopeyas) | |
| .sort((a, b) => b[0].length - a[0].length); | |
| for (let [onomatopeya, sonido] of ordenadas) { | |
| const regex = new RegExp(`\\b${onomatopeya}\\b`, 'gi'); | |
| textoLimpio = textoLimpio.replace(regex, sonido); | |
| } | |
| // Controlar repeticiones excesivas | |
| textoLimpio = textoLimpio | |
| .replace(/(.)\1{4,}/g, '$1$1$1') | |
| .replace(/\s+/g, ' ') | |
| .trim(); | |
| return textoLimpio; | |
| } | |
| app.post('/tts', (req, res) => { | |
| let { text, model, speaker_id, length_scale, noise_scale, noise_w, sentence_silence } = req.body; | |
| if (!text) { | |
| return res.status(400).json({ error: 'Falta el texto' }); | |
| } | |
| const MODEL = model || 'es_MX-ald-medium.onnx'; | |
| const SPEAKER = speaker_id !== undefined ? speaker_id : 0; | |
| const LENGTH_SCALE = length_scale || 1.0; | |
| const NOISE_SCALE = noise_scale || 0.667; | |
| const NOISE_W = noise_w || 0.8; | |
| const SENTENCE_SILENCE = sentence_silence !== undefined ? sentence_silence : 0.2; | |
| console.log('🎤 Configuración completa:'); | |
| console.log(' Modelo:', MODEL); | |
| console.log(' Velocidad (length_scale):', LENGTH_SCALE); | |
| console.log(' Expresividad (noise_scale):', NOISE_SCALE); | |
| console.log(' Variación (noise_w):', NOISE_W); | |
| console.log(' Pausa entre oraciones (sentence_silence):', SENTENCE_SILENCE, 'seg'); | |
| const textoOriginal = text; | |
| text = normalizarTexto(text); | |
| console.log('📝 Texto normalizado:', text); | |
| const timestamp = Date.now(); | |
| const inputFile = path.join('/tmp', `temp_${timestamp}.txt`); // 🐧 /tmp en Linux | |
| const outputFile = path.join('/tmp', `audio_${timestamp}.wav`); | |
| fs.writeFileSync(inputFile, text, 'utf8'); | |
| // 🐧 COMANDO PARA LINUX (sin "cd /d" y usando rutas Unix) | |
| const modelPath = path.join(PIPER_PATH, MODEL); | |
| const comando = `${PIPER_BINARY} --model "${modelPath}" --speaker ${SPEAKER} --length_scale ${LENGTH_SCALE} --noise_scale ${NOISE_SCALE} --noise_w ${NOISE_W} --sentence-silence ${SENTENCE_SILENCE} --output_file "${outputFile}" < "${inputFile}"`; | |
| console.log('🔧 Ejecutando comando:', comando); | |
| exec(comando, (error, stdout, stderr) => { | |
| // Limpiar archivo temporal de entrada | |
| if (fs.existsSync(inputFile)) { | |
| fs.unlinkSync(inputFile); | |
| } | |
| if (error) { | |
| console.error('❌ Error en Piper:', error); | |
| console.error('stderr:', stderr); | |
| return res.status(500).json({ error: 'Error generando audio', details: stderr }); | |
| } | |
| // Verificar que el archivo de salida existe | |
| if (!fs.existsSync(outputFile)) { | |
| console.error('❌ El archivo de audio no se generó'); | |
| return res.status(500).json({ error: 'El archivo de audio no se generó' }); | |
| } | |
| const audioBuffer = fs.readFileSync(outputFile); | |
| fs.unlinkSync(outputFile); | |
| res.json({ | |
| audio: audioBuffer.toString('base64'), | |
| format: 'wav' | |
| }); | |
| }); | |
| }); | |
| // Endpoint de health check para Hugging Face | |
| app.get('/', (req, res) => { | |
| res.json({ | |
| status: 'ok', | |
| service: 'Piper TTS Server', | |
| version: '1.0.0' | |
| }); | |
| }); | |
| app.get('/health', (req, res) => { | |
| res.json({ status: 'healthy' }); | |
| }); | |
| // 🐧 Puerto dinámico para Hugging Face (usa variable de entorno) | |
| const PORT = process.env.PORT || 7860; // HF usa 7860 por defecto | |
| app.listen(PORT, '0.0.0.0', () => { | |
| console.log(`🎤 Servidor Piper escuchando en http://0.0.0.0:${PORT}`); | |
| console.log(`📁 Ruta de Piper: ${PIPER_PATH}`); | |
| console.log(`🔧 Binario de Piper: ${PIPER_BINARY}`); | |
| }); |