import os import gradio as gr from werkzeug.utils import secure_filename UPLOAD_FOLDER = './' ALLOWED_EXTENSIONS = {'txt', 'pdf', 'doc', 'docx', 'png', 'jpg', 'jpeg', 'gif'} app = gr.Interface( fn=None, inputs=None, outputs=None, title='Workspace Uploader', description='Upload documents into a workspace', theme='default', layout='wide', allow_flagging=False, analytics_enabled=False, server_name=None, ) def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS def upload_file(request): if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files: return 'No file part' file = request.files['file'] # if user does not select file, browser also # submit an empty part without filename if file.filename == '': return 'No selected file' if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(UPLOAD_FOLDER, filename)) return 'File uploaded successfully' else: return 'File not allowed' @app.interface( "textbox", "file", live=True ) def upload_to_workspace(text, file): if file is not None: upload_file(file) return 'File uploaded successfully' else: return 'No file selected' if __name__ == '__main__': app.launch()