Spaces:
Runtime error

File size: 6,179 Bytes
6ac5755
 
46085c7
2703c9d
 
46085c7
 
 
 
 
 
 
aefb660
46085c7
 
 
 
 
 
aefb660
46085c7
 
 
 
 
 
 
779f136
 
 
 
 
 
 
 
 
46085c7
779f136
46085c7
779f136
aefb660
46085c7
 
 
 
 
 
 
 
 
fd0f6b9
939f16c
fd0f6b9
aefb660
939f16c
aefb660
fd0f6b9
 
 
 
 
 
aefb660
fd0f6b9
 
 
 
 
aefb660
fd0f6b9
 
 
 
 
 
 
aefb660
fd0f6b9
 
 
 
 
 
 
 
 
aefb660
fd0f6b9
 
 
 
 
 
 
 
2703c9d
aefb660
fd0f6b9
 
 
 
aefb660
 
 
fd0f6b9
aefb660
 
 
 
fd0f6b9
 
aefb660
 
 
 
 
 
fd0f6b9
 
aefb660
 
fd0f6b9
 
aefb660
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd0f6b9
 
aefb660
 
 
 
 
 
 
 
 
fd0f6b9
 
46085c7
 
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
"use client";

import { useState } from "react";
import { HugeiconsIcon } from "@hugeicons/react";
import { Clock01Icon, SentIcon } from "@hugeicons/core-free-icons";

export function SubmitTextForm() {
  const [targetUserId, setTargetUserId] = useState("");
  const [text, setText] = useState("");
  const [trackingId, setTrackingId] = useState<string | null>(null);
  const [error, setError] = useState<string | null>(null);
  const [isSubmitting, setSubmitting] = useState(false);
  const [isFallback, setIsFallback] = useState(false);

  async function onSubmit(event: React.FormEvent<HTMLFormElement>) {
    event.preventDefault();
    setSubmitting(true);
    setError(null);
    setTrackingId(null);
    setIsFallback(false);

    try {
      const response = await fetch("/api/analyze/text", {
        method: "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify({ target_user_id: targetUserId, text })
      });
      const raw = await response.text();
      let body: Record<string, unknown> = {};
      if (raw) {
        try {
          body = JSON.parse(raw) as Record<string, unknown>;
        } catch {
          body = { error: raw };
        }
      }
      if (!response.ok) {
        throw new Error(typeof body.error === "string" ? body.error : "Submission failed");
      }
      setTrackingId(typeof body.tracking_id === "string" ? body.tracking_id : null);
      setIsFallback(body.fallback === true);
      setText("");
    } catch (err) {
      setError(err instanceof Error ? err.message : "Submission failed");
    } finally {
      setSubmitting(false);
    }
  }

  return (
    <div className="grid grid-cols-1 gap-6 lg:grid-cols-2">
      <form onSubmit={onSubmit} className="ui-card flex min-h-0 flex-col sm:min-h-[640px]">
        <div>
          <h2 className="ui-section-title">Message Risk Scanner</h2>
          <p className="mt-3 max-w-md text-sm leading-6 text-muted sm:text-base sm:leading-7">
            Provide the sender and recipient identifier, along with the message contents to begin risk profiling.
          </p>
        </div>

        <div className="mt-7 space-y-5">
          <div>
            <label htmlFor="target_user_id" className="ui-label">
              Recipient user identifier (Email or ID)
            </label>
            <input
              id="target_user_id"
              value={targetUserId}
              onChange={(event) => setTargetUserId(event.target.value)}
              placeholder="recipient@example.com"
              className="ui-input"
              required
            />
          </div>
          <div>
            <div className="flex items-center justify-between gap-3">
              <label htmlFor="text" className="ui-label mb-0">
                Message contents
              </label>
              <span className="text-sm font-medium text-muted">{text.length}/5000</span>
            </div>
            <textarea
              id="text"
              value={text}
              onChange={(event) => setText(event.target.value)}
              rows={12}
              maxLength={5000}
              placeholder="Paste the message content you wish to analyze..."
              className="ui-input mt-2 resize-none"
              required
            />
          </div>
        </div>

        <div className="mt-auto pt-6">
          <button type="submit" disabled={isSubmitting} className="ui-primary-button">
            <HugeiconsIcon icon={SentIcon} size={18} strokeWidth={1.9} aria-hidden />
            {isSubmitting ? "Scanning message..." : "Scan message safety"}
          </button>
        </div>
      </form>

      <section className="ui-card flex flex-col justify-between">
        <div>
          <h2 className="ui-section-title">Results and conclusion</h2>

          <div className="mt-5 space-y-5">
            <div className="ui-card-subtle text-center">
              <p className="text-sm font-bold text-ink">Submission Status</p>
              <p className="ui-metric mt-8">{trackingId ? "Received (202)" : "--"}</p>
            </div>

            <div className="ui-card-subtle text-center">
              <p className="text-sm font-bold text-ink">Scan Progress</p>
              <div className="mt-8 flex items-center justify-center gap-2 text-3xl font-bold leading-none text-ink sm:gap-3 sm:text-5xl">
                <HugeiconsIcon icon={Clock01Icon} size={32} strokeWidth={1.9} aria-hidden />
                {trackingId ? "Processing Scan" : "System Idle"}
              </div>
            </div>

            <div className={`ui-alert-block ${error ? "bg-danger" : trackingId ? "bg-success" : "bg-brand"}`}>
              {error ? "Scan Unsuccessful" : trackingId ? "Risk Scored & Logged" : "System Idle (Awaiting Input)"}
            </div>

            {trackingId ? (
              <div className="rounded-xl border border-line bg-white px-4 py-3 text-center text-sm font-medium text-muted">
                Scan Incident ID: <span className="font-bold text-ink">{trackingId}</span>
              </div>
            ) : null}

            {isFallback ? (
              <div className="rounded-xl border border-amber-200 bg-amber-50 p-3 text-center text-xs text-amber-800 font-medium leading-relaxed">
                ⚠️ System operating in Fallback Mode. The incident has been temporarily logged to the local server memory.
              </div>
            ) : null}

            {error ? (
              <div className="rounded-xl bg-danger px-4 py-3 text-center text-base font-bold text-white">
                {error}
              </div>
            ) : null}
          </div>
        </div>

        <div className="mt-8 border-t border-line pt-6">
          <h3 className="text-xl font-normal text-ink sm:text-2xl" style={{ letterSpacing: "-0.02em" }}>
            Risk Assessment Details
          </h3>
          <p className="mt-4 text-sm leading-6 text-muted sm:text-base sm:leading-7">
            Our system evaluates tone, aggression, targeted insults, and repeated patterns to calculate the overall message severity profile.
          </p>
        </div>
      </section>
    </div>
  );
}