File size: 11,597 Bytes
1da2a5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0394b2f
1da2a5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0394b2f
1da2a5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0394b2f
1da2a5a
 
 
 
0394b2f
1da2a5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0394b2f
1da2a5a
 
 
 
 
 
 
 
0394b2f
1da2a5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!-- templates/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
    <title>File Upload</title>
    <!-- Add Bootstrap CDN link for styling -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" rel="stylesheet">
        <!-- Add these links to the head section of your HTML -->
    <link href="https://fonts.googleapis.com/css?family=Roboto:400,700" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Lora:400,700" rel="stylesheet">

    <style>
        /* Custom CSS for fade-in transition effect */
        .card {
            opacity: 0;
            transition: opacity 0.5s ease-in-out;
        }

        .card.show {
            opacity: 1;
        }
        body {
            font-family: 'Lora', sans-serif;
            font-size: 16px;
            background-color: #eaf4ea; /* Light green background */
            color: #333; /* Dark text color */
        }

        h1, h2, h3, h4, h5, h6 {
            font-family: 'Roboto', serif;
            font-weight: bold;
        }

        .container {
            background-color: rgba(255, 255, 255, 0.9); /* Semi-transparent white background for content */
            border-radius: 15px;
            padding: 20px;
            margin-top: 50px;
        }

        h1, h2 {
            color: #4CAF50; /* Dark green title color */
        }

        button.btn-primary {
            background-color: #4CAF50; /* Dark green button color */
            border: none;
        }

        button.btn-primary:hover {
            background-color: #45a049; /* Slightly darker green on hover */
        }

        .card {
            background-color: #f8f9fa; /* Light gray background for cards */
            border: 1px solid #ddd; /* Light gray border */
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); /* Subtle shadow */
        }

        .card-header {
            background-color: #4CAF50; /* Dark green header color for cards */
            color: white;
            border-radius: 10px 10px 0 0;
        }

        .card-body {
            padding: 15px;
        }

        .list-group-item {
            border: none;
        }

        .btn-success {
            background-color: #28a745; /* Dark green for success button */
            border: none;
        }

        .btn-success:hover {
            background-color: #218838; /* Slightly darker green on hover */
        }

        #predictions:hover::before {
            content: "Tables displaying TPM values corresponding to plant tissues";
            position: absolute;
            bottom: 100%; /* Position the tooltip above the text */
            left: 50%;
            transform: translateX(-50%);
            background-color: #333;
            color: #fff;
            padding: 5px;
            border-radius: 5px;
            font-size: 12px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1 class="mb-4">
            <a href="https://huggingface.co/Gurveer05/FloraBERT" target="_blank" rel="noopener noreferrer" style="color: #218838;">FloraBERT</a>
        </h1>
        <form>
            <div class="form-group">
                <label for="fileInput">Select .txt files:</label>
                <div class="input-group">
                    <div class="custom-file">
                        <input type="file" class="custom-file-input" id="fileInput" accept=".txt" multiple onchange="displaySelectedFiles()">
                        <label class="custom-file-label" for="fileInput">Choose files</label>
                    </div>
                </div>
            </div>
            <button type="button" class="btn btn-primary" id="uploadButton" onclick="processFiles()">Upload and Process</button>
            <p id="uploadMessage" class="text-muted mt-2"></p>
            <div id="selectedFiles" class="mt-2"></div>
        </form>

        <h2 class="mt-5" id="predictions" title="Table displaying TPM values corresponding to plant tissues">Predictions</h2>
        <div id="loadingIcon" class="d-none">
            <p class="text-muted">Processing... <span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span></p>
        </div>
        <div id="predictionsList" class="mt-3"></div>

        <button type="button" class="btn btn-success mt-3" onclick="downloadAllPredictions()">Download All Predictions</button>
    </div>

    <script>
        // Maintain a cache for predictions
        const predictionsCache = {};

        function displaySelectedFiles() {
            const fileInput = document.getElementById('fileInput');
            const selectedFilesContainer = document.getElementById('selectedFiles');
            const files = fileInput.files;

            selectedFilesContainer.innerHTML = '';

            for (let i = 0; i < files.length; i++) {
                const fileName = files[i].name;
                const fileLabel = document.createElement('span');
                fileLabel.innerText = fileName;

                if (i > 0) {
                    selectedFilesContainer.appendChild(document.createTextNode(', '));
                }

                selectedFilesContainer.appendChild(fileLabel);
            }
        }

        async function processFiles() {
            const fileInput = document.getElementById('fileInput');
            const files = fileInput.files;

            const uploadButton = document.getElementById('uploadButton');
            const uploadMessage = document.getElementById('uploadMessage');
            const loadingIcon = document.getElementById('loadingIcon');
            const predictionsList = document.getElementById('predictionsList');
            const selectedFilesContainer = document.getElementById('selectedFiles');

            // Disable the upload button
            uploadButton.disabled = true;

            uploadMessage.innerText = ''; // Clear previous messages

            if (files.length === 0) {
                // Update upload message for no file selected
                uploadMessage.innerText = 'Please choose at least one file.';
                // Enable the upload button
                uploadButton.disabled = false;
                return;
            }

            const url = 'http://127.0.0.1:8000';

            loadingIcon.classList.remove('d-none');
            // predictionsList.innerHTML = ''; // Do not clear previous cards

            for (let i = 0; i < files.length; i++) {
                const file = files[i];
                const formData = new FormData();
                formData.append('file', file);

                const response = await fetch(`${url}/uploadfile/`, {
                    method: 'POST',
                    body: formData
                });

                if (response.ok) {
                    const result = await response.json();
                    const filename = result.filename;

                    const predictionsResponse = await fetch(`${url}/process/${filename}`);
                    const predictions = await predictionsResponse.json();

                    predictionsCache[filename] = predictions;

                    // Display predictions as cards (newest on top)
                    if (!document.getElementById(`card-${filename}`)) {
                        const cardHtml = `
                            <div id="card-${filename}" class="card mt-3">
                                <div class="card-header">
                                    ${filename}
                                </div>
                                <div class="card-body">
                                    <ul class="list-group list-group-flush">
                                        ${predictions.map((predictionList, index) => `
                                            <li class="list-group-item">
                                                <strong>Sequence ${index + 1}</strong>
                                                <table class="table table-bordered mt-2">
                                                    <thead>
                                                        <tr>
                                                            <th scope="col">Tissue</th>
                                                            <th scope="col">Prediction</th>
                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        ${predictionList.map(prediction => `
                                                            <tr>
                                                                <td>${prediction.tissue}</td>
                                                                <td>${prediction.prediction}</td>
                                                            </tr>
                                                        `).join('')}
                                                    </tbody>
                                                </table>
                                            </li>
                                        `).join('')}
                                    </ul>
                                </div>
                            </div>
                        `;
                        predictionsList.innerHTML = cardHtml + predictionsList.innerHTML;

                        // Trigger fade-in effect
                        setTimeout(() => {
                            document.querySelectorAll('.card').forEach(card => card.classList.add('show'));
                        }, 100);
                    }

                    // Update upload message immediately
                    uploadMessage.innerText = `Processed files!`;

                    // Clear the message after a short delay (e.g., 2000 milliseconds)
                    setTimeout(() => {
                        uploadMessage.innerText = '';
                    }, 2000);
                } else {
                    console.error('Failed to upload file:', file.name);
                    uploadMessage.innerText = `Failed to upload file ${i + 1} of ${files.length}.`;
                }
            }

            // Enable the upload button after all files are processed
            uploadButton.disabled = false;

            // Hide processing icon after all files are processed
            loadingIcon.classList.add('d-none');
            
            // Clear displayed selected files after processing
            selectedFilesContainer.innerHTML = '';
        }

        function downloadAllPredictions() {
            // Convert predictionsCache to a JSON string
            const predictionsJSON = JSON.stringify(predictionsCache, null, 2);

            // Create a Blob containing the JSON data
            const blob = new Blob([predictionsJSON], { type: 'application/json' });

            // Create a link element and trigger a click to download the file
            const a = document.createElement('a');
            a.href = URL.createObjectURL(blob);
            a.download = 'all_predictions.json';
            a.click();
        }
    </script>

    <!-- Add Bootstrap JS and Popper.js CDN links -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</body>
</html>