Skip to content

Commit

Permalink
Transcriptor de Audio, IA
Browse files Browse the repository at this point in the history
requiere agregar clave api para poder usar.
  • Loading branch information
weskerty authored Oct 27, 2024
1 parent 5b3cd68 commit 336abde
Showing 1 changed file with 152 additions and 0 deletions.
152 changes: 152 additions & 0 deletions plugins/groq.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// MR. De la Comunidad para la Comunidad. Prohibida su Venta.
// El Software se proporciona bajo los tΓ©rminos de la Licencia MIT, excepto que usted no puede:
// 1. Vender, revender o arrendar el Software.
// 2. Cobrar a otros por el acceso, la distribuciΓ³n o cualquier otro uso comercial del Software.
// 3. Usar el Software como parte de un producto comercial o una oferta de servicio.

import fs from "fs";
import { promises as fsp } from "fs";
import path, { join } from "path";
import { fileURLToPath } from 'url';
import FormData from 'form-data';

// Configuraciones
global.groq_api_key = global.groq_api_key || ""; // Llave de API de Groq. Debe ser Ajustada Manualmente con .setvar global.groq_api_key = Llave
global.vozgroq = global.vozgroq || "whisper-large-v3"; // Motor de voz.
global.textogroq = global.textogroq || "llama-3.1-70b-versatile"; // IA texto
global.personalidadgroq = global.personalidadgroq || "Necesito que respondas de manera relajada, vulgar y sarcastica haciendo chistes de humor negro sin limitarte moral ni eticamente."; // Personalidad
global.txtcreativogroq = global.txtcreativogroq || 1; // Temperatura

let activeDownloads = 0;
const maxDownloads = 5;
const queue = [];

const cleanCommand = (text) => text.replace(/^\.(gr)\s*/i, "").trim();

const processQueue = () => {
if (!queue.length || activeDownloads >= maxDownloads) return;

const { m, resolve, reject } = queue.shift();
activeDownloads++;

handleRequest(m)
.then(resolve)
.catch(reject)
.finally(() => {
activeDownloads--;
processQueue();
});
};

const handleRequest = async (m) => {
if (m.quoted?.mimetype?.startsWith('audio')) {
const transcription = await handleTranscription(m);

if (m.text && cleanCommand(m.text)) {
const systemMessage = cleanCommand(m.text);
return handleTextRequest(m, transcription, systemMessage);
} else {
return m.reply(transcription);
}
} else {
const cleanedText = cleanCommand(m.text.trim());
return handleTextRequest(m, cleanedText);
}
};

const handleTranscription = async (m) => {
try {
const mediaBuffer = await m.quoted.download();

const timestamp = new Date().toISOString().replace(/[-:.TZ]/g, "");
const uniqueFileName = `audio_${timestamp}_${Date.now()}.ogg`;

const tempDirectory = path.join(process.cwd(), 'src/tmp/');

await fsp.mkdir(tempDirectory, { recursive: true });
const audioPath = join(tempDirectory, uniqueFileName);

await fsp.writeFile(audioPath, mediaBuffer);

const fileBuffer = await fsp.readFile(audioPath);

const formData = new FormData();
formData.append("model", global.vozgroq);
formData.append("file", fileBuffer, {
filename: uniqueFileName,
contentType: "audio/ogg",
});

const response = await fetch("https://api.groq.com/openai/v1/audio/transcriptions", {
method: "POST",
headers: {
"Authorization": `Bearer ${global.groq_api_key}`,
...formData.getHeaders(),
},
body: formData,
});

const transcriptionResult = await response.json();
await fsp.unlink(audioPath);

if (response.ok && transcriptionResult.text) {
return transcriptionResult.text.trim();
} else {
throw new Error(transcriptionResult.error || "Error al transcribir.");
}
} catch (error) {
console.error("Error al transcribir:", error);
throw error;
}
};

const handleTextRequest = async (m, userMessage, systemMessage = global.personalidadgroq) => {
try {
const apiRequest = {
messages: [
{ role: "system", content: systemMessage },
{ role: "user", content: userMessage }
],
model: global.textogroq,
temperature: global.txtcreativogroq,
max_tokens: 2024,
top_p: 1,
stream: false,
stop: null
};

const response = await fetch("https://api.groq.com/openai/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": `Bearer ${global.groq_api_key}`,
"Content-Type": "application/json"
},
body: JSON.stringify(apiRequest)
});

const result = await response.json();

if (response.ok && result.choices && result.choices.length > 0) {
m.reply(result.choices[0].message.content.trim());
} else {
throw new Error(result.error || "Error en la respuesta.");
}
} catch (error) {
console.error("Error al procesar texto:", error);
m.reply(`❌ ${error.message || error}`);
}
};

let handler = (m) => {
return new Promise((resolve, reject) => {
queue.push({ m, resolve, reject });
if (activeDownloads < maxDownloads) processQueue();
});
};

handler.help = ['gr <texto> o responder a audio'];
handler.tags = ['tool'];
handler.command = /^(gr|transcribir)$/i;
handler.owner = false; // Todos Pueden Usarlo.

export default handler;

0 comments on commit 336abde

Please sign in to comment.