duchaba commited on
Commit
77059fc
1 Parent(s): 2f3fde3

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +159 -0
app.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import torch
3
+ import pandas
4
+ import gradio
5
+ import PIL
6
+ import huggingface_hub
7
+ import huggingface_hub.hf_api
8
+ import json
9
+ import requests
10
+ # import openai
11
+ # openai.api_key = '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'
12
+ class HFace_Pluto(object):
13
+ #
14
+ # initialize the object
15
+ def __init__(self, name="Pluto",*args, **kwargs):
16
+ super(HFace_Pluto, self).__init__(*args, **kwargs)
17
+ self.author = "Duc Haba"
18
+ self.name = name
19
+ self._ph()
20
+ self._pp("Hello from class", str(self.__class__) + " Class: " + str(self.__class__.__name__))
21
+ self._pp("Code name", self.name)
22
+ self._pp("Author is", self.author)
23
+ self._ph()
24
+ #
25
+ # define class var for stable division
26
+ self._device = 'cuda'
27
+ self._steps = [3,8,21,55,89,144]
28
+ self._guidances = [1.1,3.0,5.0,8.0,13.0,21.0]
29
+ self._models = ['CompVis/stable-diffusion-v1-4', #default
30
+ 'stabilityai/stable-diffusion-2-1', #1 latest as of feb. 28, 2023
31
+ 'dreamlike-art/dreamlike-diffusion-1.0', #2 ilike
32
+ 'prompthero/openjourney-v2', #3 ilike
33
+ 'itecgo/sd-lexica_6k-model', #4
34
+ 'nitrosocke/mo-di-diffusion',
35
+ 'coreco/seek.art_MEGA',
36
+ 'andite/anything-v4.0', #7 anime
37
+ 'nitrosocke/Nitro-Diffusion',
38
+ '22h/vintedois-diffusion-v0-1', #9 ilike
39
+ 'Lykon/DreamShaper', #10 ilike
40
+ 'rrustom/stable-architecture-diffusers', # 11
41
+ 'hakurei/waifu-diffusion', #anime style
42
+ 'wavymulder/portraitplus', #13 ilike
43
+ 'dreamlike-art/dreamlike-photoreal-2.0', #no check
44
+ 'johnslegers/epic-diffusion', #15 ilike good example
45
+ 'nitrosocke/Arcane-Diffusion' #16 ilike
46
+ ]
47
+ self._seed = 667 # sum of walnut in ascii (or Angle 667)
48
+ self._width = 512
49
+ self._height = 512
50
+ self._step = 50
51
+ self._guidances = 7.5
52
+ #self._generator = torch.Generator(device='cuda')
53
+ self.pipes = []
54
+ self.prompts = []
55
+ self.images = []
56
+ self.seeds = []
57
+ self.fname_id = 0
58
+ self.dname_img = "img_colab/"
59
+ return
60
+ #
61
+ # pretty print output name-value line
62
+ def _pp(self, a, b):
63
+ print("%34s : %s" % (str(a), str(b)))
64
+ return
65
+ #
66
+ # pretty print the header or footer lines
67
+ def _ph(self):
68
+ print("-" * 34, ":", "-" * 34)
69
+ return
70
+ #
71
+ # fetch huggingface file
72
+ def fetch_hface_files(self,
73
+ hf_names,
74
+ hf_space="duchaba/skin_cancer_diagnose",
75
+ local_dir="/content/"):
76
+ f = str(hf_names) + " is not iteratable, type: " + str(type(hf_names))
77
+ try:
78
+ for f in hf_names:
79
+ lo = local_dir + f
80
+ huggingface_hub.hf_hub_download(repo_id=hf_space, filename=f,
81
+ use_auth_token=True,repo_type=huggingface_hub.REPO_TYPE_SPACE,
82
+ force_filename=lo)
83
+ except:
84
+ self._pp("*Error", f)
85
+ return
86
+ #
87
+ #
88
+ def push_hface_files(self,
89
+ hf_names,
90
+ hf_space="duchaba/skin_cancer_diagnose",
91
+ local_dir="/content/"):
92
+ f = str(hf_names) + " is not iteratable, type: " + str(type(hf_names))
93
+ try:
94
+ for f in hf_names:
95
+ lo = local_dir + f
96
+ huggingface_hub.upload_file(
97
+ path_or_fileobj=lo,
98
+ path_in_repo=f,
99
+ repo_id=hf_space,
100
+ repo_type=huggingface_hub.REPO_TYPE_SPACE)
101
+ except:
102
+ self._pp("*Error", f)
103
+ return
104
+ #
105
+ def write_file(self,fname, txt):
106
+ f = open(fname, "w")
107
+ f.writelines("\n".join(txt))
108
+ f.close()
109
+ return
110
+ def draw_it(self,prompt):
111
+ url = 'lion.png'
112
+ img = PIL.Image.open(url)
113
+ return img
114
+ #
115
+ # add module/method
116
+ #
117
+ import functools
118
+ def add_method(cls):
119
+ def decorator(func):
120
+ @functools.wraps(func)
121
+ def wrapper(*args, **kwargs):
122
+ return func(*args, **kwargs)
123
+ setattr(cls, func.__name__, wrapper)
124
+ return func # returning func means func can still be used normally
125
+ return decorator
126
+
127
+ # instantiate the class
128
+ monty = HFace_Pluto('Monty')
129
+
130
+ # use magic prompt model
131
+ monty.gpt2_pipe = transformers.pipeline('text-generation',
132
+ model='Gustavosta/MagicPrompt-Stable-Diffusion',
133
+ tokenizer='gpt2')
134
+
135
+ # fetch prompt
136
+ @add_method(HFace_Pluto)
137
+ def _print_response(self, response):
138
+ for x in response:
139
+ print(x['generated_text'])
140
+ return
141
+ #
142
+ @add_method(HFace_Pluto)
143
+ def fetch_prompt(self, prompt, max_num=1, max_length=240, is_print=False):
144
+ response = self.gpt2_pipe(prompt,
145
+ max_length=max_length,
146
+ num_return_sequences=max_num)
147
+ #
148
+ if (is_print):
149
+ self._print_response(response)
150
+ return response
151
+
152
+ # use pluto _pp for interface testing
153
+ # iface = gradio.Interface(fn=pluto.draw_it, inputs="text", outputs="image",
154
+ # flagging_options=["Excellent", "Good", "Not Bad"])
155
+ iface = gradio.Interface(fn=monty.fetch_prompt, inputs="text", outputs="text",
156
+ flagging_options=[])
157
+
158
+ # Launch it
159
+ iface.launch()