Spaces:
Runtime error
Runtime error
Commit
·
14310b1
1
Parent(s):
9e3aae7
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,28 +1,55 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
bucket = s3.Bucket(bucket_name)
|
| 7 |
-
bucket.put_object(Key=file_name, Body=file_data)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
def
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
app
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from werkzeug.utils import secure_filename
|
| 4 |
|
| 5 |
+
UPLOAD_FOLDER = './'
|
| 6 |
+
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'doc', 'docx', 'png', 'jpg', 'jpeg', 'gif'}
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
app = gr.Interface(
|
| 9 |
+
fn=None,
|
| 10 |
+
inputs=None,
|
| 11 |
+
outputs=None,
|
| 12 |
+
title='Workspace Uploader',
|
| 13 |
+
description='Upload documents into a workspace',
|
| 14 |
+
theme='default',
|
| 15 |
+
layout='wide',
|
| 16 |
+
allow_flagging=False,
|
| 17 |
+
analytics_enabled=False,
|
| 18 |
+
server_name=None,
|
| 19 |
+
)
|
| 20 |
|
| 21 |
+
def allowed_file(filename):
|
| 22 |
+
return '.' in filename and \
|
| 23 |
+
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
| 24 |
|
| 25 |
+
def upload_file(request):
|
| 26 |
+
if request.method == 'POST':
|
| 27 |
+
# check if the post request has the file part
|
| 28 |
+
if 'file' not in request.files:
|
| 29 |
+
return 'No file part'
|
| 30 |
+
file = request.files['file']
|
| 31 |
+
# if user does not select file, browser also
|
| 32 |
+
# submit an empty part without filename
|
| 33 |
+
if file.filename == '':
|
| 34 |
+
return 'No selected file'
|
| 35 |
+
if file and allowed_file(file.filename):
|
| 36 |
+
filename = secure_filename(file.filename)
|
| 37 |
+
file.save(os.path.join(UPLOAD_FOLDER, filename))
|
| 38 |
+
return 'File uploaded successfully'
|
| 39 |
+
else:
|
| 40 |
+
return 'File not allowed'
|
| 41 |
|
| 42 |
+
@app.interface(
|
| 43 |
+
"textbox",
|
| 44 |
+
"file",
|
| 45 |
+
live=True
|
| 46 |
+
)
|
| 47 |
+
def upload_to_workspace(text, file):
|
| 48 |
+
if file is not None:
|
| 49 |
+
upload_file(file)
|
| 50 |
+
return 'File uploaded successfully'
|
| 51 |
+
else:
|
| 52 |
+
return 'No file selected'
|
| 53 |
+
|
| 54 |
+
if __name__ == '__main__':
|
| 55 |
+
app.launch()
|