Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# Define the function to upload and save the file
|
| 4 |
+
def upload_and_save_file(input_file):
|
| 5 |
+
# Save the uploaded file with a specific name
|
| 6 |
+
file_path = "uploaded_file.txt"
|
| 7 |
+
with open(file_path, "wb") as f:
|
| 8 |
+
f.write(input_file.read())
|
| 9 |
+
return "File uploaded successfully!"
|
| 10 |
+
|
| 11 |
+
# Define the function to download the file
|
| 12 |
+
def download_file():
|
| 13 |
+
# Read the file and return it for download
|
| 14 |
+
file_path = "uploaded_file.txt"
|
| 15 |
+
with open(file_path, "rb") as f:
|
| 16 |
+
file_contents = f.read()
|
| 17 |
+
return file_contents
|
| 18 |
+
|
| 19 |
+
# Define the inputs and outputs for the Gradio interface
|
| 20 |
+
inputs = gr.inputs.File(label="Upload File")
|
| 21 |
+
output = gr.outputs.File(label="Download File")
|
| 22 |
+
|
| 23 |
+
# Create the Gradio interface
|
| 24 |
+
gr.Interface(
|
| 25 |
+
fn=upload_and_save_file,
|
| 26 |
+
inputs=inputs,
|
| 27 |
+
outputs=None,
|
| 28 |
+
title="File Uploader",
|
| 29 |
+
description="Upload a file to download later",
|
| 30 |
+
allow_flagging=False
|
| 31 |
+
).launch()
|
| 32 |
+
|
| 33 |
+
# Create the download page
|
| 34 |
+
download_interface = gr.Interface(
|
| 35 |
+
fn=download_file,
|
| 36 |
+
inputs=None,
|
| 37 |
+
outputs=output,
|
| 38 |
+
title="File Downloader",
|
| 39 |
+
description="Download the previously uploaded file",
|
| 40 |
+
allow_flagging=False
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
download_interface.launch()
|