Ocillus commited on
Commit
42fac10
·
verified ·
1 Parent(s): 54c0be9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -15
app.py CHANGED
@@ -125,13 +125,33 @@ Your primary goal is to provide students with the most helpful and accurate stud
125
 
126
  selected = None
127
 
128
- def upload_file(file):
129
- foldername = 'cache'
130
- if not os.path.exists(foldername):
131
- os.mkdir(foldername)
132
- file_path = os.path.join(foldername, os.path.basename(file.name))
133
- shutil.copy(file.name, file_path)
134
- return list_uploaded_files()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
  def list_uploaded_files():
137
  foldername = 'cache'
@@ -237,10 +257,23 @@ def query_database(query):
237
 
238
  return df
239
 
240
-
241
- with open('example_queries.txt',mode='r') as file:
242
- example_database = file.read().split('\n')
243
-
 
 
 
 
 
 
 
 
 
 
 
 
 
244
 
245
  import random
246
 
@@ -269,7 +302,19 @@ def relaunch():
269
  demo.launch(share=True)
270
 
271
  # Combine the interfaces using Tabs
272
- with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
273
  gr.Markdown("# ArcanaUI v0.8")
274
  with gr.Tabs():
275
  with gr.TabItem("Welcome Page"):
@@ -285,17 +330,22 @@ with gr.Blocks() as demo:
285
  gr.Markdown('# Upload and View Files')
286
 
287
  with gr.Row():
 
288
  # Left column: File list and buttons
289
  with gr.Column(scale=1):
 
 
 
 
290
  uploaded_files_list = gr.DataFrame(headers=["Uploaded Files"], datatype="str", interactive=False)
291
 
292
  with gr.Row():
293
- upload_button = gr.UploadButton('Upload File')
294
  refresh_button = gr.Button('Refresh')
295
  delete_button = gr.Button('Delete Selected File')
296
 
 
297
  # Right column: File viewer and Image viewer
298
- with gr.Column(scale=1):
299
  with gr.Tab("File Viewer"):
300
  file_viewer = gr.File(label="File Restore")
301
  file_status = gr.Textbox(label="File Status", interactive=False)
@@ -309,10 +359,11 @@ with gr.Blocks() as demo:
309
 
310
  with gr.Tab("Image Viewer"):
311
  image_viewer = gr.Image(label="Image Viewer", type="filepath")
 
 
312
 
313
  # Event handlers
314
  refresh_button.click(fn=refresh_files, outputs=uploaded_files_list)
315
- upload_button.upload(upload_file, inputs=upload_button, outputs=uploaded_files_list)
316
  delete_button.click(fn=delete_file, outputs=[uploaded_files_list, file_viewer, file_status, file_size, file_creation_time])
317
  uploaded_files_list.select(fn=display_file, inputs=uploaded_files_list, outputs=[file_viewer, image_viewer, file_status])
318
  uploaded_files_list.select(fn=on_select, outputs=[file_viewer, file_status, file_size, file_creation_time])
 
125
 
126
  selected = None
127
 
128
+ from concurrent.futures import ThreadPoolExecutor
129
+
130
+ # Function to handle the file upload
131
+ def handle_file_upload(file):
132
+ # Ensure the cache2 directory exists
133
+ cache_dir = 'cache'
134
+ os.makedirs(cache_dir, exist_ok=True)
135
+
136
+ # Get the uploaded file path
137
+ file_path = file.name
138
+
139
+ # Define the new path for the uploaded file
140
+ new_file_path = os.path.join(cache_dir, os.path.basename(file_path))
141
+
142
+ # Move the file to the cache2 directory
143
+ shutil.move(file_path, new_file_path)
144
+
145
+ # Get the file size
146
+ file_size = os.path.getsize(new_file_path)
147
+
148
+ return f"File saved to {new_file_path} with size: {file_size} bytes"
149
+
150
+ # Wrapper function to run the file upload in a thread
151
+ def handle_file_upload_threaded(file):
152
+ with ThreadPoolExecutor() as executor:
153
+ future = executor.submit(handle_file_upload, file)
154
+ return future.result()
155
 
156
  def list_uploaded_files():
157
  foldername = 'cache'
 
257
 
258
  return df
259
 
260
+
261
+ example_database = [
262
+ "What is Hydrogen Bonding?",
263
+ "Tell me the difference between impulse and force.",
264
+ "Tell me a joke that Calculus students will understand.",
265
+ "How should I review for the AP Biology Exam?",
266
+ "What kind of resources are available in PA and Indexademics?",
267
+ "What is the StandardCAS™ group?",
268
+ "Explain the concept of quantum entanglement.",
269
+ "What are the main differences between mitosis and meiosis?",
270
+ "How does the Doppler effect work?",
271
+ "Explain the process of photosynthesis.",
272
+ "What is the significance of the Pythagorean theorem?",
273
+ "How does natural selection contribute to evolution?",
274
+ "What is the most important chapter in AP Statistics?",
275
+ "How should I prepare on the IB Chinese Exam?"
276
+ ]
277
 
278
  import random
279
 
 
302
  demo.launch(share=True)
303
 
304
  # Combine the interfaces using Tabs
305
+ with gr.Blocks(js="""
306
+ async () => {
307
+ const originalFetch = window.fetch;
308
+ window.fetch = (url, options) => {
309
+ if (options && options.signal) {
310
+ const controller = new AbortController();
311
+ options.signal = controller.signal;
312
+ setTimeout(() => controller.abort(), 3600000); // 300000 ms = 5 minutes
313
+ }
314
+ return originalFetch(url, options);
315
+ };
316
+ }
317
+ """) as demo:
318
  gr.Markdown("# ArcanaUI v0.8")
319
  with gr.Tabs():
320
  with gr.TabItem("Welcome Page"):
 
330
  gr.Markdown('# Upload and View Files')
331
 
332
  with gr.Row():
333
+
334
  # Left column: File list and buttons
335
  with gr.Column(scale=1):
336
+ gr.Markdown("## Upload File")
337
+ file_input = gr.File(label="Upload your file here", file_types=["pdf", "jpeg", "jpg", "gif", "docx"])
338
+ file_input.change(handle_file_upload_threaded, inputs=file_input)
339
+
340
  uploaded_files_list = gr.DataFrame(headers=["Uploaded Files"], datatype="str", interactive=False)
341
 
342
  with gr.Row():
 
343
  refresh_button = gr.Button('Refresh')
344
  delete_button = gr.Button('Delete Selected File')
345
 
346
+
347
  # Right column: File viewer and Image viewer
348
+ with gr.Column(scale=1):
349
  with gr.Tab("File Viewer"):
350
  file_viewer = gr.File(label="File Restore")
351
  file_status = gr.Textbox(label="File Status", interactive=False)
 
359
 
360
  with gr.Tab("Image Viewer"):
361
  image_viewer = gr.Image(label="Image Viewer", type="filepath")
362
+
363
+
364
 
365
  # Event handlers
366
  refresh_button.click(fn=refresh_files, outputs=uploaded_files_list)
 
367
  delete_button.click(fn=delete_file, outputs=[uploaded_files_list, file_viewer, file_status, file_size, file_creation_time])
368
  uploaded_files_list.select(fn=display_file, inputs=uploaded_files_list, outputs=[file_viewer, image_viewer, file_status])
369
  uploaded_files_list.select(fn=on_select, outputs=[file_viewer, file_status, file_size, file_creation_time])