File size: 3,256 Bytes
fcb4607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from mp_api.client import MPRester
import pymatgen.core as mp
import gradio as gr
from typing import Literal
import os

mpr = MPRester(os.getenv("MP_API_KEY"))

available_fields = ['nsites', 'elements', 'nelements', 'composition', 'composition_reduced', 'formula_pretty', 'formula_anonymous', 'chemsys', 'volume', 'density', 'density_atomic', 'symmetry', 'property_name', 'material_id', 'structure', 'task_ids', 'uncorrected_energy_per_atom', 'energy_per_atom', 'formation_energy_per_atom', 'energy_above_hull', 'is_stable', 'equilibrium_reaction_energy_per_atom', 'decomposes_to', 'xas', 'grain_boundaries', 'band_gap', 'cbm', 'vbm', 'efermi', 'is_gap_direct', 'is_metal', 'bandstructure', 'dos', 'dos_energy_up', 'dos_energy_down', 'is_magnetic', 'ordering', 'total_magnetization', 'total_magnetization_normalized_vol', 'total_magnetization_normalized_formula_units', 'num_magnetic_sites', 'num_unique_magnetic_sites', 'types_of_magnetic_species', 'bulk_modulus', 'shear_modulus', 'universal_anisotropy', 'homogeneous_poisson', 'e_total', 'e_ionic', 'e_electronic', 'n', 'e_ij_max', 'weighted_surface_energy_EV_PER_ANG2', 'weighted_surface_energy', 'weighted_work_function', 'surface_anisotropy', 'shape_factor', 'has_reconstructed']

def material_property_search(formula:str=None, elements: list[str]=None, num_sites_min:int=None, num_sites_max:int=None, spacegroup_number:int=None, fields: list[available_fields] = available_fields):
    props = ['material_id','formula_pretty']
    print(fields)
    search_params = {}
    if elements!="None":
        elements = elements.replace(',',' ').split(" ")
        search_params["elements"] = elements
    if formula!="None":
        search_params["formula"] = formula
    if spacegroup_number!=0:
        search_params["spacegroup_number"] = spacegroup_number
    if type(fields) == str: #list comprehension from string
        if fields[0] == '[' and fields[-1] == ']':
            fields = fields.strip('[]').replace(" ","").split("','")
        else:
            fields = [fields]
    props = list(dict.fromkeys(props+fields))
    search_params["num_sites"] = (num_sites_min, num_sites_max)
    search_params["fields"] = props
    print(search_params)
    
    try:
        search=mpr.summary.search(**search_params)
    except:
        return "search failed"
        
    results = []
    for i in search:
        a = i.__dict__
        b = {}
        for j in props:
            b[j] = a[j]
        results.append(b)
    return results
    
def structure_from_id(material_id):
    struc = mpr.get_structure_by_material_id(material_id)
    return print(struc)
    
    
demo = gr.Interface(
    fn=material_property_search,
    inputs=[
        gr.Textbox(label="Formula", value="None"), 
        gr.Textbox(label="Elements", value="None"), 
        gr.Number(label="Min Sites", value=0),
        gr.Number(label="Max Sites", value=100), 
        gr.Number(label="Spacegroup Number", value=0), 
        gr.CheckboxGroup(choices=list(available_fields), value=['material_id','formula_pretty','band_gap'])
    ],
    outputs=gr.JSON(),
    title="Materials Project Search",
    description="Search for materials by given parameters"
)
if __name__ == "__main__":
    demo.launch(mcp_server=True, share=True)