Rahul8827 commited on
Commit
9431016
·
1 Parent(s): af8ed0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -34
app.py CHANGED
@@ -5,47 +5,65 @@ from datetime import datetime
5
  import boto3
6
  import gradio as gr
7
 
8
- # S3 client
9
- s3 = boto3.client('s3')
10
 
11
- # Bucket name
12
- bucket_name = 'myorg'
13
 
14
- def create_organization(org_name, org_handle):
15
 
16
- # Create organization
17
- org_details = {
18
- 'name': org_name,
19
- 'handle': org_handle,
20
- 'created_by': socket.gethostname(),
21
- 'created_at': datetime.now().strftime("%d/%m/%Y %H:%M:%S")
22
- }
 
 
 
 
 
 
 
23
 
24
- # Save to S3
25
- s3.put_object(
26
- Bucket=bucket_name,
27
- Key=f"{org_handle}/details.json",
28
- Body=json.dumps(org_details)
29
- )
30
 
31
- # Get existing organizations
32
- existing_orgs = []
33
- objects = s3.list_objects(Bucket=bucket_name)['Contents']
34
-
35
- for obj in objects:
36
- data = json.loads(s3.get_object(Bucket=bucket_name, Key=obj['Key'])['Body'].read())
37
- existing_orgs.append(data['name'])
38
-
39
- return ", ".join(existing_orgs)
40
 
41
  inputs = [
42
- gr.inputs.Textbox(label="Organization Name"),
43
- gr.inputs.Textbox(label="Organization Handle")
44
  ]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45
 
46
- outputs = [
47
- gr.outputs.Textbox(label="Existing Organizations"),
48
- gr.outputs.Textbox(label="Message")
49
- ]
50
 
51
- gr.Interface(fn=create_organization, inputs=inputs, outputs=outputs).launch()
 
5
  import boto3
6
  import gradio as gr
7
 
 
 
8
 
 
 
9
 
10
+ s3 = boto3.resource('s3')
11
 
12
+ def Create_Organization(org_name, org_handle):
13
+ on = org_name
14
+ o = org_handle
15
+ path = o
16
+ isExist = os.path.exists(path)
17
+ hostname = socket.gethostname()
18
+ now = datetime.now()
19
+ tim = now.strftime("%d/%m/%Y %H:%M:%S")
20
+ Organizationdetails = {}
21
+ Organizationdetails[org_handle] = {}
22
+ Organizationdetails[org_handle]['Organizationame'] = org_name
23
+ Organizationdetails[org_handle]['OrganizationHandle'] = org_handle
24
+ Organizationdetails[org_handle]['Created_by'] = hostname
25
+ Organizationdetails[org_handle]['Created_Time'] = tim
26
 
27
+ # Create a bucket in S3
28
+ bucket_name = 'myorg'
29
+ bucket = s3.Bucket(bucket_name)
 
 
 
30
 
31
+ # Save the organization details to S3 bucket
32
+ key = org_name + '/Organizationdetails.json'
33
+ blob = bucket.put_object(Key=key, Body=json.dumps(Organizationdetails))
34
+
35
+ return "Organization Created: " + o
 
 
 
 
36
 
37
  inputs = [
38
+ gr.inputs.Textbox(label="Organization Name"),
39
+ gr.inputs.Textbox(label="Organization Handle"),
40
  ]
41
+ outputs = gr.outputs.Textbox(label="Message")
42
+
43
+ gr.Interface(Create_Organization, inputs, outputs).launch()
44
+
45
+
46
+ import gradio as gr
47
+
48
+ def list_s3_folders(bucket_name):
49
+ import boto3
50
+
51
+ s3 = boto3.client('s3')
52
+ result = s3.list_objects(Bucket=bucket_name, Delimiter='/')
53
+
54
+ folders = [o.get('Prefix') for o in result.get('CommonPrefixes')]
55
+ return folders
56
+
57
+ iface = gr.Interface(
58
+ fn=list_s3_folders,
59
+ inputs="text",
60
+ outputs="text",
61
+ layout="vertical",
62
+ title="List S3 Folders",
63
+ description="Enter the S3 bucket name to list all folders in the bucket.",
64
+ examples=[["myorg"]]
65
+ )
66
+
67
+ iface.launch()
68
 
 
 
 
 
69