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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2453a6
d74b4aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1cdedda
d74b4aa
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
# [BEGIN OF pluto_happy]
# [BEGIN OF pluto_happy]
# required pip install
import pynvml # for GPU info
## standard libs, no need to install
import numpy
import PIL
import pandas
import matplotlib
import torch
# standard libs (system)
import json
import time
import os
import random
import re
import sys
import psutil
import socket
import importlib.metadata
import types
import cpuinfo
import pathlib
import subprocess
import fastai
# define class Pluto_Happy
class Pluto_Happy(object):
  """
  The Pluto projects starts with fun AI hackings and become a part of my
  first book "Data Augmentation with Python" with Packt Publishing.

  In particular, Pluto_Happy is a clean and lite kernel of a simple class,
  and using @add_module decoractor to add in specific methods to be a new class,
  such as Pluto_HFace with a lot more function on HuggingFace, LLM and Transformers.

  Args:
      name (str): the display name, e.g. "Hanna the seeker"

  Returns:
      (object): the class instance.
  """

  # initialize the object
  def __init__(self, name="Pluto",*args, **kwargs):
    super(Pluto_Happy, self).__init__(*args, **kwargs)
    self.author = "Duc Haba"
    self.name = name
    self._ph()
    self._pp("Hello from class", str(self.__class__) + " Class: " + str(self.__class__.__name__))
    self._pp("Code name", self.name)
    self._pp("Author is", self.author)
    self._ph()
    #
    # define class var for stable division
    self.fname_requirements = './pluto_happy/requirements.txt'
    #
    self.color_primary = '#2780e3' #blue
    self.color_secondary = '#373a3c' #dark gray
    self.color_success = '#3fb618' #green
    self.color_info = '#9954bb' #purple
    self.color_warning = '#ff7518' #orange
    self.color_danger = '#ff0039' #red
    self.color_mid_gray = '#495057'
    self._xkeyfile = '.xoxo'
    return
  #
  # pretty print output name-value line
  def _pp(self, a, b,is_print=True):

    """
    Pretty print output name-value line

    Args:
        a (str) :
        b (str) :
        is_print (bool): whether to print the header or footer lines to console or return a str.

    Returns:
        y : None or output as (str)

    """
    # print("%34s : %s" % (str(a), str(b)))
    x = f'{"%34s" % str(a)} : {str(b)}'
    y = None
    if (is_print):
      print(x)
    else:
      y = x
    return y
  #
  # pretty print the header or footer lines
  def _ph(self,is_print=True):
    """
    Pretty prints the header or footer lines.

    Args:
      is_print (bool): whether to print the header or footer lines to console or return a str.

    Return:
      y : None or output as (str)

    """
    x = f'{"-"*34} : {"-"*34}'
    y = None
    if (is_print):
      print(x)
    else:
      y = x
    return y
  #
 
  # Define a function to display available CPU and RAM
  def fetch_info_system(self, is_print=False):

    """
    Fetches system information, such as CPU usage and memory usage.

    Args:
        None.

    Returns:
        s: (str) A string containing the system information.
    """

    s=''
    # Get CPU usage as a percentage
    cpu_usage = psutil.cpu_percent()
    # Get available memory in bytes
    mem = psutil.virtual_memory()
    # Convert bytes to gigabytes
    mem_total_gb = mem.total / (1024 ** 3)
    mem_available_gb = mem.available / (1024 ** 3)
    mem_used_gb = mem.used / (1024 ** 3)
    #
    # print it nicely
    # save the results
    s += f"Total memory: {mem_total_gb:.2f} GB\n"
    s += f"Available memory: {mem_available_gb:.2f} GB\n"
    # print(f"Used memory: {mem_used_gb:.2f} GB")
    s += f"Memory usage: {mem_used_gb/mem_total_gb:.2f}%\n"
    try:
      cpu_info = cpuinfo.get_cpu_info()
      s += f'CPU type: {cpu_info["brand_raw"]}, arch: {cpu_info["arch"]}\n'
      s += f'Number of CPU cores: {cpu_info["count"]}\n'
      s += f"CPU usage: {cpu_usage}%\n"
      s += f'Python version: {cpu_info["python_version"]}'
      if (is_print is True):
        self._ph()
        self._pp("System", "Info")
        self._ph()
        self._pp("Total Memory", f"{mem_total_gb:.2f} GB")
        self._pp("Available Memory", f"{mem_available_gb:.2f} GB")
        self._pp("Memory Usage", f"{mem_used_gb/mem_total_gb:.2f}%")
        self._pp("CPU Type", f'{cpu_info["brand_raw"]}, arch: {cpu_info["arch"]}')
        self._pp("CPU Cores Count", f'{cpu_info["count"]}')
        self._pp("CPU Usage", f"{cpu_usage}%")
        self._pp("Python Version", f'{cpu_info["python_version"]}')
    except Exception as e:
      s += f'CPU type: Not accessible, Error: {e}'
      if (is_print is True):
        self._ph()
        self._pp("CPU", f"*Warning* No CPU Access: {e}")
    return s
  #
  # fetch GPU RAM info
  def fetch_info_gpu(self, is_print=False):

    """
    Function to fetch GPU RAM info

    Args:
        None.

    Returns:
        s: (str) GPU RAM info in human readable format.
    """

    s=''
    mtotal = 0
    mfree = 0
    try:
      nvml_handle = pynvml.nvmlInit()
      devices = pynvml.nvmlDeviceGetCount()
      for i in range(devices):
        device = pynvml.nvmlDeviceGetHandleByIndex(i)
        memory_info = pynvml.nvmlDeviceGetMemoryInfo(device)
        mtotal += memory_info.total
        mfree += memory_info.free
      mtotal = mtotal / 1024**3
      mfree = mfree / 1024**3
      # print(f"GPU {i}: Total Memory: {memory_info.total/1024**3} GB, Free Memory: {memory_info.free/1024**3} GB")
      s += f'GPU type: {torch.cuda.get_device_name(0)}\n'
      s += f'GPU ready staus: {torch.cuda.is_available()}\n'
      s += f'Number of GPUs: {devices}\n'
      s += f'Total Memory: {mtotal:.2f} GB\n'
      s += f'Free Memory: {mfree:.2f} GB\n'
      s += f'GPU allocated RAM: {round(torch.cuda.memory_allocated(0)/1024**3,2)} GB\n'
      s += f'GPU reserved RAM {round(torch.cuda.memory_reserved(0)/1024**3,2)} GB\n'
      if (is_print is True):
        self._ph()
        self._pp("GPU", "Info")
        self._ph()
        self._pp("GPU Type", f'{torch.cuda.get_device_name(0)}')
        self._pp("GPU Ready Status", f'{torch.cuda.is_available()}')
        self._pp("GPU Count", f'{devices}')
        self._pp("GPU Total Memory", f'{mtotal:.2f} GB')
        self._pp("GPU Free Memory", f'{mfree:.2f} GB')
        self._pp("GPU allocated RAM", f'{round(torch.cuda.memory_allocated(0)/1024**3,2)} GB')
        self._pp("GPU reserved RAM", f'{round(torch.cuda.memory_reserved(0)/1024**3,2)} GB')
    except Exception as e:
      s += f'**Warning, No GPU: {e}'
      if (is_print is True):
        self._ph()
        self._pp("GPU", f"*Warning* No GPU: {e}")
    return s
  #
  # fetch info about host ip
  def fetch_info_host_ip(self, is_print=True):
    """
    Function to fetch current host name and ip address

    Args:
        None.

    Returns:
        s: (str) host name and ip info in human readable format.
    """
    s=''
    try:
      hostname = socket.gethostname()
      ip_address = socket.gethostbyname(hostname)
      s += f"Hostname: {hostname}\n"
      s += f"IP Address: {ip_address}\n"
      if (is_print is True):
        self._ph()
        self._pp('Host and Notebook', 'Info')
        self._ph()
        self._pp('Host Name', f"{hostname}")
        self._pp("IP Address", f"{ip_address}")
        try:
          from jupyter_server import serverapp 
          self._pp("Jupyter Server", f'{serverapp.__version__}')
        except ImportError:
          self._pp("Jupyter Server", "Not accessible")
        try:
          import notebook 
          self._pp("Jupyter Notebook", f'{notebook.__version__}')
        except ImportError:
          self._pp("Jupyter Notebook ", "Not accessible")
    except Exception as e:
      s += f"**Warning, No hostname: {e}"
      if (is_print is True):
        self._ph()
        self._pp('Host Name and Notebook', 'Not accessible')
    return s
  #
  #
  # fetch import libraries
  def _fetch_lib_import(self):

    """
    This function fetches all the imported libraries that are installed.

    Args:
        None

    Returns:
      x (list):
          list of strings containing the name of the imported libraries.
    """

    x = []
    for name, val in globals().items():
      if isinstance(val, types.ModuleType):
        x.append(val.__name__)
    x.sort()
    return x
  #
  # fetch lib version
  def _fetch_lib_version(self,lib_name):

    """
    This function fetches the version of the imported libraries.

    Args:
        lib_name (list):
            list of strings containing the name of the imported libraries.

    Returns:
        val (list):
            list of strings containing the version of the imported libraries.
    """

    val = []
    for x in lib_name:
      try:
        y = importlib.metadata.version(x)
        val.append(f'{x}=={y}')
      except Exception as e:
        val.append(f'|{x}==unknown_*or_system')
    val.sort()
    return val
  #
  # fetch the lib name and version
  def fetch_info_lib_import(self):
    """
    This function fetches all the imported libraries name and version that are installed.

    Args:
        None

    Returns:
      x (list):
          list of strings containing the name and version of the imported libraries.
    """
    x = self._fetch_lib_version(self._fetch_lib_import())
    return x
  #
  # write a file to local or cloud diskspace
  def write_file(self,fname, in_data):

    """
    Write a file to local or cloud diskspace or append to it if it already exists.

    Args:
        fname (str): The name of the file to write.
        in_data (list): The

    This is a utility function that writes a file to disk.
    The file name and text to write are passed in as arguments.
    The file is created, the text is written to it, and then the file is closed.

    Args:
        fname (str): The name of the file to write.
        in_data (list): The text to write to the file.

    Returns:
        None
    """

    if os.path.isfile(fname):
      f = open(fname, "a")
    else:
      f = open(fname, "w")
    f.writelines("\n".join(in_data))
    f.close()
    return
  #
  
  def fetch_installed_libraries(self):
    """
    Retrieves and prints the names and versions of Python libraries installed by the user,
    excluding the standard libraries.

    Args:
    -----
      None

    Returns:
    --------
    dictionary: (dict)
      A dictionary where keys are the names of the libraries and values are their respective versions.

    Examples:
    ---------
      libraries = get_installed_libraries()
      for name, version in libraries.items():
        print(f"{name}: {version}")
    """
    # List of standard libraries (this may not be exhaustive and might need updates based on the Python version)
    # Run pip freeze command to get list of installed packages with their versions
    result = subprocess.run(['pip', 'freeze'], stdout=subprocess.PIPE)
    
    # Decode result and split by lines
    packages = result.stdout.decode('utf-8').splitlines()

    # Split each line by '==' to separate package names and versions
    installed_libraries = {}
    for package in packages:
      try:
        name, version = package.split('==')
        installed_libraries[name] = version
      except Exception as e:
        #print(f'{package}: Error: {e}')
        pass
    return installed_libraries
  #
  #
  def fetch_match_file_dict(self, file_path, reference_dict):
    """
    Reads a file from the disk, creates an array with each line as an item,
    and checks if each line exists as a key in the provided dictionary. If it exists, 
    the associated value from the dictionary is also returned.

    Parameters:
    -----------
    file_path: str
        Path to the file to be read.
    reference_dict: dict
        Dictionary against which the file content (each line) will be checked.

    Returns:
    --------
    dict:
        A dictionary where keys are the lines from the file and values are either 
        the associated values from the reference dictionary or None if the key 
        doesn't exist in the dictionary.

    Raises:
    -------
    FileNotFoundError:
        If the provided file path does not exist.
    """

    if not os.path.exists(file_path):
        raise FileNotFoundError(f"The file at {file_path} does not exist.")

    with open(file_path, 'r') as file:
        lines = file.readlines()

    # Check if each line (stripped of whitespace and newline characters) exists in the reference dictionary.
    # If it exists, fetch its value. Otherwise, set the value to None.
    results = {line.strip(): reference_dict.get(line.strip().replace('_', '-'), None) for line in lines}

    return results
  # print fech_info about myself
  def print_info_self(self):

    """
    Prints information about the model/myself.

    Args:
        None

    Returns:
        None
    """
    self._ph()
    self._pp("Hello, I am", self.name)
    self._pp("I will display", "Python, Jupyter, and system info.")
    self._pp("Note", "For doc type: help(pluto) ...or help(your_object_name)")
    self._pp("Let Rock and Roll", "¯\_(ツ)_/¯")
    # system
    x = self.fetch_info_system(is_print=True)
    # print(x)
    # self._ph()
    # gpu
    # self._pp('GPU', 'Info')
    x = self.fetch_info_gpu(is_print=True)
    # print(x)
    self._ph()
    # lib used
    self._pp('Installed lib from', self.fname_requirements)
    self._ph()
    x = self.fetch_match_file_dict(self.fname_requirements, self.fetch_installed_libraries())
    for item, value in x.items():
      self._pp(f'{item} version', value)
    #
    self._ph()
    self._pp('Standard lib from', 'System')
    self._ph()
    self._pp('matplotlib version', matplotlib.__version__)
    self._pp('numpy version', numpy.__version__)
    self._pp('pandas version',pandas.__version__)
    self._pp('PIL version', PIL.__version__)
    self._pp('torch version', torch.__version__)
    #
    self.print_ml_libraries()
    # host ip
    x = self.fetch_info_host_ip()
    # print(x)
    self._ph()
    #
    return
  #
  def print_ml_libraries(self):
    """
    Checks for the presence of Gradio, fastai, huggingface_hub, and transformers libraries.

    Prints a message indicating whether each library is found or not.
    If a library is not found, it prints an informative message specifying the missing library.
    """
    self._ph()
    self._pp("ML Lib", "Info")
    try:
      import fastai
      self._pp("fastai", f"{fastai.__version__}")
    except ImportError:
      self._pp("fastai", "*Warning* library not found.")
    #
    try:
      import transformers
      self._pp("transformers", f"{transformers.__version__}")
    except ImportError:
      self._pp("transformers", "*Warning* library not found.") 
    #
    try:
      import diffusers
      self._pp("diffusers", f"{diffusers.__version__}")
    except ImportError:
      self._pp("diffusers",  "*Warning* library not found.") 
    #
    try:
      import gradio
      self._pp("gradio", f"{gradio.__version__}")
    except ImportError:
      self._pp("Gradio", "*Warning* library not found.")

    try:
      import huggingface_hub
      self._pp("HuggingFace Hub", f"{huggingface_hub.__version__}")
    except ImportError:
      self._pp("huggingface_hub", "*Warning* library not found.")
    return
  #
  def print_learner_meta_info(self, learner):
    """
      Print all the leaner meta data and more.

      Args: None

      Return: None
    """
    self._ph()
    self._pp("Name", learner._meta_project_name)
    self._ph()
    self._pp("Error_rate", learner._meta_error_rate)
    self._pp("Base Model", learner._meta_base_model_name)
    self._pp("Data Source", learner._meta_data_source)
    self._pp("Data Info", learner._meta_data_info)
    try:
      t = time.strftime('%Y-%b-%d %H:%M:%S %p', time.gmtime(learner._meta_training_unix_time))
    except Exception as e:
      t = learner._meta_training_unix_time
    self._pp("Time Stamp", t)
    # self._pp("Time Stamp", learner._meta_training_unix_time)
    self._pp("Learning Rate", learner.lr)
    self._pp("Base Learning Rate", learner._meta_base_lr)
    self._pp("Batch Size", learner.dls.bs)
    self._pp("Momentum", learner.moms)
    self._pp("AI Dev Stack", learner._meta_ai_dev_stack)
    self._pp("Learner Vocab", learner.dls.vocab)
    self._pp("Learner Vocab Size", len(learner.dls.vocab))
    #
    self._ph()
    self._pp("Author", learner._meta_author)
    self._pp("AI Assistant", learner._meta_ai_assistant)
    self._pp("GenAI Coder", learner._meta_genai)
    self._pp("[Friends] Human Coder", learner._meta_human_coder)
    self._pp("License", learner._meta_license)
    #
    self._ph()
    self._pp("Conclusion", learner._meta_notes)
    self._ph()
    return
  # 
