Decision-Update Abschnitt 12 (E15, S24): Sichtung auf offensichtliche Probleme, keine Rechteprüfung, öffentlich weiterhin „automatisierte Prüfung". - ImagePrecheckManager mit openai- und deterministic-Treiber (Muster der Text-Prüfstrecke), Bild auf 1024 px, Metadaten gestrippt, JPEG als Data-URI, Antwortschema 12.4; Fehler → Ausgang `error`, durchlassen mit Vermerk. - Auswertung 12.3: reject (Stock-Wasserzeichen, Nacktheit/Gewalt, Dokument ab 0,85), queue (Screenshot, fremdes Logo, Kennzeichen ab 0,6 sowie Ablehnungskategorien 0,6–0,85), hint (Personen, „wirkt KI" nur bei „ohne KI", technischer Mangel). Stufe 1 wertet nur das Wasserzeichen; die KI setzt nie die Kennzeichnung (E9). Herabstufung per Config (12.8). - Uploader: Check vor jedem Schreiben, Ablehnung speichert nichts und landet als ki_audits `image_precheck` an der PM; Hash-Bindung zwischen Vorprüfung im Formular und Speichern; Spalten precheck*, Snapshot `precheck_snapshot` im Bestätigungsprotokoll, Archiv-Payload erweitert. - Queue: Bild zurückgehalten (withheld_at), Meldung läuft weiter; Admin entscheidet mit Vermerk (Freigabe/Ablehnung → ki_audits `image_precheck_review`); Ansicht „Bild-Sichtung" in der PM-Liste, Sichtungsblock auf der PM-Seite, Systemstatus-Zähler. - Formular v2: Ablehnungsgrund unter dem Dateifeld, Hinweise nach der Dateiauswahl, „wird kurz gesichtet" an der Karte; API 422 mit Grund, Warnung `image_under_review`. - Config images.precheck (IMAGE_PRECHECK aus, STAGE 1); 17 Tests. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
200 lines
7.3 KiB
PHP
200 lines
7.3 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Image\Precheck;
|
|
|
|
use App\Enums\AiImageDeclaration;
|
|
use App\Enums\ImagePrecheckCategory;
|
|
use App\Enums\ImagePrecheckOutcome;
|
|
use App\Models\KiAudit;
|
|
use App\Models\PressRelease;
|
|
use App\Models\PressReleaseImage;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Throwable;
|
|
|
|
/**
|
|
* KI-Vorab-Check für Bilder (Bildkennzeichnung Block K, Decision-Update
|
|
* Abschnitt 12). Drei Regeln (12.2):
|
|
*
|
|
* 1. Jeder Befund hat einen definierten Ausgang (reject, queue, hint, pass);
|
|
* ein Fehler des Anbieters lässt durch und wird als `error` vermerkt.
|
|
* 2. Öffentlich heißt es weiterhin „automatisierte Prüfung".
|
|
* 3. Der Check setzt nie die KI-Kennzeichnung — `looks_ai_generated` ist
|
|
* höchstens ein Hinweis.
|
|
*/
|
|
class ImagePrecheckService
|
|
{
|
|
public function __construct(private readonly ImagePrecheckManager $manager) {}
|
|
|
|
public function enabled(): bool
|
|
{
|
|
return (bool) config('images.precheck.enabled', false);
|
|
}
|
|
|
|
public function stage(): int
|
|
{
|
|
return max(1, (int) config('images.precheck.stage', 1));
|
|
}
|
|
|
|
/**
|
|
* Sichtung einer Datei. Liefert null, wenn der Check aus ist. Jeder
|
|
* Fehler des Treibers wird zum `error`-Ergebnis (DU 12.3: durchlassen,
|
|
* Vermerk im Protokoll) und ins `ki`-Log geschrieben.
|
|
*/
|
|
public function run(string $absolutePath, string $fileSha256): ?ImagePrecheckResult
|
|
{
|
|
if (! $this->enabled()) {
|
|
return null;
|
|
}
|
|
|
|
$provider = (string) config('images.precheck.provider', 'deterministic');
|
|
|
|
try {
|
|
return $this->manager->driver($provider)->check($absolutePath)->withFileSha256($fileSha256);
|
|
} catch (Throwable $exception) {
|
|
Log::channel('ki')->warning('KI-Vorab-Check für ein Bild fehlgeschlagen; das Bild wird durchgelassen.', [
|
|
'provider' => $provider,
|
|
'error' => $exception->getMessage(),
|
|
]);
|
|
|
|
return ImagePrecheckResult::error(
|
|
mb_substr($exception->getMessage(), 0, 200),
|
|
$provider,
|
|
config('images.precheck.model') ?: null,
|
|
$fileSha256,
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Vorhandenes Ergebnis wiederverwenden, wenn es zur Datei passt
|
|
* (Hash-Bindung zwischen Vorprüfung und Speichern), sonst neu rechnen.
|
|
*/
|
|
public function resolve(string $absolutePath, string $fileSha256, ?ImagePrecheckResult $existing): ?ImagePrecheckResult
|
|
{
|
|
if (! $this->enabled()) {
|
|
return null;
|
|
}
|
|
|
|
if ($existing !== null && $existing->fileSha256 !== null && hash_equals($existing->fileSha256, $fileSha256)) {
|
|
return $existing;
|
|
}
|
|
|
|
return $this->run($absolutePath, $fileSha256);
|
|
}
|
|
|
|
/**
|
|
* Ausgang nach DU 12.3. Stufe 1 wertet nur `stock_watermark`; die
|
|
* Kundenangabe entscheidet nur darüber, ob „wirkt KI-generiert" als
|
|
* Hinweis erscheint.
|
|
*/
|
|
public function evaluate(ImagePrecheckResult $result, ?AiImageDeclaration $userChoice): ImagePrecheckEvaluation
|
|
{
|
|
$stage = $this->stage();
|
|
|
|
if ($result->isError()) {
|
|
return new ImagePrecheckEvaluation(ImagePrecheckOutcome::Error, [], $stage);
|
|
}
|
|
|
|
$reject = (float) config('images.precheck.reject_threshold', 0.85);
|
|
$queue = (float) config('images.precheck.queue_threshold', 0.6);
|
|
$downgraded = (array) config('images.precheck.downgrade_to_queue', []);
|
|
|
|
$triggers = [];
|
|
|
|
foreach (ImagePrecheckCategory::cases() as $category) {
|
|
if (! $category->activeInStage($stage)) {
|
|
continue;
|
|
}
|
|
|
|
$confidence = $result->confidence($category);
|
|
$default = $category->defaultOutcome();
|
|
|
|
if ($default === ImagePrecheckOutcome::Reject && in_array($category->value, $downgraded, true)) {
|
|
$default = ImagePrecheckOutcome::Queue;
|
|
}
|
|
|
|
$outcome = match ($default) {
|
|
ImagePrecheckOutcome::Reject => $confidence >= $reject
|
|
? ImagePrecheckOutcome::Reject
|
|
: ($confidence >= $queue ? ImagePrecheckOutcome::Queue : null),
|
|
ImagePrecheckOutcome::Queue => $confidence >= $queue ? ImagePrecheckOutcome::Queue : null,
|
|
ImagePrecheckOutcome::Hint => $this->hintApplies($category, $confidence, $queue, $userChoice) ? ImagePrecheckOutcome::Hint : null,
|
|
default => null,
|
|
};
|
|
|
|
if ($outcome === null) {
|
|
continue;
|
|
}
|
|
|
|
$triggers[] = [
|
|
'category' => $category,
|
|
'confidence' => $confidence,
|
|
'outcome' => $outcome,
|
|
'note' => $category === ImagePrecheckCategory::TechnicalDefect ? $result->note($category) : null,
|
|
];
|
|
}
|
|
|
|
$overall = ImagePrecheckOutcome::Pass;
|
|
|
|
foreach ($triggers as $trigger) {
|
|
if ($trigger['outcome']->rank() > $overall->rank()) {
|
|
$overall = $trigger['outcome'];
|
|
}
|
|
}
|
|
|
|
return new ImagePrecheckEvaluation($overall, $triggers, $stage);
|
|
}
|
|
|
|
/**
|
|
* Ablehnung vor dem Speichern (DU 12.3): kein Bild, aber ein Eintrag im
|
|
* unveränderlichen KI-Protokoll an der Meldung — der Beleg eigener
|
|
* Sorgfalt, ohne die Datei.
|
|
*/
|
|
public function auditRejected(PressRelease $pressRelease, ImagePrecheckResult $result, ImagePrecheckEvaluation $evaluation): KiAudit
|
|
{
|
|
return $this->audit($pressRelease->getKey(), null, $result, $evaluation);
|
|
}
|
|
|
|
public function auditStored(PressReleaseImage $image, ImagePrecheckResult $result, ImagePrecheckEvaluation $evaluation): KiAudit
|
|
{
|
|
return $this->audit((int) $image->press_release_id, (int) $image->getKey(), $result, $evaluation);
|
|
}
|
|
|
|
/**
|
|
* Wert für die Spalte `precheck` am Bild und den Snapshot im
|
|
* Bestätigungsprotokoll: Rohbefund plus Auswertung.
|
|
*
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function storedPayload(ImagePrecheckResult $result, ImagePrecheckEvaluation $evaluation): array
|
|
{
|
|
return $result->toArray() + ['evaluation' => $evaluation->toArray(), 'prompt_version' => $result->raw['prompt_version'] ?? config('images.precheck.prompt_version')];
|
|
}
|
|
|
|
private function hintApplies(ImagePrecheckCategory $category, float $confidence, float $queueThreshold, ?AiImageDeclaration $userChoice): bool
|
|
{
|
|
if ($confidence < $queueThreshold) {
|
|
return false;
|
|
}
|
|
|
|
if ($category === ImagePrecheckCategory::LooksAiGenerated) {
|
|
return $userChoice === null || $userChoice === AiImageDeclaration::Original;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private function audit(int $pressReleaseId, ?int $imageId, ImagePrecheckResult $result, ImagePrecheckEvaluation $evaluation): KiAudit
|
|
{
|
|
return KiAudit::query()->create([
|
|
'press_release_id' => $pressReleaseId,
|
|
'type' => KiAudit::TYPE_IMAGE_PRECHECK,
|
|
'provider' => $result->provider,
|
|
'model' => $result->model,
|
|
'result' => $evaluation->outcome->value,
|
|
'reason' => $evaluation->summary(),
|
|
'raw_response' => $this->storedPayload($result, $evaluation) + ['image_id' => $imageId],
|
|
'created_at' => now(),
|
|
]);
|
|
}
|
|
}
|