我用Flask写了一个web Demo程序,调用智源OpenAI接口进行文生图演示,初始化的时候加载模型,页面提交请求的时候生成图片。用Python来运行完全没有问题,用gunicorn来运行,则会反复初始化,也就是反复加载模型。各位大佬看看如果处理?
app.py
from flask import Flask
from flask import render_template,jsonify,request
from baaiImpl import text2imgImpl
app = Flask(name,
static_folder=‘./dist/static’,
template_folder = “./dist”)
@app.route(‘/’)
def index():
return render_template(‘index.html’,name=‘index’)
@app.route(‘/creatimg’,methods=[“POST”])
def creatimg():
info=request.json.get(‘info’)
print("Prompt : "+info)
data=text2imgImpl.Predict(info)
return jsonify({“data”:data})
if name == ‘main’:
app.run(
debug=False,
host=‘0.0.0.0’,
port=5000,
# ssl_context=‘adhoc’, # 默认SSL证书,实现https
threaded=True, # 多线程
)
baaiImpl下的__init__.py
#coding:utf8
from baaiImpl.Text2Image import Text2Image
text2imgImpl = Text2Image()
Text2Image.py
import torch
from flagai.auto_model.auto_loader import AutoLoader
from flagai.model.predictor.predictor import Predictor
import os
class Text2Image:
def init(self):
self.device = torch.device(“cuda” if torch.cuda.is_available() else “cpu”)
self.loader = AutoLoader(task_name=“text2img”, #contrastive learning
model_name=“AltDiffusion”,
model_dir=“./checkpoints”)
self.model = self.loader.get_model()
self.model.eval()
self.model.to(self.device)
self.predictor = Predictor(self.model)
def Predict(self,prompt):
outpath='dist/static/imgout/'
self.predictor.predict_generate_images(prompt,n_samples=4,skip_grid=False,outpath=outpath)
webpath='./static/imgout/samples/'
filelist=os.listdir(outpath+'samples/')
filelist.sort(key=lambda x: int(x[0:-4]),reverse=True) # 文件名 按数字排序
retval={}
for i in range(4):
retval['img{}'.format(i+1)]=webpath+filelist[3-i]
return retval