GirishKiran commited on
Commit
239c004
1 Parent(s): cd56108

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +366 -0
app.py ADDED
@@ -0,0 +1,366 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ ## required lib, required "pip install"
3
+ # import transformers
4
+ # import accelerate
5
+ import openai
6
+ import llama_index
7
+ import torch
8
+ import cryptography
9
+ import cryptography.fernet
10
+ ## interface libs, required "pip install"
11
+ import gradio
12
+ import huggingface_hub
13
+ import huggingface_hub.hf_api
14
+ ## standard libs, no need to install
15
+ import json
16
+ import requests
17
+ import time
18
+ import os
19
+ import random
20
+ import re
21
+ import sys
22
+ import psutil
23
+ import threading
24
+ import socket
25
+ # import PIL
26
+ # import pandas
27
+ import matplotlib
28
+ class HFace_Pluto(object):
29
+ #
30
+ # initialize the object
31
+ def __init__(self, name="Pluto",*args, **kwargs):
32
+ super(HFace_Pluto, self).__init__(*args, **kwargs)
33
+ self.author = "Duc Haba, Girish"
34
+ self.name = name
35
+ self._ph()
36
+ self._pp("Hello from class", str(self.__class__) + " Class: " + str(self.__class__.__name__))
37
+ self._pp("Code name", self.name)
38
+ self._pp("Author is", self.author)
39
+ self._ph()
40
+ #
41
+ # define class var for stable division
42
+ self._device = 'cuda'
43
+ self._steps = [3,8,21,55,89,144]
44
+ self._guidances = [1.1,3.0,5.0,8.0,13.0,21.0]
45
+ self._xkeyfile = '.xoxo'
46
+ self._models = []
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._llama_query_engine = None
53
+ self._llama_index_doc = None
54
+ #self._generator = torch.Generator(device='cuda')
55
+ self.pipes = []
56
+ self.prompts = []
57
+ self.images = []
58
+ self.seeds = []
59
+ self.fname_id = 0
60
+ self.dname_img = "img_colab/"
61
+ self._huggingface_key="gAAAAABkgtmOIjpnjwXFWmgh1j2et2kMjHUze-ym6h3BieAp34Sqkqv3EVYvRinETvpw-kXu7RSRl5_9FqrYe-7unfakMvMkU8nHrfB3hBSC76ZTXwkVSzlN0RfBNs9NL8BGjaSJ8mz8"
62
+ self._gpt_key="'gAAAAABkgtoTOLPegnxNIAfBfAda17h5HIHTS_65bobO3SdDlJam07AHGrcolvk9c6IWNJtTTxaCb8_JtWnLz0Y5h9doyfL-nJZggeQ6kLtaD4XwZYcG-AtYNNGCnJzVt9AaysPDnu-KWVhnJSe-DyH0oOO33doE0g=='"
63
+ self._fkey="=cvsOPRcWD6JONmdr4Sh6-PqF6nT1InYh965mI8f_sef"
64
+ self._color_primary = '#2780e3' #blue
65
+ self._color_secondary = '#373a3c' #dark gray
66
+ self._color_success = '#3fb618' #green
67
+ self._color_info = '#9954bb' #purple
68
+ self._color_warning = '#ff7518' #orange
69
+ self._color_danger = '#ff0039' #red
70
+ self._color_mid_gray = '#495057'
71
+ return
72
+ #
73
+ # pretty print output name-value line
74
+ def _pp(self, a, b,is_print=True):
75
+ # print("%34s : %s" % (str(a), str(b)))
76
+ x = f'{"%34s" % str(a)} : {str(b)}'
77
+ y = None
78
+ if (is_print):
79
+ print(x)
80
+ else:
81
+ y = x
82
+ return y
83
+ #
84
+ # pretty print the header or footer lines
85
+ def _ph(self,is_print=True):
86
+ x = f'{"-"*34} : {"-"*34}'
87
+ y = None
88
+ if (is_print):
89
+ print(x)
90
+ else:
91
+ y = x
92
+ return y
93
+ #
94
+ # fetch huggingface file
95
+ def fetch_hface_files(self,
96
+ hf_names,
97
+ hf_space="duchaba/monty",
98
+ local_dir="/content/"):
99
+ f = str(hf_names) + " is not iteratable, type: " + str(type(hf_names))
100
+ try:
101
+ for f in hf_names:
102
+ lo = local_dir + f
103
+ huggingface_hub.hf_hub_download(repo_id=hf_space, filename=f,
104
+ use_auth_token=True,repo_type=huggingface_hub.REPO_TYPE_SPACE,
105
+ force_filename=lo)
106
+ except:
107
+ self._pp("*Error", f)
108
+ return
109
+ #
110
+ #
111
+ def push_hface_files(self,
112
+ hf_names,
113
+ hf_space="duchaba/skin_cancer_diagnose",
114
+ local_dir="/content/"):
115
+ f = str(hf_names) + " is not iteratable, type: " + str(type(hf_names))
116
+ try:
117
+ for f in hf_names:
118
+ lo = local_dir + f
119
+ huggingface_hub.upload_file(
120
+ path_or_fileobj=lo,
121
+ path_in_repo=f,
122
+ repo_id=hf_space,
123
+ repo_type=huggingface_hub.REPO_TYPE_SPACE)
124
+ except Exception as e:
125
+ self._pp("*Error", e)
126
+ return
127
+ #
128
+ def push_hface_folder(self, hf_folder, hf_space_id, hf_dest_folder=None):
129
+ api = huggingface_hub.HfApi()
130
+ api.upload_folder(folder_path=hf_folder,
131
+ repo_id=hf_space_id,
132
+ path_in_repo=hf_dest_folder,
133
+ repo_type="space")
134
+ return
135
+ #
136
+ # Define a function to display available CPU and RAM
137
+ def fetch_system_info(self):
138
+ s=''
139
+ # Get CPU usage as a percentage
140
+ cpu_usage = psutil.cpu_percent()
141
+ # Get available memory in bytes
142
+ mem = psutil.virtual_memory()
143
+ # Convert bytes to gigabytes
144
+ mem_total_gb = mem.total / (1024 ** 3)
145
+ mem_available_gb = mem.available / (1024 ** 3)
146
+ mem_used_gb = mem.used / (1024 ** 3)
147
+ # Print the results
148
+ s += f"CPU usage: {cpu_usage}%\n"
149
+ s += f"Total memory: {mem_total_gb:.2f} GB\n"
150
+ s += f"Available memory: {mem_available_gb:.2f} GB\n"
151
+ # print(f"Used memory: {mem_used_gb:.2f} GB")
152
+ s += f"Memory usage: {mem_used_gb/mem_total_gb:.2f}%\n"
153
+ return s
154
+ #
155
+ def restart_script_periodically(self):
156
+ while True:
157
+ #random_time = random.randint(540, 600)
158
+ random_time = random.randint(15800, 21600)
159
+ time.sleep(random_time)
160
+ os.execl(sys.executable, sys.executable, *sys.argv)
161
+ return
162
+ #
163
+ def write_file(self,fname, txt):
164
+ f = open(fname, "w")
165
+ f.writelines("\n".join(txt))
166
+ f.close()
167
+ return
168
+ #
169
+ def fetch_gpu_info(self):
170
+ s=''
171
+ try:
172
+ s += f'Your GPU is the {torch.cuda.get_device_name(0)}\n'
173
+ s += f'GPU ready staus {torch.cuda.is_available()}\n'
174
+ s += f'GPU allocated RAM: {round(torch.cuda.memory_allocated(0)/1024**3,1)} GB\n'
175
+ s += f'GPU reserved RAM {round(torch.cuda.memory_reserved(0)/1024**3,1)} GB\n'
176
+ except Exception as e:
177
+ s += f'**Warning, No GPU: {e}'
178
+ return s
179
+ #
180
+ def _fetch_crypt(self,is_generate=False):
181
+ s=self._fkey[::-1]
182
+ if (is_generate):
183
+ s=open(self._xkeyfile, "rb").read()
184
+ return s
185
+ #
186
+ def _gen_key(self):
187
+ key = cryptography.fernet.Fernet.generate_key()
188
+ with open(self._xkeyfile, "wb") as key_file:
189
+ key_file.write(key)
190
+ return
191
+ #
192
+ def _decrypt_it(self, x):
193
+ y = self._fetch_crypt()
194
+ f = cryptography.fernet.Fernet(y)
195
+ m = f.decrypt(x)
196
+ return m.decode()
197
+ #
198
+ def _encrypt_it(self, x):
199
+ key = self._fetch_crypt()
200
+ p = x.encode()
201
+ f = cryptography.fernet.Fernet(key)
202
+ y = f.encrypt(p)
203
+ return y
204
+ #
205
+ def _login_hface(self):
206
+ huggingface_hub.login(self._decrypt_it(self._huggingface_key),
207
+ add_to_git_credential=True) # non-blocking login
208
+ self._ph()
209
+ return
210
+ #
211
+ def _fetch_version(self):
212
+ s = ''
213
+ # print(f"{'torch: 2.0.1':<25} Actual: {torch.__version__}")
214
+ # print(f"{'transformers: 4.29.2':<25} Actual: {transformers.__version__}")
215
+ s += f"{'openai: 0.27.7,':<28} Actual: {openai.__version__}\n"
216
+ s += f"{'huggingface_hub: 0.14.1,':<28} Actual: {huggingface_hub.__version__}\n"
217
+ s += f"{'gradio: 3.32.0,':<28} Actual: {gradio.__version__}\n"
218
+ s += f"{'cryptography: 3.34.0,':<28} Actual: {cryptography.__version__}\n"
219
+ s += f"{'llama_index: 0.6.21.post1,':<28} Actual: {llama_index.__version__}\n"
220
+ return s
221
+ #
222
+ def _fetch_host_ip(self):
223
+ s=''
224
+ hostname = socket.gethostname()
225
+ ip_address = socket.gethostbyname(hostname)
226
+ s += f"Hostname: {hostname}\n"
227
+ s += f"IP Address: {ip_address}\n"
228
+ return s
229
+ #
230
+ def _setup_openai(self,key=None):
231
+ if (key is None):
232
+ key = self._decrypt_it(self._gpt_key)
233
+ #
234
+ openai.api_key = key
235
+ os.environ["OPENAI_API_KEY"] = key
236
+ return
237
+ #
238
+ def _fetch_index_files(self,llama_ix):
239
+ res = []
240
+ x = llama_ix.ref_doc_info
241
+ for val in x.values():
242
+ jdata = json.loads(val.to_json())
243
+ fname = jdata['extra_info']['file_name']
244
+ res.append(fname)
245
+ # remove dublication name
246
+ res = list(set(res))
247
+ return res
248
+ # add module/method
249
+ #
250
+ import functools
251
+ def add_method(cls):
252
+ def decorator(func):
253
+ @functools.wraps(func)
254
+ def wrapper(*args, **kwargs):
255
+ return func(*args, **kwargs)
256
+ setattr(cls, func.__name__, wrapper)
257
+ return func # returning func means func can still be used normally
258
+ return decorator
259
+ #
260
+ monty = HFace_Pluto("Monty")
261
+ monty._login_hface()
262
+ # print(monty._fetch_version())
263
+ # monty._ph()
264
+ # print(monty.fetch_system_info())
265
+ # monty._ph()
266
+ # print(monty.fetch_gpu_info())
267
+ # monty._ph()
268
+ # print(monty._fetch_host_ip())
269
+ monty._ph()
270
+ monty._setup_openai()
271
+
272
+ @add_method(HFace_Pluto)
273
+ def gen_llama_index(self, doc_path, vindex='vector_index', vpath='./index_storage'):
274
+ # load doc
275
+ doc = llama_index.SimpleDirectoryReader(doc_path).load_data()
276
+ # need openai key
277
+ self._llama_index_doc = llama_index.VectorStoreIndex.from_documents(doc)
278
+ # save index to disk
279
+ self._llama_index_doc.set_index_id(vindex)
280
+ self._llama_index_doc.storage_context.persist(vpath)
281
+ print(f'Index doc are: {self._fetch_index_files(self._llama_index_doc)}')
282
+ return
283
+
284
+ @add_method(HFace_Pluto)
285
+ def load_llama_index(self,vindex='vector_index',vpath='./index_storage'):
286
+ try:
287
+ storage_context = llama_index.StorageContext.from_defaults(persist_dir=vpath)
288
+ # load index
289
+ self._llama_index_doc = llama_index.load_index_from_storage(storage_context, index_id=vindex)
290
+ print(f'Index doc are: {self._fetch_index_files(self._llama_index_doc)}')
291
+ except Exception as e:
292
+ print('**Error: can not load index, check the index_storage directory or the GPT auth token')
293
+ print('If do not have index tokens then run the .gen_llama_index() function')
294
+ print(f'Exception: {e}')
295
+ return
296
+
297
+ monty.load_llama_index()
298
+
299
+ @add_method(HFace_Pluto)
300
+ def ask_me(self, p, ll_sign_in_member='Girish'):
301
+ # todo: make this into a dict so not to use if/else
302
+ if (self._llama_query_engine is None):
303
+ self._llama_query_engine = self._llama_index_doc.as_query_engine()
304
+
305
+ ll_engine = self._llama_query_engine
306
+
307
+ px = f'My name is {ll_sign_in_member}, and I want answer to the following: {p}.'
308
+ print("##### " + px)
309
+ resp = ll_engine.query(px)
310
+
311
+ return resp
312
+ # @add_method(HFace_Pluto)
313
+ def ask_me(self, p):
314
+ if (self._llama_query_engine is None):
315
+ self._llama_query_engine = self._llama_index_doc.as_chat_engine()
316
+ resp = self._llama_query_engine.chat(p)
317
+ return resp
318
+
319
+ p = 'How is my Humana plan chaning?'
320
+ resp = monty.ask_me(p)
321
+ print(resp)
322
+
323
+ gradio.Interface(fn=monty.ask_me,
324
+ inputs=in_box,
325
+ outputs=out_box,
326
+ examples=exp,
327
+ title=title,
328
+ description=desc,
329
+ article=arti,
330
+ flagging_options=flag_options).launch(debug=True)
331
+
332
+ in_box = [gradio.Textbox(lines=1, label="Your Humana request", placeholder="Your Humana request...see example if you need help.")
333
+ ,gradio.Radio(["Duc", "Girish", "Mayo"], label="Login Member", value='Duc', info="Who had login?")
334
+ ,gradio.Radio(["Humana", "Kohli Fighters", "Ally Bank"], label="Login Member", value='Humana', info="Fine-Tune LLM for:")
335
+ ]
336
+ out_box = [gradio.Textbox(label="Humana response:")]
337
+ #
338
+
339
+ title = "Humana and YML Fine-tune LLM model"
340
+ desc = '*Note: This model is fine-tuned by YML using GPT3.5 as the base LLM.'
341
+ arti = f'<ul><li>The documents for fine-tuning are:</li>{monty.self._fetch_index_files(monty._llama_index_doc)}'
342
+ arti += '<li><i>**Note: You can add more documentation. The more documentation the model has the smarter it will be.</i></li></ul>'
343
+ exp = [
344
+ ['Tell me the Humana Gold Plus plan.'],
345
+ ['Please write a summary in bullet point of the Humana Gold Plus SNP-DE H0028-015 (HMO-POS D-SNP) Annual Notice of Changes for 2023.'],
346
+ ['Compare the Humana monthly premium and maximum out-of-pocket for in-network and out-network.'],
347
+ ['What is the maximum dollar value allowance for in-network over the counter drug?'],
348
+ ['Write a newsletter introducing Humana Gold Plus plan, and target it to senior citizen demographic.'],
349
+ ['Please write a summary about the Humana and Longevity Health Partner so that a teenage can understand.'],
350
+ ['Tell me about the state agency contact information in bullet point.'],
351
+ ['Please tell me more about the Humana Offer Free Counseling about Medicare and Medicaid'],
352
+ ['Write four engaging tweets about the Humana Gold Plus plan.'],
353
+ ['Tell me something funny about Humana.'],
354
+ ['Is Humana is same as human?']
355
+ ]
356
+ flag_opt = [': Good', ': Bad']
357
+ flag_dir = './user_feed_back'
358
+
359
+ gradio.Interface(fn=monty.ask_me,
360
+ inputs=in_box,
361
+ outputs=out_box,
362
+ examples=exp,
363
+ title=title,
364
+ description=desc,
365
+ article=arti,
366
+ flagging_options=flag_options).launch(debug=True)