AstraOS commited on
Commit
c67ad58
·
verified ·
1 Parent(s): a0463fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -18
app.py CHANGED
@@ -1,7 +1,15 @@
1
  import gradio as gr
2
  from uuid import uuid4
3
- from fileManager import FileManager
4
  import re
 
 
 
 
 
 
 
 
5
 
6
  fileManager = FileManager()
7
  fileManager.start()
@@ -10,19 +18,11 @@ fileManager.start()
10
  def saveFile(file):
11
  with open(file.name, "rb") as rfile:
12
  file_infor = fileManager.saveFile(rfile.read(), file.orig_name)
13
- return f"https://astraos-nx-storage.hf.space/file=./{file_infor['path']}"
14
-
15
-
16
 
17
 
18
- import gradio as gr
19
- import os
20
- import random
21
- import string
22
- import glob
23
-
24
  ##############################################################
25
- # Generate some text files and save them in persistent storage
26
  ##############################################################
27
 
28
  def generate_random_string(length=100):
@@ -33,9 +33,9 @@ num_files=10
33
  file_length=1000
34
 
35
 
36
- for directory in ["/home/user/app", "/data"]:
37
  if not os.path.exists(directory):
38
- break
39
 
40
  for i in range(num_files):
41
  file_name = os.path.join(directory, f'random_file_{i}.txt')
@@ -45,12 +45,21 @@ for directory in ["/home/user/app", "/data"]:
45
 
46
 
47
  ##############################################################
48
- # The Gradio app
49
  ##############################################################
50
 
51
- #with gr.Blocks() as demo:
52
- demo=[gr.Interface(fn=saveFile, inputs="file", outputs="text"),
53
- gr.FileExplorer(label="Working directory"),
54
- gr.FileExplorer(root="/home/user/app", label="Persistent storage")]
 
 
 
 
 
 
 
 
 
55
 
56
  demo.launch()
 
1
  import gradio as gr
2
  from uuid import uuid4
3
+ from fileManager import FileManager # Assuming fileManager is defined elsewhere
4
  import re
5
+ import os
6
+ import random
7
+ import string
8
+ import glob
9
+
10
+ ##############################################################
11
+ # File Upload and Storage Functionality
12
+ ##############################################################
13
 
14
  fileManager = FileManager()
15
  fileManager.start()
 
18
  def saveFile(file):
19
  with open(file.name, "rb") as rfile:
20
  file_infor = fileManager.saveFile(rfile.read(), file.orig_name)
21
+ return f"https://emee-nx-storage.hf.space/file=./{file_infor['path']}"
 
 
22
 
23
 
 
 
 
 
 
 
24
  ##############################################################
25
+ # Random File Generation Functionality (Optional)
26
  ##############################################################
27
 
28
  def generate_random_string(length=100):
 
33
  file_length=1000
34
 
35
 
36
+ def generate_random_files(directory):
37
  if not os.path.exists(directory):
38
+ return
39
 
40
  for i in range(num_files):
41
  file_name = os.path.join(directory, f'random_file_{i}.txt')
 
45
 
46
 
47
  ##############################################################
48
+ # The Gradio App with Combined Functionality
49
  ##############################################################
50
 
51
+ with gr.Blocks() as demo:
52
+ # File Upload for Saving
53
+ with gr.Column():
54
+ uploaded_file = gr.File(label="Upload File")
55
+ save_button = gr.Button(value="Save File")
56
+ output_text = gr.Textbox(label="File URL")
57
+
58
+ # Random File Generation (Optional)
59
+ with gr.Column():
60
+ generate_button = gr.Button(value="Generate Random Files")
61
+
62
+ save_button.click(lambda: output_text.setValue(saveFile(uploaded_file)) if uploaded_file else None)
63
+ generate_button.click(lambda: [generate_random_files(d) for d in ["/home/user/app", "/data"]])
64
 
65
  demo.launch()