alfrds commited on
Commit
7703dd9
·
1 Parent(s): cb460c0

new image generator

Browse files
Files changed (1) hide show
  1. generate.py +115 -0
generate.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from gradio.inputs import Textbox
3
+
4
+ import torch
5
+ from diffusers import StableDiffusionPipeline
6
+ import boto3
7
+ from io import BytesIO
8
+ import os
9
+ import botocore
10
+ from time import sleep
11
+
12
+ AWS_ACCESS_KEY_ID = os.getenv("AWS_ACCESS_KEY_ID")
13
+ AWS_SECRET_ACCESS_KEY = os.getenv("AWS_SECRET_ACCESS_KEY")
14
+ S3_BUCKET_NAME = 'pineblogs101145-dev'
15
+
16
+ model_id = "CompVis/stable-diffusion-v1-4"
17
+ device = "cuda" if torch.cuda.is_available() else "cpu"
18
+
19
+ pipe = StableDiffusionPipeline.from_pretrained(
20
+ model_id, torch_dtype=torch.float32)
21
+
22
+ pipe = pipe.to(device)
23
+
24
+ s3 = boto3.resource('s3')
25
+
26
+ s3_client = boto3.client('s3')
27
+
28
+
29
+ def text_to_image(prompt, save_as, key_id):
30
+
31
+ if AWS_ACCESS_KEY_ID != key_id:
32
+ return "not permition"
33
+
34
+ # Create an instance of the S3 client
35
+ s3 = boto3.client('s3',
36
+ aws_access_key_id=AWS_ACCESS_KEY_ID,
37
+ aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
38
+
39
+ image_name = '-'.join(save_as.split()) + ".webp"
40
+
41
+ def save_image_to_s3(image):
42
+ # Create a BytesIO object to store the image.
43
+ image_buffer = BytesIO()
44
+ image.save(image_buffer, format='WEBP')
45
+ image_buffer.seek(0)
46
+
47
+ # Full path of the file in the bucket
48
+ s3_key = "public/" + image_name
49
+
50
+ # Upload the image to the S3 bucket
51
+ s3.upload_fileobj(image_buffer, S3_BUCKET_NAME, s3_key)
52
+
53
+ def generator_image(prompt):
54
+ prompt = prompt
55
+ image = pipe(prompt).images[0]
56
+
57
+ # Save the image in S3
58
+ save_image_to_s3(image)
59
+
60
+ generator_image(prompt)
61
+ return image_name
62
+
63
+
64
+
65
+ # iface = gr.Interface(fn=text_to_image, inputs=[Textbox(label="prompt"), Textbox(label="s3_save_as"), Textbox(label="aws_key_id")], outputs="text")
66
+ # iface.launch()
67
+
68
+
69
+ def check_if_exist(bucket_name, key):
70
+
71
+ try:
72
+ s3.Object(bucket_name, key).load()
73
+ except botocore.exceptions.ClientError as e:
74
+ if e.response['Error']['Code'] == "404":
75
+ # The object does not exist.
76
+ return False
77
+ else:
78
+ # Something else has gone wrong.
79
+ raise
80
+ else:
81
+ return True
82
+
83
+
84
+ def list_s3_files(bucket_name, folder):
85
+
86
+ my_bucket = s3.Bucket(bucket_name)
87
+
88
+ for objects in my_bucket.objects.filter(Prefix=folder):
89
+ print(objects.key)
90
+
91
+ filename_ext = '%s' % os.path.basename(objects.key)
92
+ filename = os.path.splitext(filename_ext)[0]
93
+ s3image = 'public/%s.webp' % filename
94
+
95
+ if check_if_exist(bucket_name, s3image):
96
+ print('Image %s already exists!' % s3image)
97
+ else:
98
+ response = s3_client.head_object(Bucket=bucket_name, Key=objects.key)
99
+ metadata = response['Metadata']
100
+ print(metadata)
101
+ if 'resumen' in metadata:
102
+ print('Has resume, ready to create image!')
103
+ print('Start creating image.. %s ' % s3image)
104
+ resumen = metadata['resumen']
105
+ else:
106
+ print('There is NOT resume, skipping..')
107
+
108
+ sleep(500/1000)
109
+ text_to_image(resumen, filename, AWS_ACCESS_KEY_ID)
110
+
111
+
112
+ bucket_name = 'pineblogs101145-dev'
113
+ folder = 'public/mdx/'
114
+
115
+ list_s3_files(bucket_name, folder)