File size: 5,111 Bytes
f0b240d
46cc63a
f0b240d
035d8b3
f0b240d
 
 
 
035d8b3
f0b240d
 
035d8b3
f0b240d
 
 
 
035d8b3
0f0ce9b
f0b240d
 
 
 
 
 
 
035d8b3
 
 
 
f0b240d
 
 
 
035d8b3
f0b240d
 
 
 
 
 
 
 
 
 
035d8b3
 
f0b240d
 
 
035d8b3
0f0ce9b
f0b240d
 
 
035d8b3
 
46cc63a
 
 
 
f0b240d
 
035d8b3
 
f0b240d
0f0ce9b
 
f0b240d
 
 
 
 
 
 
 
 
035d8b3
 
 
 
 
 
f0b240d
035d8b3
f0b240d
 
 
 
 
 
 
035d8b3
f0b240d
035d8b3
 
 
 
 
f0b240d
 
 
 
 
 
 
 
 
 
0f0ce9b
f0b240d
 
 
 
 
 
 
 
 
 
 
 
035d8b3
f0b240d
 
 
 
 
 
035d8b3
f0b240d
 
 
 
 
 
 
 
035d8b3
 
 
f0b240d
 
 
035d8b3
f0b240d
 
 
 
 
 
 
035d8b3
f0b240d
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { useEffect, useState } from "react";
import { getModelInfo, getModelsStatus, predict, setModel } from "../api/client";
import { useApp } from "../context/AppContext";
import { useI18n } from "../i18n/I18nContext";
import type { ModelStatusEntry } from "../types/api";

export function SettingsPage() {
  const { threshold, setThreshold } = useApp();
  const { t } = useI18n();
  const [modelStatus, setModelStatus] = useState<ModelStatusEntry[]>([]);
  const [active, setActive] = useState("");
  const [testText, setTestText] = useState<string>(() => t.settings.defaultTestText);
  const [testResult, setTestResult] = useState<string | null>(null);
  const [testError, setTestError] = useState<string | null>(null);
  const [testing, setTesting] = useState(false);
  const [message, setMessage] = useState<string | null>(null);
  const [messageIsError, setMessageIsError] = useState(false);
  const [switching, setSwitching] = useState(false);

  const loadStatus = () => {
    getModelsStatus()
      .then((r) => {
        setModelStatus(r.models);
        setActive(r.active);
      })
      .catch(() => {
        setMessage(t.settings.couldNotLoadStatus);
        setMessageIsError(true);
      });
  };

  useEffect(() => {
    loadStatus();
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, []);

  useEffect(() => {
    setTestResult(null);
    setTestError(null);
  }, [testText, threshold]);

  const switchModel = async (name: string) => {
    const entry = modelStatus.find((m) => m.name === name);
    if (entry && !entry.available) {
      setMessage(entry.reason ?? "");
      setMessageIsError(true);
      return;
    }
    setMessage(null);
    setMessageIsError(false);
    setSwitching(true);
    try {
      await setModel(name);
      setActive(name);
      setMessage(t.settings.activeModelMsg(name));
      setMessageIsError(false);
      const info = await getModelInfo();
      if (info.recommended_threshold != null) {
        setThreshold(info.recommended_threshold);
      }
      loadStatus();
    } catch (e) {
      setMessage(e instanceof Error ? e.message : t.settings.failedSwitch);
      setMessageIsError(true);
      loadStatus();
    } finally {
      setSwitching(false);
    }
  };

  const runTest = async () => {
    setTesting(true);
    setTestResult(null);
    setTestError(null);
    try {
      const r = await predict(testText, threshold);
      setTestResult(
        t.settings.testResult(
          r.is_toxic ? t.badges.toxic : t.badges.safe,
          String(Math.round(r.probability * 100))
        )
      );
    } catch (e) {
      setTestError(e instanceof Error ? e.message : t.settings.analysisFailed);
    } finally {
      setTesting(false);
    }
  };

  return (
    <div className="settings-page">
      <h1>{t.settings.title}</h1>
      <section className="settings-card">
        <h2>{t.settings.activeModel}</h2>
        <p className="production-model-note">{t.settings.productionNote("0.805", "2.54")}</p>
        <p className="production-model-note">{t.settings.baselinesNote("0.758", "0.790", "0.16")}</p>
        <p className="hint">{t.settings.installHint}</p>
        {switching && <p className="hint">{t.settings.switching}</p>}
        <div className="model-list">
          {modelStatus.map((m) => (
            <label
              key={m.name}
              className={`model-option ${!m.available ? "model-unavailable" : ""}`}
            >
              <input
                type="radio"
                name="model"
                checked={active === m.name}
                disabled={!m.available || switching}
                onChange={() => void switchModel(m.name)}
              />
              <span>
                {m.name}
                {!m.available && m.reason && (
                  <span className="model-reason"> — {m.reason}</span>
                )}
              </span>
            </label>
          ))}
        </div>
        {message && (
          <p className={messageIsError ? "error-text" : "settings-msg"}>
            {message}
          </p>
        )}
      </section>

      <section className="settings-card">
        <h2>{t.settings.thresholdTitle}</h2>
        <input
          type="range"
          min={0.1}
          max={0.9}
          step={0.05}
          value={threshold}
          onChange={(e) => setThreshold(Number(e.target.value))}
        />
        <p>
          {threshold.toFixed(2)} — {t.settings.thresholdNote}
        </p>
      </section>

      <section className="settings-card">
        <h2>{t.settings.quickTest}</h2>
        <textarea value={testText} onChange={(e) => setTestText(e.target.value)} rows={2} />
        <button
          type="button"
          className="btn-primary"
          disabled={testing || !testText.trim()}
          onClick={() => void runTest()}
        >
          {testing ? t.settings.analyzing : t.settings.analyze}
        </button>
        {testResult && <p className="settings-msg">{testResult}</p>}
        {testError && <p className="error-text">{testError}</p>}
      </section>
    </div>
  );
}