Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,6 +6,12 @@ import time
|
|
| 6 |
import os
|
| 7 |
import datetime
|
| 8 |
from datetime import datetime
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
print('for update')
|
| 11 |
|
|
@@ -28,7 +34,33 @@ except requests.exceptions.ReadTimeout as e:
|
|
| 28 |
|
| 29 |
prompt_text=str(r.content, 'UTF-8')
|
| 30 |
print(prompt_text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
|
|
|
| 32 |
def infer(prompt,
|
| 33 |
max_length = 250,
|
| 34 |
top_k = 0,
|
|
@@ -100,19 +132,137 @@ def getadline(text_inp):
|
|
| 100 |
print(topic)
|
| 101 |
return(topic)
|
| 102 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
with gr.Blocks() as demo:
|
| 104 |
-
gr.Markdown("<h1><center>
|
| 105 |
gr.Markdown(
|
| 106 |
-
"""ChatGPT based Insights from <a href="https://www.decodem.ai">Decodem.ai</a> for businesses.\nWhile ChatGPT has multiple use cases we have evolved specific use cases/ templates for businesses \n\n This template provides ideas on how a business can
|
| 107 |
)
|
| 108 |
-
textbox = gr.Textbox(placeholder="Enter
|
| 109 |
btn = gr.Button("Generate")
|
| 110 |
#output1 = gr.Textbox(lines=2,label='Market Sizing Framework')
|
| 111 |
-
output_image = gr.components.Image(label="
|
| 112 |
|
| 113 |
|
| 114 |
-
btn.click(
|
| 115 |
-
examples = gr.Examples(examples=['
|
| 116 |
inputs=[textbox])
|
| 117 |
|
| 118 |
|
|
|
|
| 6 |
import os
|
| 7 |
import datetime
|
| 8 |
from datetime import datetime
|
| 9 |
+
from PIL import Image
|
| 10 |
+
from PIL import ImageOps
|
| 11 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 12 |
+
from textwrap import wrap
|
| 13 |
+
import json
|
| 14 |
+
from io import BytesIO
|
| 15 |
|
| 16 |
print('for update')
|
| 17 |
|
|
|
|
| 34 |
|
| 35 |
prompt_text=str(r.content, 'UTF-8')
|
| 36 |
print(prompt_text)
|
| 37 |
+
data={"prompt_type":'ad_image_prompt',"decodem_token":DECODEM_TOKEN}
|
| 38 |
+
try:
|
| 39 |
+
r = requests.post(url_decodemprompts, data=json.dumps(data), headers=headers)
|
| 40 |
+
except requests.exceptions.ReadTimeout as e:
|
| 41 |
+
print(e)
|
| 42 |
+
#print(r.content)
|
| 43 |
+
|
| 44 |
+
prompt_image=str(r.content, 'UTF-8')
|
| 45 |
+
print(prompt_image)
|
| 46 |
+
|
| 47 |
+
ENDPOINT_URL="https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2-1" # url of your endpoint
|
| 48 |
+
#ENDPOINT_URL="https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-1-5" # url of your endpoint
|
| 49 |
+
HF_TOKEN=API_TOKEN# token where you deployed your endpoint
|
| 50 |
+
|
| 51 |
+
def generate_image(prompt_SD:str):
|
| 52 |
+
payload = {"inputs": prompt_SD,}
|
| 53 |
+
headers = {
|
| 54 |
+
"Authorization": f"Bearer {HF_TOKEN}",
|
| 55 |
+
"Content-Type": "application/json",
|
| 56 |
+
"Accept": "image/png" # important to get an image back
|
| 57 |
+
}
|
| 58 |
+
response = r.post(ENDPOINT_URL, headers=headers, json=payload)
|
| 59 |
+
print(response.content)
|
| 60 |
+
img = Image.open(BytesIO(response.content))
|
| 61 |
|
| 62 |
+
return img
|
| 63 |
+
|
| 64 |
def infer(prompt,
|
| 65 |
max_length = 250,
|
| 66 |
top_k = 0,
|
|
|
|
| 132 |
print(topic)
|
| 133 |
return(topic)
|
| 134 |
|
| 135 |
+
def getadvertisement(topic):
|
| 136 |
+
input_keyword=topic
|
| 137 |
+
backdrop=['surrounded by water droplets','in front of a brick wall','in front of a wooden wall','in a white boho style studio','with nature backdrop','with water splash','laying on a wooden table',]
|
| 138 |
+
whichitem=random.randint(0,len(backdrop)-1)
|
| 139 |
+
prompt_SD='product photograph of '+input_keyword+' '+backdrop[whichitem]+prompt_image
|
| 140 |
+
# generate image
|
| 141 |
+
image = generate_image(prompt_SD)
|
| 142 |
+
|
| 143 |
+
# save to disk
|
| 144 |
+
image.save("generation.png")
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
# Set the font to be used
|
| 148 |
+
req = requests.get("https://github.com/openmaptiles/fonts/raw/master/roboto/Roboto-Regular.ttf")
|
| 149 |
+
|
| 150 |
+
FONT_USER_INFO = ImageFont.truetype(BytesIO(req.content), 75, encoding="utf-8")
|
| 151 |
+
FONT_TEXT = ImageFont.truetype(BytesIO(req.content), 75, encoding="utf-8")
|
| 152 |
+
TITLE_TEXT = ImageFont.truetype(BytesIO(req.content), 75, encoding="utf-8")
|
| 153 |
+
|
| 154 |
+
#FONT_USER_INFO = ImageFont.load_default()
|
| 155 |
+
#FONT_TEXT = ImageFont.load_default()
|
| 156 |
+
|
| 157 |
+
# Image dimensions (pixels)
|
| 158 |
+
WIDTH = 768
|
| 159 |
+
HEIGHT = 768
|
| 160 |
+
# Color scheme
|
| 161 |
+
COLOR_BG = 'white'
|
| 162 |
+
COLOR_NAME = 'black'
|
| 163 |
+
COLOR_TAG = (64, 64, 64)
|
| 164 |
+
COLOR_TEXT = 'black'
|
| 165 |
+
# Write coordinates
|
| 166 |
+
COORD_PHOTO = (10, 40)
|
| 167 |
+
COORD_NAME = (10, 200)
|
| 168 |
+
COORD_TAG = (10, 280)
|
| 169 |
+
COORD_TEXT = (10, 128)
|
| 170 |
+
# Extra space to add in between lines of text
|
| 171 |
+
LINE_MARGIN = 5
|
| 172 |
+
# -----------------------------------------------------------------------------
|
| 173 |
+
|
| 174 |
+
# Information for the image
|
| 175 |
+
# -----------------------------------------------------------------------------
|
| 176 |
+
text = getadline(input_keyword)
|
| 177 |
+
print(text)
|
| 178 |
+
img_name = "textimage"
|
| 179 |
+
# -----------------------------------------------------------------------------
|
| 180 |
+
|
| 181 |
+
# Setup of variables and calculations
|
| 182 |
+
# -----------------------------------------------------------------------------
|
| 183 |
+
# Break the text string into smaller strings, each having a maximum of 37\
|
| 184 |
+
# characters (a.k.a. create the lines of text for the image)
|
| 185 |
+
text_string_lines = wrap(text, 10)
|
| 186 |
+
|
| 187 |
+
# Horizontal position at which to start drawing each line of the tweet body
|
| 188 |
+
x = COORD_TEXT[0]
|
| 189 |
+
|
| 190 |
+
# Current vertical position of drawing (starts as the first vertical drawing\
|
| 191 |
+
# position of the tweet body)
|
| 192 |
+
y = COORD_TEXT[1]
|
| 193 |
+
|
| 194 |
+
# Create an Image object to be used as a means of extracting the height needed\
|
| 195 |
+
# to draw each line of text
|
| 196 |
+
temp_img = Image.new('RGB', (0, 0))
|
| 197 |
+
temp_img_draw_interf = ImageDraw.Draw(temp_img)
|
| 198 |
+
|
| 199 |
+
# List with the height (pixels) needed to draw each line of the tweet body
|
| 200 |
+
# Loop through each line of text, and extract the height needed to draw it,\
|
| 201 |
+
# using our font settings
|
| 202 |
+
line_height = [
|
| 203 |
+
temp_img_draw_interf.textsize(text_string_lines[i], font=FONT_TEXT )[1]
|
| 204 |
+
for i in range(len(text_string_lines))
|
| 205 |
+
]
|
| 206 |
+
# -----------------------------------------------------------------------------
|
| 207 |
+
|
| 208 |
+
# Image creation
|
| 209 |
+
# -----------------------------------------------------------------------------
|
| 210 |
+
# Create what will be the final image
|
| 211 |
+
img_final = Image.new('RGB', (WIDTH, HEIGHT), color='white')
|
| 212 |
+
# Create the drawing interface
|
| 213 |
+
draw_interf = ImageDraw.Draw(img_final)
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
# Draw each line of the tweet body. To find the height at which the next\
|
| 217 |
+
# line will be drawn, add the line height of the next line to the current\
|
| 218 |
+
# y position, along with a small margin
|
| 219 |
+
for index, line in enumerate(text_string_lines):
|
| 220 |
+
# Draw a line of text
|
| 221 |
+
draw_interf.text((x, y), line, font=FONT_USER_INFO, fill=COLOR_TEXT)
|
| 222 |
+
# Increment y to draw the next line at the adequate height
|
| 223 |
+
y += line_height[index] + LINE_MARGIN
|
| 224 |
+
|
| 225 |
+
# Load the user photo (read-mode). It should be a 250x250 circle
|
| 226 |
+
#user_photo = Image.open('userprofilepic.png', 'r').convert("RGBA")
|
| 227 |
+
|
| 228 |
+
# Paste the user photo into the working image. We also use the photo for\
|
| 229 |
+
# its own mask to keep the photo's transparencies
|
| 230 |
+
#img_final.paste(user_photo, COORD_PHOTO, mask=user_photo)
|
| 231 |
+
|
| 232 |
+
# Finally, save the created image
|
| 233 |
+
img_final.save(f'{img_name}.png')
|
| 234 |
+
# -----------------------------------------------------------------------------
|
| 235 |
+
|
| 236 |
+
im = Image.open(img_name+".png")
|
| 237 |
+
width_orig, height_orig = im.size
|
| 238 |
+
print(width_orig, height_orig)
|
| 239 |
+
|
| 240 |
+
im_bar = Image.open("generation.png")
|
| 241 |
+
width_orig_x, height_orig_x = im_bar.size
|
| 242 |
+
print(width_orig_x, height_orig_x)
|
| 243 |
+
|
| 244 |
+
im_bar = im_bar.resize((int(width_orig / 1), int(height_orig / 1)))
|
| 245 |
+
new_im = Image.new('RGB', (2*im.size[0],1*im_bar.size[1]), (250,250,250))
|
| 246 |
+
|
| 247 |
+
new_im.paste(im, (0,0))
|
| 248 |
+
new_im.paste(im_bar, (im.size[0],0))
|
| 249 |
+
new_im.save('finalimage.png')
|
| 250 |
+
return 'finalimage.png'
|
| 251 |
+
|
| 252 |
+
|
| 253 |
with gr.Blocks() as demo:
|
| 254 |
+
gr.Markdown("<h1><center>Ad for Your Business</center></h1>")
|
| 255 |
gr.Markdown(
|
| 256 |
+
"""ChatGPT based Insights from <a href="https://www.decodem.ai">Decodem.ai</a> for businesses.\nWhile ChatGPT has multiple use cases we have evolved specific use cases/ templates for businesses \n\n This template provides ideas on how a business can generate Advertisement ideas for a product. Enter a product area to size and get the results. Use examples as a guide. We use a equally powerful AI model bigscience/bloom."""
|
| 257 |
)
|
| 258 |
+
textbox = gr.Textbox(placeholder="Enter product name...", lines=1,label='Your product')
|
| 259 |
btn = gr.Button("Generate")
|
| 260 |
#output1 = gr.Textbox(lines=2,label='Market Sizing Framework')
|
| 261 |
+
output_image = gr.components.Image(label="Your Advertisement")
|
| 262 |
|
| 263 |
|
| 264 |
+
btn.click(getadvertisement,inputs=[textbox], outputs=[output_image])
|
| 265 |
+
examples = gr.Examples(examples=['spectacles','rice cooker','smart watch','coffee mug',],
|
| 266 |
inputs=[textbox])
|
| 267 |
|
| 268 |
|