Spaces:
Runtime error
Runtime error
Create mp_mcp.py
Browse files
mp_mcp.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from mp_api.client import MPRester
|
| 2 |
+
import pymatgen.core as mp
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from typing import Literal
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
mpr = MPRester(os.getenv("MP_API_KEY"))
|
| 8 |
+
|
| 9 |
+
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']
|
| 10 |
+
|
| 11 |
+
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):
|
| 12 |
+
props = ['material_id','formula_pretty']
|
| 13 |
+
print(fields)
|
| 14 |
+
search_params = {}
|
| 15 |
+
if elements!="None":
|
| 16 |
+
elements = elements.replace(',',' ').split(" ")
|
| 17 |
+
search_params["elements"] = elements
|
| 18 |
+
if formula!="None":
|
| 19 |
+
search_params["formula"] = formula
|
| 20 |
+
if spacegroup_number!=0:
|
| 21 |
+
search_params["spacegroup_number"] = spacegroup_number
|
| 22 |
+
if type(fields) == str: #list comprehension from string
|
| 23 |
+
if fields[0] == '[' and fields[-1] == ']':
|
| 24 |
+
fields = fields.strip('[]').replace(" ","").split("','")
|
| 25 |
+
else:
|
| 26 |
+
fields = [fields]
|
| 27 |
+
props = list(dict.fromkeys(props+fields))
|
| 28 |
+
search_params["num_sites"] = (num_sites_min, num_sites_max)
|
| 29 |
+
search_params["fields"] = props
|
| 30 |
+
print(search_params)
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
search=mpr.summary.search(**search_params)
|
| 34 |
+
except:
|
| 35 |
+
return "search failed"
|
| 36 |
+
|
| 37 |
+
results = []
|
| 38 |
+
for i in search:
|
| 39 |
+
a = i.__dict__
|
| 40 |
+
b = {}
|
| 41 |
+
for j in props:
|
| 42 |
+
b[j] = a[j]
|
| 43 |
+
results.append(b)
|
| 44 |
+
return results
|
| 45 |
+
|
| 46 |
+
def structure_from_id(material_id):
|
| 47 |
+
struc = mpr.get_structure_by_material_id(material_id)
|
| 48 |
+
return print(struc)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
demo = gr.Interface(
|
| 52 |
+
fn=material_property_search,
|
| 53 |
+
inputs=[
|
| 54 |
+
gr.Textbox(label="Formula", value="None"),
|
| 55 |
+
gr.Textbox(label="Elements", value="None"),
|
| 56 |
+
gr.Number(label="Min Sites", value=0),
|
| 57 |
+
gr.Number(label="Max Sites", value=100),
|
| 58 |
+
gr.Number(label="Spacegroup Number", value=0),
|
| 59 |
+
gr.CheckboxGroup(choices=list(available_fields), value=['material_id','formula_pretty','band_gap'])
|
| 60 |
+
],
|
| 61 |
+
outputs=gr.JSON(),
|
| 62 |
+
title="Materials Project Search",
|
| 63 |
+
description="Search for materials by given parameters"
|
| 64 |
+
)
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
demo.launch(mcp_server=True, share=True)
|