Xhaheen commited on
Commit
ef8d465
1 Parent(s): e6aa4f7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +118 -0
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ os.environ['STABILITY_HOST'] = 'grpc.stability.ai:443'
3
+ STABILITY_KEY = os.environ["STABILITY_KEY"]
4
+
5
+ import io
6
+ import os
7
+ import warnings
8
+
9
+ from IPython.display import display
10
+ from PIL import Image
11
+ from stability_sdk import client
12
+ import stability_sdk.interfaces.gooseai.generation.generation_pb2 as generation
13
+ from PIL import Image
14
+
15
+ stability_api = client.StabilityInference(
16
+ key=os.environ['STABILITY_KEY'],
17
+ verbose=True,
18
+ )
19
+
20
+
21
+ 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=[]):
22
+
23
+ response = co.generate(
24
+ model=model,
25
+ prompt=prompt,
26
+ max_tokens=max_tokens,
27
+ temperature=temperature,
28
+ k=k,
29
+ p=p,
30
+ frequency_penalty=frequency_penalty,
31
+ presence_penalty=presence_penalty,
32
+ stop_sequences=stop_sequences,
33
+ return_likelihoods='NONE')
34
+
35
+ def highlight_keywords(text):
36
+ keywords = []
37
+ text = text.lower()
38
+ text = re.sub(r'[^a-z\s]', '', text) # remove punctuation
39
+ text = re.sub(r'\b(the|and|of)\b', '', text) # remove stop words
40
+ words = text.split()
41
+ for word in words:
42
+ if word not in keywords:
43
+ keywords.append(word)
44
+ return keywords
45
+
46
+ caption = response.generations[0].text
47
+ keywords = highlight_keywords(caption)
48
+ keywords_string = ', '.join(keywords)
49
+
50
+ return caption, keywords_string
51
+
52
+
53
+ def img2img( path ,is_HD,design,x_prompt,alt_prompt,strength,guidance_scale,steps):
54
+
55
+
56
+
57
+ img = Image.open(path)
58
+
59
+ try:
60
+ caption, keywords = generate_caption_keywords(design)
61
+ prompt = keywords
62
+ except:
63
+
64
+ prompt = design
65
+
66
+
67
+
68
+ if x_prompt == True:
69
+
70
+ prompt=alt_prompt
71
+
72
+
73
+ answers = stability_api.generate(
74
+
75
+ prompt,
76
+
77
+ init_image=img,
78
+ seed=54321, # if we're passing in an image generated by SD, you may get better results by providing a different seed value than was used to generate the image
79
+ start_schedule=strength, # this controls the "strength" of the prompt relative to the init image
80
+ )
81
+
82
+ # iterating over the generator produces the api response
83
+ for resp in answers:
84
+ for artifact in resp.artifacts:
85
+ if artifact.finish_reason == generation.FILTER:
86
+ warnings.warn(
87
+ "Your request activated the API's safety filters and could not be processed."
88
+ "Please modify the prompt and try again.")
89
+ if artifact.type == generation.ARTIFACT_IMAGE:
90
+ img2 = Image.open(io.BytesIO(artifact.binary))
91
+ im1 = img2.save("new_image.jpg")
92
+
93
+ print(type(img2))
94
+ return img2
95
+
96
+
97
+
98
+ import gradio as gr
99
+
100
+ gr.Interface(img2img, [gr.Image(source="upload", type="filepath", label="Input Image"),
101
+ gr.Checkbox(label="Click HD to get HD output(Not working in HF spaces ,contact for Colab)",value = False),
102
+ gr.Dropdown(['interior design of living room',
103
+ 'interior design of gaming room',
104
+ 'interior design of kitchen',
105
+ 'interior design of bedroom',
106
+ 'interior design of bathroom',
107
+ 'interior design of office',
108
+ 'interior design of meeting room',
109
+ 'interior design of personal room'],label="Click here to select your design",value = 'interior design'),
110
+ gr.Checkbox(label="Custom design",value = True),
111
+
112
+ gr.Textbox(label = ' Input custom Prompt Text'),
113
+ gr.Slider(label='Strength', minimum = 0, maximum = 1, step = .01, value = .65),
114
+ gr.Slider(2, 15, value = 7, label = 'Guidence Scale'),
115
+ gr.Slider(10, 50, value = 50, step = 1, label = 'Number of Iterations')
116
+ ],
117
+ gr.Image(), title = "" +'Baith ﺑَﻴﺖ 🏡💡🤖, Transform your space with the power of artificial intelligence. '+ "",
118
+ description="Baith AI ﺑَﻴﺖ 🏡💡🤖 is an App that uses the power of artificial intelligence to transform your space. With the Cohere language Command model, it can generate descriptions of your desired design, and the Stable Diffusion algorithm creates relevant images to bring your vision to life. Give Baith AI a try and see how it can elevate your interior design.--if you want to scale or reaserch this space konnect me @[here](https://www.linkedin.com/in/sallu-mandya/)").launch(share = True, debug = True)