Skip to content

Commit af88a75

Browse files
authored
Merge pull request #1 from 0xb79/main
Update code to use data folder and clear cache
2 parents e8a6bd9 + d90d95b commit af88a75

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

README.MD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
* `python3 -m venv venv`
88
* `source venv/bin/activate`
99
2. Install requirements `pip install -r requirements.txt`
10+
3. Sign up for Hugging Face and generate auth token (https://huggingface.co/docs/hub/security-tokens)
11+
4. Request access to stable diffusion model https://huggingface.co/CompVis/stable-diffusion-v1-4
12+
5. Input access token to `access_token` in `server.py` and `stable-diffusion.py`
1013

1114
## Playground
1215

@@ -16,4 +19,4 @@
1619
## Server
1720

1821
1. run `venv/bin/python server.py`
19-
2. endpoint available at `<ipaddress>:port/stable-diffusion/?prompt=<prompt>`
22+
2. endpoint available at `<ipaddress>:port/stable-diffusion?prompt=<prompt>`

server.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
from flask import Flask, request, send_file
22
from torch import autocast, cuda
3+
import torch, os
34
from diffusers import StableDiffusionPipeline, LMSDiscreteScheduler
45
app = Flask(__name__)
56

7+
access_token = "enter access token"
8+
9+
10+
611
# this will substitute the default PNDM scheduler for K-LMS
712
lms = LMSDiscreteScheduler(
813
beta_start=0.00085,
@@ -13,7 +18,8 @@
1318
pipe = StableDiffusionPipeline.from_pretrained(
1419
"CompVis/stable-diffusion-v1-4",
1520
scheduler=lms,
16-
use_auth_token=True
21+
torch_dtype=torch.float16,
22+
use_auth_token=access_token
1723
)
1824

1925
if cuda.is_available():
@@ -23,7 +29,7 @@
2329
@app.route("/stable_diffusion")
2430
def get_result():
2531
prompt = request.args.get("prompt")
26-
prompt_path = "data/" + prompt.replace(" ", "_") + ".png"
32+
prompt_path = os.path.join(root_dir(), "data",prompt.replace(" ", "_") + ".png")
2733
print(prompt, prompt_path)
2834
if cuda.is_available():
2935
with autocast("cuda"):
@@ -32,8 +38,12 @@ def get_result():
3238
image = pipe(prompt)["sample"][0]
3339

3440
image.save(prompt_path)
35-
41+
torch.cuda.empty_cache()
42+
3643
return send_file(prompt_path, mimetype='image/png')
3744

45+
def root_dir():
46+
return os.path.abspath(os.path.dirname(__file__))
47+
3848
if __name__=="__main__":
39-
app.run(debug = True)
49+
app.run(host="127.0.0.1", port="5555",debug = True)

stable-diffusion.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
from torch import autocast
22
from diffusers import StableDiffusionPipeline, LMSDiscreteScheduler
33
from IPython.display import Image
4+
import os
5+
6+
access_token = "enter access token"
47

58
# this will substitute the default PNDM scheduler for K-LMS
69
lms = LMSDiscreteScheduler(
@@ -12,13 +15,16 @@
1215
pipe = StableDiffusionPipeline.from_pretrained(
1316
"CompVis/stable-diffusion-v1-4",
1417
scheduler=lms,
15-
use_auth_token=True
18+
use_auth_token=access_token
1619
).to("cuda")
1720

1821
prompt = "a photo of an astronaut riding a horse on mars"
19-
file_name = "astronaut_rides_horse.png"
22+
file_name = os.path.join(root_dir(), "data", "astronaut_rides_horse.png")
2023

2124
with autocast("cuda"):
2225
image = pipe(prompt)["sample"][0]
26+
image.save(file_name)
2327

24-
image.save(file_name)
28+
torch.cuda.empty_cache()
29+
def root_dir():
30+
return os.path.abspath(os.path.dirname(__file__))

0 commit comments

Comments
 (0)