Deva
commited on
Commit
·
2fa18c8
1
Parent(s):
ed96d64
Feature upload a whole folder
Browse files- app.py +5 -147
- archive.py +143 -0
app.py
CHANGED
|
@@ -10,158 +10,16 @@ from pathlib import Path
|
|
| 10 |
|
| 11 |
def upload_file(files):
|
| 12 |
file_paths = [file.name for file in files]
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
with gr.Blocks() as demo:
|
| 17 |
upload_button = gr.UploadButton(
|
| 18 |
"Click to Upload a File", file_types=["image", "video"], file_count="multiple"
|
| 19 |
)
|
| 20 |
-
file_output = gr.File()
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
demo.launch()
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
"""
|
| 27 |
-
current_file_path = Path(__file__).resolve()
|
| 28 |
-
relative_path = "path/to/file"
|
| 29 |
-
absolute_path = (current_file_path.parent / ".." / ".." / "gradio").resolve()
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
def get_file_content(file):
|
| 33 |
-
return (file,)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
with gr.Blocks() as demo:
|
| 37 |
-
gr.Markdown('### `FileExplorer` to `FileExplorer` -- `file_count="multiple"`')
|
| 38 |
-
submit_btn = gr.Button("Select")
|
| 39 |
-
with gr.Row():
|
| 40 |
-
file = gr.FileExplorer(
|
| 41 |
-
glob="**/components/*.py",
|
| 42 |
-
# value=["themes/utils"],
|
| 43 |
-
root_dir=absolute_path,
|
| 44 |
-
ignore_glob="**/__init__.py",
|
| 45 |
-
)
|
| 46 |
-
|
| 47 |
-
file2 = gr.FileExplorer(
|
| 48 |
-
glob="**/components/**/*.py",
|
| 49 |
-
root_dir=absolute_path,
|
| 50 |
-
ignore_glob="**/__init__.py",
|
| 51 |
-
)
|
| 52 |
-
submit_btn.click(lambda x: x, file, file2)
|
| 53 |
-
|
| 54 |
-
gr.Markdown("---")
|
| 55 |
-
gr.Markdown('### `FileExplorer` to `Code` -- `file_count="single"`')
|
| 56 |
-
with gr.Group():
|
| 57 |
-
with gr.Row():
|
| 58 |
-
file_3 = gr.FileExplorer(
|
| 59 |
-
scale=1,
|
| 60 |
-
glob="**/components/**/*.py",
|
| 61 |
-
value=["themes/utils"],
|
| 62 |
-
file_count="single",
|
| 63 |
-
root_dir=absolute_path,
|
| 64 |
-
ignore_glob="**/__init__.py",
|
| 65 |
-
elem_id="file",
|
| 66 |
-
)
|
| 67 |
-
|
| 68 |
-
code = gr.Code(lines=30, scale=2, language="python")
|
| 69 |
-
|
| 70 |
-
file_3.change(get_file_content, file_3, code)
|
| 71 |
-
|
| 72 |
-
if __name__ == "__main__":
|
| 73 |
-
demo.launch()
|
| 74 |
-
"""
|
| 75 |
-
""" Version 1
|
| 76 |
-
def display_name_files(_folder_path):
|
| 77 |
-
return f"{[img_name for img_name in os.listdir(_folder_path)]} \n"
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
def on_browse(data_type):
|
| 81 |
-
root = Tk()
|
| 82 |
-
root.attributes("-topmost", True)
|
| 83 |
-
root.withdraw()
|
| 84 |
-
if data_type == "Files":
|
| 85 |
-
filenames = filedialog.askopenfilenames()
|
| 86 |
-
if len(filenames) > 0:
|
| 87 |
-
root.destroy()
|
| 88 |
-
return str(filenames)
|
| 89 |
-
else:
|
| 90 |
-
filename = "Files not seleceted"
|
| 91 |
-
root.destroy()
|
| 92 |
-
return str(filename)
|
| 93 |
-
|
| 94 |
-
elif data_type == "Folder":
|
| 95 |
-
filename = filedialog.askdirectory()
|
| 96 |
-
print(filename)
|
| 97 |
-
print(type(filename))
|
| 98 |
-
if filename:
|
| 99 |
-
if os.path.isdir(filename):
|
| 100 |
-
root.destroy()
|
| 101 |
-
return str("\n".join(os.listdir(filename)))
|
| 102 |
-
# return str(filename)
|
| 103 |
-
else:
|
| 104 |
-
root.destroy()
|
| 105 |
-
return str(filename)
|
| 106 |
-
else:
|
| 107 |
-
filename = "Folder not seleceted"
|
| 108 |
-
root.destroy()
|
| 109 |
-
return str(filename)
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
def main():
|
| 113 |
-
with gr.Blocks() as demo:
|
| 114 |
-
data_type = gr.Radio(
|
| 115 |
-
choices=["Files", "Folder"], value="Files", label="Offline data type"
|
| 116 |
-
)
|
| 117 |
-
input_path = gr.Textbox(
|
| 118 |
-
label="Select Multiple videos", scale=5, interactive=False
|
| 119 |
-
)
|
| 120 |
-
image_browse_btn = gr.Button("Browse", min_width=1)
|
| 121 |
-
image_browse_btn.click(
|
| 122 |
-
on_browse, inputs=data_type, outputs=input_path, show_progress="hidden"
|
| 123 |
-
)
|
| 124 |
-
return demo
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
demo = main()
|
| 128 |
-
demo.launch(inbrowser=True)
|
| 129 |
-
"""
|
| 130 |
-
|
| 131 |
-
'''
|
| 132 |
-
def get_image_name(_image):
|
| 133 |
-
if _image.any():
|
| 134 |
-
# print(type(_image))
|
| 135 |
-
output_info = f"""
|
| 136 |
-
Image shape:
|
| 137 |
-
{_image.shape}
|
| 138 |
-
"""
|
| 139 |
-
return output_info
|
| 140 |
-
else:
|
| 141 |
-
return "No file uploaded"
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
def list_images(dir):
|
| 145 |
-
return [d.name for d in dir]
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
with gr.Blocks() as interface:
|
| 149 |
-
gr.Markdown("Import your image and then click the button to see the output.")
|
| 150 |
-
with gr.Row():
|
| 151 |
-
input_img = gr.Image(label="Input Image", height=500, width=500)
|
| 152 |
-
# output_img = gr.Image(label="Recognized Image")
|
| 153 |
-
# display_df = gr.DataFrame(export_results)
|
| 154 |
-
output_info = gr.Textbox()
|
| 155 |
-
# export_info = gr.Textbox()
|
| 156 |
-
labelling_btn = gr.Button("Apply Species Recognition")
|
| 157 |
-
# export_btn = gr.Button("Export result as a CSV")
|
| 158 |
-
labelling_btn.click(fn=get_image_name, inputs=input_img, outputs=[output_info])
|
| 159 |
-
# export_btn.click(fn=export_results, inputs=input_img, outputs=[export_info])
|
| 160 |
-
|
| 161 |
-
with gr.Row():
|
| 162 |
-
gr.Dataframe(pd.DataFrame({"col1": [1, 2], "col2": [3, 4]}))
|
| 163 |
-
|
| 164 |
-
# Launch the app
|
| 165 |
-
if __name__ == "__main__":
|
| 166 |
-
interface.launch()
|
| 167 |
-
'''
|
|
|
|
| 10 |
|
| 11 |
def upload_file(files):
|
| 12 |
file_paths = [file.name for file in files]
|
| 13 |
+
df_file_paths = pd.DataFrame(file_paths, columns=["file_name"])
|
| 14 |
+
return df_file_paths
|
| 15 |
|
| 16 |
|
| 17 |
with gr.Blocks() as demo:
|
| 18 |
upload_button = gr.UploadButton(
|
| 19 |
"Click to Upload a File", file_types=["image", "video"], file_count="multiple"
|
| 20 |
)
|
| 21 |
+
# file_output = gr.File()
|
| 22 |
+
output_df = gr.DataFrame()
|
| 23 |
+
upload_button.upload(upload_file, upload_button, output_df)
|
| 24 |
|
| 25 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
current_file_path = Path(__file__).resolve()
|
| 3 |
+
relative_path = "path/to/file"
|
| 4 |
+
absolute_path = (current_file_path.parent / ".." / ".." / "gradio").resolve()
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def get_file_content(file):
|
| 8 |
+
return (file,)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
with gr.Blocks() as demo:
|
| 12 |
+
gr.Markdown('### `FileExplorer` to `FileExplorer` -- `file_count="multiple"`')
|
| 13 |
+
submit_btn = gr.Button("Select")
|
| 14 |
+
with gr.Row():
|
| 15 |
+
file = gr.FileExplorer(
|
| 16 |
+
glob="**/components/*.py",
|
| 17 |
+
# value=["themes/utils"],
|
| 18 |
+
root_dir=absolute_path,
|
| 19 |
+
ignore_glob="**/__init__.py",
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
file2 = gr.FileExplorer(
|
| 23 |
+
glob="**/components/**/*.py",
|
| 24 |
+
root_dir=absolute_path,
|
| 25 |
+
ignore_glob="**/__init__.py",
|
| 26 |
+
)
|
| 27 |
+
submit_btn.click(lambda x: x, file, file2)
|
| 28 |
+
|
| 29 |
+
gr.Markdown("---")
|
| 30 |
+
gr.Markdown('### `FileExplorer` to `Code` -- `file_count="single"`')
|
| 31 |
+
with gr.Group():
|
| 32 |
+
with gr.Row():
|
| 33 |
+
file_3 = gr.FileExplorer(
|
| 34 |
+
scale=1,
|
| 35 |
+
glob="**/components/**/*.py",
|
| 36 |
+
value=["themes/utils"],
|
| 37 |
+
file_count="single",
|
| 38 |
+
root_dir=absolute_path,
|
| 39 |
+
ignore_glob="**/__init__.py",
|
| 40 |
+
elem_id="file",
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
code = gr.Code(lines=30, scale=2, language="python")
|
| 44 |
+
|
| 45 |
+
file_3.change(get_file_content, file_3, code)
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
demo.launch()
|
| 49 |
+
"""
|
| 50 |
+
|
| 51 |
+
""" Version 1
|
| 52 |
+
def display_name_files(_folder_path):
|
| 53 |
+
return f"{[img_name for img_name in os.listdir(_folder_path)]} \n"
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def on_browse(data_type):
|
| 57 |
+
root = Tk()
|
| 58 |
+
root.attributes("-topmost", True)
|
| 59 |
+
root.withdraw()
|
| 60 |
+
if data_type == "Files":
|
| 61 |
+
filenames = filedialog.askopenfilenames()
|
| 62 |
+
if len(filenames) > 0:
|
| 63 |
+
root.destroy()
|
| 64 |
+
return str(filenames)
|
| 65 |
+
else:
|
| 66 |
+
filename = "Files not seleceted"
|
| 67 |
+
root.destroy()
|
| 68 |
+
return str(filename)
|
| 69 |
+
|
| 70 |
+
elif data_type == "Folder":
|
| 71 |
+
filename = filedialog.askdirectory()
|
| 72 |
+
print(filename)
|
| 73 |
+
print(type(filename))
|
| 74 |
+
if filename:
|
| 75 |
+
if os.path.isdir(filename):
|
| 76 |
+
root.destroy()
|
| 77 |
+
return str("\n".join(os.listdir(filename)))
|
| 78 |
+
# return str(filename)
|
| 79 |
+
else:
|
| 80 |
+
root.destroy()
|
| 81 |
+
return str(filename)
|
| 82 |
+
else:
|
| 83 |
+
filename = "Folder not seleceted"
|
| 84 |
+
root.destroy()
|
| 85 |
+
return str(filename)
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
def main():
|
| 89 |
+
with gr.Blocks() as demo:
|
| 90 |
+
data_type = gr.Radio(
|
| 91 |
+
choices=["Files", "Folder"], value="Files", label="Offline data type"
|
| 92 |
+
)
|
| 93 |
+
input_path = gr.Textbox(
|
| 94 |
+
label="Select Multiple videos", scale=5, interactive=False
|
| 95 |
+
)
|
| 96 |
+
image_browse_btn = gr.Button("Browse", min_width=1)
|
| 97 |
+
image_browse_btn.click(
|
| 98 |
+
on_browse, inputs=data_type, outputs=input_path, show_progress="hidden"
|
| 99 |
+
)
|
| 100 |
+
return demo
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
demo = main()
|
| 104 |
+
demo.launch(inbrowser=True)
|
| 105 |
+
"""
|
| 106 |
+
|
| 107 |
+
'''
|
| 108 |
+
def get_image_name(_image):
|
| 109 |
+
if _image.any():
|
| 110 |
+
# print(type(_image))
|
| 111 |
+
output_info = f"""
|
| 112 |
+
Image shape:
|
| 113 |
+
{_image.shape}
|
| 114 |
+
"""
|
| 115 |
+
return output_info
|
| 116 |
+
else:
|
| 117 |
+
return "No file uploaded"
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def list_images(dir):
|
| 121 |
+
return [d.name for d in dir]
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
with gr.Blocks() as interface:
|
| 125 |
+
gr.Markdown("Import your image and then click the button to see the output.")
|
| 126 |
+
with gr.Row():
|
| 127 |
+
input_img = gr.Image(label="Input Image", height=500, width=500)
|
| 128 |
+
# output_img = gr.Image(label="Recognized Image")
|
| 129 |
+
# display_df = gr.DataFrame(export_results)
|
| 130 |
+
output_info = gr.Textbox()
|
| 131 |
+
# export_info = gr.Textbox()
|
| 132 |
+
labelling_btn = gr.Button("Apply Species Recognition")
|
| 133 |
+
# export_btn = gr.Button("Export result as a CSV")
|
| 134 |
+
labelling_btn.click(fn=get_image_name, inputs=input_img, outputs=[output_info])
|
| 135 |
+
# export_btn.click(fn=export_results, inputs=input_img, outputs=[export_info])
|
| 136 |
+
|
| 137 |
+
with gr.Row():
|
| 138 |
+
gr.Dataframe(pd.DataFrame({"col1": [1, 2], "col2": [3, 4]}))
|
| 139 |
+
|
| 140 |
+
# Launch the app
|
| 141 |
+
if __name__ == "__main__":
|
| 142 |
+
interface.launch()
|
| 143 |
+
'''
|