hagras commited on
Commit
70d2d0c
·
verified ·
1 Parent(s): 7d2736e

Update server.py

Browse files
Files changed (1) hide show
  1. server.py +4 -394
server.py CHANGED
@@ -12,7 +12,7 @@ To install the few extra deps:
12
  pip install gradio==4.* fastapi jinja2 python-multipart
13
 
14
  If you used Flask’s built-in dev server before, you can still start this file
15
- with `python app_gradio.py`, or with `uvicorn app_gradio:app --reload`.
16
  """
17
 
18
  from __future__ import annotations
@@ -110,397 +110,7 @@ def _redirect_with_flash(
110
  # --------------------------------------------------------------------------- #
111
  from jinja2 import Template # noqa: E402 (import after constant section)
112
 
113
- HTML_TEMPLATE = """
114
- <!DOCTYPE html>
115
- <html lang="en">
116
- <head>
117
- <meta charset="UTF-8">
118
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
119
- <title>File Upload Demo</title>
120
- <style>
121
- * {
122
- margin: 0;
123
- padding: 0;
124
- box-sizing: border-box;
125
- font-family: 'Helvetica Neue', Arial, sans-serif;
126
- }
127
-
128
- body {
129
- background: -webkit-linear-gradient(135deg, hsla(0, 0%, 0%, 1) 0%, hsla(0, 0%, 11%, 1) 100%);
130
- background: linear-gradient(135deg, hsla(0, 0%, 0%, 1) 0%, hsla(0, 0%, 11%, 1) 100%);
131
- background-attachment: fixed;
132
- min-height: 100vh;
133
- position: relative;
134
- color: #f3f3f3;
135
- line-height: 1.6;
136
- }
137
-
138
- .container {
139
- max-width: 800px;
140
- margin: 0 auto;
141
- padding: 20px;
142
- }
143
-
144
- /* Upload Area Styles */
145
- .upload-container {
146
- display: flex;
147
- align-items: center;
148
- justify-content: center;
149
- min-height: 100vh;
150
- }
151
-
152
- .upload-area {
153
- width: 100%;
154
- max-width: 500px;
155
- height: 300px;
156
- border: 2px dashed #4a5568;
157
- border-radius: 8px;
158
- background: rgba(255, 255, 255, 0.05);
159
- display: flex;
160
- flex-direction: column;
161
- align-items: center;
162
- justify-content: center;
163
- cursor: pointer;
164
- transition: all 0.2s ease;
165
- position: relative;
166
- overflow: hidden;
167
- padding: 0 2rem;
168
- margin: 0 1rem;
169
- }
170
-
171
- .upload-area:hover {
172
- border-color: #6b7280;
173
- background: rgba(255, 255, 255, 0.08);
174
- }
175
-
176
- .upload-area.dragover {
177
- border-color: #9ca3af;
178
- background: rgba(255, 255, 255, 0.1);
179
- transform: translateY(-2px);
180
- box-shadow: 0 4px 16px rgba(255, 255, 255, 0.1);
181
- }
182
-
183
- .upload-icon {
184
- font-size: 48px;
185
- color: #9ca3af;
186
- margin-bottom: 20px;
187
- }
188
-
189
- .upload-text {
190
- font-size: 18px;
191
- color: #f3f3f3;
192
- margin-bottom: 10px;
193
- font-weight: 500;
194
- }
195
-
196
- .upload-subtext {
197
- font-size: 14px;
198
- color: #9ca3af;
199
- text-align: center;
200
- }
201
-
202
- .file-input {
203
- display: none;
204
- }
205
-
206
- /* Content Area Styles */
207
- .content-container {
208
- min-height: 90vh;
209
- padding: 20px;
210
- }
211
-
212
- .content-area {
213
- width: 100%;
214
- height: calc(100vh - 80px);
215
- background: rgba(255, 255, 255, 0.05);
216
- border-radius: 8px;
217
- box-shadow: 0 1px 3px rgba(0,0,0,0.3), 0 1px 2px rgba(0,0,0,0.2);
218
- overflow: hidden;
219
- border: 1px solid #374151;
220
- }
221
-
222
- .content-iframe {
223
- width: 100%;
224
- height: 100%;
225
- border: none;
226
- background: rgba(255, 255, 255, 0.05);
227
- display: block;
228
- box-sizing: border-box;
229
- }
230
-
231
- /* Refresh Button Styles */
232
- .refresh-btn {
233
- font-family: helvetica neue;
234
- font-weight: bold;
235
- position: fixed;
236
- top: 0.5rem;
237
- left: 0.5rem;
238
- background: #212529;
239
- color: #fff;
240
- border: 0px;
241
- border-radius: 6px;
242
- padding: 12px 20px;
243
- font-size: 14px;
244
- cursor: pointer;
245
- backdrop-filter: blur(10px);
246
- transition: all 0.2s ease;
247
- opacity: 1;
248
- z-index: 1000;
249
- white-space: nowrap;
250
- width: auto;
251
- max-width: fit-content;
252
- }
253
-
254
- .refresh-btn:hover {
255
- background: #424649;
256
- border-color: #6b7280;
257
- box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
258
- }
259
-
260
- /* Flash Messages */
261
- .flash-messages {
262
- position: fixed;
263
- top: 25px;
264
- left: 50%;
265
- transform: translateX(-50%);
266
- z-index: 1001;
267
- }
268
-
269
- .flash-message {
270
- background: rgba(0, 57, 150, 0.4);
271
- color: #fff;
272
- padding: 12px 20px;
273
- border-radius: 6px;
274
- margin-bottom: 10px;
275
- box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3), 0 1px 2px rgba(0, 0, 0, 0.2);
276
- font-weight: 500;
277
- backdrop-filter: blur(10px);
278
- text-align: center;
279
- }
280
-
281
- /* Loading Animation */
282
- .loading {
283
- display: none;
284
- position: absolute;
285
- top: 50%;
286
- left: 50%;
287
- transform: translate(-50%, -50%);
288
- }
289
-
290
- .loading.active {
291
- display: block;
292
- }
293
-
294
- .spinner {
295
- width: 40px;
296
- height: 40px;
297
- border: 3px solid #4a5568;
298
- border-top: 3px solid #f3f3f3;
299
- border-radius: 50%;
300
- animation: spin 1s linear infinite;
301
- }
302
-
303
- @keyframes spin {
304
- 0% { transform: rotate(0deg); }
305
- 100% { transform: rotate(360deg); }
306
- }
307
-
308
- /* Responsive Design */
309
- @media (max-width: 768px) {
310
- .upload-area {
311
- height: 250px;
312
- margin: 0 1rem;
313
- padding: 0 1rem;
314
- max-width: calc(100vw - 2rem);
315
- }
316
-
317
- .upload-text {
318
- font-size: 16px;
319
- text-align: center;
320
- }
321
-
322
- .upload-subtext {
323
- font-size: 13px;
324
- text-align: center;
325
- }
326
-
327
- .upload-container {
328
- padding: 0 1rem;
329
- }
330
-
331
- .refresh-btn {
332
- top: 10px;
333
- left: 10px;
334
- padding: 8px 12px;
335
- font-size: 11px;
336
- white-space: nowrap;
337
- width: auto;
338
- max-width: fit-content;
339
- }
340
- }
341
-
342
- @media (max-width: 480px) {
343
- .upload-area {
344
- height: 220px;
345
- margin: 0 0.5rem;
346
- padding: 0 0.5rem;
347
- max-width: calc(100vw - 1rem);
348
- }
349
-
350
- .upload-text {
351
- font-size: 14px;
352
- }
353
-
354
- .upload-subtext {
355
- font-size: 12px;
356
- }
357
-
358
- .upload-icon {
359
- font-size: 36px;
360
- margin-bottom: 15px;
361
- }
362
-
363
- .upload-container {
364
- padding: 0 0.5rem;
365
- }
366
- }
367
- </style>
368
- </head>
369
- <body>
370
- <!-- Flash Messages -->
371
- {% with messages = get_flashed_messages() %}
372
- {% if messages %}
373
- <div class="flash-messages">
374
- {% for message in messages %}
375
- <div class="flash-message">{{ message }}</div>
376
- {% endfor %}
377
- </div>
378
- {% endif %}
379
- {% endwith %}
380
-
381
- <!-- Refresh Button (only visible after upload) -->
382
- {% if session.uploaded_file %}
383
- <button class="refresh-btn" onclick="refreshSession()">
384
- Start over
385
- </button>
386
- {% endif %}
387
-
388
- {% if not session.uploaded_file %}
389
- <!-- Upload Interface -->
390
- <h2 style="text-align: center; margin-top: 0.25rem;">Rag Document Viewer Demo</h2>
391
- <div class="upload-container">
392
- <form id="uploadForm" method="POST" action="/upload" enctype="multipart/form-data">
393
- <div class="upload-area" id="uploadArea">
394
- <div class="upload-icon">📂</div>
395
- <div class="upload-text">Drop your file here or click to browse</div>
396
- <div class="upload-subtext">Supports: PDFs, Office Documents, OpenOffice Documents <br>(Max: 16MB)</div>
397
- <input type="file" name="file" class="file-input" id="fileInput" accept=".txt,.pdf,.doc,.docx,.ppt,.pptx,.xls,.xlsx,.odt,.odp,.ods">
398
- <div class="loading" id="loading">
399
- <div class="spinner"></div>
400
- </div>
401
- </div>
402
- </form>
403
- </div>
404
- {% else %}
405
- <!-- Content Interface -->
406
- <h2 style="text-align: center; margin-top: 0.25rem;">Rag Document Viewer Demo</h2>
407
- <div class="content-container">
408
- <div class="content-area">
409
- <div class="content-iframe">
410
- <iframe src="{{ url_for('load', filename=session.uploaded_file) }}"
411
- style="width: 100%; height: 100%; border: none; display: block;">
412
- </iframe>
413
- </div>
414
- </div>
415
- </div>
416
- {% endif %}
417
-
418
- <script>
419
- // File upload handling
420
- const uploadArea = document.getElementById('uploadArea');
421
- const fileInput = document.getElementById('fileInput');
422
- const uploadForm = document.getElementById('uploadForm');
423
- const loading = document.getElementById('loading');
424
-
425
- if (uploadArea) {
426
- // Click to browse
427
- uploadArea.addEventListener('click', () => {
428
- fileInput.click();
429
- });
430
-
431
- // Drag and drop
432
- uploadArea.addEventListener('dragover', (e) => {
433
- e.preventDefault();
434
- uploadArea.classList.add('dragover');
435
- });
436
-
437
- uploadArea.addEventListener('dragleave', () => {
438
- uploadArea.classList.remove('dragover');
439
- });
440
-
441
- uploadArea.addEventListener('drop', (e) => {
442
- e.preventDefault();
443
- uploadArea.classList.remove('dragover');
444
-
445
- const files = e.dataTransfer.files;
446
- if (files.length > 0) {
447
- const file = files[0];
448
- const maxSize = 16 * 1024 * 1024; // 16MB
449
-
450
- if (file.size > maxSize) {
451
- // Redirect to file size error route instead of alert
452
- window.location.href = '/file_size_error';
453
- return;
454
- }
455
-
456
- fileInput.files = files;
457
- submitForm();
458
- }
459
- });
460
-
461
- // File input change
462
- fileInput.addEventListener('change', () => {
463
- if (fileInput.files.length > 0) {
464
- const file = fileInput.files[0];
465
- const maxSize = 16 * 1024 * 1024; // 16MB
466
-
467
- if (file.size > maxSize) {
468
- // Redirect to file size error route instead of alert
469
- fileInput.value = ''; // Clear the file input
470
- window.location.href = '/file_size_error';
471
- return;
472
- }
473
-
474
- submitForm();
475
- }
476
- });
477
-
478
- function submitForm() {
479
- loading.classList.add('active');
480
- uploadForm.submit();
481
- }
482
- }
483
-
484
- // Refresh session function
485
- function refreshSession() {
486
- window.location.href = '/refresh';
487
- }
488
-
489
- // Auto-hide flash messages after 3 seconds
490
- setTimeout(() => {
491
- const flashMessages = document.querySelector('.flash-messages');
492
- if (flashMessages) {
493
- flashMessages.style.opacity = '0';
494
- flashMessages.style.transition = 'opacity 0.5s ease';
495
- setTimeout(() => {
496
- flashMessages.style.display = 'none';
497
- }, 500);
498
- }
499
- }, 5000);
500
- </script>
501
- </body>
502
- </html>
503
- """ # <-- paste your original template here
504
 
505
  # The string was abbreviated above to keep this snippet short. Please replace
506
  # it with the *entire* HTML_TEMPLATE from your Flask application.
@@ -683,8 +293,8 @@ app = gr.mount_gradio_app(
683
  path="/" # mount at root so URLs remain identical
684
  )
685
 
686
- # Launch via `python app_gradio.py` or `uvicorn app_gradio:app --reload`
687
  if __name__ == "__main__":
688
  import uvicorn
689
 
690
- uvicorn.run("app_gradio:app", host="0.0.0.0", port=7860, reload=True)
 
12
  pip install gradio==4.* fastapi jinja2 python-multipart
13
 
14
  If you used Flask’s built-in dev server before, you can still start this file
15
+ with `python server.py`, or with `uvicorn server:app --reload`.
16
  """
17
 
18
  from __future__ import annotations
 
110
  # --------------------------------------------------------------------------- #
111
  from jinja2 import Template # noqa: E402 (import after constant section)
112
 
113
+ HTML_TEMPLATE = """{{ html_source }}""" # <-- paste your original template here
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
  # The string was abbreviated above to keep this snippet short. Please replace
116
  # it with the *entire* HTML_TEMPLATE from your Flask application.
 
293
  path="/" # mount at root so URLs remain identical
294
  )
295
 
296
+ # Launch via `python server.py` or `uvicorn server:app --reload`
297
  if __name__ == "__main__":
298
  import uvicorn
299
 
300
+ uvicorn.run("server:app", host="0.0.0.0", port=7860, reload=True)