File size: 9,574 Bytes
f6e574f
25bda12
 
f6e574f
 
 
 
25bda12
f6e574f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25bda12
 
f6e574f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25bda12
f6e574f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25bda12
f6e574f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25bda12
 
 
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import React, { useState } from "react";

export default function App() {
  const [file, setFile] = useState(null);
  const [loading, setLoading] = useState(false);
  const [result, setResult] = useState(null);
  const [error, setError] = useState(null);

  const handleFileChange = (e) => {
    const selectedFile = e.target.files[0];
    if (selectedFile) {
      if (selectedFile.type !== "application/pdf") {
        setError("Please select a PDF file");
        setFile(null);
        return;
      }
      setFile(selectedFile);
      setError(null);
      setResult(null);
    }
  };

  const handleClassify = async () => {
    if (!file) {
      setError("Please select a PDF file first");
      return;
    }

    setLoading(true);
    setError(null);
    setResult(null);

    const formData = new FormData();
    formData.append("file", file);

    try {
      const response = await fetch("/api/classify", {
        method: "POST",
        body: formData,
      });

      if (!response.ok) {
        const errorData = await response.json();
        throw new Error(errorData.detail || "Classification failed");
      }

      const data = await response.json();
      setResult(data);
    } catch (err) {
      setError(err.message || "An error occurred during classification");
    } finally {
      setLoading(false);
    }
  };

  const handleReset = () => {
    setFile(null);
    setResult(null);
    setError(null);
    // Reset file input
    const fileInput = document.getElementById("pdf-upload");
    if (fileInput) fileInput.value = "";
  };

  return (
    <div
      style={{
        fontFamily: "system-ui, -apple-system, sans-serif",
        maxWidth: "800px",
        margin: "0 auto",
        padding: "24px",
        lineHeight: 1.6,
      }}
    >
      <div style={{ textAlign: "center", marginBottom: "32px" }}>
        <h1 style={{ margin: "0 0 8px 0", color: "#1a1a1a" }}>
          📄 Document Classifier
        </h1>
        <p style={{ color: "#666", margin: "0" }}>
          Upload a PDF file to classify its document type using BERT-tiny
        </p>
      </div>

      <div
        style={{
          border: "2px dashed #ddd",
          borderRadius: "12px",
          padding: "32px",
          textAlign: "center",
          backgroundColor: "#fafafa",
          marginBottom: "24px",
        }}
      >
        <input
          id="pdf-upload"
          type="file"
          accept=".pdf"
          onChange={handleFileChange}
          style={{ display: "none" }}
        />
        <label
          htmlFor="pdf-upload"
          style={{
            display: "inline-block",
            padding: "12px 24px",
            backgroundColor: "#4f46e5",
            color: "white",
            borderRadius: "8px",
            cursor: "pointer",
            fontSize: "16px",
            fontWeight: "500",
            marginBottom: "16px",
            transition: "background-color 0.2s",
          }}
          onMouseOver={(e) => (e.target.style.backgroundColor = "#4338ca")}
          onMouseOut={(e) => (e.target.style.backgroundColor = "#4f46e5")}
        >
          {file ? "Change PDF File" : "Select PDF File"}
        </label>

        {file && (
          <div style={{ marginTop: "16px" }}>
            <p style={{ margin: "8px 0", color: "#333" }}>
              <strong>Selected:</strong> {file.name}
            </p>
            <p style={{ margin: "4px 0", color: "#666", fontSize: "14px" }}>
              Size: {(file.size / 1024).toFixed(2)} KB
            </p>
          </div>
        )}

        <div style={{ marginTop: "24px" }}>
          <button
            onClick={handleClassify}
            disabled={!file || loading}
            style={{
              padding: "12px 32px",
              fontSize: "16px",
              fontWeight: "600",
              backgroundColor: file && !loading ? "#10b981" : "#9ca3af",
              color: "white",
              border: "none",
              borderRadius: "8px",
              cursor: file && !loading ? "pointer" : "not-allowed",
              transition: "background-color 0.2s",
            }}
            onMouseOver={(e) => {
              if (file && !loading) {
                e.target.style.backgroundColor = "#059669";
              }
            }}
            onMouseOut={(e) => {
              if (file && !loading) {
                e.target.style.backgroundColor = "#10b981";
              }
            }}
          >
            {loading ? "Classifying..." : "Classify Document"}
          </button>

          {file && (
            <button
              onClick={handleReset}
              disabled={loading}
              style={{
                padding: "12px 24px",
                fontSize: "16px",
                fontWeight: "500",
                backgroundColor: "transparent",
                color: "#666",
                border: "1px solid #ddd",
                borderRadius: "8px",
                cursor: loading ? "not-allowed" : "pointer",
                marginLeft: "12px",
              }}
            >
              Reset
            </button>
          )}
        </div>
      </div>

      {error && (
        <div
          style={{
            padding: "16px",
            backgroundColor: "#fee2e2",
            border: "1px solid #fecaca",
            borderRadius: "8px",
            color: "#991b1b",
            marginBottom: "24px",
          }}
        >
          <strong>Error:</strong> {error}
        </div>
      )}

      {result && (
        <div
          style={{
            padding: "24px",
            backgroundColor: "#f0fdf4",
            border: "2px solid #86efac",
            borderRadius: "12px",
            marginBottom: "24px",
          }}
        >
          <h2 style={{ margin: "0 0 16px 0", color: "#166534" }}>
            Classification Result
          </h2>

          <div
            style={{
              backgroundColor: "white",
              padding: "20px",
              borderRadius: "8px",
              marginBottom: "16px",
            }}
          >
            <div style={{ marginBottom: "12px" }}>
              <span style={{ color: "#666", fontSize: "14px" }}>
                Document Type:
              </span>
              <div
                style={{
                  fontSize: "24px",
                  fontWeight: "700",
                  color: "#10b981",
                  marginTop: "4px",
                  textTransform: "capitalize",
                }}
              >
                {result.classification.document_type}
              </div>
            </div>

            <div style={{ marginBottom: "12px" }}>
              <span style={{ color: "#666", fontSize: "14px" }}>
                Confidence:
              </span>
              <div
                style={{
                  fontSize: "20px",
                  fontWeight: "600",
                  color: "#059669",
                  marginTop: "4px",
                }}
              >
                {(result.classification.confidence * 100).toFixed(1)}%
              </div>
            </div>

            <div style={{ marginTop: "16px", paddingTop: "16px", borderTop: "1px solid #e5e7eb" }}>
              <span style={{ color: "#666", fontSize: "14px" }}>
                File: {result.filename}
              </span>
              <br />
              <span style={{ color: "#666", fontSize: "14px" }}>
                Text Length: {result.text_length.toLocaleString()} characters
              </span>
            </div>
          </div>

          {result.classification.all_scores && (
            <div>
              <h3 style={{ margin: "0 0 12px 0", fontSize: "16px", color: "#166534" }}>
                Top 5 Classifications:
              </h3>
              <div style={{ backgroundColor: "white", padding: "16px", borderRadius: "8px" }}>
                {Object.entries(result.classification.all_scores).map(
                  ([type, score]) => (
                    <div
                      key={type}
                      style={{
                        display: "flex",
                        justifyContent: "space-between",
                        alignItems: "center",
                        padding: "8px 0",
                        borderBottom: "1px solid #f3f4f6",
                      }}
                    >
                      <span style={{ textTransform: "capitalize", color: "#374151" }}>
                        {type}
                      </span>
                      <span
                        style={{
                          fontWeight: "600",
                          color: type === result.classification.document_type ? "#10b981" : "#6b7280",
                        }}
                      >
                        {(score * 100).toFixed(1)}%
                      </span>
                    </div>
                  )
                )}
              </div>
            </div>
          )}
        </div>
      )}

      {loading && (
        <div style={{ textAlign: "center", padding: "24px" }}>
          <div
            style={{
              display: "inline-block",
              width: "40px",
              height: "40px",
              border: "4px solid #e5e7eb",
              borderTop: "4px solid #4f46e5",
              borderRadius: "50%",
              animation: "spin 1s linear infinite",
            }}
          />
          <p style={{ marginTop: "16px", color: "#666" }}>
            Processing your document...
          </p>
        </div>
      )}
    </div>
  );
}