File size: 10,128 Bytes
80a97c1
458d25d
21c220d
d43e911
e27f93b
227fdeb
ef8d465
2a06a6d
4dd61ad
19e6ee3
4dd61ad
ef8d465
 
 
84341eb
318085a
ef8d465
 
 
 
 
a57452c
ef8d465
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2496621
def1799
 
 
 
25e34d3
 
 
e4228c4
318085a
 
 
 
 
 
 
 
 
e4228c4
 
 
318085a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25e34d3
318085a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25e34d3
e4228c4
318085a
e4228c4
 
 
def1799
6f2ea54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
def1799
6f2ea54
 
e27f93b
6f2ea54
e27f93b
def1799
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6f2ea54
 
 
 
 
 
 
ef8d465
 
 
 
 
 
665e1ab
75fd8f4
ef8d465
 
 
 
 
 
 
6450ea1
9c6d5ac
ef8d465
 
88efdef
ef8d465
 
 
68d749e
def1799
 
 
 
07c0235
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import os
from PIL import ImageDraw
from PIL import ImageFont

#https://huggingface.co/spaces/Galis/room_interior_quality/tree/main/FGoWx7peJuJ/secret_santa
STABILITY_HOST = os.environ["STABILITY_HOST"]
STABILITY_KEY = os.environ["STABILITY_KEY"]
cohere_key = os.environ["cohere_key"]
import cohere
import random
co = cohere.Client(cohere_key)
import io
import os
import warnings
import math
from math import sqrt
from IPython.display import display
from PIL import Image
from stability_sdk import client
import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
from PIL import Image
 
stability_api = client.StabilityInference(
    key=os.environ['STABILITY_KEY'], 
    verbose=True,
)


def generate_caption_keywords(prompt, model='command-xlarge-20221108', max_tokens=200, temperature=random.uniform(0.1, 2), k=0, p=0.75, frequency_penalty=0, presence_penalty=0, stop_sequences=[]):
    
    response = co.generate(
      model=model,
      prompt=prompt,
      max_tokens=max_tokens,
      temperature=temperature,
      k=k,
      p=p,
      frequency_penalty=frequency_penalty,
      presence_penalty=presence_penalty,
      stop_sequences=stop_sequences,
      return_likelihoods='NONE')

    def highlight_keywords(text):
        keywords = []
        text = text.lower()
        text = re.sub(r'[^a-z\s]', '', text) # remove punctuation
        text = re.sub(r'\b(the|and|of)\b', '', text) # remove stop words
        words = text.split()
        for word in words:
            if word not in keywords:
                keywords.append(word)
        return keywords

    caption = response.generations[0].text
    keywords = highlight_keywords(caption)
    keywords_string = ', '.join(keywords)

    return caption, keywords_string


 
def img2img( path ,secret_key,design,x_prompt,alt_prompt,strength,guidance_scale,steps):
    # Read the size of the image
