Spaces:
Runtime error
Runtime error
Commit
·
a8aa40c
0
Parent(s):
Duplicate from sandl/private_inverse_design_alloy
Browse filesCo-authored-by: L B <[email protected]>
- .gitattributes +35 -0
- README.md +13 -0
- app.py +283 -0
- app_old.py +311 -0
- explainer.bz2 +3 -0
- hardness.h5 +3 -0
- hardness_nn_graph_separate_elements.h5 +3 -0
- osiumai_favicon.ico +0 -0
- osiumai_logo.jpg +0 -0
- requirements.txt +6 -0
- utils.py +156 -0
.gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Inverse Design Alloy
|
3 |
+
emoji: 😻
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: indigo
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.35.2
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
duplicated_from: sandl/private_inverse_design_alloy
|
11 |
+
---
|
12 |
+
|
13 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,283 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import csv
|
3 |
+
import gradio as gr
|
4 |
+
import tensorflow as tf
|
5 |
+
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
+
from datetime import datetime
|
8 |
+
import utils
|
9 |
+
from huggingface_hub import Repository
|
10 |
+
import itertools
|
11 |
+
import GPyOpt
|
12 |
+
|
13 |
+
# Unique phase elements
|
14 |
+
|
15 |
+
# Load access tokens
|
16 |
+
WRITE_TOKEN = os.environ.get("WRITE_PER") # write
|
17 |
+
|
18 |
+
# Logs repo path
|
19 |
+
dataset_url = "https://huggingface.co/datasets/sandl/upload_alloy_hardness"
|
20 |
+
dataset_path = "logs_alloy_hardness.csv"
|
21 |
+
|
22 |
+
scaling_factors = {'PROPERTY: Calculated Density (g/cm$^3$)': (5.5, 13.7),
|
23 |
+
'PROPERTY: Calculated Young modulus (GPa)': (77.0, 336.0),
|
24 |
+
'PROPERTY: HV': (107.0, 1183.0),
|
25 |
+
'PROPERTY: YS (MPa)': (62.0, 3416.0)}
|
26 |
+
|
27 |
+
input_mapping = {'PROPERTY: BCC/FCC/other': {'BCC': 0, 'FCC': 1, 'OTHER': 2},#, 'nan': 2},
|
28 |
+
'PROPERTY: Processing method': {'ANNEAL': 0, 'CAST': 1, 'OTHER': 2, 'POWDER': 3, 'WROUGHT': 4},#, 'nan': 2},
|
29 |
+
'PROPERTY: Microstructure': {'B2': 0, 'B2+BCC': 1, 'B2+L12': 2, 'B2+Laves+Sec.': 3, 'B2+Sec.': 4, 'BCC': 5,
|
30 |
+
'BCC+B2': 6, 'BCC+B2+FCC': 7, 'BCC+B2+FCC+Sec.': 8, 'BCC+B2+L12': 9, 'BCC+B2+Laves': 10,
|
31 |
+
'BCC+B2+Sec.': 11, 'BCC+BCC': 12, 'BCC+BCC+HCP': 13, 'BCC+BCC+Laves': 14,
|
32 |
+
'BCC+BCC+Laves(C14)': 15, 'BCC+BCC+Laves(C15)': 16, 'BCC+FCC': 17, 'BCC+HCP': 18,
|
33 |
+
'BCC+Laves': 19, 'BCC+Laves(C14)': 20, 'BCC+Laves(C15)': 21, 'BCC+Laves+Sec.': 22,
|
34 |
+
'BCC+Sec.': 23, 'FCC': 24, 'FCC+B2': 25, 'FCC+B2+Sec.': 26, 'FCC+BCC': 27,
|
35 |
+
'FCC+BCC+B2': 28, 'FCC+BCC+B2+Sec.': 29, 'FCC+BCC+BCC': 30, 'FCC+BCC+Sec.': 31,
|
36 |
+
'FCC+FCC': 32, 'FCC+HCP': 33, 'FCC+HCP+Sec.': 34, 'FCC+L12': 35, 'FCC+L12+B2': 36,
|
37 |
+
'FCC+L12+Sec.': 37, 'FCC+Laves': 38, 'FCC+Laves(C14)': 39, 'FCC+Laves+Sec.': 40,
|
38 |
+
'FCC+Sec.': 41, 'L12+B2': 42, 'Laves(C14)+Sec.': 43, 'OTHER': 44},#, 'nan': 44},
|
39 |
+
'PROPERTY: Single/Multiphase': {'': 0, 'M': 1, 'S': 2, 'OTHER': 3}}#, 'nan': 3}}
|
40 |
+
|
41 |
+
unique_phase_elements = ['B2', 'BCC', 'FCC', 'HCP', 'L12', 'Laves', 'Laves(C14)', 'Laves(C15)', 'Sec.', 'OTHER']
|
42 |
+
|
43 |
+
input_cols = {
|
44 |
+
"PROPERTY: Alloy formula": "(PROPERTY: Alloy formula) "
|
45 |
+
"Enter alloy formula using proportions representation (i.e. Al0.25 Co1 Fe1 Ni1)",
|
46 |
+
"PROPERTY: Single/Multiphase": "(PROPERTY: Single/Multiphase) "
|
47 |
+
"Choose between Single (S), Multiphase (M) and other (OTHER)",
|
48 |
+
"PROPERTY: BCC/FCC/other": "(PROPERTY: BCC/FCC/other) "
|
49 |
+
"Choose between BCC, FCC and other ",
|
50 |
+
"PROPERTY: Processing method": "(PROPERTY: Processing method) "
|
51 |
+
"Choose your processing method (ANNEAL, CAST, POWDER, WROUGHT or OTHER)",
|
52 |
+
"PROPERTY: Microstructure": "(PROPERTY: Microstructure) "
|
53 |
+
"Choose the microstructure (SEC means the secondary/tertiary microstructure is not one of FCC, BCC, HCP, L12, B2, Laves, Laves (C14), Laves (C15))",
|
54 |
+
}
|
55 |
+
|
56 |
+
def process_microstructure(list_phases):
|
57 |
+
permutations = list(itertools.permutations(list_phases))
|
58 |
+
permutations_strings = [str('+'.join(list(e))) for e in permutations]
|
59 |
+
for e in permutations_strings:
|
60 |
+
if e in list(input_mapping['PROPERTY: Microstructure'].keys()):
|
61 |
+
return e
|
62 |
+
return 'OTHER'
|
63 |
+
|
64 |
+
def write_logs(message, message_type="Prediction"):
|
65 |
+
"""
|
66 |
+
Write logs
|
67 |
+
"""
|
68 |
+
#with Repository(local_dir="data", clone_from=dataset_url, use_auth_token=WRITE_TOKEN).commit(commit_message="from private", blocking=False):
|
69 |
+
# with open(dataset_path, "a") as csvfile:
|
70 |
+
# writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
|
71 |
+
# writer.writerow(
|
72 |
+
# {"name": message_type, "message": message, "time": str(datetime.now())}
|
73 |
+
# )
|
74 |
+
return
|
75 |
+
|
76 |
+
def predict(x, request: gr.Request):
|
77 |
+
"""
|
78 |
+
Predict the hardness and yield strength using the ML model. Input data is a dataframe
|
79 |
+
"""
|
80 |
+
loaded_model = tf.keras.models.load_model("hardness_nn_graph_separate_elements.h5")
|
81 |
+
print("summary is", loaded_model.summary())
|
82 |
+
#x = x.replace("", 0)
|
83 |
+
x = np.asarray(x).astype("float32")
|
84 |
+
y = loaded_model.predict(x)
|
85 |
+
y_hardness = y[0][0]
|
86 |
+
y_ys = y[0][1]
|
87 |
+
minimum_hardness, maximum_hardness = scaling_factors['PROPERTY: HV']
|
88 |
+
minimum_ys, maximum_ys = scaling_factors['PROPERTY: YS (MPa)']
|
89 |
+
print("Prediction is ", y)
|
90 |
+
if request is not None: # Verify if request is not None (when building the app the first request is None)
|
91 |
+
message = f"{request.username}_{request.client.host}"
|
92 |
+
print("MESSAGE")
|
93 |
+
print(message)
|
94 |
+
res = write_logs(message)
|
95 |
+
#interpret_fig = utils.interpret(x)
|
96 |
+
return (round(y_hardness*(maximum_hardness-minimum_hardness)+minimum_hardness, 2), 12,
|
97 |
+
round(y_ys*(maximum_ys-minimum_ys)+minimum_ys, 2), 12)
|
98 |
+
|
99 |
+
def fit_outputs_constraints(x, hardness_target, ys_target, request: gr.Request):
|
100 |
+
predictions = predict(x, request)
|
101 |
+
error_hardness = np.sqrt(np.square(predictions[0]-float(hardness_target)))
|
102 |
+
error_ys = np.sqrt(np.square(predictions[2]-float(ys_target)))
|
103 |
+
print("Optimization step is ", predictions, float(hardness_target), float(ys_target),
|
104 |
+
error_hardness, error_ys)
|
105 |
+
return error_hardness + error_ys
|
106 |
+
|
107 |
+
def predict_inverse(hardness_original_target, ys_original_target, metals_to_use, request: gr.Request):
|
108 |
+
|
109 |
+
one_hot_columns = utils.return_feature_names()
|
110 |
+
min_df_hardness, max_df_hardness = scaling_factors["PROPERTY: HV"]
|
111 |
+
hardness_original_target = float(hardness_original_target)
|
112 |
+
min_df_ys, max_df_ys = scaling_factors["PROPERTY: YS (MPa)"]
|
113 |
+
ys_original_target = float(ys_original_target)
|
114 |
+
|
115 |
+
hardness_target = (hardness_original_target-min_df_hardness)/(max_df_hardness-min_df_hardness)
|
116 |
+
ys_target = (ys_original_target-min_df_ys)/(max_df_ys-min_df_ys)
|
117 |
+
|
118 |
+
continuous_variables = ['PROPERTY: Calculated Density (g/cm$^3$)',
|
119 |
+
'PROPERTY: Calculated Young modulus (GPa)',
|
120 |
+
'PROPERTY: Metal Al', 'PROPERTY: Metal Co',
|
121 |
+
'PROPERTY: Metal Fe', 'PROPERTY: Metal Ni', 'PROPERTY: Metal Si',
|
122 |
+
'PROPERTY: Metal Cr', 'PROPERTY: Metal Nb', 'PROPERTY: Metal Ti',
|
123 |
+
'PROPERTY: Metal Mn', 'PROPERTY: Metal V', 'PROPERTY: Metal Mo',
|
124 |
+
'PROPERTY: Metal Cu', 'PROPERTY: Metal Ta', 'PROPERTY: Metal Zr',
|
125 |
+
'PROPERTY: Metal Hf', 'PROPERTY: Metal W', 'PROPERTY: Metal Zn',
|
126 |
+
'PROPERTY: Metal Sn', 'PROPERTY: Metal Re', 'PROPERTY: Metal C',
|
127 |
+
'PROPERTY: Metal Pd', 'PROPERTY: Metal Sc', 'PROPERTY: Metal Y']
|
128 |
+
categorical_variables = list(one_hot_columns)
|
129 |
+
for c in continuous_variables:
|
130 |
+
categorical_variables.remove(c)
|
131 |
+
|
132 |
+
# Metals constraints
|
133 |
+
metals_elements = [c for c in continuous_variables if c.startswith("PROPERTY: Metal")]
|
134 |
+
# metals_to_use = ['Al', 'Co', 'Fe', 'Cr']
|
135 |
+
metals_to_use = ["PROPERTY: Metal " + metals_to_use[i] for i in range(len(metals_to_use))]
|
136 |
+
|
137 |
+
# Domain
|
138 |
+
domain = []
|
139 |
+
for c in one_hot_columns:
|
140 |
+
if c in continuous_variables:
|
141 |
+
if c.startswith("PROPERTY: Metal") and c not in metals_to_use:
|
142 |
+
domain.append({'name': str(c), 'type': 'continuous', 'domain': (0., 0.)})
|
143 |
+
else:
|
144 |
+
domain.append({'name': str(c), 'type': 'continuous', 'domain': (0., 1.)})#(0.,1.)})
|
145 |
+
else:
|
146 |
+
domain.append({'name': str(c), 'type': 'discrete', 'domain': (0,1)})
|
147 |
+
|
148 |
+
# Constraints
|
149 |
+
constraints = []
|
150 |
+
constrained_columns = ['Single/Multiphase', 'Preprocessing method', 'BCC/FCC/other'] #'PROPERTY: Metal']#, 'Microstructure']
|
151 |
+
for constraint in constrained_columns:
|
152 |
+
sum_string = ''
|
153 |
+
for i in range (len(one_hot_columns)):
|
154 |
+
column_one_hot = one_hot_columns[i]
|
155 |
+
if column_one_hot.startswith(constraint):
|
156 |
+
sum_string = sum_string+"+x[:," + str(i) + "]"
|
157 |
+
constraints.append({'name': constraint + "+1", 'constraint': sum_string + '-1'})
|
158 |
+
constraints.append({'name': constraint + "-1", 'constraint': '-1*(' + sum_string + ')+1'})
|
159 |
+
|
160 |
+
|
161 |
+
def fit_outputs(x):
|
162 |
+
return fit_outputs_constraints(x, hardness_target, ys_target, request)
|
163 |
+
|
164 |
+
opt = GPyOpt.methods.BayesianOptimization(f = fit_outputs, # function to optimize
|
165 |
+
domain = domain, # box-constraints of the problem
|
166 |
+
constraints = constraints,
|
167 |
+
acquisition_type ='LCB', # LCB acquisition
|
168 |
+
acquisition_weight = 0.1) # Exploration exploitation
|
169 |
+
# it may take a few seconds
|
170 |
+
opt.run_optimization(max_iter=5)
|
171 |
+
# opt.plot_convergence()
|
172 |
+
x_best = opt.X[np.argmin(opt.Y)]
|
173 |
+
best_params = dict(zip(
|
174 |
+
[el['name'] for el in domain],
|
175 |
+
[[x] for x in x_best]))
|
176 |
+
optimized_x = pd.DataFrame.from_dict(best_params)
|
177 |
+
for c in optimized_x.columns:
|
178 |
+
if c in continuous_variables:
|
179 |
+
if c in ['PROPERTY: Calculated Density (g/cm$^3$)', 'PROPERTY: Calculated Young modulus (GPa)']:
|
180 |
+
optimized_x[c]=round(optimized_x[c]*(scaling_factors[c][1]-scaling_factors[c][0])+scaling_factors[c][0], 2)
|
181 |
+
result = optimized_x
|
182 |
+
result = result[result>0.0].dropna(axis=1)
|
183 |
+
|
184 |
+
# Normalize metals outputs
|
185 |
+
sum_metals = np.sum(result[c] for c in list(result.columns) if c.startswith("PROPERTY: Metal"))
|
186 |
+
for column in result.columns:
|
187 |
+
if column.startswith("PROPERTY: Metal"):
|
188 |
+
result[column]/= sum_metals
|
189 |
+
result[column] = round(result[column], 2)
|
190 |
+
|
191 |
+
columns = list(result.columns)
|
192 |
+
return (result[columns[2:-3]], columns[-3], result.at[0, columns[0]],
|
193 |
+
result.at[0, columns[1]], columns[-2], columns[-1])
|
194 |
+
|
195 |
+
|
196 |
+
example_inputs = [820, 1800, ['Al', 'Fe']]
|
197 |
+
|
198 |
+
css_styling = """#submit {background: #1eccd8}
|
199 |
+
#submit:hover {background: #a2f1f6}
|
200 |
+
.output-image, .input-image, .image-preview {height: 250px !important}
|
201 |
+
.output-plot {height: 250px !important}"""
|
202 |
+
|
203 |
+
light_theme_colors = gr.themes.Color(c50="#e4f3fa", # Dataframe background cell content - light mode only
|
204 |
+
c100="#e4f3fa", # Top corner of clear button in light mode + markdown text in dark mode
|
205 |
+
c200="#a1c6db", # Component borders
|
206 |
+
c300="#FFFFFF", #
|
207 |
+
c400="#e4f3fa", # Footer text
|
208 |
+
c500="#0c1538", # Text of component headers in light mode only
|
209 |
+
c600="#a1c6db", # Top corner of button in dark mode
|
210 |
+
c700="#475383", # Button text in light mode + component borders in dark mode
|
211 |
+
c800="#0c1538", # Markdown text in light mode
|
212 |
+
c900="#a1c6db", # Background of dataframe - dark mode
|
213 |
+
c950="#0c1538") # Background in dark mode only
|
214 |
+
# secondary color used for highlight box content when typing in light mode, and download option in dark mode
|
215 |
+
# primary color used for login button in dark mode
|
216 |
+
osium_theme = gr.themes.Default(primary_hue="cyan", secondary_hue="cyan", neutral_hue=light_theme_colors)
|
217 |
+
page_title = "Alloys' hardness and yield strength prediction"
|
218 |
+
favicon_path = "osiumai_favicon.ico"
|
219 |
+
logo_path = "osiumai_logo.jpg"
|
220 |
+
html = f"""<html> <link rel="icon" type="image/x-icon" href="file={favicon_path}">
|
221 |
+
<img src='file={logo_path}' alt='Osium AI logo' width='200' height='100'> </html>"""
|
222 |
+
|
223 |
+
|
224 |
+
with gr.Blocks(css=css_styling, title=page_title, theme=osium_theme) as demo:
|
225 |
+
#gr.HTML(html)
|
226 |
+
gr.Markdown("# <p style='text-align: center;'>Get optimal alloy recommendations based on your target performance</p>")
|
227 |
+
gr.Markdown("This AI model provides a recommended alloy formula, microstructure and processing conditions based on your target hardness and yield strength")
|
228 |
+
with gr.Row():
|
229 |
+
clear_button = gr.Button("Clear")
|
230 |
+
prediction_button = gr.Button("Predict", elem_id="submit")
|
231 |
+
with gr.Row():
|
232 |
+
with gr.Column():
|
233 |
+
gr.Markdown("### The target performance of your alloy")
|
234 |
+
input_hardness = gr.Text(label="Enter your target hardness (in HV)")
|
235 |
+
input_yield_strength = gr.Text(label="Enter your target yield strength (MPa)")
|
236 |
+
gr.Markdown('### Your metallic elements constraints')
|
237 |
+
metals_constraints = gr.CheckboxGroup(
|
238 |
+
choices=['Al', 'Co', 'Fe', 'Ni', 'Si', 'Cr', 'Nb', 'Ti',
|
239 |
+
'Mn', 'V', 'Mo', 'Cu', 'Ta', 'Zr',
|
240 |
+
'Hf', 'W', 'Zn', 'Sn', 'Re', 'C',
|
241 |
+
'Pd', 'Sc', 'Y'], label="Your metals constraints",
|
242 |
+
)
|
243 |
+
with gr.Column():
|
244 |
+
with gr.Row():
|
245 |
+
with gr.Column():
|
246 |
+
gr.Markdown("### Your optimal alloy formula and processing conditions")
|
247 |
+
optimal_formula = gr.DataFrame(label="Your optimal alloy formula", wrap=True)
|
248 |
+
optimal_processing_method = gr.Text(label="Processing method")
|
249 |
+
gr.Markdown("### Additional information about your optimal alloy")
|
250 |
+
density = gr.Text(label="Density (g/cm3)")
|
251 |
+
young_modulus = gr.Text(label = "Young modulus (GPa)")
|
252 |
+
microstructure = gr.Text(label="Microstructure (BCC/FCC/Other)")
|
253 |
+
phase = gr.Text(label="Number of phases (S/M)")
|
254 |
+
|
255 |
+
with gr.Row():
|
256 |
+
gr.Examples([example_inputs], [input_hardness, input_yield_strength, metals_constraints])
|
257 |
+
|
258 |
+
|
259 |
+
|
260 |
+
prediction_button.click(
|
261 |
+
fn=predict_inverse,
|
262 |
+
inputs=[input_hardness, input_yield_strength, metals_constraints],
|
263 |
+
outputs=[optimal_formula, optimal_processing_method, density, young_modulus, microstructure, phase],
|
264 |
+
show_progress=True,
|
265 |
+
)
|
266 |
+
clear_button.click(
|
267 |
+
lambda x: [gr.update(value=None)] * 9,
|
268 |
+
[],
|
269 |
+
[
|
270 |
+
input_hardness,
|
271 |
+
input_yield_strength,
|
272 |
+
metals_constraints,
|
273 |
+
optimal_formula,
|
274 |
+
optimal_processing_method,
|
275 |
+
density, young_modulus,
|
276 |
+
microstructure, phase
|
277 |
+
],
|
278 |
+
)
|
279 |
+
|
280 |
+
|
281 |
+
if __name__ == "__main__":
|
282 |
+
demo.queue(concurrency_count=2)
|
283 |
+
demo.launch()
|
app_old.py
ADDED
@@ -0,0 +1,311 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import csv
|
3 |
+
import gradio as gr
|
4 |
+
import tensorflow as tf
|
5 |
+
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
+
from datetime import datetime
|
8 |
+
import utils
|
9 |
+
from huggingface_hub import Repository
|
10 |
+
import itertools
|
11 |
+
import GPyOpt
|
12 |
+
|
13 |
+
# Unique phase elements
|
14 |
+
|
15 |
+
# Load access tokens
|
16 |
+
WRITE_TOKEN = os.environ.get("WRITE_PER") # write
|
17 |
+
|
18 |
+
# Logs repo path
|
19 |
+
dataset_url = "https://huggingface.co/datasets/sandl/upload_alloy_hardness"
|
20 |
+
dataset_path = "logs_alloy_hardness.csv"
|
21 |
+
|
22 |
+
scaling_factors = {'PROPERTY: Calculated Density (g/cm$^3$)': (5.5, 13.7),
|
23 |
+
'PROPERTY: Calculated Young modulus (GPa)': (77.0, 336.0),
|
24 |
+
'PROPERTY: HV': (107.0, 1183.0),
|
25 |
+
'PROPERTY: YS (MPa)': (62.0, 3416.0)}
|
26 |
+
|
27 |
+
input_mapping = {'PROPERTY: BCC/FCC/other': {'BCC': 0, 'FCC': 1, 'OTHER': 2},#, 'nan': 2},
|
28 |
+
'PROPERTY: Processing method': {'ANNEAL': 0, 'CAST': 1, 'OTHER': 2, 'POWDER': 3, 'WROUGHT': 4},#, 'nan': 2},
|
29 |
+
'PROPERTY: Microstructure': {'B2': 0, 'B2+BCC': 1, 'B2+L12': 2, 'B2+Laves+Sec.': 3, 'B2+Sec.': 4, 'BCC': 5,
|
30 |
+
'BCC+B2': 6, 'BCC+B2+FCC': 7, 'BCC+B2+FCC+Sec.': 8, 'BCC+B2+L12': 9, 'BCC+B2+Laves': 10,
|
31 |
+
'BCC+B2+Sec.': 11, 'BCC+BCC': 12, 'BCC+BCC+HCP': 13, 'BCC+BCC+Laves': 14,
|
32 |
+
'BCC+BCC+Laves(C14)': 15, 'BCC+BCC+Laves(C15)': 16, 'BCC+FCC': 17, 'BCC+HCP': 18,
|
33 |
+
'BCC+Laves': 19, 'BCC+Laves(C14)': 20, 'BCC+Laves(C15)': 21, 'BCC+Laves+Sec.': 22,
|
34 |
+
'BCC+Sec.': 23, 'FCC': 24, 'FCC+B2': 25, 'FCC+B2+Sec.': 26, 'FCC+BCC': 27,
|
35 |
+
'FCC+BCC+B2': 28, 'FCC+BCC+B2+Sec.': 29, 'FCC+BCC+BCC': 30, 'FCC+BCC+Sec.': 31,
|
36 |
+
'FCC+FCC': 32, 'FCC+HCP': 33, 'FCC+HCP+Sec.': 34, 'FCC+L12': 35, 'FCC+L12+B2': 36,
|
37 |
+
'FCC+L12+Sec.': 37, 'FCC+Laves': 38, 'FCC+Laves(C14)': 39, 'FCC+Laves+Sec.': 40,
|
38 |
+
'FCC+Sec.': 41, 'L12+B2': 42, 'Laves(C14)+Sec.': 43, 'OTHER': 44},#, 'nan': 44},
|
39 |
+
'PROPERTY: Single/Multiphase': {'': 0, 'M': 1, 'S': 2, 'OTHER': 3}}#, 'nan': 3}}
|
40 |
+
|
41 |
+
unique_phase_elements = ['B2', 'BCC', 'FCC', 'HCP', 'L12', 'Laves', 'Laves(C14)', 'Laves(C15)', 'Sec.', 'OTHER']
|
42 |
+
|
43 |
+
input_cols = {
|
44 |
+
"PROPERTY: Alloy formula": "(PROPERTY: Alloy formula) "
|
45 |
+
"Enter alloy formula using proportions representation (i.e. Al0.25 Co1 Fe1 Ni1)",
|
46 |
+
"PROPERTY: Single/Multiphase": "(PROPERTY: Single/Multiphase) "
|
47 |
+
"Choose between Single (S), Multiphase (M) and other (OTHER)",
|
48 |
+
"PROPERTY: BCC/FCC/other": "(PROPERTY: BCC/FCC/other) "
|
49 |
+
"Choose between BCC, FCC and other ",
|
50 |
+
"PROPERTY: Processing method": "(PROPERTY: Processing method) "
|
51 |
+
"Choose your processing method (ANNEAL, CAST, POWDER, WROUGHT or OTHER)",
|
52 |
+
"PROPERTY: Microstructure": "(PROPERTY: Microstructure) "
|
53 |
+
"Choose the microstructure (SEC means the secondary/tertiary microstructure is not one of FCC, BCC, HCP, L12, B2, Laves, Laves (C14), Laves (C15))",
|
54 |
+
}
|
55 |
+
|
56 |
+
def process_microstructure(list_phases):
|
57 |
+
permutations = list(itertools.permutations(list_phases))
|
58 |
+
permutations_strings = [str('+'.join(list(e))) for e in permutations]
|
59 |
+
for e in permutations_strings:
|
60 |
+
if e in list(input_mapping['PROPERTY: Microstructure'].keys()):
|
61 |
+
return e
|
62 |
+
return 'OTHER'
|
63 |
+
|
64 |
+
def write_logs(message, message_type="Prediction"):
|
65 |
+
"""
|
66 |
+
Write logs
|
67 |
+
"""
|
68 |
+
#with Repository(local_dir="data", clone_from=dataset_url, use_auth_token=WRITE_TOKEN).commit(commit_message="from private", blocking=False):
|
69 |
+
# with open(dataset_path, "a") as csvfile:
|
70 |
+
# writer = csv.DictWriter(csvfile, fieldnames=["name", "message", "time"])
|
71 |
+
# writer.writerow(
|
72 |
+
# {"name": message_type, "message": message, "time": str(datetime.now())}
|
73 |
+
# )
|
74 |
+
return
|
75 |
+
|
76 |
+
def predict(x, request: gr.Request):
|
77 |
+
"""
|
78 |
+
Predict the hardness and yield strength using the ML model. Input data is a dataframe
|
79 |
+
"""
|
80 |
+
loaded_model = tf.keras.models.load_model("hardness.h5")
|
81 |
+
print("summary is", loaded_model.summary())
|
82 |
+
#x = x.replace("", 0)
|
83 |
+
x = np.asarray(x).astype("float32")
|
84 |
+
y = loaded_model.predict(x)
|
85 |
+
y_hardness = y[0][0]
|
86 |
+
y_ys = y[0][1]
|
87 |
+
minimum_hardness, maximum_hardness = scaling_factors['PROPERTY: HV']
|
88 |
+
minimum_ys, maximum_ys = scaling_factors['PROPERTY: YS (MPa)']
|
89 |
+
print("Prediction is ", y)
|
90 |
+
if request is not None: # Verify if request is not None (when building the app the first request is None)
|
91 |
+
message = f"{request.username}_{request.client.host}"
|
92 |
+
print("MESSAGE")
|
93 |
+
print(message)
|
94 |
+
res = write_logs(message)
|
95 |
+
#interpret_fig = utils.interpret(x)
|
96 |
+
return (round(y_hardness*(maximum_hardness-minimum_hardness)+minimum_hardness, 2), 12,
|
97 |
+
round(y_ys*(maximum_ys-minimum_ys)+minimum_ys, 2), 12)
|
98 |
+
|
99 |
+
|
100 |
+
def predict_from_tuple(in1, in2, in3, in4, in5, request: gr.Request):
|
101 |
+
"""
|
102 |
+
Predict the hardness using the ML model. Input data is a tuple. Input order should be the same as the cols list
|
103 |
+
"""
|
104 |
+
input_tuple = (in1, in2, in3, in4, in5)
|
105 |
+
formula = utils.normalize_and_alphabetize_formula(in1)
|
106 |
+
density = utils.calculate_density(formula)
|
107 |
+
young_modulus = utils.calculate_youngs_modulus(formula)
|
108 |
+
input_dict = {}
|
109 |
+
|
110 |
+
in2 = input_mapping['PROPERTY: Single/Multiphase'][str(in2)]
|
111 |
+
input_dict['PROPERTY: Single/Multiphase'] = [int(in2)]
|
112 |
+
|
113 |
+
in3 = input_mapping['PROPERTY: BCC/FCC/other'][str(in3)]
|
114 |
+
input_dict['PROPERTY: BCC/FCC/other'] = [int(in3)]
|
115 |
+
|
116 |
+
in4 = input_mapping['PROPERTY: Processing method'][str(in4)]
|
117 |
+
input_dict['PROPERTY: Processing method'] = [int(in4)]
|
118 |
+
|
119 |
+
in5 = process_microstructure(in5)
|
120 |
+
in5 = input_mapping['PROPERTY: Microstructure'][in5]
|
121 |
+
input_dict['PROPERTY: Microstructure'] = [int(in5)]
|
122 |
+
|
123 |
+
density_scaling_factors = scaling_factors['PROPERTY: Calculated Density (g/cm$^3$)']
|
124 |
+
density = (density-density_scaling_factors[0])/(
|
125 |
+
density_scaling_factors[1]-density_scaling_factors[0])
|
126 |
+
input_dict['PROPERTY: Calculated Density (g/cm$^3$)'] = [float(density)]
|
127 |
+
|
128 |
+
|
129 |
+
ym_scaling_factors = scaling_factors['PROPERTY: Calculated Young modulus (GPa)']
|
130 |
+
young_modulus = (young_modulus-ym_scaling_factors[0])/(
|
131 |
+
ym_scaling_factors[1]-ym_scaling_factors[0])
|
132 |
+
input_dict['PROPERTY: Calculated Young modulus (GPa)'] = [float(young_modulus)]
|
133 |
+
|
134 |
+
input_df = pd.DataFrame.from_dict(input_dict)
|
135 |
+
one_hot = utils.turn_into_one_hot(input_df, input_mapping)
|
136 |
+
print("One hot columns are ", one_hot.columns)
|
137 |
+
return predict(one_hot, request)
|
138 |
+
|
139 |
+
def fit_outputs_constraints(x, hardness_target, ys_target, request: gr.Request):
|
140 |
+
predictions = predict(x, request)
|
141 |
+
error_hardness = np.sqrt(np.square(predictions[0]-float(hardness_target)))
|
142 |
+
error_ys = np.sqrt(np.square(predictions[2]-float(ys_target)))
|
143 |
+
print("Optimization step is ", predictions, float(hardness_target), float(ys_target),
|
144 |
+
error_hardness, error_ys)
|
145 |
+
return error_hardness + error_ys
|
146 |
+
|
147 |
+
def predict_inverse(hardness_target, ys_target, formula, request: gr.Request):
|
148 |
+
|
149 |
+
one_hot_columns = utils.return_feature_names()
|
150 |
+
|
151 |
+
continuous_variables = ['PROPERTY: Calculated Density (g/cm$^3$)',
|
152 |
+
'PROPERTY: Calculated Young modulus (GPa)']
|
153 |
+
categorical_variables = list(one_hot_columns)
|
154 |
+
for c in continuous_variables:
|
155 |
+
categorical_variables.remove(c)
|
156 |
+
|
157 |
+
|
158 |
+
fixed_density = utils.calculate_density(str(formula))
|
159 |
+
fixed_ym = utils.calculate_youngs_modulus(str(formula))
|
160 |
+
|
161 |
+
domain = []
|
162 |
+
for c in one_hot_columns:
|
163 |
+
if c in continuous_variables:
|
164 |
+
if c == continuous_variables[0]:
|
165 |
+
domain_density = (fixed_density-scaling_factors[c][0])/(
|
166 |
+
scaling_factors[c][1]-scaling_factors[c][0])
|
167 |
+
domain.append({'name': str(c), 'type': 'continuous', 'domain': (domain_density, domain_density)})#(0.,1.)})
|
168 |
+
else:
|
169 |
+
domain_ym = (fixed_ym-scaling_factors[c][0])/(
|
170 |
+
scaling_factors[c][1]-scaling_factors[c][0])
|
171 |
+
domain.append({'name': str(c), 'type': 'continuous', 'domain': (domain_ym, domain_ym)})#(0.,1.)})
|
172 |
+
else:
|
173 |
+
domain.append({'name': str(c), 'type': 'discrete', 'domain': (0,1)})
|
174 |
+
|
175 |
+
print("Domain is ", domain)
|
176 |
+
constraints = []
|
177 |
+
constrained_columns = ['Single/Multiphase', 'Preprocessing method', 'BCC/FCC/other']#, 'Microstructure']
|
178 |
+
|
179 |
+
for constraint in constrained_columns:
|
180 |
+
sum_string = ''
|
181 |
+
for i in range (len(one_hot_columns)):
|
182 |
+
column_one_hot = one_hot_columns[i]
|
183 |
+
if column_one_hot.startswith(constraint):
|
184 |
+
sum_string = sum_string+"+x[:," + str(i) + "]"
|
185 |
+
constraints.append({'name': constraint + "+1", 'constraint': sum_string + '-1'})
|
186 |
+
constraints.append({'name': constraint + "-1", 'constraint': '-1*(' + sum_string + ')+1'})
|
187 |
+
|
188 |
+
def fit_outputs(x):
|
189 |
+
return fit_outputs_constraints(x, hardness_target, ys_target, request)
|
190 |
+
|
191 |
+
opt = GPyOpt.methods.BayesianOptimization(f = fit_outputs, # function to optimize
|
192 |
+
domain = domain, # box-constraints of the problem
|
193 |
+
constraints = constraints,
|
194 |
+
acquisition_type ='LCB', # LCB acquisition
|
195 |
+
acquisition_weight = 0.1) # Exploration exploitation
|
196 |
+
# it may take a few seconds
|
197 |
+
opt.run_optimization(max_iter=20)
|
198 |
+
opt.plot_convergence()
|
199 |
+
x_best = opt.X[np.argmin(opt.Y)]
|
200 |
+
best_params = dict(zip(
|
201 |
+
[el['name'] for el in domain],
|
202 |
+
[[x] for x in x_best]))
|
203 |
+
optimized_x = pd.DataFrame.from_dict(best_params)
|
204 |
+
#for c in optimized_x.columns:
|
205 |
+
# if c in continuous_variables:
|
206 |
+
# optimized_x[c]=optimized_x[c]*(scaling_factors[c][1]-scaling_factors[c][0])+scaling_factors[c][0]
|
207 |
+
optimized_x = optimized_x[['PROPERTY: Calculated Density (g/cm$^3$)',
|
208 |
+
'PROPERTY: Calculated Young modulus (GPa)',
|
209 |
+
'Preprocessing method ANNEAL',
|
210 |
+
'Preprocessing method CAST', 'Preprocessing method OTHER',
|
211 |
+
'Preprocessing method POWDER', 'Preprocessing method WROUGHT',
|
212 |
+
'BCC/FCC/other BCC', 'BCC/FCC/other FCC', 'BCC/FCC/other OTHER',
|
213 |
+
'Single/Multiphase ', 'Single/Multiphase M', 'Single/Multiphase S']]
|
214 |
+
result = optimized_x
|
215 |
+
result = result[result>0.0].dropna(axis=1)
|
216 |
+
return list(result.keys())[2:]
|
217 |
+
|
218 |
+
|
219 |
+
example_inputs = ["Al0.25 Co1 Fe1 Ni1", 820, 1800]
|
220 |
+
|
221 |
+
css_styling = """#submit {background: #1eccd8}
|
222 |
+
#submit:hover {background: #a2f1f6}
|
223 |
+
.output-image, .input-image, .image-preview {height: 250px !important}
|
224 |
+
.output-plot {height: 250px !important}"""
|
225 |
+
|
226 |
+
light_theme_colors = gr.themes.Color(c50="#e4f3fa", # Dataframe background cell content - light mode only
|
227 |
+
c100="#e4f3fa", # Top corner of clear button in light mode + markdown text in dark mode
|
228 |
+
c200="#a1c6db", # Component borders
|
229 |
+
c300="#FFFFFF", #
|
230 |
+
c400="#e4f3fa", # Footer text
|
231 |
+
c500="#0c1538", # Text of component headers in light mode only
|
232 |
+
c600="#a1c6db", # Top corner of button in dark mode
|
233 |
+
c700="#475383", # Button text in light mode + component borders in dark mode
|
234 |
+
c800="#0c1538", # Markdown text in light mode
|
235 |
+
c900="#a1c6db", # Background of dataframe - dark mode
|
236 |
+
c950="#0c1538") # Background in dark mode only
|
237 |
+
# secondary color used for highlight box content when typing in light mode, and download option in dark mode
|
238 |
+
# primary color used for login button in dark mode
|
239 |
+
osium_theme = gr.themes.Default(primary_hue="cyan", secondary_hue="cyan", neutral_hue=light_theme_colors)
|
240 |
+
page_title = "Alloys' hardness and yield strength prediction"
|
241 |
+
favicon_path = "osiumai_favicon.ico"
|
242 |
+
logo_path = "osiumai_logo.jpg"
|
243 |
+
html = f"""<html> <link rel="icon" type="image/x-icon" href="file={favicon_path}">
|
244 |
+
<img src='file={logo_path}' alt='Osium AI logo' width='200' height='100'> </html>"""
|
245 |
+
|
246 |
+
|
247 |
+
with gr.Blocks(css=css_styling, title=page_title, theme=osium_theme) as demo:
|
248 |
+
#gr.HTML(html)
|
249 |
+
gr.Markdown("# <p style='text-align: center;'>Get optimal alloy recommendations based on your target performance</p>")
|
250 |
+
gr.Markdown("This AI model provides a recommended alloy formula, microstructure and processing conditions based on your target hardness and yield strength")
|
251 |
+
with gr.Row():
|
252 |
+
clear_button = gr.Button("Clear")
|
253 |
+
prediction_button = gr.Button("Predict", elem_id="submit")
|
254 |
+
with gr.Row():
|
255 |
+
with gr.Column():
|
256 |
+
gr.Markdown("### Your alloy formula")
|
257 |
+
formula = gr.Text(label = "Alloy formula")
|
258 |
+
gr.Markdown("### The target performance of your alloy")
|
259 |
+
input_hardness = gr.Text(label="Enter your target hardness (in HV)")
|
260 |
+
input_yield_strength = gr.Text(label="Enter your target yield strength (MPa)")
|
261 |
+
with gr.Column():
|
262 |
+
with gr.Row():
|
263 |
+
with gr.Column():
|
264 |
+
gr.Markdown("### Your optimal microstructure and processing conditions")
|
265 |
+
#optimal_parameters = gr.DataFrame(label="Optimal parameters", wrap=True)
|
266 |
+
with gr.Column():
|
267 |
+
param1 = gr.Text(label="Processing method")
|
268 |
+
with gr.Column():
|
269 |
+
param2 = gr.Text(label="Microstructure")
|
270 |
+
with gr.Column():
|
271 |
+
param3 = gr.Text(label="Phase")
|
272 |
+
#with gr.Row():
|
273 |
+
#with gr.Column():
|
274 |
+
#with gr.Row():
|
275 |
+
# gr.Markdown("### Interpretation of hardness prediction")
|
276 |
+
# gr.Markdown("### Interpretation of yield strength prediction")
|
277 |
+
#with gr.Row():
|
278 |
+
# output_interpretation = gr.Plot(label="Interpretation")
|
279 |
+
|
280 |
+
with gr.Row():
|
281 |
+
gr.Examples([example_inputs], [formula, input_hardness, input_yield_strength])
|
282 |
+
|
283 |
+
|
284 |
+
|
285 |
+
prediction_button.click(
|
286 |
+
fn=predict_inverse,
|
287 |
+
inputs=[input_hardness, input_yield_strength, formula],
|
288 |
+
outputs=[
|
289 |
+
param1,
|
290 |
+
param2,
|
291 |
+
param3,
|
292 |
+
],
|
293 |
+
show_progress=True,
|
294 |
+
)
|
295 |
+
clear_button.click(
|
296 |
+
lambda x: [gr.update(value=None)] * 6,
|
297 |
+
[],
|
298 |
+
[
|
299 |
+
param1,
|
300 |
+
param2,
|
301 |
+
param3,
|
302 |
+
input_hardness,
|
303 |
+
input_yield_strength,
|
304 |
+
formula
|
305 |
+
],
|
306 |
+
)
|
307 |
+
|
308 |
+
|
309 |
+
if __name__ == "__main__":
|
310 |
+
demo.queue(concurrency_count=2)
|
311 |
+
demo.launch()
|
explainer.bz2
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5bccd1ee1a1c1302e9a66df1a35d1eadfc11bdcc5947f3adfbaf946fee4c9475
|
3 |
+
size 10252441
|
hardness.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:95b25ecbb6995afe26613e0a17c3a1bb2398da8c054819d808dd875c61401945
|
3 |
+
size 39240
|
hardness_nn_graph_separate_elements.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:43447f7245ff3c9ba0925a3e8a83e67faa583d1157de81a739f8c3c5181eb276
|
3 |
+
size 37192
|
osiumai_favicon.ico
ADDED
|
osiumai_logo.jpg
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
GPyOpt
|
2 |
+
tensorflow-cpu
|
3 |
+
pymatgen
|
4 |
+
gradio==3.28.3
|
5 |
+
gradio_client==0.1.4
|
6 |
+
shap
|
utils.py
ADDED
@@ -0,0 +1,156 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import pymatgen as mg
|
3 |
+
from pymatgen.core.structure import Composition
|
4 |
+
import numpy as np
|
5 |
+
import tensorflow as tf
|
6 |
+
import shap
|
7 |
+
import joblib
|
8 |
+
import matplotlib.pyplot as plt
|
9 |
+
|
10 |
+
# Explainer path
|
11 |
+
explainer_filename = "explainer.bz2"
|
12 |
+
|
13 |
+
feature_names = ['PROPERTY: Calculated Density (g/cm$^3$)',
|
14 |
+
'PROPERTY: Calculated Young modulus (GPa)', 'PROPERTY: Metal Al',
|
15 |
+
'PROPERTY: Metal Co', 'PROPERTY: Metal Fe', 'PROPERTY: Metal Ni',
|
16 |
+
'PROPERTY: Metal Si', 'PROPERTY: Metal Cr', 'PROPERTY: Metal Nb',
|
17 |
+
'PROPERTY: Metal Ti', 'PROPERTY: Metal Mn', 'PROPERTY: Metal V',
|
18 |
+
'PROPERTY: Metal Mo', 'PROPERTY: Metal Cu', 'PROPERTY: Metal Ta',
|
19 |
+
'PROPERTY: Metal Zr', 'PROPERTY: Metal Hf', 'PROPERTY: Metal W',
|
20 |
+
'PROPERTY: Metal Zn', 'PROPERTY: Metal Sn', 'PROPERTY: Metal Re',
|
21 |
+
'PROPERTY: Metal C', 'PROPERTY: Metal Pd', 'PROPERTY: Metal Sc',
|
22 |
+
'PROPERTY: Metal Y', 'Preprocessing method ANNEAL',
|
23 |
+
'Preprocessing method CAST', 'Preprocessing method OTHER',
|
24 |
+
'Preprocessing method POWDER', 'Preprocessing method WROUGHT',
|
25 |
+
'BCC/FCC/other BCC', 'BCC/FCC/other FCC', 'BCC/FCC/other OTHER',
|
26 |
+
'Single/Multiphase ', 'Single/Multiphase M', 'Single/Multiphase S']
|
27 |
+
|
28 |
+
def return_feature_names():
|
29 |
+
return feature_names
|
30 |
+
|
31 |
+
def normalize_and_alphabetize_formula(formula):
|
32 |
+
'''Normalizes composition labels. Used to enable matching / groupby on compositions.'''
|
33 |
+
|
34 |
+
if formula:
|
35 |
+
try:
|
36 |
+
comp = Composition(formula)
|
37 |
+
weights = [comp.get_atomic_fraction(ele) for ele in comp.elements]
|
38 |
+
normalized_weights = [round(w/max(weights), 3) for w in weights]
|
39 |
+
normalized_comp = "".join([str(x)+str(y) for x,y in zip(comp.elements, normalized_weights)])
|
40 |
+
|
41 |
+
return Composition(normalized_comp).alphabetical_formula
|
42 |
+
except:
|
43 |
+
print("INVALID: ", formula)
|
44 |
+
return None
|
45 |
+
else:
|
46 |
+
return None
|
47 |
+
|
48 |
+
def calculate_density(formula):
|
49 |
+
'''Calculates densisty based on Rule of Mixtures (ROM).'''
|
50 |
+
|
51 |
+
comp = Composition(formula)
|
52 |
+
|
53 |
+
weights = [comp.get_atomic_fraction(e)for e in comp.elements]
|
54 |
+
vols = np.array([e.molar_volume for e in comp.elements])
|
55 |
+
atomic_masses = np.array([e.atomic_mass for e in comp.elements])
|
56 |
+
|
57 |
+
val = np.sum(weights*atomic_masses) / np.sum(weights*vols)
|
58 |
+
|
59 |
+
return round(val, 1)
|
60 |
+
|
61 |
+
def calculate_youngs_modulus(formula):
|
62 |
+
'''Calculates Young Modulus based on Rule of Mixtures (ROM).'''
|
63 |
+
|
64 |
+
comp = Composition(formula)
|
65 |
+
|
66 |
+
weights = np.array([comp.get_atomic_fraction(e)for e in comp.elements])
|
67 |
+
vols = np.array([e.molar_volume for e in comp.elements])
|
68 |
+
ym_vals = []
|
69 |
+
for e in comp.elements:
|
70 |
+
if str(e) == 'C': #use diamond form for carbon
|
71 |
+
ym_vals.append(1050)
|
72 |
+
elif str(e) == 'B': #use minimum value for Boron Carbide
|
73 |
+
ym_vals.append(362)
|
74 |
+
elif str(e) == 'Mo':
|
75 |
+
ym_vals.append(329)
|
76 |
+
elif str(e) == 'Co':
|
77 |
+
ym_vals.append(209)
|
78 |
+
else:
|
79 |
+
ym_vals.append(e.youngs_modulus)
|
80 |
+
|
81 |
+
#ym_vals = np.array([e.youngs_modulus for e in comp.elements])
|
82 |
+
ym_vals = np.array(ym_vals)
|
83 |
+
|
84 |
+
if None in ym_vals:
|
85 |
+
print(formula, ym_vals)
|
86 |
+
return ''
|
87 |
+
|
88 |
+
val = np.sum(weights*vols*ym_vals) / np.sum(weights*vols)
|
89 |
+
|
90 |
+
return int(round(val, 0))
|
91 |
+
|
92 |
+
def interpret(input):
|
93 |
+
plt.clf()
|
94 |
+
ex = joblib.load(filename=explainer_filename)
|
95 |
+
shap_values = ex.shap_values(input)
|
96 |
+
shap.summary_plot(shap_values[0], input, feature_names=feature_names)
|
97 |
+
fig = plt.gcf()
|
98 |
+
return fig, None
|
99 |
+
|
100 |
+
def to_categorical_num_classes_microstructure(X, num_classes_one_hot):
|
101 |
+
return tf.keras.utils.to_categorical(X, num_classes_one_hot["Num classes microstructure"])
|
102 |
+
|
103 |
+
def to_categorical_num_classes_processing(X, num_classes_one_hot):
|
104 |
+
return tf.keras.utils.to_categorical(X, num_classes_one_hot["Num classes preprocessing"])
|
105 |
+
|
106 |
+
def to_categorical_bcc_fcc_other(X, num_classes_one_hot):
|
107 |
+
return tf.keras.utils.to_categorical(X, num_classes_one_hot["Num classes bcc/fcc/other"])
|
108 |
+
|
109 |
+
def to_categorical_single_multiphase(X, num_classes_one_hot):
|
110 |
+
return tf.keras.utils.to_categorical(X, num_classes_one_hot["Num classes single/multiphase"])
|
111 |
+
|
112 |
+
def return_num_classes_one_hot(df):
|
113 |
+
num_classes_microstructure = len(np.unique(np.asarray(df['PROPERTY: Microstructure'])))
|
114 |
+
num_classes_processing = len(np.unique(np.asarray(df['PROPERTY: Processing method'])))
|
115 |
+
num_classes_single_multiphase = len(np.unique(np.asarray(df['PROPERTY: Single/Multiphase'])))
|
116 |
+
num_classes_bcc_fcc_other = len(np.unique(np.asarray(df['PROPERTY: BCC/FCC/other'])))
|
117 |
+
return {"Num classes microstructure": num_classes_microstructure,
|
118 |
+
"Num classes preprocessing": num_classes_processing,
|
119 |
+
"Num classes single/multiphase": num_classes_single_multiphase,
|
120 |
+
"Num classes bcc/fcc/other": num_classes_bcc_fcc_other}
|
121 |
+
|
122 |
+
def turn_into_one_hot(X, mapping_dict):
|
123 |
+
one_hot = X
|
124 |
+
num_classes_one_hot = {'Num classes microstructure': 45, 'Num classes preprocessing': 5,
|
125 |
+
'Num classes single/multiphase': 3, 'Num classes bcc/fcc/other': 3}
|
126 |
+
#one_hot["Microstructure One Hot"] = X["PROPERTY: Microstructure"].apply(to_categorical_num_classes_microstructure, num_classes_one_hot=num_classes_one_hot)
|
127 |
+
one_hot["Processing Method One Hot"] = X["PROPERTY: Processing method"].apply(to_categorical_num_classes_processing,
|
128 |
+
num_classes_one_hot=num_classes_one_hot)
|
129 |
+
one_hot["BCC/FCC/other One Hot"] = X["PROPERTY: BCC/FCC/other"].apply(to_categorical_bcc_fcc_other,
|
130 |
+
num_classes_one_hot=num_classes_one_hot)
|
131 |
+
one_hot["Single/Multiphase One Hot"] = X["PROPERTY: Single/Multiphase"].apply(to_categorical_single_multiphase,
|
132 |
+
num_classes_one_hot=num_classes_one_hot)
|
133 |
+
|
134 |
+
#flatten_microstructure = one_hot["Microstructure One Hot"].apply(pd.Series)
|
135 |
+
flatten_processing = one_hot["Processing Method One Hot"].apply(pd.Series)
|
136 |
+
flatten_bcc_fcc_other = one_hot["BCC/FCC/other One Hot"].apply(pd.Series)
|
137 |
+
flatten_single_multiphase = one_hot["Single/Multiphase One Hot"].apply(pd.Series)
|
138 |
+
|
139 |
+
one_hot.drop(columns=[#"Microstructure One Hot",
|
140 |
+
"Processing Method One Hot", "BCC/FCC/other One Hot",
|
141 |
+
"Single/Multiphase One Hot"])
|
142 |
+
|
143 |
+
#for column in flatten_microstructure.columns:
|
144 |
+
# one_hot["Microstructure " + str(
|
145 |
+
# list(mapping_dict["PROPERTY: Microstructure"].keys())[int(column)])] = flatten_microstructure[int(column)]
|
146 |
+
for column in flatten_processing.columns:
|
147 |
+
one_hot["Preprocessing method " + str(list(mapping_dict["PROPERTY: Processing method"].keys())[int(column)])] = flatten_processing[column]
|
148 |
+
for column in flatten_bcc_fcc_other.columns:
|
149 |
+
one_hot["BCC/FCC/other " + str(list(mapping_dict["PROPERTY: BCC/FCC/other"].keys())[int(column)])] = flatten_bcc_fcc_other[column]
|
150 |
+
for column in flatten_single_multiphase.columns:
|
151 |
+
one_hot["Single/Multiphase " + str(list(mapping_dict["PROPERTY: Single/Multiphase"].keys())[int(column)])] = flatten_single_multiphase[column]
|
152 |
+
|
153 |
+
one_hot = one_hot.drop(columns=[#"PROPERTY: Microstructure", "Microstructure One Hot",
|
154 |
+
"BCC/FCC/other One Hot", "Single/Multiphase One Hot",
|
155 |
+
"Processing Method One Hot", "PROPERTY: Processing method", "PROPERTY: BCC/FCC/other", "PROPERTY: Single/Multiphase"])
|
156 |
+
return one_hot
|