Spaces:
Running
Running
Upload old_app.py
Browse files- old_app.py +110 -0
old_app.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from pymatgen.ext.matproj import MPRester
|
| 4 |
+
from crystal_toolkit.components.structure import StructureMoleculeComponent
|
| 5 |
+
import json
|
| 6 |
+
from crystal_toolkit.core.scene import Scene
|
| 7 |
+
|
| 8 |
+
# Get the API key from environment variable
|
| 9 |
+
MATERIALS_PROJECT_API_KEY = os.getenv('MATERIALS_PROJECT_API_KEY')
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def search_materials(query):
|
| 13 |
+
with MPRester(MATERIALS_PROJECT_API_KEY) as mpr:
|
| 14 |
+
results = mpr.summary.search(
|
| 15 |
+
chemsys=query,
|
| 16 |
+
fields=["material_id", "formula_pretty"]
|
| 17 |
+
)
|
| 18 |
+
options = {res.material_id: f"{res.formula_pretty} ({res.material_id})" for res in results}
|
| 19 |
+
if not options:
|
| 20 |
+
return gr.update(choices=[], label="Select Material", value=None)
|
| 21 |
+
return gr.update(choices=list(options.keys()), label="Select Material", value=list(options.keys())[0])
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def get_material_data(material_id):
|
| 25 |
+
with MPRester(MATERIALS_PROJECT_API_KEY) as mpr:
|
| 26 |
+
material = mpr.get_structure_by_material_id(material_id)
|
| 27 |
+
summary = mpr.summary.get_data_by_id(material_id)
|
| 28 |
+
return material, summary
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def generate_structure_html(scene_json_str):
|
| 32 |
+
import uuid
|
| 33 |
+
div_id = 'structure_' + str(uuid.uuid4())
|
| 34 |
+
html = f'''
|
| 35 |
+
<div id="{div_id}" style="width: 500px; height: 500px;"></div>
|
| 36 |
+
<script src="https://unpkg.com/crystaltoolkit@latest/crystaltoolkit.min.js"></script>
|
| 37 |
+
<script type="application/json" id="{div_id}_data">
|
| 38 |
+
{scene_json_str}
|
| 39 |
+
</script>
|
| 40 |
+
<script>
|
| 41 |
+
const sceneData = JSON.parse(document.getElementById("{div_id}_data").textContent);
|
| 42 |
+
const viewer = new crystalToolkit.ThreeJSViewer("{div_id}", sceneData);
|
| 43 |
+
viewer.render();
|
| 44 |
+
</script>
|
| 45 |
+
'''
|
| 46 |
+
return html
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def display_material(material_id):
|
| 50 |
+
if not material_id:
|
| 51 |
+
return "", "Please select a material."
|
| 52 |
+
|
| 53 |
+
material, summary = get_material_data(material_id)
|
| 54 |
+
|
| 55 |
+
# Create StructureMoleculeComponent
|
| 56 |
+
structure_component = StructureMoleculeComponent(material, id="my_structure")
|
| 57 |
+
|
| 58 |
+
# Access the scene json from the component
|
| 59 |
+
scene_json = structure_component._initial_data["scene"]
|
| 60 |
+
|
| 61 |
+
# What to do here???
|
| 62 |
+
structure_html = "<p></p>"
|
| 63 |
+
|
| 64 |
+
# Extract key properties
|
| 65 |
+
properties = {
|
| 66 |
+
"Material ID": material_id,
|
| 67 |
+
"Formula": summary.formula_pretty,
|
| 68 |
+
"Energy Above Hull (eV/atom)": summary.energy_above_hull,
|
| 69 |
+
"Space Group": summary.symmetry.symbol,
|
| 70 |
+
"Band Gap (eV)": summary.band_gap,
|
| 71 |
+
"Formation Energy (eV/atom)": summary.formation_energy_per_atom,
|
| 72 |
+
"Magnetic Ordering": summary.ordering,
|
| 73 |
+
"Total Magnetization (μB/f.u.)": summary.total_magnetization,
|
| 74 |
+
"Experimentally Observed": summary.is_stable,
|
| 75 |
+
"Crystal System": summary.symmetry.crystal_system,
|
| 76 |
+
"Density (g/cm³)": summary.density,
|
| 77 |
+
}
|
| 78 |
+
# Format properties as HTML
|
| 79 |
+
properties_html = "<table>"
|
| 80 |
+
for key, value in properties.items():
|
| 81 |
+
properties_html += f"<tr><td><strong>{key}</strong></td><td>{value}</td></tr>"
|
| 82 |
+
properties_html += "</table>"
|
| 83 |
+
|
| 84 |
+
return structure_html, properties_html
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
with gr.Blocks() as demo:
|
| 88 |
+
gr.Markdown("## Interactive Crystal Viewer")
|
| 89 |
+
with gr.Row():
|
| 90 |
+
query_input = gr.Textbox(label="Search by Chemical System (e.g., 'Ac-Cd-Ge')", value="Ac-Cd-Ge")
|
| 91 |
+
search_button = gr.Button("Search")
|
| 92 |
+
material_dropdown = gr.Dropdown(label="Select Material", choices=[])
|
| 93 |
+
display_button = gr.Button("Display Material")
|
| 94 |
+
with gr.Row():
|
| 95 |
+
structure_viewer = gr.HTML()
|
| 96 |
+
properties_output = gr.HTML()
|
| 97 |
+
|
| 98 |
+
search_button.click(
|
| 99 |
+
fn=search_materials,
|
| 100 |
+
inputs=query_input,
|
| 101 |
+
outputs=material_dropdown
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
display_button.click(
|
| 105 |
+
fn=display_material,
|
| 106 |
+
inputs=material_dropdown,
|
| 107 |
+
outputs=[structure_viewer, properties_output]
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
demo.launch()
|