Spaces:
Sleeping
Sleeping
import os | |
import csv | |
import gradio as gr | |
import tensorflow as tf | |
import numpy as np | |
import pandas as pd | |
from datetime import datetime | |
import utils | |
from huggingface_hub import Repository | |
import itertools | |
# Unique phase elements | |
# Load access tokens | |
WRITE_TOKEN = os.environ.get("WRITE_PER") # write | |
# Logs repo path | |
dataset_url = "https://huggingface.co/datasets/sandl/upload_alloy_hardness" | |
dataset_path = "logs_alloy_hardness.csv" | |
scaling_factors = {'PROPERTY: Calculated Density (g/cm$^3$)': (2.7, 13.7), | |
'PROPERTY: Calculated Young modulus (GPa)': (66, 336), | |
'PROPERTY: HV': (94.7, 1183.0)} | |
input_mapping = {'PROPERTY: BCC/FCC/other': {'BCC': 0, 'FCC': 1, 'OTHER': 2},# 'nan': 2}, | |
'PROPERTY: Processing method': {'ANNEAL': 0, 'CAST': 1, 'OTHER': 2, 'POWDER': 3, 'WROUGHT': 4},# 'nan': 2}, | |
'PROPERTY: Microstructure': {'B2': 0, 'B2+BCC': 1, 'B2+Sec.': 2, 'BCC': 3, 'BCC+B2': 4, 'BCC+B2+Laves': 5, | |
'BCC+B2+Sec.': 6, 'BCC+BCC': 7, 'BCC+BCC+HCP': 8, 'BCC+BCC+Laves(C15)': 9, | |
'BCC+FCC': 10, 'BCC+HCP': 11, 'BCC+Laves': 12, 'BCC+Laves(C14)': 13, | |
'BCC+Laves(C15)': 14, 'BCC+Laves+Sec.': 15, 'BCC+Sec.': 16, 'FCC': 17, | |
'FCC+B2': 18, 'FCC+B2+Sec.': 19, 'FCC+BCC': 20, 'FCC+BCC+B2': 21, 'FCC+BCC+B2+Sec.': 22, | |
'FCC+BCC+Sec.': 23, 'FCC+FCC': 24, 'FCC+HCP': 25, 'FCC+L12': 26, 'FCC+L12+Sec.': 27, | |
'FCC+Sec.': 28, 'OTHER': 29}, #'nan': 29}, | |
'PROPERTY: Single/Multiphase': {'M': 0, 'S': 1, 'OTHER': 3}} #'nan': 3, | |
unique_phase_elements = ['B2', 'BCC', 'FCC', 'HCP', 'L12', 'Laves', 'Laves(C14)', 'Laves(C15)', 'Sec.', 'OTHER'] | |
input_cols = { | |
"PROPERTY: Alloy formula": "(PROPERTY: Alloy formula) " | |
"Enter alloy formula using proportions representation (i.e. Al0.25 Co1 Fe1 Ni1)", | |
"PROPERTY: Single/Multiphase": "(PROPERTY: Single/Multiphase) " | |
"Choose between Single (S), Multiphase (M) and other (OTHER)", | |
"PROPERTY: BCC/FCC/other": "(PROPERTY: BCC/FCC/other) " | |
"Choose between BCC, FCC and other ", | |
"PROPERTY: Processing method": "(PROPERTY: Processing method) " | |
"Choose your processing method (ANNEAL, CAST, POWDER, WROUGHT or OTHER)", | |
"PROPERTY: Microstructure": "(PROPERTY: Microstructure) " | |
"Choose the microstructure (SEC means the secondary/tertiary microstructure is not one of FCC, BCC, HCP, L12, B2, Laves, Laves (C14), Laves (C15))", | |
} | |
def process_microstructure(list_phases): | |
permutations = list(itertools.permutations(list_phases)) | |
permutations_strings = [str('+'.join(list(e))) for e in permutations] | |
for e in permutations_strings: | |
if e in list(input_mapping['PROPERTY: Microstructure'].keys()): | |
return e | |
return 'OTHER' | |
def write_logs(message, message_type="Prediction"): | |
""" | |
Write logs | |
""" | |
with Repository(local_dir="data", clone_from=dataset_url, use_auth_token=WRITE_TOKEN).commit(commit_message="from private", blocking=False): | |
with open(dataset_path, "a") as csvfile: | |
writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"]) | |
writer.writerow( | |
{"name": message_type, "message": message, "time": str(datetime.now())} | |
) | |
return | |
def predict(x, request: gr.Request): | |
""" | |
Predict the hardness using the ML model. Input data is a dataframe | |
""" | |
loaded_model = tf.keras.models.load_model("hardness.h5") | |
x = x.replace("", 0) | |
x = np.asarray(x).astype("float32") | |
y = loaded_model.predict(x)[0][0] | |
minimum, maximum = scaling_factors['PROPERTY: HV'] | |
print("Prediction is ", y) | |
if request is not None: # Verify if request is not None (when building the app the first request is None) | |
message = f"{request.username}_{request.client.host}" | |
print("MESSAGE") | |
print(message) | |
res = write_logs(message) | |
interpret_fig = utils.interpret(x) | |
return round(y*(maximum-minimum)+minimum, 2), 12, interpret_fig | |
def predict_from_tuple(in1, in2, in3, in4, in5, request: gr.Request): | |
""" | |
Predict the hardness using the ML model. Input data is a tuple. Input order should be the same as the cols list | |
""" | |
input_tuple = (in1, in2, in3, in4, in5) | |
formula = utils.normalize_and_alphabetize_formula(in1) | |
density = utils.calculate_density(formula) | |
young_modulus = utils.calculate_youngs_modulus(formula) | |
input_dict = {} | |
in2 = input_mapping['PROPERTY: Single/Multiphase'][str(in2)] | |
input_dict['PROPERTY: Single/Multiphase'] = [int(in2)] | |
in3 = input_mapping['PROPERTY: BCC/FCC/other'][str(in3)] | |
input_dict['PROPERTY: BCC/FCC/other'] = [int(in3)] | |
in4 = input_mapping['PROPERTY: Processing method'][str(in4)] | |
input_dict['PROPERTY: Processing method'] = [int(in4)] | |
in5 = process_microstructure(in5) | |
in5 = input_mapping['PROPERTY: Microstructure'][in5] | |
input_dict['PROPERTY: Microstructure'] = [int(in5)] | |
density_scaling_factors = scaling_factors['PROPERTY: Calculated Density (g/cm$^3$)'] | |
density = (density-density_scaling_factors[0])/( | |
density_scaling_factors[1]-density_scaling_factors[0]) | |
input_dict['PROPERTY: Calculated Density (g/cm$^3$)'] = [float(density)] | |
ym_scaling_factors = scaling_factors['PROPERTY: Calculated Young modulus (GPa)'] | |
young_modulus = (young_modulus-ym_scaling_factors[0])/( | |
ym_scaling_factors[1]-ym_scaling_factors[0]) | |
input_dict['PROPERTY: Calculated Young modulus (GPa)'] = [float(young_modulus)] | |
input_df = pd.DataFrame.from_dict(input_dict) | |
one_hot = utils.turn_into_one_hot(input_df, input_mapping) | |
print(one_hot.columns) | |
return predict(one_hot, request) | |
input_formula = gr.Textbox( | |
lines=1, placeholder=input_cols["PROPERTY: Alloy formula"], label=input_cols["PROPERTY: Alloy formula"] | |
) | |
input_phase = gr.Dropdown( | |
choices=list(input_mapping["PROPERTY: Single/Multiphase"].keys()), | |
label=input_cols["PROPERTY: Single/Multiphase"], | |
) | |
input_bccfcc = gr.Dropdown( | |
choices=list(input_mapping["PROPERTY: BCC/FCC/other"].keys()), | |
label=input_cols["PROPERTY: BCC/FCC/other"], | |
) | |
input_processing = gr.Dropdown( | |
choices=list(input_mapping["PROPERTY: Processing method"].keys()), | |
label=input_cols["PROPERTY: Processing method"], | |
) | |
input_microstructure = gr.CheckboxGroup( | |
choices=unique_phase_elements, #list(input_mapping["PROPERTY: Microstructure"].keys()), | |
label=input_cols["PROPERTY: Microstructure"], | |
) | |
input_list = [input_formula, input_phase, input_bccfcc, input_processing, input_microstructure] | |
examples_inputs = ['Al0.25 Co1 Fe1 Ni1', 'S', 'BCC', 'CAST', ['B2', 'BCC']] | |
# Version where input is a DataFrame | |
# demo = gr.Interface(fn=predict, | |
# inputs=gr.DataFrame(headers=cols), | |
# outputs=gr.Text(label="Hardness (in HV)")) | |
demo = gr.Interface( | |
fn=predict_from_tuple, | |
inputs=input_list, | |
outputs=[gr.Text(label="Hardness (in HV)"), gr.Text(label="Uncertainty (%)"), gr.Plot(label="Interpretation")], | |
title="Predict your alloy's hardness", | |
description="This AI model provides the estimation of hardness based on the input alloy description", | |
examples=[examples_inputs], | |
) | |
if __name__ == "__main__": | |
demo.launch(show_error=True) |