webnowa commited on
Commit
cf19e19
·
verified ·
1 Parent(s): 10a81a9

Update api.py

Browse files
Files changed (1) hide show
  1. api.py +130 -9
api.py CHANGED
@@ -1,10 +1,14 @@
1
  from fastapi import FastAPI, UploadFile, File
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from diffusers import AutoPipelineForText2Image
4
- import torch
5
  from PIL import Image
 
6
  import io
 
7
  from deep_translator import GoogleTranslator
 
 
 
8
 
9
  app = FastAPI()
10
 
@@ -16,19 +20,46 @@ app.add_middleware(
16
  allow_headers=["*"],
17
  )
18
 
 
 
 
 
19
  pipe = AutoPipelineForText2Image.from_pretrained(
20
  "stabilityai/sdxl-turbo",
21
- torch_dtype=torch.float16,
22
- variant="fp16"
23
  )
24
 
25
- pipe.to("cuda" if torch.cuda.is_available() else "cpu")
 
 
 
 
 
 
 
26
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
 
28
  @app.post("/generate")
29
- async def generate(prompt: str):
30
 
31
- prompt_en = GoogleTranslator(source='auto', target='en').translate(prompt)
 
 
 
 
32
 
33
  image = pipe(
34
  prompt_en,
@@ -36,7 +67,97 @@ async def generate(prompt: str):
36
  guidance_scale=0.0
37
  ).images[0]
38
 
39
- buf = io.BytesIO()
40
- image.save(buf, format="PNG")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
- return {"image": buf.getvalue()}
 
1
  from fastapi import FastAPI, UploadFile, File
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from diffusers import AutoPipelineForText2Image
 
4
  from PIL import Image
5
+ import torch
6
  import io
7
+ import base64
8
  from deep_translator import GoogleTranslator
9
+ from rembg import remove
10
+ import numpy as np
11
+ import cv2
12
 
13
  app = FastAPI()
14
 
 
20
  allow_headers=["*"],
21
  )
22
 
23
+ device = "cuda" if torch.cuda.is_available() else "cpu"
24
+
25
+ print("Loading SDXL Turbo...")
26
+
27
  pipe = AutoPipelineForText2Image.from_pretrained(
28
  "stabilityai/sdxl-turbo",
29
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
 
30
  )
31
 
32
+ pipe = pipe.to(device)
33
+
34
+
35
+ def img_to_base64(img):
36
+ buf = io.BytesIO()
37
+ img.save(buf, format="PNG")
38
+ return base64.b64encode(buf.getvalue()).decode()
39
+
40
 
41
+ def base64_to_img(data):
42
+ return Image.open(io.BytesIO(base64.b64decode(data)))
43
+
44
+
45
+ def translate_prompt(prompt):
46
+ try:
47
+ return GoogleTranslator(source="auto", target="en").translate(prompt)
48
+ except:
49
+ return prompt
50
+
51
+
52
+ # -----------------------
53
+ # GENERATE IMAGE
54
+ # -----------------------
55
 
56
  @app.post("/generate")
 
57
 
58
+ async def generate(data: dict):
59
+
60
+ prompt = data.get("prompt", "")
61
+
62
+ prompt_en = translate_prompt(prompt)
63
 
64
  image = pipe(
65
  prompt_en,
 
67
  guidance_scale=0.0
68
  ).images[0]
69
 
70
+ return {"image": img_to_base64(image)}
71
+
72
+
73
+ # -----------------------
74
+ # PRODUCT IMAGE
75
+ # -----------------------
76
+
77
+ @app.post("/product")
78
+
79
+ async def product(data: dict):
80
+
81
+ prompt = data.get("prompt", "")
82
+
83
+ prompt_en = translate_prompt(prompt)
84
+
85
+ prompt_en += ", product photography, studio lighting, white background"
86
+
87
+ image = pipe(
88
+ prompt_en,
89
+ num_inference_steps=2,
90
+ guidance_scale=0.0
91
+ ).images[0]
92
+
93
+ return {"image": img_to_base64(image)}
94
+
95
+
96
+ # -----------------------
97
+ # UPSCALE
98
+ # -----------------------
99
+
100
+ @app.post("/upscale")
101
+
102
+ async def upscale(file: UploadFile = File(...)):
103
+
104
+ image = Image.open(file.file).convert("RGB")
105
+
106
+ w, h = image.size
107
+
108
+ image = image.resize((w * 2, h * 2), Image.LANCZOS)
109
+
110
+ return {"image": img_to_base64(image)}
111
+
112
+
113
+ # -----------------------
114
+ # RESTORE
115
+ # -----------------------
116
+
117
+ @app.post("/restore")
118
+
119
+ async def restore(file: UploadFile = File(...)):
120
+
121
+ image = Image.open(file.file).convert("RGB")
122
+
123
+ img = np.array(image)
124
+
125
+ img = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)
126
+
127
+ result = Image.fromarray(img)
128
+
129
+ return {"image": img_to_base64(result)}
130
+
131
+
132
+ # -----------------------
133
+ # COLORIZE
134
+ # -----------------------
135
+
136
+ @app.post("/colorize")
137
+
138
+ async def colorize(file: UploadFile = File(...)):
139
+
140
+ image = Image.open(file.file).convert("L")
141
+
142
+ img = np.array(image)
143
+
144
+ color = cv2.applyColorMap(img, cv2.COLORMAP_JET)
145
+
146
+ result = Image.fromarray(color)
147
+
148
+ return {"image": img_to_base64(result)}
149
+
150
+
151
+ # -----------------------
152
+ # REMOVE BACKGROUND
153
+ # -----------------------
154
+
155
+ @app.post("/removebg")
156
+
157
+ async def removebg(file: UploadFile = File(...)):
158
+
159
+ image = Image.open(file.file)
160
+
161
+ result = remove(image)
162
 
163
+ return {"image": img_to_base64(result)}