Spaces:
Sleeping
Sleeping
Merge pull request #10 from Tobkubos/backend-setup
Browse files
backend/app/config_manager.py
CHANGED
|
@@ -39,6 +39,16 @@ def get_active_text_model(guild_id: str) -> Optional[str]:
|
|
| 39 |
guild_config = configs.get(guild_id, {})
|
| 40 |
model = guild_config.get("active_text_model", "none")
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
if not model or model.lower() == "none":
|
| 43 |
return None
|
| 44 |
return model
|
|
|
|
| 39 |
guild_config = configs.get(guild_id, {})
|
| 40 |
model = guild_config.get("active_text_model", "none")
|
| 41 |
|
| 42 |
+
if not model or model.lower() == "none":
|
| 43 |
+
return None
|
| 44 |
+
return model
|
| 45 |
+
|
| 46 |
+
def get_active_image_model(guild_id: str) -> Optional[str]:
|
| 47 |
+
"""Zwraca aktywny model obrazu dla serwera. Je艣li brak konfiguracji, zwraca None."""
|
| 48 |
+
configs = _load_all_configs()
|
| 49 |
+
guild_config = configs.get(guild_id, {})
|
| 50 |
+
model = guild_config.get("active_image_model", "none")
|
| 51 |
+
|
| 52 |
if not model or model.lower() == "none":
|
| 53 |
return None
|
| 54 |
return model
|
backend/app/services/image_analyzer.py
CHANGED
|
@@ -1,30 +1,60 @@
|
|
| 1 |
import io
|
| 2 |
import logging
|
| 3 |
import time
|
|
|
|
| 4 |
from typing import Dict, Any
|
| 5 |
from PIL import Image
|
| 6 |
from transformers import pipeline
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
logger = logging.getLogger(__name__)
|
| 9 |
|
|
|
|
|
|
|
| 10 |
_image_classifier = None
|
| 11 |
|
| 12 |
-
def _load_model():
|
| 13 |
-
global _image_classifier
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
return _image_classifier
|
| 23 |
|
| 24 |
-
async def analyze_image(image_bytes: bytes) -> Dict[str, Any]:
|
| 25 |
start_time = time.time()
|
| 26 |
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
try:
|
| 30 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
|
@@ -32,14 +62,15 @@ async def analyze_image(image_bytes: bytes) -> Dict[str, Any]:
|
|
| 32 |
logger.error(f"Failed to parse image bytes: {str(e)}")
|
| 33 |
raise ValueError("Invalid image format or corrupted bytes") from e
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
result = classifier(image)
|
| 38 |
|
| 39 |
label = result[0]["label"]
|
| 40 |
score = result[0]["score"]
|
| 41 |
|
| 42 |
-
|
|
|
|
| 43 |
confidence = score
|
| 44 |
|
| 45 |
analysis_time = time.time() - start_time
|
|
@@ -48,6 +79,7 @@ async def analyze_image(image_bytes: bytes) -> Dict[str, Any]:
|
|
| 48 |
"is_deepfake": is_deepfake,
|
| 49 |
"confidence": round(confidence, 3),
|
| 50 |
"analysis_time": round(analysis_time, 3),
|
|
|
|
| 51 |
}
|
| 52 |
|
| 53 |
logger.info(f"Image analysis completed. Result: {response}")
|
|
|
|
| 1 |
import io
|
| 2 |
import logging
|
| 3 |
import time
|
| 4 |
+
import gc
|
| 5 |
from typing import Dict, Any
|
| 6 |
from PIL import Image
|
| 7 |
from transformers import pipeline
|
| 8 |
|
| 9 |
+
# Importujemy helper do konfiguracji oraz wyj膮tek braku konfiguracji
|
| 10 |
+
from app.config_manager import get_active_image_model
|
| 11 |
+
from app.utils.exceptions import SetupRequiredError
|
| 12 |
+
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
+
# Przechowujemy referencje do aktualnie za艂adowanego modelu obraz贸w
|
| 16 |
+
_loaded_model_name = None
|
| 17 |
_image_classifier = None
|
| 18 |
|
| 19 |
+
def _load_model(target_model_name: str):
|
| 20 |
+
global _image_classifier, _loaded_model_name
|
| 21 |
+
|
| 22 |
+
# Je艣li model o tej nazwie jest ju偶 za艂adowany w pami臋ci, u偶ywamy go ponownie
|
| 23 |
+
if _image_classifier is not None and _loaded_model_name == target_model_name:
|
| 24 |
+
return _image_classifier
|
| 25 |
+
|
| 26 |
+
logger.info(f"Wymagana zmiana modelu obrazu. Obecny w RAM: {_loaded_model_name}, Nowy: {target_model_name}")
|
| 27 |
+
|
| 28 |
+
# Czyszczenie pami臋ci po poprzednim modelu obraz贸w
|
| 29 |
+
_image_classifier = None
|
| 30 |
+
gc.collect()
|
| 31 |
+
|
| 32 |
+
logger.info(f"艁adowanie modelu image detector: {target_model_name}...")
|
| 33 |
+
_image_classifier = pipeline(
|
| 34 |
+
"image-classification",
|
| 35 |
+
model=target_model_name,
|
| 36 |
+
device=-1 # -1 oznacza CPU
|
| 37 |
+
)
|
| 38 |
+
_loaded_model_name = target_model_name
|
| 39 |
+
logger.info(f"Model {target_model_name} zosta艂 pomy艣lnie za艂adowany.")
|
| 40 |
+
|
| 41 |
return _image_classifier
|
| 42 |
|
| 43 |
+
async def analyze_image(image_bytes: bytes, guild_id: str) -> Dict[str, Any]:
|
| 44 |
start_time = time.time()
|
| 45 |
|
| 46 |
+
# 1. Sprawdzamy konfiguracj臋 modelu dla danego serwera Discord
|
| 47 |
+
active_model = get_active_image_model(guild_id)
|
| 48 |
+
|
| 49 |
+
# BLOKADA: Je偶eli model to 'none' lub brak konfiguracji, natychmiast przerywamy i zg艂aszamy b艂膮d
|
| 50 |
+
if not active_model:
|
| 51 |
+
logger.warning(f"Zablokowano zapytanie! Serwer {guild_id} nie ma skonfigurowanego modelu dla obraz贸w.")
|
| 52 |
+
raise SetupRequiredError(
|
| 53 |
+
f"Serwer o ID '{guild_id}' nie zosta艂 jeszcze skonfigurowany pod k膮tem analizy obraz贸w. "
|
| 54 |
+
"U偶yj komendy setup na Discordzie przed wykonaniem analizy."
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
logger.info(f"Starting image analysis for guild: {guild_id}, model: {active_model}, size: {len(image_bytes)} bytes")
|
| 58 |
|
| 59 |
try:
|
| 60 |
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
|
|
|
| 62 |
logger.error(f"Failed to parse image bytes: {str(e)}")
|
| 63 |
raise ValueError("Invalid image format or corrupted bytes") from e
|
| 64 |
|
| 65 |
+
# 2. Dynamicznie pobieramy/艂adujemy wskazany model
|
| 66 |
+
classifier = _load_model(active_model)
|
| 67 |
result = classifier(image)
|
| 68 |
|
| 69 |
label = result[0]["label"]
|
| 70 |
score = result[0]["score"]
|
| 71 |
|
| 72 |
+
# Dostosowanie do najcz臋stszych etykiet fa艂szywych obraz贸w (np. "fake", "ai", "synthetic")
|
| 73 |
+
is_deepfake = label.lower() in ["fake", "ai", "synthetic", "label_1"]
|
| 74 |
confidence = score
|
| 75 |
|
| 76 |
analysis_time = time.time() - start_time
|
|
|
|
| 79 |
"is_deepfake": is_deepfake,
|
| 80 |
"confidence": round(confidence, 3),
|
| 81 |
"analysis_time": round(analysis_time, 3),
|
| 82 |
+
"used_model": active_model,
|
| 83 |
}
|
| 84 |
|
| 85 |
logger.info(f"Image analysis completed. Result: {response}")
|