-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathmain.py
41 lines (29 loc) · 1.01 KB
/
main.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
from flask_ngrok import run_with_ngrok
from flask import Flask, render_template, request
import torch
from diffusers import StableDiffusionPipeline
import base64
from io import BytesIO
# Load model
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5", revision="fp16", torch_dtype=torch.float16)
pipe.to("cuda")
# Start flask app and set to ngrok
app = Flask(__name__)
run_with_ngrok(app)
@app.route('/')
def initial():
return render_template('index.html')
@app.route('/submit-prompt', methods=['POST'])
def generate_image():
prompt = request.form['prompt-input']
print(f"Generating an image of {prompt}")
image = pipe(prompt).images[0]
print("Image generated! Converting image ...")
buffered = BytesIO()
image.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue())
img_str = "data:image/png;base64," + str(img_str)[2:-1]
print("Sending image ...")
return render_template('index.html', generated_image=img_str)
if __name__ == '__main__':
app.run()