Update api.py
Browse files4 unmodified lines5678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091922 unmodified lines95969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351365 unmodified lines1421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652664 unmodified linesfrom PIL import Image, ImageFilterfrom deep_translator import GoogleTranslatorprint("IMAGE PRO AI STARTING...")CACHE_DIR = "./models"OUTPUT_DIR = "outputs"os.makedirs(OUTPUT_DIR, exist_ok=True)# ===============================# TRANSLATOR# ===============================def translate_prompt(prompt):try:prompt_en = GoogleTranslator(source="pl", target="en").translate(prompt)print("PL:", prompt)print("EN:", prompt_en)return prompt_enexcept:return prompt# ===============================# LOAD MODEL# ===============================print("Loading model...")pipe = StableDiffusionPipeline.from_pretrained("SG161222/Realistic_Vision_V5.1_noVAE",cache_dir=CACHE_DIR,torch_dtype=torch.float32,safety_checker=None)pipe.enable_attention_slicing()print("MODEL READY")# ===============================# FLASK# ===============================app = Flask(__name__)# ===============================# HTML PANEL# ===============================HTML = """<html><head><title>IMAGE PRO AI</title><style>body{background:black;color:white;font-family:Arial;text-align:center;}input{width:400px;padding:10px;}button{padding:10px 20px;background:orange;border:none;color:white;font-size:16px;cursor:pointer;}img{margin-top:20px;max-width:512px;}</style>2 unmodified lines<body><h1>IMAGE PRO AI</h1><input id="prompt" placeholder="np. truskawka na stole"/><br><br><button onclick="generate()">GENERUJ</button><h2>Wynik</h2><img id="result"/><script>async function generate(){prompt = document.getElementById("prompt").valueresponse = await fetch("/generate",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({prompt:prompt})})blob = await response.blob()url = URL.createObjectURL(blob)document.getElementById("result").src = url}5 unmodified lines"""# ===============================# HOME# ===============================@app.route("/")def home():return render_template_string(HTML)# ===============================# GENERATE IMAGE# ===============================@app.route("/generate", methods=["POST"])def generate():data = request.jsonprompt = translate_prompt(data["prompt"])image = pipe(prompt,num_inference_steps=30,guidance_scale=7.5).images[0]path = OUTPUT_DIR + "/gen.png"image.save(path)return send_file(path, mimetype="image/png")# ===============================# PRODUCT AD# ===============================@app.route("/product_ad", methods=["POST"])def product_ad():data = request.jsonprompt = translate_prompt(data["prompt"])style = prompt + ", luxury product photography"image = pipe(style).images[0]path = OUTPUT_DIR + "/product.png"image.save(path)return send_file(path, mimetype="image/png")# ===============================# RESTORE PHOTO# ===============================@app.route("/restore", methods=["POST"])def restore():file = request.files["image"]img = Image.open(file.stream)img = img.filter(ImageFilter.SHARPEN)path = OUTPUT_DIR + "/restored.png"img.save(path)return send_file(path, mimetype="image/png")# ===============================# UPSCALE# ===============================@app.route("/upscale", methods=["POST"])def upscale():file = request.files["image"]img = Image.open(file.stream)w,h = img.sizeimg = img.resize((w*2,h*2), Image.LANCZOS)path = OUTPUT_DIR + "/hd.png"img.save(path)return send_file(path, mimetype="image/png")# ===============================# COLORIZE# ===============================@app.route("/colorize", methods=["POST"])def colorize():file = request.files["image"]img = Image.open(file.stream)img = img.convert("RGB")path = OUTPUT_DIR + "/color.png"img.save(path)return send_file(path, mimetype="image/png")# ===============================# START SERVER# ===============================if __name__ == "__main__":print("SERVER READY")app.run(host="0.0.0.0",port=7860)4 unmodified lines567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991002 unmodified lines1031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532545 unmodified lines2602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234 unmodified linesfrom PIL import Image, ImageFilterfrom deep_translator import GoogleTranslatorprint("IMAGE PRO AI STUDIO MAX STARTING")CACHE_DIR="./models"OUTPUT_DIR="outputs"os.makedirs(OUTPUT_DIR,exist_ok=True)# =====================================# TRANSLATOR# =====================================def translate(prompt):try:return GoogleTranslator(source="pl",target="en").translate(prompt)except:return prompt# =====================================# LOAD MODEL# =====================================print("Loading model...")pipe=StableDiffusionPipeline.from_pretrained("SG161222/Realistic_Vision_V5.1_noVAE",cache_dir=CACHE_DIR,torch_dtype=torch.float32,safety_checker=None)pipe.enable_attention_slicing()print("MODEL READY")# =====================================# FLASK# =====================================app=Flask(__name__)# =====================================# HTML UI# =====================================HTML="""<html><head><title>IMAGE PRO AI STUDIO MAX</title><style>body{background:#0b0b0b;color:white;font-family:Arial;text-align:center;}h1{font-size:42px;}input{width:420px;padding:12px;font-size:16px;}button{padding:12px 22px;margin:4px;background:#ff6a00;border:none;color:white;font-size:15px;cursor:pointer;}img{max-width:512px;margin-top:20px;}#gallery img{width:200px;margin:10px;}</style>2 unmodified lines<body><h1>IMAGE PRO AI STUDIO MAX</h1><input id="prompt" placeholder="np luksusowa reklama perfum"><br><br><button onclick="generate()">IMAGE</button><button onclick="product()">PRODUCT</button><button onclick="logo()">LOGO</button><button onclick="banner()">BANNER</button><button onclick="social()">SOCIAL</button><br><br><input type="file" id="upload"><br><button onclick="restore()">RESTORE</button><button onclick="upscale()">UPSCALE</button><button onclick="colorize()">COLORIZE</button><h2>RESULT</h2><img id="result"><h2>GALLERY</h2><div id="gallery"></div><script>function show(blob){url=URL.createObjectURL(blob)document.getElementById("result").src=urlimg=document.createElement("img")img.src=urldocument.getElementById("gallery").appendChild(img)}async function generate(){prompt=document.getElementById("prompt").valuer=await fetch("/generate",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({prompt:prompt})})show(await r.blob())}async function product(){prompt=document.getElementById("prompt").valuer=await fetch("/product",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({prompt:prompt})})show(await r.blob())}async function logo(){prompt=document.getElementById("prompt").valuer=await fetch("/logo",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({prompt:prompt})})show(await r.blob())}async function banner(){prompt=document.getElementById("prompt").valuer=await fetch("/banner",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({prompt:prompt})})show(await r.blob())}async function social(){prompt=document.getElementById("prompt").valuer=await fetch("/social",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({prompt:prompt})})show(await r.blob())}async function restore(){file=document.getElementById("upload").files[0]f=new FormData()f.append("image",file)r=await fetch("/restore",{method:"POST",body:f})show(await r.blob())}async function upscale(){file=document.getElementById("upload").files[0]f=new FormData()f.append("image",file)r=await fetch("/upscale",{method:"POST",body:f})show(await r.blob())}async function colorize(){file=document.getElementById("upload").files[0]f=new FormData()f.append("image",file)r=await fetch("/colorize",{method:"POST",body:f})show(await r.blob())}5 unmodified lines"""# =====================================# HOME# =====================================@app.route("/")def home():return render_template_string(HTML)# =====================================# GENERATE# =====================================@app.route("/generate",methods=["POST"])def generate():prompt=translate(request.json["prompt"])img=pipe(prompt).images[0]path=OUTPUT_DIR+"/image.png"img.save(path)return send_file(path,mimetype="image/png")# =====================================# PRODUCT PHOTO# =====================================@app.route("/product",methods=["POST"])def product():prompt=translate(request.json["prompt"])prompt+=", luxury product photography"img=pipe(prompt).images[0]path=OUTPUT_DIR+"/product.png"img.save(path)return send_file(path,mimetype="image/png")# =====================================# LOGO# =====================================@app.route("/logo",methods=["POST"])def logo():prompt=translate(request.json["prompt"])prompt+=", modern vector logo"img=pipe(prompt).images[0]path=OUTPUT_DIR+"/logo.png"img.save(path)return send_file(path,mimetype="image/png")# =====================================# BANNER# =====================================@app.route("/banner",methods=["POST"])def banner():prompt=translate(request.json["prompt"])prompt+=", marketing banner"img=pipe(prompt).images[0]path=OUTPUT_DIR+"/banner.png"img.save(path)return send_file(path,mimetype="image/png")# =====================================# SOCIAL MEDIA# =====================================@app.route("/social",methods=["POST"])def social():prompt=translate(request.json["prompt"])prompt+=", instagram ad design"img=pipe(prompt).images[0]path=OUTPUT_DIR+"/social.png"img.save(path)return send_file(path,mimetype="image/png")# =====================================# RESTORE# =====================================@app.route("/restore",methods=["POST"])def restore():img=Image.open(request.files["image"].stream)img=img.filter(ImageFilter.SHARPEN)path=OUTPUT_DIR+"/restore.png"img.save(path)return send_file(path,mimetype="image/png")# =====================================# UPSCALE# =====================================@app.route("/upscale",methods=["POST"])def upscale():img=Image.open(request.files["image"].stream)w,h=img.sizeimg=img.resize((w*2,h*2),Image.LANCZOS)path=OUTPUT_DIR+"/upscale.png"img.save(path)return send_file(path,mimetype="image/png")# =====================================# COLORIZE# =====================================@app.route("/colorize",methods=["POST"])def colorize():img=Image.open(request.files["image"].stream)img=img.convert("RGB")path=OUTPUT_DIR+"/color.png"img.save(path)return send_file(path,mimetype="image/png")# =====================================# START# =====================================if __name__=="__main__":app.run(host="0.0.0.0",port=7860)
api.py+275-118