nostalgebraist commited on
Commit
a54a213
·
1 Parent(s): 27e37c1
Files changed (1) hide show
  1. app.py +80 -2
app.py CHANGED
@@ -1,4 +1,82 @@
 
 
 
 
 
 
1
  import streamlit as st
2
 
3
- x = st.slider('Select a value')
4
- st.write(x, 'squared is', x * x)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, subprocess, sys
2
+ os.system("git clone https://github.com/nostalgebraist/improved-diffusion.git && cd improved-diffusion && git fetch origin nbar-space && git checkout nbar-dev && pip install -e .")
3
+ os.system("pip install tokenizers x-transformers==0.22.0 axial-positional-embedding")
4
+ os.system("pip install einops==0.3.2")
5
+ sys.path.append("improved-diffusion")
6
+
7
  import streamlit as st
8
 
9
+ import numpy as np
10
+ from PIL import Image
11
+
12
+ import improved_diffusion.pipeline
13
+ from transformer_utils.util.tfm_utils import get_local_path_from_huggingface_cdn
14
+
15
+ # constants
16
+ HF_REPO_NAME_DIFFUSION = 'nostalgebraist/nostalgebraist-autoresponder-diffusion'
17
+ model_path_diffusion = 'nostalgebraist-autoresponder-diffusion'
18
+ timestep_respacing_sres1 = '20' # '90,60,60,20,20'
19
+ timestep_respacing_sres2 = '20' # '250'
20
+
21
+ DIFFUSION_DEFAULTS = dict(
22
+ batch_size=1,
23
+ n_samples=1,
24
+ clf_free_guidance=False,
25
+ clf_free_guidance_sres=False,
26
+ guidance_scale=1,
27
+ guidance_scale_sres=0,
28
+ yield_intermediates=True
29
+ )
30
+
31
+ if not os.path.exists(model_path_diffusion):
32
+ model_tar_name = 'model.tar'
33
+ model_tar_path = get_local_path_from_huggingface_cdn(
34
+ HF_REPO_NAME_DIFFUSION, model_tar_name
35
+ )
36
+ subprocess.run(f"tar -xf {model_tar_path} && rm {model_tar_path}", shell=True)
37
+
38
+ checkpoint_path_sres1 = os.path.join(model_path_diffusion, "sres1.pt")
39
+ config_path_sres1 = os.path.join(model_path_diffusion, "config_sres1.json")
40
+
41
+ checkpoint_path_sres2 = os.path.join(model_path_diffusion, "sres2.pt")
42
+ config_path_sres2 = os.path.join(model_path_diffusion, "config_sres2.json")
43
+
44
+ # load
45
+ sampling_model_sres1 = improved_diffusion.pipeline.SamplingModel.from_config(
46
+ checkpoint_path=checkpoint_path_sres1,
47
+ config_path=config_path_sres1,
48
+ timestep_respacing=timestep_respacing_sres1
49
+ )
50
+
51
+ sampling_model_sres2 = improved_diffusion.pipeline.SamplingModel.from_config(
52
+ checkpoint_path=checkpoint_path_sres2,
53
+ config_path=config_path_sres2,
54
+ timestep_respacing=timestep_respacing_sres2
55
+ )
56
+
57
+ pipeline = improved_diffusion.pipeline.SamplingPipeline(sampling_model_sres1, sampling_model_sres2)
58
+
59
+ def handler(text, ts1, ts2, gs1):
60
+ # a = np.random.randint(0, 255, (128, 128, 3)).astype(np.uint8)
61
+ data = {'text': text[:380], 'guidance_scale': gs1}
62
+ args = {k: v for k, v in DIFFUSION_DEFAULTS.items()}
63
+ args.update(data)
64
+
65
+ print(f"running: {args}")
66
+
67
+ pipeline.base_model.set_timestep_respacing(str(ts1))
68
+ pipeline.super_res_model.set_timestep_respacing(str(ts2))
69
+
70
+ gen = pipeline.sample(**args)
71
+
72
+ for sample, pred_xstart in
73
+
74
+ return result
75
+
76
+ text = st.text_area('asdf')
77
+
78
+ if st.button('rweerew'):
79
+ handler(text, 20, 20, 0)
80
+
81
+ # x = st.slider('Select a value')
82
+ # st.write(x, 'squared is', x * x)