Tinsae commited on
Commit
945deda
·
1 Parent(s): 6577ea5
Files changed (2) hide show
  1. app.py +73 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from PIL import Image
2
+ import numpy as np
3
+ from rembg import remove
4
+ import cv2
5
+ import os
6
+ from torchvision.transforms import GaussianBlur
7
+ import replicate
8
+ import gradio as gr
9
+ import requests
10
+
11
+ os.environ["API_TOKEN"]
12
+
13
+ model = replicate.models.get("cjwbw/stable-diffusion-v2-inpainting")
14
+ version = model.versions.get("f9bb0632bfdceb83196e85521b9b55895f8ff3d1d3b487fd1973210c0eb30bec")
15
+
16
+ def generate_image(input, prompt):
17
+ input_path = 'input.png'
18
+ output_path = 'output.png'
19
+
20
+ #input = Image.open(input_path)
21
+ input = input.resize((512, 512))
22
+ input.save(input_path)
23
+ bg_removed = remove(input)
24
+
25
+ img2_grayscale = bg_removed.convert('L')
26
+ img2_a = np.array(img2_grayscale)
27
+
28
+ mask = np.array(img2_grayscale)
29
+ threshhold = 0
30
+ mask[img2_a==threshhold] = 1 # this is white
31
+ mask[img2_a>threshhold] = 0 # this is gray
32
+ #The mask structure is white for inpainting and black for keeping as is
33
+
34
+ strength = 1 # This controls the strength of our prompt relative to the init image.
35
+ seed = 123
36
+
37
+ d = int(255 * (1-strength))
38
+ mask *= 255-d # Converts our range from [0,1] to [0,255]
39
+ mask += d
40
+
41
+ mask = Image.fromarray(mask)
42
+
43
+ blur = GaussianBlur(11,20)
44
+ mask = blur(mask)
45
+ mask.save("blured_mask.png")
46
+
47
+ url = version.predict(prompt=prompt, image=open(input_path,"rb"), mask=open("blured_mask.png","rb"))[0]
48
+ response = requests.get(url)
49
+
50
+ with open('output.png', 'wb') as f:
51
+ f.write(response.content)
52
+
53
+ return Image.open('output.png')
54
+
55
+ with gr.Blocks() as demo:
56
+ gr.Markdown("# Advertise better with AI")
57
+ with gr.Row():
58
+
59
+ with gr.Column():
60
+ input_image = gr.Image(label = "Upload your product's photo", type = 'pil')
61
+
62
+ target_name = gr.Textbox(label="Write your prompt here")
63
+ # result_prompt = product_name + ' in ' + target_name + 'product photograpy ultrarealist'
64
+
65
+ image_button = gr.Button("Generate")
66
+
67
+ with gr.Column():
68
+ image_output = gr.Image()
69
+
70
+ image_button.click(generate_image, inputs=[input_image, target_name ], outputs=image_output)
71
+
72
+
73
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ rembg
2
+ replicate