# 
# add module/method
#
import functools
def add_method(cls):
  def decorator(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
      return func(*args, **kwargs)
    setattr(cls, func.__name__, wrapper)
    return func # returning func means func can still be used normally
  return decorator
#
# [END OF pluto_happy]
#
#
# ----------[END OF CODE]----------
# %%write -a app.py
# prompt: create the new class foxy from Pluto_FastAI

# wake up foxy
foxy = Pluto_Happy('Foxy, the seeker of truth.')
# %%write -a app.py
# check out my environments

foxy.fname_requirements = './requirements.txt'
foxy.print_info_self()
# %%write -a app.py
# prompt: find a 8 length hash number for a string

import hashlib
import fastai
import fastai.learner
import gradio

def generate_hash(text, max_length=8):
  """Generates an x-length hash for a given string."""
  hash_object = hashlib.md5(text.encode())
  hash_hex = hash_object.hexdigest()
  return hash_hex[:max_length]

# # Read the file content
# file_content = os.environ['huggingface_key']

# # Generate the 8-length hash
# hash_value = generate_hash(file_content)
# print(f"The 8-length hash for the file is: {hash_value}")
# %%write -a app.py
# prompt: manual

def is_system_verified():
  if (generate_hash(os.environ['huggingface_key']) == '15d797fe'):
    return (True)
  else:
    return (False)
# %%write -a app.py
# prompt: using fast.ai to load image learner from file butterfly_learner_1703921531_loss_0.061586.pkl

# from fastai.learner import load_learner
import fastai
import fastai.learner
fname = "./butterfly_learner_1722973740.pkl"
foxy.learner = fastai.learner.load_learner(fname)
# %%write -a app.py

import datetime
foxy.print_learner_meta_info(foxy.learner)
# %%write -a app.py
# prompt: combine the above code cells in the "Predict using download images" into a function with documentation.

@add_method(Pluto_Happy)
def predict_butterfly(self, img_pil, return_top=3):

  """
  Predict a butterfly image from a list of downloaded images.

  Args:
    img_pil: (PIL image) the image to be predict.
    return_top: (int) the maximum number of perdiction to return.
      the default is 3.

  Returns:
    (list) An array of the prediction (dictionary):
      1. classification: (str) the classification prediction
      2. accuracy score: (float) the accuracy value of the prediction
      3. index: (int) the index of the prediction array
      4. pre_arr: (list) the the prediction array
      5. file_name: (str) the full-path file name of the image.
  """
  names = []
  values = []

  # predict image
  a1,b1,c1 = self.learner.predict(img_pil)

  # prompt: covert c1 to a list
  predict_list = c1.tolist()
  #print(predict_list)

  # prompt: print the top 3 largest number and index of the predict_list
  top_x = sorted(range(len(predict_list)), key=lambda k: predict_list[k], reverse=True)[:return_top]
  #print(top_3)

  # prompt: show the name in the foxy.vocab using the top_3 as index
  for idx in top_x:
    # print(f"name: {foxy.learner.dls.vocab[idx]}, value: {predict_list[idx]}")
    names.append(foxy.learner.dls.vocab[idx])
    values.append(predict_list[idx])
  #

  return names, values

# %%write -a app.py
# prompt: (Gemini and codey)
# prompt: use matplotlib to draw a donut graph taking a list as name and list of value as input
# prompt: add value to the label in the draw_donut_chart function
# prompt: replace the white center of the draw_donut_chart function with an image
# prompt: add text line to matplotlib plot bottom left position
# prompt: change the draw_donut_graph function to use matplotlib.pyplot.subplots

import matplotlib

@add_method(Pluto_Happy)
def draw_donut_chart(self, names, values, img_center=None,
  title="Donut Chart", figsize=(12, 6), is_show_plot=False):
  """
  Creates a donut chart using Matplotlib, with 4 distinct colors for up to 4 items.

  Args:
      names (list): A list of names for the slices of the donut chart (max 4).
      values (list): A list of numerical values corresponding to the slices.
      img_center: (PIL or None) the center image or white blank image.
      title (str, optional): The title of the chart. Defaults to "Donut Chart".
      figsize (tuple, optional): The size of the figure in inches. Defaults to (8, 6).
  """

  total = sum(values)
  values = [value / total * 100 for value in values]

  fig, ax = matplotlib.pyplot.subplots(figsize=figsize)

  # #FF6F61 (coral), #6B5B95 (purple), #88B04B (green), #F7CAC9 (pink)
  colors = ['#257180', '#F2E5BF', '#FD8B51', self.color_secondary]  # Define 4 distinct colors
  # colors = [self.color_primary, self.color_success, self.color_info, self.color_secondary]
  wedges, texts = ax.pie(values, labels=names, wedgeprops=dict(width=0.6), colors=colors[:len(names)])  # Use the first 4 colors
  legend_title = [f"{name} ({value:.2f}%)" for name, value in zip(names, values)]
  ax.legend(wedges, legend_title, loc='best') # was loc="upper right"

  # Add an image to the center of the donut chart
  # image_path = "/content/butterfly_img/Monarch460CL.jpg"
  # img = matplotlib.image.imread(image_path)
  fig = matplotlib.pyplot.gcf()
  if img_center is None:
    center_circle = matplotlib.pyplot.Circle((0, 0), 0.4, fc='white', ec='#333333')
    ax.add_artist(center_circle)
  else:
    # img = PIL.Image.open(img_center_path)
    ax.imshow(img_center, extent=(-0.5, 0.5, -0.5, 0.5))
  t = f"{title}:\n{names[0]}, {round(values[0], 2)}% certainty"
  ax.set_title(t, fontsize=16)
  ax.set_axis_off()
  #
  copyw = f"*{self.author}, [AI] {self.name} (GNU 3.0) 2024"
  ax.text(x=0.05, y=0.05, s=copyw, ha='left', va='bottom',
    fontsize=7.0, transform=ax.transAxes)
  #
  fig.tight_layout()
  if (is_show_plot is True):
    fig.show()
    print("show me")
    # plt.show()
  return fig

# %%write -a app.py
# manual

# define all components use in Gradio
xtitle = """
🦋 Welcome: Butterfly CNN Image Classification App

### Identify 75 Butterfly Species From Photo.

>**Requirement Statement:** (From the client) We aim to boost butterfly numbers by creating and maintaining suitable habitats, promoting biodiversity, and implementing conservation measures that protect them from threats such as habitat loss, climate change, and pesticides.
>
>**Problem Facing:** Butterfly populations are decreasing due to habitat loss, climate change, and pesticides. This issue endangers their diversity and risks essential pollination services, impacting food production and natural environments. We need the **butterfly population count** from around the world to assess the damage.
>
> This real-world CNN app is from the ["AI Solution Architect," by ELVTR and Duc Haba](https://elvtr.com/course/ai-solution-architect?utm_source=instructor&utm_campaign=AISA&utm_content=linkedin).

---

### 🌴 Helpful Instruction:

1. Take a picture or upload a picture.

2. Click the "Submit" button.
3. View the result on the Donut plot.
4. (Optional) Rate the correctness of the identification.
"""
xdescription = """

---

### 🌴 Author Note:

- The final UI is a sophisticated iOS, Android, and web app developed by the UI team. It may or may not include the donut graph, but they all utilize the same REST input-output JSON API.

- *I hope you enjoy this as much as I enjoyed making it.*

- **For Fun:** Upload your face picture and see what kind of butterfly you are.

---

"""
xallow_flagging = "manual"
xflagging_options = ["Good", "Bad"]
xarticle = """

---

### 🌻 About:

- Develop by Duc Haba (human) and GenAI partners (2024).
  - AI Codey (for help in coding)
  - AI GPT-4o (for help in coding)
  - AI Copilot (for help in coding)

- Python Jupyter Notebook on Google Colab Pro.
  - Python 3.10
  - 8 CPU Cores (Intel Xeon)
  - 60 GB RAM
  - 1 GPU (Tesla T4)
  - 15 GB GPU RAM
  - 254 GB Disk Space

- Primary Lib:
  - Fastai (2.7.17)
- Standard Lib:
  - PyTorch
  - Gradio
  - PIL
  - Matplotlib
  - Numpy
  - Pandas

- Dataset (labled butterfly images)
  - Kaggle website
  - The University of Florida's McGuire Center for Lepidoptera and Biodiversity (United States)

- Deployment Model and Hardware:
  - Butterfly CNN model (inference engine)
  - 2 CPU Cores (Intel Xeon)
  - 16 GB RAM
  - No GPU
  - 16 GB Disk Space
  - Virtual container (for scaleability in server-cluster)
  - No Data and no other ML or LLM
  - Own 100% Intellectual Property

---
### 🤔 Accuracy and Benchmark

**Task:** Indentify 75 type of butterfly species from user taking photo with their iPhone.

- **94.1% Accurate**: This Butterfly CNN Image Classification developed by Duc Haba and GenAI friends (Deep Learning, CNN)

- **Average 87.5% Accurate**: Lepidopterist (human)

- **Less than 50% Accurate**: Generative AI, like Genini or Claude 3.5 (AI)

(NOTE: Lepidopterist and GenAI estimate are from online sources and GenAI.)

---

### 🦋 KPIs (Key Performance Indicator by Client)


1. **AI-Powered Identification:** The app leverages an advanced CNN model to achieve identification accuracy on par with or surpassing that of expert lepidopterists. It quickly and precisely recognizes butterfly species from user-uploaded images, making it an invaluable tool for butterfly enthusiasts, citizen scientists, and researchers.
  - Complied. Detail on seperate document.

2. **Accessible API for Integration:** We'll expose an API to integrate the AI with mobile and web apps. It will encourage open-source developers to build hooks into existing or new apps.
  - Complied. Detail on seperate document.

3. **Universal Access:** The Butterfly app is for everyone, from citizens to experts. We want to create a community that cares about conservation.
  - Complied. Detail on seperate document.

4. **Shared Database for Research:** Our solution includes
a shared database that will hold all collected data. It will
be a valuable resource for researchers studying butterfly populations, their distribution, and habitat changes. The database will consolidate real-world data to support scientific research and comprehensive conservation planning.
  - Complied. Detail on seperate document.

5. **Budget and Schedule:** *Withheld.*
  - Complied ...mostly :-)

---

### 🤖 The First Law of AI Collaboration:
- This CNN Image Classification app development is in compliance with [The First Law of AI Collaboration](https://www.linkedin.com/pulse/first-law-ai-collaboration-duc-haba-hcqkc/)

---

### 🌟 "AI Solution Architect" Course by ELVTR

>Welcome to the fascinating world of AI and Convolutional Neural Network (CNN) Image Classification. This CNN model is a part of one of three hands-on application. In our journey together, we will explore the [AI Solution Architect](https://elvtr.com/course/ai-solution-architect?utm_source=instructor&utm_campaign=AISA&utm_content=linkedin) course, meticulously crafted by ELVTR in collaboration with Duc Haba. This course is intended to serve as your gateway into the dynamic and constantly evolving field of AI Solution Architect, providing you with a comprehensive understanding of its complexities and applications.

>An AI Solution Architect (AISA) is a mastermind who possesses a deep understanding of the complex technicalities of AI and knows how to creatively integrate them into real-world solutions. They bridge the gap between theoretical AI models and practical, effective applications. AISA works as a strategist to design AI systems that align with business objectives and technical requirements. They delve into algorithms, data structures, and computational theories to translate them into tangible, impactful AI solutions that have the potential to revolutionize industries.

> 🍎 [Sign up for the course today](https://elvtr.com/course/ai-solution-architect?utm_source=instructor&utm_campaign=AISA&utm_content=linkedin), and I will see you in class.

- An article about the Butterfly CNN Image Classification will be coming soon.

---

### 🙈 Legal:

- The intent is to share with Duc's friends and students in the AI Solution Architect course by ELVTR, but for those with nefarious intent, this Butterfly CNN Image Classification is governed by the GNU 3.0 License: https://www.gnu.org/licenses/gpl-3.0.en.html
- Author: Copyright (C), 2024 **[Duc Haba](https://linkedin.com/in/duchaba)**
---
"""
# xinputs = ["image"]
xinputs = [gradio.Image(type="pil")]
xoutputs = ["plot"]
# %%write -a app.py
# prompt: write a python code using gradio for simple hello world app
# prompt: show all the possible parameters from gradio Interface function
# manual: edit the rest

def say_butterfly_name(img):
  # check for access
  if(is_system_verified() is False):
    fname = "ezirohtuanU metsyS"[::-1]
    names = [fname]
    values= [1.0]
    return names, values
  #
  names, values = foxy.predict_butterfly(img)
  # add in the other
  names.append("All Others")
  values.append(1-sum(values))
  # #   val.append(item)
  xcanvas = foxy.draw_donut_chart(names, values,
    img_center=img,
    title="Top 3 (out of 75) Butterfly CNN Prediction",
    is_show_plot=False,
    figsize=(9,9))
  return xcanvas
#
#
# theme, "base, default, glass, soft, monochrome"
app = gradio.Interface(fn=say_butterfly_name,
  inputs=xinputs,
  outputs=xoutputs,
  live=False,
  allow_duplication=False,
  theme="soft",
  title=xtitle,
  description=xdescription,
  article=xarticle,
  allow_flagging=xallow_flagging,
  flagging_options=xflagging_options)
#
inline = True
width = "80%"
height = "80%" # 1200
app.launch()
# app.launch(debug=True)