|
|
|
|
|
|
|
import pynvml |
|
|
|
import numpy |
|
import PIL |
|
import pandas |
|
import matplotlib |
|
import torch |
|
|
|
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 |
|
|
|
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. |
|
""" |
|
|
|
|
|
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() |
|
|
|
|
|
self.fname_requirements = './pluto_happy/requirements.txt' |
|
|
|
self.color_primary = '#2780e3' |
|
self.color_secondary = '#373a3c' |
|
self.color_success = '#3fb618' |
|
self.color_info = '#9954bb' |
|
self.color_warning = '#ff7518' |
|
self.color_danger = '#ff0039' |
|
self.color_mid_gray = '#495057' |
|
self._xkeyfile = '.xoxo' |
|
return |
|
|
|
|
|
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) |
|
|
|
""" |
|
|
|
x = f'{"%34s" % str(a)} : {str(b)}' |
|
y = None |
|
if (is_print): |
|
print(x) |
|
else: |
|
y = x |
|
return y |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
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='' |
|
|
|
cpu_usage = psutil.cpu_percent() |
|
|
|
mem = psutil.virtual_memory() |
|
|
|
mem_total_gb = mem.total / (1024 ** 3) |
|
mem_available_gb = mem.available / (1024 ** 3) |
|
mem_used_gb = mem.used / (1024 ** 3) |
|
|
|
|
|
|
|
s += f"Total memory: {mem_total_gb:.2f} GB\n" |
|
s += f"Available memory: {mem_available_gb:.2f} GB\n" |
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
|
|
|
|
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}") |
|
""" |
|
|
|
|
|
result = subprocess.run(['pip', 'freeze'], stdout=subprocess.PIPE) |
|
|
|
|
|
packages = result.stdout.decode('utf-8').splitlines() |
|
|
|
|
|
installed_libraries = {} |
|
for package in packages: |
|
try: |
|
name, version = package.split('==') |
|
installed_libraries[name] = version |
|
except Exception as 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() |
|
|
|
|
|
|
|
results = {line.strip(): reference_dict.get(line.strip().replace('_', '-'), None) for line in lines} |
|
|
|
return results |
|
|
|
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", "¯\_(ツ)_/¯") |
|
|
|
x = self.fetch_info_system(is_print=True) |
|
|
|
|
|
|
|
|
|
x = self.fetch_info_gpu(is_print=True) |
|
|
|
self._ph() |
|
|
|
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() |
|
|
|
x = self.fetch_info_host_ip() |
|
|
|
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("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 |
|
|
|
|
|
|
|
|
|
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 |
|
return decorator |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foxy = Pluto_Happy('Foxy, the seeker of truth.') |
|
|
|
|
|
|
|
foxy.fname_requirements = './requirements.txt' |
|
foxy.print_info_self() |
|
|
|
|
|
|
|
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] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def is_system_verified(): |
|
if (generate_hash(os.environ['huggingface_key']) == '15d797fe'): |
|
return (True) |
|
else: |
|
return (False) |
|
|
|
|
|
|
|
|
|
import fastai |
|
import fastai.learner |
|
fname = "./butterfly_learner_1722973740.pkl" |
|
foxy.learner = fastai.learner.load_learner(fname) |
|
|
|
|
|
import datetime |
|
foxy.print_learner_meta_info(foxy.learner) |
|
|
|
|
|
|
|
@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 = [] |
|
|
|
|
|
a1,b1,c1 = self.learner.predict(img_pil) |
|
|
|
|
|
predict_list = c1.tolist() |
|
|
|
|
|
|
|
top_x = sorted(range(len(predict_list)), key=lambda k: predict_list[k], reverse=True)[:return_top] |
|
|
|
|
|
|
|
for idx in top_x: |
|
|
|
names.append(foxy.learner.dls.vocab[idx]) |
|
values.append(predict_list[idx]) |
|
|
|
|
|
return names, values |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
colors = ['#257180', '#F2E5BF', '#FD8B51', self.color_secondary] |
|
|
|
wedges, texts = ax.pie(values, labels=names, wedgeprops=dict(width=0.6), colors=colors[:len(names)]) |
|
legend_title = [f"{name} ({value:.2f}%)" for name, value in zip(names, values)] |
|
ax.legend(wedges, legend_title, loc='best') |
|
|
|
|
|
|
|
|
|
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: |
|
|
|
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") |
|
|
|
return fig |
|
|
|
|
|
|
|
|
|
|
|
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 = [gradio.Image(type="pil")] |
|
xoutputs = ["plot"] |
|
|
|
|
|
|
|
|
|
|
|
def say_butterfly_name(img): |
|
|
|
if(is_system_verified() is False): |
|
fname = "ezirohtuanU metsyS"[::-1] |
|
names = [fname] |
|
values= [1.0] |
|
return names, values |
|
|
|
names, values = foxy.predict_butterfly(img) |
|
|
|
names.append("All Others") |
|
values.append(1-sum(values)) |
|
|
|
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 |
|
|
|
|
|
|
|
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%" |
|
app.launch() |
|
|