import os import gradio as gr from pandas import Series, DataFrame import pandas as pd import sys import numpy as np from Interface.empirical_parameter_calculator import EmpiricalParams from pymatgen.core.periodic_table import Element from pip._internal import main main(['install', 'joblib']) main(['install', 'matminer']) import joblib ''' Get the path of all of the saved models. ''' lr_compressive_strength = "./Model/saved_model/lr/compressive_strength.pkl" lr_elongation = "./Model/saved_model/lr/elongation.pkl" lr_hardness = "./Model/saved_model/lr/hardness.pkl" lr_plasticity ="./Model/saved_model/lr/plasticity.pkl" lr_tensile_strength ="./Model/saved_model/lr/tensile_strength.pkl" lr_yield_strength ="./Model/saved_model/lr/yield_strength.pkl" kmeans_ssl_compressive_strength = "./Model/saved_model/kmeans_ssl/compressive_strength.pkl" kmeans_ssl_elongation = "./Model/saved_model/kmeans_ssl/elongation.pkl" kmeans_ssl_hardness = "./Model/saved_model/kmeans_ssl/hardness.pkl" kmeans_ssl_plasticity = "./Model/saved_model/kmeans_ssl/plasticity.pkl" kmeans_ssl_tensile_strength = "./Model/saved_model/kmeans_ssl/tensile_strength.pkl" kmeans_ssl_yield_strength = "./Model/saved_model/kmeans_ssl/yield_strength.pkl" gmm_ssl_compressive_strength = "./Model/saved_model/gmm_ssl/compressive_strength.pkl" gmm_ssl_elongation = "./Model/saved_model/gmm_ssl/elongation.pkl" gmm_ssl_hardness ="./Model/saved_model/gmm_ssl/hardness.pkl" gmm_ssl_plasticity = "./Model/saved_model/gmm_ssl/plasticity.pkl" gmm_ssl_tensile_strength = "./Model/saved_model/gmm_ssl/tensile_strength.pkl" gmm_ssl_yield_strength = "./Model/saved_model/gmm_ssl/yield_strength.pkl" ''' Get the composition of input alloy. ''' def normalize_molar_ratios(ratios): normalized_ratios = list() ele_sum = sum(ratios) for ele in ratios: ele = float(ele / ele_sum) normalized_ratios.append(ele) return normalized_ratios ''' Load the saved ML models. ''' def predict_input(path): with open(path, 'rb') as p: loaded_model = joblib.load(p) return loaded_model ''' Predict the six properties of the input alloy, with Linear Regression Models, K-Means Semi-supervised Model and GMM Semi-supervised Model. ''' def pred(Al, B, C, Co, Cr, Cu, Fe, Ga, Ge, Hf, Li, Mg, Mn, Mo, N, Nb, Ni, Sc, Si, Sn, Ta, Ti, V, W, Y, Zn, Zr, operation): # Get the model acceptable composition format. # print(B) comp = {"Al": Al, "B": B, "C": C, "Co": Co, "Cr": Cr, "Cu": Cu, "Fe": Fe, "Ga": Ga, "Ge": Ge, "Hf": Hf, "Li": Li, "Mg": Mg, "Mn": Mn, "Mo": Mo, "N": N, "Nb": Nb, "Ni": Ni, "Sc": Sc, "Si": Si, "Sn": Sn, "Ta": Ta, "Ti": Ti, "V": V, "W": W, "Y": Y, "Zn": Zn, "Zr": Zr} df_values = normalize_molar_ratios(comp.values()) df = pd.DataFrame(data=[df_values], columns=["Al", "B", "C", "Co", "Cr", "Cu", "Fe", "Ga", "Ge", "Hf", "Li", "Mg", "Mn", "Mo", "N", "Nb", "Ni", "Sc", "Si", "Sn", "Ta", "Ti", "V", "W", "Y", "Zn", "Zr"]) # print(df) Composition = "" index = 0 for k, v in comp.items(): if v != 0: Composition = Composition + k + str(round(df_values[index], 2)) index += 1 # print(Composition) # print(comp.values()) if operation=="Linear Regression": #Using RandomForest to predict properties. Hardness = predict_input(lr_hardness).predict(df) YieldStrength=predict_input(lr_yield_strength).predict(df) TensileStrength=predict_input(lr_tensile_strength).predict(df) Elongation=predict_input(lr_elongation).predict(df) CompressiveStrength=predict_input(lr_compressive_strength).predict(df) Plasticity=predict_input(lr_plasticity).predict(df) # print("1") elif operation=="K-Means Semi-supervisor Model": #Using KNN & RandomForest to predict properties. Hardness = predict_input(kmeans_ssl_hardness).predict(df) YieldStrength = predict_input(kmeans_ssl_yield_strength).predict(df) TensileStrength = predict_input(kmeans_ssl_tensile_strength).predict(df) Elongation = predict_input(kmeans_ssl_elongation).predict(df) CompressiveStrength = predict_input(kmeans_ssl_compressive_strength).predict(df) Plasticity = predict_input(kmeans_ssl_plasticity).predict(df) # print("2") elif operation == "GMM Semi-supervisor Model": # Using semi_supervisor Label Propagation to predict properties. Hardness = predict_input(gmm_ssl_hardness).predict(df) YieldStrength = predict_input(gmm_ssl_yield_strength).predict(df) TensileStrength = predict_input(gmm_ssl_tensile_strength).predict(df) Elongation = predict_input(gmm_ssl_elongation).predict(df) CompressiveStrength = predict_input(gmm_ssl_compressive_strength).predict(df) Plasticity = predict_input(gmm_ssl_plasticity).predict(df) Hardness = round(float(Hardness),2) YieldStrength = round(float(YieldStrength),2) TensileStrength = round(float(TensileStrength),2) Elongation = round(float(Elongation),2) CompressiveStrength = round(float(CompressiveStrength),2) Plasticity = round(float(Plasticity),2) return Composition, Hardness, YieldStrength, TensileStrength, Elongation, CompressiveStrength, Plasticity ''' Import the function to Calculate the empirical parameters. ''' def empirical_parameter(Al, B, C, Co, Cr, Cu, Fe, Ga, Ge, Hf, Li, Mg, Mn, Mo, N, Nb, Ni, Sc, Si, Sn, Ta, Ti, V, W, Y, Zn, Zr): # Get the model acceptable composition format. # print(B) comp = {"Al": Al, "B": B, "C": C, "Co": Co, "Cr": Cr, "Cu": Cu, "Fe": Fe, "Ga": Ga, "Ge": Ge, "Hf": Hf, "Li": Li, "Mg": Mg, "Mn": Mn, "Mo": Mo, "N": N, "Nb": Nb, "Ni": Ni, "Sc": Sc, "Si": Si, "Sn": Sn, "Ta": Ta, "Ti": Ti, "V": V, "W": W, "Y": Y, "Zn": Zn, "Zr": Zr} df_values = normalize_molar_ratios(comp.values()) print(df_values) df_element=[] df_ratio=[] index = 0 for key, value in comp.items(): if int(value) != 0: df_element.append(key) df_ratio.append(df_values[index]) index += 1 print(df_element) print(df_ratio) df_elements = [] for i in df_element: df_elements.append(Element[i]) print(df_elements) input_ele = EmpiricalParams(element_list=df_elements,mol_ratio=df_ratio) # 1. Calculate the entropy mixing. para1 = round(float(input_ele.entropy_mixing()),2) #2. Calculate the average atomic radius. para2 = round(float(input_ele.mean_atomic_radius()),2) #3. Calculate the atomic size difference. para3 = round(float(input_ele.atomic_size_difference()),2) #4. Calculate the enthalpy of mixing. para4= round(float(input_ele.enthalpy_mixing()),2) #5. Calculate the standard deviation of enthalpy. para5= round(float(input_ele.std_enthalpy_mixing()),2) #6. Calculate the average melting point. para6= round(float(input_ele.average_melting_point()),2) #7. Calculate the standard melting point. para7= round(float(input_ele.std_melting_point()),2) #8. Calculate the average electronegativity. para8= round(float(input_ele.mean_electronegativity()),2) #9. Calculate the standard deviation of electronegativity. para9= round(float(input_ele.std_electronegativity()),2) #10. Calculate the valence electron concentration. para10= round(float(input_ele.average_vec()),2) #11. Calculate the standard deviation of valence electron concentration. para11= round(float(input_ele.std_vec()),2) #12. Calculate the omega. para12= round(float(input_ele.calc_omega()),2) #13. Calculate the density. para13= round(float(input_ele.calc_density()),2) #14. Calculate the price. para14= round(float(input_ele.calc_price()),2) return para1, para2, para3, para4,para5, para6, para7, para8, para9, para10, para11, para12, para13, para14 ''' The function of Clear button. ''' def clear_input(): return 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "", 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ''' Interface Details ''' with gr.Blocks() as demo: # The title and description of interface. gr.Markdown("# Multi Principal Element Alloy Property Predictor") gr.Markdown("Input alloy composition to obtain output of alloy properties (sum of composition should be equal to 100)") # The section to select the Machine Learning Model. with gr.Row(): operation = gr.inputs.Radio(["Linear Regression","K-Means Semi-supervisor Model", "GMM Semi-supervisor Model"]) # The section to input the element ratio. with gr.Row(): Al = gr.Number(label="Al") B = gr.Number(label="B") C = gr.Number(label="C") Co = gr.Number(label="Co") Cr = gr.Number(label="Cr") Cu = gr.Number(label="Cu") Fe = gr.Number(label="Fe") Ga = gr.Number(label="Ga") Ge = gr.Number(label="Ge") Hf = gr.Number(label="Hf") Li = gr.Number(label="Li") Mg = gr.Number(label="Mg") Mn = gr.Number(label="Mn") Mo = gr.Number(label="Mo") N = gr.Number(label="N") Nb = gr.Number(label="Nb") Ni = gr.Number(label="Ni") Sc = gr.Number(label="Sc") Si = gr.Number(label="Si") Sn = gr.Number(label="Sn") Ta = gr.Number(label="Ta") Ti = gr.Number(label="Ti") V = gr.Number(label="V") W = gr.Number(label="W") Y = gr.Number(label="Y") Zn = gr.Number(label="Zn") Zr = gr.Number(label="Zr") Composition = gr.Text(label="Alloy Composition") #Action buttons.("Clear" and "Prediction") with gr.Row(): clear = gr.Button("Clear") submit = gr.Button("Predict") #The prediction result(Six mechanical properties) of ML models. with gr.Row(): Hardness = gr.Number(label="Hardness (VHN)") YieldStrength = gr.Number(label="Yield Strength (MPa)") TensileStrength = gr.Number(label="Tensile Strength (MPa)") Elongation = gr.Number(label="Elongation (%)") CompressiveStrength = gr.Number(label="Compressive Strength (MPa)") Plasticity = gr.Number(label="Plasticity (from compression)") #Calculer resutl of empirical parameters. with gr.Row(): entropy_mixing = gr.Number(label="Entropy of Mixing (J/K*mol)") average_atomic_radius = gr.Number(label="Average Atomic Radius (Angstroms)") atomic_size_dif = gr.Number(label="Atomic Size Difference") enthalpy_mixing = gr.Number(label="Enthalpy of Mixing (kJ/mol)") std_deviation_enthalpy = gr.Number(label="Standard Deviation of Enthalpy") average_melting_point = gr.Number(label="Average Melting Point (Tm, in Celcius)") std_deviation_melting_point = gr.Number(label="Standard Deviation of Melting Point") average_electronegativity = gr.Number(label="Average Electronegativity (X)") std_deviation_electronegativity= gr.Number(label="Standard Deviation of Electronegativity") valence_electron_concentration = gr.Number(label="Valence Electron Concentration (VEC)") std_deviation_valence_electron_concentration = gr.Number(label="Standard Deviation of Valence Electron Concentration (VEC)") omega = gr.Number(label="The Unitless Parameter Omega") density = gr.Number(label="Density (g/cm^3)") price = gr.Number(label="Price (USD/kg)") # Define the action of "Clear" button. clear.click(fn=clear_input, inputs=[], outputs=[Al, B, C, Co, Cr, Cu, Fe, Ga, Ge, Hf, Li, Mg, Mn, Mo, N, Nb, Ni, Sc, Si, Sn, Ta, Ti, V, W, Y, Zn, Zr, Composition, Hardness, YieldStrength, TensileStrength, Elongation, CompressiveStrength, Plasticity, entropy_mixing, average_atomic_radius, atomic_size_dif, enthalpy_mixing, std_deviation_enthalpy, average_melting_point, std_deviation_melting_point, average_electronegativity, std_deviation_electronegativity, valence_electron_concentration, std_deviation_valence_electron_concentration, omega, density, price]) # Define the action of "Predict" button. # 1.Predict the result of "Composition", "Hardness", "YieldStrength", "TensileStrength", "Elongation", "CompressiveStrength", "Plasticity". submit.click(fn=pred, inputs=[Al, B, C, Co, Cr, Cu, Fe, Ga, Ge, Hf, Li, Mg, Mn, Mo, N, Nb, Ni, Sc, Si, Sn, Ta, Ti, V, W, Y, Zn, Zr,operation], outputs=[Composition, Hardness, YieldStrength, TensileStrength, Elongation, CompressiveStrength, Plasticity]) # 2.Activate the empirical parameter calculator. submit.click(fn=empirical_parameter, inputs=[Al, B, C, Co, Cr, Cu, Fe, Ga, Ge, Hf, Li, Mg, Mn, Mo, N, Nb, Ni, Sc, Si, Sn, Ta, Ti, V, W, Y, Zn, Zr], outputs=[entropy_mixing, average_atomic_radius, atomic_size_dif, enthalpy_mixing, std_deviation_enthalpy, average_melting_point, std_deviation_melting_point, average_electronegativity, std_deviation_electronegativity, valence_electron_concentration, std_deviation_valence_electron_concentration, omega, density, price]) ''' Launch the interface. ''' if __name__ == "__main__": # Run the Alloy Property Predictor Interface, without public URL. # demo.launch() # Run the Alloy Property Predictor Interface, with a public URL. demo.launch(share="True")