Spaces:
Sleeping
Sleeping
Tobkubos commited on
Commit ·
480a4f9
1
Parent(s): f19cc50
mocks deleted
Browse files- configManager.js +9 -3
- index.js +76 -83
configManager.js
CHANGED
|
@@ -3,10 +3,10 @@ import path from "path";
|
|
| 3 |
|
| 4 |
const filePath = path.resolve("./guildConfigs.json");
|
| 5 |
|
|
|
|
| 6 |
export const DEFAULT_CONFIG = {
|
| 7 |
logChannelId: null,
|
| 8 |
-
|
| 9 |
-
imageModel: "capcheck/ai-image-detection"
|
| 10 |
};
|
| 11 |
|
| 12 |
export function loadConfig(guildId) {
|
|
@@ -15,7 +15,13 @@ export function loadConfig(guildId) {
|
|
| 15 |
}
|
| 16 |
try {
|
| 17 |
const data = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
} catch (err) {
|
| 20 |
console.error("Błąd podczas odczytu konfiguracji:", err);
|
| 21 |
return { ...DEFAULT_CONFIG };
|
|
|
|
| 3 |
|
| 4 |
const filePath = path.resolve("./guildConfigs.json");
|
| 5 |
|
| 6 |
+
// Domyślne ustawienia (teraz modele są zapisywane dynamicznie w obiekcie)
|
| 7 |
export const DEFAULT_CONFIG = {
|
| 8 |
logChannelId: null,
|
| 9 |
+
models: {}
|
|
|
|
| 10 |
};
|
| 11 |
|
| 12 |
export function loadConfig(guildId) {
|
|
|
|
| 15 |
}
|
| 16 |
try {
|
| 17 |
const data = JSON.parse(fs.readFileSync(filePath, "utf-8"));
|
| 18 |
+
const config = data[guildId] || { ...DEFAULT_CONFIG };
|
| 19 |
+
|
| 20 |
+
// Upewniamy się, że obiekt "models" zawsze istnieje
|
| 21 |
+
if (!config.models) {
|
| 22 |
+
config.models = {};
|
| 23 |
+
}
|
| 24 |
+
return config;
|
| 25 |
} catch (err) {
|
| 26 |
console.error("Błąd podczas odczytu konfiguracji:", err);
|
| 27 |
return { ...DEFAULT_CONFIG };
|
index.js
CHANGED
|
@@ -32,13 +32,6 @@ const client = new Client({
|
|
| 32 |
|
| 33 |
const API_URL = process.env.API_URL || "http://127.0.0.1:8000";
|
| 34 |
|
| 35 |
-
// Domyślne modele zapasowe (używane gdyby backend był wyłączony podczas konfiguracji)
|
| 36 |
-
const FALLBACK_MODELS = {
|
| 37 |
-
text: ["yaya36095/xlm-roberta-text-detector", "mock"],
|
| 38 |
-
image: ["capcheck/ai-image-detection", "mock"]
|
| 39 |
-
};
|
| 40 |
-
|
| 41 |
-
// Pamięć podręczna przechowuje konfigurację oraz pobrane dynamicznie modele
|
| 42 |
const activeSetupSessions = new Map();
|
| 43 |
|
| 44 |
client.once(Events.ClientReady, async () => {
|
|
@@ -68,27 +61,20 @@ client.once(Events.ClientReady, async () => {
|
|
| 68 |
}
|
| 69 |
});
|
| 70 |
|
| 71 |
-
//
|
| 72 |
async function fetchAvailableModels() {
|
| 73 |
try {
|
| 74 |
const response = await fetch(API_URL);
|
| 75 |
if (response.ok) {
|
| 76 |
const data = await response.json();
|
| 77 |
if (data.available_models) {
|
| 78 |
-
|
| 79 |
-
const imageModels = data.available_models.image || [];
|
| 80 |
-
|
| 81 |
-
// Upewniamy się, że zawsze mamy opcję testową "mock"
|
| 82 |
-
if (!textModels.includes("mock")) textModels.push("mock");
|
| 83 |
-
if (!imageModels.includes("mock")) imageModels.push("mock");
|
| 84 |
-
|
| 85 |
-
return { text: textModels, image: imageModels };
|
| 86 |
}
|
| 87 |
}
|
| 88 |
} catch (err) {
|
| 89 |
-
console.
|
| 90 |
}
|
| 91 |
-
return
|
| 92 |
}
|
| 93 |
|
| 94 |
function preparePayload(input) {
|
|
@@ -142,60 +128,59 @@ function getProgressBar(confidence, isDeepfake) {
|
|
| 142 |
return blockEmoji.repeat(filledBlocks) + "⬛".repeat(emptyBlocks);
|
| 143 |
}
|
| 144 |
|
| 145 |
-
//
|
| 146 |
function generateSetupView(tempConfig, availableModels) {
|
| 147 |
const embed = new EmbedBuilder()
|
| 148 |
.setColor(0x5865F2)
|
| 149 |
.setTitle("⚙️ Konfiguracja Systemu Detekcji")
|
| 150 |
-
.setDescription("Wybierz kanał do wysyłania logów oraz aktywne modele
|
| 151 |
-
.
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
-
.setTimestamp();
|
| 170 |
|
| 171 |
const channelSelect = new ChannelSelectMenuBuilder()
|
| 172 |
.setCustomId("setup_log_channel")
|
| 173 |
.setPlaceholder("Wybierz kanał dla raportów")
|
| 174 |
.addChannelTypes(ChannelType.GuildText);
|
| 175 |
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
.
|
| 185 |
-
|
| 186 |
-
.
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
.
|
| 198 |
-
|
| 199 |
|
| 200 |
const buttonsRow = new ActionRowBuilder().addComponents(
|
| 201 |
new ButtonBuilder()
|
|
@@ -210,14 +195,11 @@ function generateSetupView(tempConfig, availableModels) {
|
|
| 210 |
.setEmoji("❌")
|
| 211 |
);
|
| 212 |
|
|
|
|
|
|
|
| 213 |
return {
|
| 214 |
embeds: [embed],
|
| 215 |
-
components:
|
| 216 |
-
new ActionRowBuilder().addComponents(channelSelect),
|
| 217 |
-
new ActionRowBuilder().addComponents(textModelSelect),
|
| 218 |
-
new ActionRowBuilder().addComponents(imageModelSelect),
|
| 219 |
-
buttonsRow
|
| 220 |
-
]
|
| 221 |
};
|
| 222 |
}
|
| 223 |
|
|
@@ -243,13 +225,13 @@ async function handleAnalysis(interaction, userContent, targetMessage = null) {
|
|
| 243 |
try {
|
| 244 |
const { type, payload } = preparePayload(userContent);
|
| 245 |
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
payload.model =
|
| 250 |
}
|
| 251 |
|
| 252 |
-
console.log(`Wysyłanie zapytania typu: ${type} do API z modelem: ${payload.model}...`);
|
| 253 |
|
| 254 |
const response = await fetch(`${API_URL}/analyze`, {
|
| 255 |
method: "POST",
|
|
@@ -355,18 +337,30 @@ client.on(Events.InteractionCreate, async (interaction) => {
|
|
| 355 |
await interaction.showModal(modal);
|
| 356 |
}
|
| 357 |
|
| 358 |
-
// ZMIANA: Pobieranie modeli z API na żywo przed pokazaniem setupu
|
| 359 |
if (interaction.commandName === "setup") {
|
| 360 |
const guildId = interaction.guildId;
|
| 361 |
const currentConfig = loadConfig(guildId);
|
| 362 |
|
| 363 |
-
// Informujemy Discord, że pobieramy konfigurację z API
|
| 364 |
await interaction.deferReply({ flags: [MessageFlags.Ephemeral] });
|
| 365 |
|
| 366 |
// Pobieramy aktywne modele bezpośrednio z FastAPI
|
| 367 |
const availableModels = await fetchAvailableModels();
|
| 368 |
|
| 369 |
-
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
activeSetupSessions.set(guildId, {
|
| 371 |
config: { ...currentConfig },
|
| 372 |
availableModels
|
|
@@ -377,7 +371,6 @@ client.on(Events.InteractionCreate, async (interaction) => {
|
|
| 377 |
}
|
| 378 |
}
|
| 379 |
|
| 380 |
-
// OBSŁUGA ZMIANY KANAŁU LOGÓW
|
| 381 |
if (interaction.isChannelSelectMenu()) {
|
| 382 |
if (interaction.customId === "setup_log_channel") {
|
| 383 |
const guildId = interaction.guildId;
|
|
@@ -389,18 +382,18 @@ client.on(Events.InteractionCreate, async (interaction) => {
|
|
| 389 |
}
|
| 390 |
}
|
| 391 |
|
| 392 |
-
// OBSŁUGA
|
| 393 |
if (interaction.isStringSelectMenu()) {
|
| 394 |
const guildId = interaction.guildId;
|
| 395 |
const tempSession = activeSetupSessions.get(guildId);
|
| 396 |
|
| 397 |
if (tempSession) {
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
tempSession.config.
|
|
|
|
| 402 |
}
|
| 403 |
-
await interaction.update(generateSetupView(tempSession.config, tempSession.availableModels));
|
| 404 |
}
|
| 405 |
}
|
| 406 |
|
|
|
|
| 32 |
|
| 33 |
const API_URL = process.env.API_URL || "http://127.0.0.1:8000";
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
const activeSetupSessions = new Map();
|
| 36 |
|
| 37 |
client.once(Events.ClientReady, async () => {
|
|
|
|
| 61 |
}
|
| 62 |
});
|
| 63 |
|
| 64 |
+
// Pobieranie modeli bezpośrednio z FastAPI
|
| 65 |
async function fetchAvailableModels() {
|
| 66 |
try {
|
| 67 |
const response = await fetch(API_URL);
|
| 68 |
if (response.ok) {
|
| 69 |
const data = await response.json();
|
| 70 |
if (data.available_models) {
|
| 71 |
+
return data.available_models;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
}
|
| 74 |
} catch (err) {
|
| 75 |
+
console.error("Błąd połączenia z FastAPI:", err.message);
|
| 76 |
}
|
| 77 |
+
return null;
|
| 78 |
}
|
| 79 |
|
| 80 |
function preparePayload(input) {
|
|
|
|
| 128 |
return blockEmoji.repeat(filledBlocks) + "⬛".repeat(emptyBlocks);
|
| 129 |
}
|
| 130 |
|
| 131 |
+
// CAŁKOWICIE DYNAMICZNY GENERATOR WIDOKU SETUPU
|
| 132 |
function generateSetupView(tempConfig, availableModels) {
|
| 133 |
const embed = new EmbedBuilder()
|
| 134 |
.setColor(0x5865F2)
|
| 135 |
.setTitle("⚙️ Konfiguracja Systemu Detekcji")
|
| 136 |
+
.setDescription("Wybierz kanał do wysyłania logów oraz aktywne modele dla poszczególnych formatów danych.")
|
| 137 |
+
.setTimestamp()
|
| 138 |
+
.setFooter({ text: "Wybierz opcje i kliknij Zapisz ustawienia" });
|
| 139 |
+
|
| 140 |
+
embed.addFields({
|
| 141 |
+
name: "📂 Kanał logów (Raporty)",
|
| 142 |
+
value: tempConfig.logChannelId ? `<#${tempConfig.logChannelId}>` : "*Wysyłanie tylko do konsoli*",
|
| 143 |
+
inline: false
|
| 144 |
+
});
|
| 145 |
+
|
| 146 |
+
// Dynamicznie dodajemy pola dla każdego formatu zwróconego przez FastAPI
|
| 147 |
+
for (const [contentType, models] of Object.entries(availableModels)) {
|
| 148 |
+
const currentSelected = tempConfig.models[contentType] || models[0] || "Brak";
|
| 149 |
+
embed.addFields({
|
| 150 |
+
name: `⚙️ Model dla formatu: ${contentType.toUpperCase()}`,
|
| 151 |
+
value: `\`${currentSelected}\``,
|
| 152 |
+
inline: true
|
| 153 |
+
});
|
| 154 |
+
}
|
|
|
|
| 155 |
|
| 156 |
const channelSelect = new ChannelSelectMenuBuilder()
|
| 157 |
.setCustomId("setup_log_channel")
|
| 158 |
.setPlaceholder("Wybierz kanał dla raportów")
|
| 159 |
.addChannelTypes(ChannelType.GuildText);
|
| 160 |
|
| 161 |
+
const components = [
|
| 162 |
+
new ActionRowBuilder().addComponents(channelSelect)
|
| 163 |
+
];
|
| 164 |
+
|
| 165 |
+
// Dynamicznie generujemy menu rozwijane dla każdego formatu danych (tekst, obraz, wideo itp.)
|
| 166 |
+
for (const [contentType, models] of Object.entries(availableModels)) {
|
| 167 |
+
if (components.length >= 4) break; // Limit Discorda (max 5 rzędów komponentów na wiadomość)
|
| 168 |
+
|
| 169 |
+
const currentSelected = tempConfig.models[contentType] || models[0];
|
| 170 |
+
|
| 171 |
+
const selectOptions = models.map(model => ({
|
| 172 |
+
label: model,
|
| 173 |
+
value: model,
|
| 174 |
+
default: currentSelected === model
|
| 175 |
+
}));
|
| 176 |
+
|
| 177 |
+
const modelSelect = new StringSelectMenuBuilder()
|
| 178 |
+
.setCustomId(`setup_model_${contentType}`)
|
| 179 |
+
.setPlaceholder(`Wybierz model dla ${contentType}`)
|
| 180 |
+
.addOptions(selectOptions);
|
| 181 |
+
|
| 182 |
+
components.push(new ActionRowBuilder().addComponents(modelSelect));
|
| 183 |
+
}
|
| 184 |
|
| 185 |
const buttonsRow = new ActionRowBuilder().addComponents(
|
| 186 |
new ButtonBuilder()
|
|
|
|
| 195 |
.setEmoji("❌")
|
| 196 |
);
|
| 197 |
|
| 198 |
+
components.push(buttonsRow);
|
| 199 |
+
|
| 200 |
return {
|
| 201 |
embeds: [embed],
|
| 202 |
+
components: components
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
};
|
| 204 |
}
|
| 205 |
|
|
|
|
| 225 |
try {
|
| 226 |
const { type, payload } = preparePayload(userContent);
|
| 227 |
|
| 228 |
+
// DYNAMICZNE POBIERANIE MODELU Z PLIKU KONFIGURACYJNEGO DLA DANEGO FORMATU (np. text, image, video)
|
| 229 |
+
const chosenModel = serverConfig.models[type];
|
| 230 |
+
if (chosenModel) {
|
| 231 |
+
payload.model = chosenModel;
|
| 232 |
}
|
| 233 |
|
| 234 |
+
console.log(`Wysyłanie zapytania typu: ${type} do API z modelem: ${payload.model || "domyślny"}...`);
|
| 235 |
|
| 236 |
const response = await fetch(`${API_URL}/analyze`, {
|
| 237 |
method: "POST",
|
|
|
|
| 337 |
await interaction.showModal(modal);
|
| 338 |
}
|
| 339 |
|
|
|
|
| 340 |
if (interaction.commandName === "setup") {
|
| 341 |
const guildId = interaction.guildId;
|
| 342 |
const currentConfig = loadConfig(guildId);
|
| 343 |
|
|
|
|
| 344 |
await interaction.deferReply({ flags: [MessageFlags.Ephemeral] });
|
| 345 |
|
| 346 |
// Pobieramy aktywne modele bezpośrednio z FastAPI
|
| 347 |
const availableModels = await fetchAvailableModels();
|
| 348 |
|
| 349 |
+
// Jeśli backend nie działa, natychmiast przerywamy i wyświetlamy błąd
|
| 350 |
+
if (!availableModels || Object.keys(availableModels).length === 0) {
|
| 351 |
+
return interaction.editReply({
|
| 352 |
+
content: "❌ **Błąd konfiguracji:** Nie udało się nawiązać połączenia z backendem (FastAPI). Uruchom swój backend w Pythonie i spróbuj ponownie!"
|
| 353 |
+
});
|
| 354 |
+
}
|
| 355 |
+
|
| 356 |
+
// Inicjalizujemy domyślne modele w konfiguracji, jeśli nie były wcześniej ustawione
|
| 357 |
+
for (const [contentType, models] of Object.entries(availableModels)) {
|
| 358 |
+
if (!currentConfig.models[contentType] && models.length > 0) {
|
| 359 |
+
currentConfig.models[contentType] = models[0];
|
| 360 |
+
}
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
// Zapisujemy sesję z konfiguracją oraz pobranymi modelami
|
| 364 |
activeSetupSessions.set(guildId, {
|
| 365 |
config: { ...currentConfig },
|
| 366 |
availableModels
|
|
|
|
| 371 |
}
|
| 372 |
}
|
| 373 |
|
|
|
|
| 374 |
if (interaction.isChannelSelectMenu()) {
|
| 375 |
if (interaction.customId === "setup_log_channel") {
|
| 376 |
const guildId = interaction.guildId;
|
|
|
|
| 382 |
}
|
| 383 |
}
|
| 384 |
|
| 385 |
+
// OBSŁUGA DYNAMICZNYCH MENU ROZWIJANYCH DLA MODELI
|
| 386 |
if (interaction.isStringSelectMenu()) {
|
| 387 |
const guildId = interaction.guildId;
|
| 388 |
const tempSession = activeSetupSessions.get(guildId);
|
| 389 |
|
| 390 |
if (tempSession) {
|
| 391 |
+
// Sprawdzamy czy zmieniany jest model (szukamy przedrostka setup_model_)
|
| 392 |
+
if (interaction.customId.startsWith("setup_model_")) {
|
| 393 |
+
const contentType = interaction.customId.replace("setup_model_", "");
|
| 394 |
+
tempSession.config.models[contentType] = interaction.values[0];
|
| 395 |
+
await interaction.update(generateSetupView(tempSession.config, tempSession.availableModels));
|
| 396 |
}
|
|
|
|
| 397 |
}
|
| 398 |
}
|
| 399 |
|