Update api.py
Browse files123456789105 unmodified lines161718192021222324252627282930313233341 unmodified line36373839404142from fastapi import FastAPI, UploadFile, Filefrom fastapi.middleware.cors import CORSMiddlewarefrom diffusers import AutoPipelineForText2Imageimport torchfrom PIL import Imageimport iofrom deep_translator import GoogleTranslatorapp = FastAPI()5 unmodified linesallow_headers=["*"],)pipe = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo",torch_dtype=torch.float16,variant="fp16")pipe.to("cuda" if torch.cuda.is_available() else "cpu")@app.post("/generate")async def generate(prompt: str):prompt_en = GoogleTranslator(source='auto', target='en').translate(prompt)image = pipe(prompt_en,1 unmodified lineguidance_scale=0.0).images[0]buf = io.BytesIO()image.save(buf, format="PNG")return {"image": buf.getvalue()}12345678910111213145 unmodified lines202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364651 unmodified line676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163from fastapi import FastAPI, UploadFile, Filefrom fastapi.middleware.cors import CORSMiddlewarefrom diffusers import AutoPipelineForText2Imagefrom PIL import Imageimport torchimport ioimport base64from deep_translator import GoogleTranslatorfrom rembg import removeimport numpy as npimport cv2app = FastAPI()5 unmodified linesallow_headers=["*"],)device = "cuda" if torch.cuda.is_available() else "cpu"print("Loading SDXL Turbo...")pipe = AutoPipelineForText2Image.from_pretrained("stabilityai/sdxl-turbo",torch_dtype=torch.float16 if device == "cuda" else torch.float32,)pipe = pipe.to(device)def img_to_base64(img):buf = io.BytesIO()img.save(buf, format="PNG")return base64.b64encode(buf.getvalue()).decode()def base64_to_img(data):return Image.open(io.BytesIO(base64.b64decode(data)))def translate_prompt(prompt):try:return GoogleTranslator(source="auto", target="en").translate(prompt)except:return prompt# -----------------------# GENERATE IMAGE# -----------------------@app.post("/generate")async def generate(data: dict):prompt = data.get("prompt", "")prompt_en = translate_prompt(prompt)image = pipe(prompt_en,1 unmodified lineguidance_scale=0.0).images[0]return {"image": img_to_base64(image)}# -----------------------# PRODUCT IMAGE# -----------------------@app.post("/product")async def product(data: dict):prompt = data.get("prompt", "")prompt_en = translate_prompt(prompt)prompt_en += ", product photography, studio lighting, white background"image = pipe(prompt_en,num_inference_steps=2,guidance_scale=0.0).images[0]return {"image": img_to_base64(image)}# -----------------------# UPSCALE# -----------------------@app.post("/upscale")async def upscale(file: UploadFile = File(...)):image = Image.open(file.file).convert("RGB")w, h = image.sizeimage = image.resize((w * 2, h * 2), Image.LANCZOS)return {"image": img_to_base64(image)}# -----------------------# RESTORE# -----------------------@app.post("/restore")async def restore(file: UploadFile = File(...)):image = Image.open(file.file).convert("RGB")img = np.array(image)img = cv2.fastNlMeansDenoisingColored(img, None, 10, 10, 7, 21)result = Image.fromarray(img)return {"image": img_to_base64(result)}# -----------------------# COLORIZE# -----------------------@app.post("/colorize")async def colorize(file: UploadFile = File(...)):image = Image.open(file.file).convert("L")img = np.array(image)color = cv2.applyColorMap(img, cv2.COLORMAP_JET)result = Image.fromarray(color)return {"image": img_to_base64(result)}# -----------------------# REMOVE BACKGROUND# -----------------------@app.post("/removebg")async def removebg(file: UploadFile = File(...)):image = Image.open(file.file)result = remove(image)return {"image": img_to_base64(result)}
api.py+130-9