srinivas-mushroom commited on
Commit
14310b1
1 Parent(s): 9e3aae7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -22
app.py CHANGED
@@ -1,28 +1,55 @@
 
1
  import gradio as gr
2
- import boto3
3
 
4
- def upload_to_s3(bucket_name, file_name, file_data):
5
- s3 = boto3.resource('s3')
6
- bucket = s3.Bucket(bucket_name)
7
- bucket.put_object(Key=file_name, Body=file_data)
8
 
9
- def file_upload(workspace_name, document_name, document_file):
10
- # Assume the user is authenticated and has access to workspace_name
11
- # Store the document_file in the cloud storage solution for workspace_name
12
- bucket_name = f"{workspace_name}-bucket"
13
- upload_to_s3(bucket_name, document_name, document_file.read())
14
- return f"Document {document_name} uploaded successfully to workspace {workspace_name}."
 
 
 
 
 
 
15
 
16
- workspace_input = gr.inputs.Textbox(label="Workspace Name")
17
- document_name_input = gr.inputs.Textbox(label="Document Name")
18
- document_file_input = gr.inputs.File(label="Document File")
19
 
20
- def upload():
21
- # Check that user is authenticated and has access to workspace_input
22
- workspace_name = workspace_input.value
23
- document_name = document_name_input.value
24
- document_file = document_file_input.value
25
- return file_upload(workspace_name, document_name, document_file)
 
 
 
 
 
 
 
 
 
 
26
 
27
- app = gr.Interface(upload, inputs=[workspace_input, document_name_input, document_file_input], outputs="text")
28
- app.launch(debug = False)
 
 
 
 
 
 
 
 
 
 
 
 
 
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()