generated from voctory/serverless-template-stable-diffusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
50 lines (39 loc) · 1.83 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import torch
from torch import autocast
from diffusers import DiffusionPipeline, EulerDiscreteScheduler
import base64
from io import BytesIO
import os
# Init is ran on server startup
# Load your model to GPU as a global variable here using the variable name "model"
def init():
global model
HF_AUTH_TOKEN = os.getenv("HF_AUTH_TOKEN")
repo_id = "stabilityai/stable-diffusion-2"
scheduler = EulerDiscreteScheduler.from_pretrained(repo_id, subfolder="scheduler", prediction_type="v_prediction")
model = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float16, revision="fp16", scheduler=scheduler, use_auth_token=HF_AUTH_TOKEN).to("cuda")
# Inference is ran for every server call
# Reference your preloaded global model variable here.
def inference(model_inputs:dict) -> dict:
global model
# Parse out your arguments
prompt = model_inputs.get('prompt', None)
height = model_inputs.get('height', 768)
width = model_inputs.get('width', 768)
num_inference_steps = model_inputs.get('num_inference_steps', 50)
guidance_scale = model_inputs.get('guidance_scale', 7.5)
input_seed = model_inputs.get("seed",None)
#If "seed" is not sent, we won't specify a seed in the call
generator = None
if input_seed != None:
generator = torch.Generator("cuda").manual_seed(input_seed)
if prompt == None:
return {'message': "No prompt provided"}
# Run the model
with autocast("cuda"):
image = model(prompt,height=height,width=width,num_inference_steps=num_inference_steps,guidance_scale=guidance_scale,generator=generator).images[0]
buffered = BytesIO()
image.save(buffered,format="JPEG")
image_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
# Return the results as a dictionary
return {'image_base64': image_base64}