Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +13 -0
- app.py +116 -0
- requirements.txt +6 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11
|
| 2 |
+
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 8 |
+
|
| 9 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 10 |
+
|
| 11 |
+
COPY --chown=user . /app
|
| 12 |
+
|
| 13 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import re
|
| 3 |
+
import os
|
| 4 |
+
import random
|
| 5 |
+
import requests
|
| 6 |
+
import time
|
| 7 |
+
from PIL import Image
|
| 8 |
+
from io import BytesIO
|
| 9 |
+
from typing import Tuple
|
| 10 |
+
from diffusers import DiffusionPipeline
|
| 11 |
+
import torch
|
| 12 |
+
from datasets import load_dataset
|
| 13 |
+
from dotenv import load_dotenv
|
| 14 |
+
from flask import Flask, request, jsonify
|
| 15 |
+
app = Flask(__name__)
|
| 16 |
+
|
| 17 |
+
style_list = [
|
| 18 |
+
{
|
| 19 |
+
"name": "(No style)",
|
| 20 |
+
"prompt": "{prompt}",
|
| 21 |
+
"negative_prompt": "",
|
| 22 |
+
},
|
| 23 |
+
{
|
| 24 |
+
"name": "Cinematic",
|
| 25 |
+
"prompt": "cinematic still {prompt} . emotional, harmonious, vignette, highly detailed, high budget, bokeh, cinemascope, moody, epic, gorgeous, film grain, grainy",
|
| 26 |
+
"negative_prompt": "anime, cartoon, graphic, text, painting, crayon, graphite, abstract, glitch, deformed, mutated, ugly, disfigured",
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"name": "Photographic",
|
| 30 |
+
"prompt": "cinematic photo {prompt} . 35mm photograph, film, bokeh, professional, 4k, highly detailed",
|
| 31 |
+
"negative_prompt": "drawing, painting, crayon, sketch, graphite, impressionist, noisy, blurry, soft, deformed, ugly",
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
"name": "Anime",
|
| 35 |
+
"prompt": "anime artwork {prompt} . anime style, key visual, vibrant, studio anime, highly detailed",
|
| 36 |
+
"negative_prompt": "photo, deformed, black and white, realism, disfigured, low contrast",
|
| 37 |
+
},
|
| 38 |
+
{
|
| 39 |
+
"name": "Manga",
|
| 40 |
+
"prompt": "manga style {prompt} . vibrant, high-energy, detailed, iconic, Japanese comic style",
|
| 41 |
+
"negative_prompt": "ugly, deformed, noisy, blurry, low contrast, realism, photorealistic, Western comic style",
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"name": "Digital Art",
|
| 45 |
+
"prompt": "concept art {prompt} . digital artwork, illustrative, painterly, matte painting, highly detailed",
|
| 46 |
+
"negative_prompt": "photo, photorealistic, realism, ugly",
|
| 47 |
+
},
|
| 48 |
+
{
|
| 49 |
+
"name": "Pixel art",
|
| 50 |
+
"prompt": "pixel-art {prompt} . low-res, blocky, pixel art style, 8-bit graphics",
|
| 51 |
+
"negative_prompt": "sloppy, messy, blurry, noisy, highly detailed, ultra textured, photo, realistic",
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
"name": "Fantasy art",
|
| 55 |
+
"prompt": "ethereal fantasy concept art of {prompt} . magnificent, celestial, ethereal, painterly, epic, majestic, magical, fantasy art, cover art, dreamy",
|
| 56 |
+
"negative_prompt": "photographic, realistic, realism, 35mm film, dslr, cropped, frame, text, deformed, glitch, noise, noisy, off-center, deformed, cross-eyed, closed eyes, bad anatomy, ugly, disfigured, sloppy, duplicate, mutated, black and white",
|
| 57 |
+
},
|
| 58 |
+
{
|
| 59 |
+
"name": "Neonpunk",
|
| 60 |
+
"prompt": "neonpunk style {prompt} . cyberpunk, vaporwave, neon, vibes, vibrant, stunningly beautiful, crisp, detailed, sleek, ultramodern, magenta highlights, dark purple shadows, high contrast, cinematic, ultra detailed, intricate, professional",
|
| 61 |
+
"negative_prompt": "painting, drawing, illustration, glitch, deformed, mutated, cross-eyed, ugly, disfigured",
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
"name": "3D Model",
|
| 65 |
+
"prompt": "professional 3d model {prompt} . octane render, highly detailed, volumetric, dramatic lighting",
|
| 66 |
+
"negative_prompt": "ugly, deformed, noisy, low poly, blurry, painting",
|
| 67 |
+
},
|
| 68 |
+
]
|
| 69 |
+
|
| 70 |
+
def infer(prompt, negative="low_quality", style_name=None, guidance_scale=None):
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
seed = random.randint(0,4294967295)
|
| 74 |
+
prompt, negative = apply_style(style_name, prompt, negative)
|
| 75 |
+
print(prompt)
|
| 76 |
+
print(negative)
|
| 77 |
+
|
| 78 |
+
# Load the Stable Diffusion model
|
| 79 |
+
pipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, use_safetensors=True,variant="fp16")
|
| 80 |
+
pipe.to("cuda")
|
| 81 |
+
|
| 82 |
+
# Generate the images
|
| 83 |
+
images = pipe(prompt=prompt, negative_prompt=negative, guidance_scale=guidance_scale, seed=seed).images
|
| 84 |
+
|
| 85 |
+
# Convert the images to base64-encoded strings
|
| 86 |
+
image_urls = []
|
| 87 |
+
for i, image in enumerate(images):
|
| 88 |
+
buffered = BytesIO()
|
| 89 |
+
image.save(buffered, format="JPEG")
|
| 90 |
+
image_b64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 91 |
+
#image_url = f"data:image/jpeg;base64,{image_b64}"
|
| 92 |
+
#image_urls.append(image_url)
|
| 93 |
+
|
| 94 |
+
return image_b64
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
@app.route('/', methods=['POST'])
|
| 98 |
+
def generate_image():
|
| 99 |
+
data = request.get_json()
|
| 100 |
+
if 'prompt' in data and 'style_name' in data and 'guidance_scale' in data:
|
| 101 |
+
prompt = data['prompt']
|
| 102 |
+
style_name = data['style_name']
|
| 103 |
+
guidance_scale = data['guidance_scale']
|
| 104 |
+
|
| 105 |
+
image_urls = infer(prompt, style_name=style_name, guidance_scale=guidance_scale)
|
| 106 |
+
|
| 107 |
+
# Convert the first generated image to base64
|
| 108 |
+
image_b64 = image_urls[0].split(",")[1]
|
| 109 |
+
|
| 110 |
+
return jsonify({'image_base64': image_b64})
|
| 111 |
+
else:
|
| 112 |
+
return jsonify({'error': 'Missing required parameters'}), 400
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
if __name__ == '__main__':
|
| 116 |
+
app.run(host='0.0.0.0',port=6002,debug=True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
diffusers
|
| 2 |
+
transformers
|
| 3 |
+
ftfy
|
| 4 |
+
accelerate
|
| 5 |
+
flask
|
| 6 |
+
flask_cors
|