############################
 
    img = Image.open(path)
    width, height = img.size
    num_pixels = width * height
    
    # Calculate the maximum number of pixels allowed
    max_pixels = 1048576

    # Calculate the new size of the image, making sure that the number of pixels does not exceed the maximum limit
    if width * height > max_pixels:
        # Calculate the new width and height of the image
        ratio = width / height
        new_width = int(math.sqrt(max_pixels * ratio))
        new_height = int(math.sqrt(max_pixels / ratio))
    else:
        new_width = width
        new_height = height

    # Make sure that either the width or the height of the resized image is a multiple of 64
    if new_width % 64 != 0:
        new_width = ((new_width + 63) // 64) * 64
    if new_height % 64 != 0:
        new_height = ((new_height + 63) // 64) * 64

    # Resize the image
    img = img.resize((new_width, new_height), resample=Image.BILINEAR)

    # Check if the number of pixels in the resized image is within the maximum limit
    # If not, adjust the width and height of the image to bring the number of pixels within the maximum limit
    if new_width * new_height > max_pixels:
        while new_width * new_height > max_pixels:
            new_width -= 1
            new_height = int(max_pixels / new_width)



    
            # Calculate the closest multiple of 64 for each value
            if new_width % 64 != 0:
                new_width = (new_width // 64) * 64
            if new_height % 64 != 0:
                new_height = (new_height // 64) * 64

            # Make sure that the final values are less than the original values
            if new_width > 1407:
                new_width -= 64
            if new_height > 745:
                new_height -= 64
    
    new_height ,new_width
    # Initialize the values
    widthz = new_width
    heightz = new_height

    # Calculate the closest multiple of 64 for each value
    if widthz % 64 != 0:
        widthz = (widthz // 64) * 64
    if heightz % 64 != 0:
        heightz = (heightz // 64) * 64

    # Make sure that the final values are less than the original values
    if widthz > 1407:
        widthz -= 64
    if heightz > 745:
        heightz -= 64

    

    img = img.resize((widthz, heightz), resample=Image.BILINEAR)
 


######################################
    max_attempts = 5 # maximum number of attempts before giving up
    attempts = 0 # current number of attempts
    while attempts < max_attempts:
        try:
            if x_prompt == True:
                prompt = alt_prompt
            else:
                try:
                    caption, keywords = generate_caption_keywords(design)
                    prompt = keywords
                except:
                    prompt = design

            # call the GRPC service to generate the image
            answers = stability_api.generate(
                prompt,
                init_image=img,
                seed=54321,
                start_schedule=strength,
            )
            for resp in answers:
                for artifact in resp.artifacts:
                    if artifact.finish_reason == generation.FILTER:
                        warnings.warn(
                            "Your request activated the API's safety filters and could not be processed."
                            "Please modify the prompt and try again.")
                    if artifact.type == generation.ARTIFACT_IMAGE:
                        img2 = Image.open(io.BytesIO(artifact.binary))
                        img2 = img2.resize((new_width, new_height), resample=Image.BILINEAR)
                        img2.save("new_image.jpg")
                        print(type(img2))
                         
            # if the function reaches this point, it means it succeeded, so we can return the result
                        if secret_key not in os.environ['secretz']:

                          draw = ImageDraw.Draw(img2)

                          # Set the font and text color
                          font = ImageFont.truetype('arial.ttf', 32)
                          text_color = (255, 255, 255)

                          # Get the size of the image
                          width, height = img2.size

                          # Calculate the x and y coordinates for the text
                          text_x = 10
                          text_y = height - 100

                          # Draw the text on the image
                          draw.text((text_x, text_y), 'Please enter secret key to get HD image without \n  watermark', font=font, fill=text_color)

                          # Draw the diagonal lines
                          line_color = (0, 0, 0)
                          draw.line((0, 0) + (width, height), fill=line_color, width=5)
                          draw.line((0, height) + (width, 0), fill=line_color, width=5)

                          # Save the image with the watermark
                          img2.save('image_with_watermark.jpg')
                          img2


            return img2
        except Exception as e:
            # if an exception is thrown, we will increment the attempts counter and try again
            attempts += 1
            print("Attempt {} failed: {}".format(attempts, e))
    # if the function reaches this point, it means the maximum number of attempts has been reached, so we will raise an exception
    raise Exception("Maximum number of attempts reached, unable to generate image")



import gradio as gr  

gr.Interface(img2img,  [gr.Image(source="upload", type="filepath", label="Input Image"),
                        gr.Textbox(label = 'enter secret key to get HD image without watermark , connect with Xhaheen to get key',value = 'secret_santa', type="password" ),
    
                        gr.Dropdown(['interior design of living room', 
                                         'interior design of gaming room',
                                         'interior design of kitchen',
                                         'interior design of bedroom',
                                         'interior design of bathroom',
                                         'interior design of office',
                                         'interior design of meeting room',
                                         'interior design of personal room'],label="Click here to select your design by GPT-3/Cohere Language model",value = 'interior design'), 
                        gr.Checkbox(label="Check Custom design if you already have prompt",value = False),

                        gr.Textbox(label = ' Input custom Prompt Text'),
                        gr.Slider(label='Strength , try with multiple value betweens 0.55 to 0.9 ', minimum = 0, maximum = 1, step = .01, value = .65),
                        gr.Slider(2, 15, value = 7, label = 'Guidence Scale'),
                        gr.Slider(10, 50, value = 50, step = 1, label = 'Number of Iterations')
                        ], 
                        gr.Image(), 
             examples =[['1.png',"xxx",'interior design of living room','False','interior design',0.6,7,50],
                  ['2.png',"xxx",'interior design of hall ','False','interior design',0.7,7,50],
                  ['3.png',"xxx",'interior design of bedroom','False','interior design',0.6,7,50]],
             title = "" +'**Baith-al-suroor بَیتُ الْسرور  🏡🤖**, Transform your space with the power of artificial intelligence. '+ "",
                                    description="Baith al suroor بَیتُ الْسرور  (house of happiness in Arabic)  🏡🤖  is a deeptech app that uses the power of artificial intelligence to transform your space. With the Cohere/GPT3 language   model, it can generate descriptions of your desired design, and the Stable Diffusion algorithm creates relevant images to bring your vision to your thoughts. Give Baith AI a try and see how it can elevate your interior design.--if you want to scale / reaserch / build mobile app / get secret key for research purpose on this space konnect me   @[Xhaheen](https://www.linkedin.com/in/sallu-mandya/)").launch( show_api=False,debug = True)