Spaces:
Runtime error
Runtime error
Upload 2 files
Browse files- app.py +82 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pymatgen.ext.matproj import MPRester
|
| 3 |
+
from pymatgen.analysis.phase_diagram import PhaseDiagram, PDPlotter
|
| 4 |
+
from pymatgen.core.composition import Composition
|
| 5 |
+
import plotly.graph_objs as go
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
MATERIALS_PROJECT_API_KEY = os.getenv('MATERIALS_PROJECT_API_KEY')
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def create_phase_diagram(elements, max_e_above_hull, color_scheme, plot_style, functional, finite_temp):
|
| 12 |
+
# Split elements and remove any whitespace
|
| 13 |
+
element_list = [el.strip() for el in elements.split('-')]
|
| 14 |
+
|
| 15 |
+
with MPRester(MATERIALS_PROJECT_API_KEY) as m:
|
| 16 |
+
# Fetch all entries from the Materials Project database
|
| 17 |
+
entries = m.get_entries_in_chemsys(element_list, compatible_only=True, inc_structure='final')
|
| 18 |
+
|
| 19 |
+
# Fetch elemental entries (they are usually GGA calculations)
|
| 20 |
+
elemental_entries = m.get_entries_in_chemsys(element_list, compatible_only=True, inc_structure='final')
|
| 21 |
+
elemental_entries = [e for e in elemental_entries if e.composition.is_element]
|
| 22 |
+
|
| 23 |
+
# Filter entries based on functional
|
| 24 |
+
if functional == "GGA":
|
| 25 |
+
entries = [e for e in entries if not e.parameters.get("run_type", "").startswith("GGA+U")]
|
| 26 |
+
elif functional == "GGA+U":
|
| 27 |
+
entries = [e for e in entries if e.parameters.get("run_type", "").startswith("GGA+U")]
|
| 28 |
+
# Add elemental entries to ensure they are included
|
| 29 |
+
entries.extend([e for e in elemental_entries if e not in entries])
|
| 30 |
+
|
| 31 |
+
# Build the phase diagram
|
| 32 |
+
try:
|
| 33 |
+
phase_diagram = PhaseDiagram(entries)
|
| 34 |
+
except ValueError as e:
|
| 35 |
+
return go.Figure().add_annotation(text=str(e))
|
| 36 |
+
|
| 37 |
+
# Generate plotly figure
|
| 38 |
+
if plot_style == "2D":
|
| 39 |
+
plotter = PDPlotter(phase_diagram, show_unstable=True, backend="plotly")
|
| 40 |
+
fig = plotter.get_plot()
|
| 41 |
+
else:
|
| 42 |
+
# For 3D plots, limit to ternary systems
|
| 43 |
+
if len(element_list) == 3:
|
| 44 |
+
plotter = PDPlotter(phase_diagram, show_unstable=True, backend="plotly", ternary_style='3d')
|
| 45 |
+
fig = plotter.get_plot()
|
| 46 |
+
else:
|
| 47 |
+
return go.Figure().add_annotation(text="3D plots are only available for ternary systems.")
|
| 48 |
+
|
| 49 |
+
# Adjust the maximum energy above hull
|
| 50 |
+
# (This is a placeholder as PDPlotter does not support direct filtering)
|
| 51 |
+
|
| 52 |
+
# Return the figure
|
| 53 |
+
return fig
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
# Define Gradio interface components
|
| 57 |
+
elements_input = gr.Textbox(label="Elements (e.g., 'Li-Fe-O')", placeholder="Enter elements separated by '-'",
|
| 58 |
+
value="Li-Fe-O")
|
| 59 |
+
max_e_above_hull_slider = gr.Slider(minimum=0, maximum=1, value=0.1, label="Maximum Energy Above Hull (eV)")
|
| 60 |
+
color_scheme_dropdown = gr.Dropdown(choices=["Energy Above Hull", "Formation Energy"], label="Color Scheme")
|
| 61 |
+
plot_style_dropdown = gr.Dropdown(choices=["2D", "3D"], label="Plot Style")
|
| 62 |
+
functional_dropdown = gr.Dropdown(choices=["GGA", "GGA+U", "Both"], label="Functional")
|
| 63 |
+
finite_temp_toggle = gr.Checkbox(label="Enable Finite Temperature Estimation")
|
| 64 |
+
|
| 65 |
+
# Create Gradio interface
|
| 66 |
+
iface = gr.Interface(
|
| 67 |
+
fn=create_phase_diagram,
|
| 68 |
+
inputs=[
|
| 69 |
+
elements_input,
|
| 70 |
+
max_e_above_hull_slider,
|
| 71 |
+
color_scheme_dropdown,
|
| 72 |
+
plot_style_dropdown,
|
| 73 |
+
functional_dropdown,
|
| 74 |
+
finite_temp_toggle
|
| 75 |
+
],
|
| 76 |
+
outputs=gr.Plot(label="Phase Diagram"),
|
| 77 |
+
title="Materials Project Phase Diagram",
|
| 78 |
+
description="Generate a phase diagram for a set of elements using Materials Project data."
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# Launch the app
|
| 82 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
plotly
|
| 2 |
+
pymatgen
|
| 3 |
+
gradio
|
| 4 |
+
datasets
|