Spaces:
Runtime error
Runtime error
Vaishnav14220
commited on
Commit
·
e46b082
1
Parent(s):
27caf5c
Restore all original app features: Search, Reaction Detail, Reaction SVG tabs, plus add individual database tabs
Browse files
app.py
CHANGED
|
@@ -662,10 +662,134 @@ def generate_arrhenius_plot(A, n, Ea, Tmin, Tmax, num_points=100, point_text="")
|
|
| 662 |
|
| 663 |
def build_interface() -> gr.Blocks:
|
| 664 |
with gr.Blocks(title="NIST Chemistry Explorer") as demo:
|
| 665 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 666 |
|
| 667 |
with gr.Tabs():
|
| 668 |
-
# Tab 1:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 669 |
with gr.TabItem("Kinetics Plotter"):
|
| 670 |
with gr.Row():
|
| 671 |
with gr.Column():
|
|
@@ -691,7 +815,7 @@ def build_interface() -> gr.Blocks:
|
|
| 691 |
outputs=[plot_output, thermo_table, info_output]
|
| 692 |
)
|
| 693 |
|
| 694 |
-
# Tabs
|
| 695 |
for db_name in DB_TABS.keys():
|
| 696 |
with gr.TabItem(db_name):
|
| 697 |
gr.Markdown(f"### {db_name}\n{DB_TABS[db_name]['summary']}")
|
|
@@ -722,6 +846,26 @@ def build_interface() -> gr.Blocks:
|
|
| 722 |
return fetch_specific_db(db_name, formula)
|
| 723 |
fetch_btn.click(wrapped_fetch, inputs=[formula_input], outputs=[output_md, output_df, output_plot])
|
| 724 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 725 |
# Examples (global or per-tab)
|
| 726 |
gr.Examples(
|
| 727 |
examples=[
|
|
|
|
| 662 |
|
| 663 |
def build_interface() -> gr.Blocks:
|
| 664 |
with gr.Blocks(title="NIST Chemistry Explorer") as demo:
|
| 665 |
+
gr.Markdown(
|
| 666 |
+
dedent(
|
| 667 |
+
"""
|
| 668 |
+
# NIST Chemical Kinetics Explorer
|
| 669 |
+
|
| 670 |
+
Search the [NIST Chemical Kinetics Database](https://kinetics.nist.gov/kinetics/)
|
| 671 |
+
directly from Hugging Face Spaces. This tool mirrors the public advanced search form,
|
| 672 |
+
sends the same query to NIST, and formats summary plus detailed kinetics data.
|
| 673 |
+
|
| 674 |
+
⚠️ *All results come from the live NIST website. Please respect their usage policies
|
| 675 |
+
and keep queries reasonable.*
|
| 676 |
+
"""
|
| 677 |
+
)
|
| 678 |
+
)
|
| 679 |
+
|
| 680 |
+
results_state = gr.State([])
|
| 681 |
|
| 682 |
with gr.Tabs():
|
| 683 |
+
# Tab 1: Search (Original functionality)
|
| 684 |
+
with gr.TabItem("Search"):
|
| 685 |
+
simple_search = gr.Textbox(label="Search Query", placeholder="Enter reactants, products, or keywords (e.g., CH4 + O2)")
|
| 686 |
+
|
| 687 |
+
with gr.Row():
|
| 688 |
+
decomp = gr.Checkbox(label="Only decomposition reactions", value=False)
|
| 689 |
+
category = gr.Dropdown(label="Result type filter", choices=CATEGORY_CHOICES, value=str(Category.any.value))
|
| 690 |
+
units = gr.Textbox(
|
| 691 |
+
label="Optional Units token",
|
| 692 |
+
placeholder="Leave blank to use NIST account defaults",
|
| 693 |
+
)
|
| 694 |
+
|
| 695 |
+
search_button = gr.Button("Search NIST", variant="primary")
|
| 696 |
+
search_status = gr.Markdown()
|
| 697 |
+
result_table = gr.Dataframe(
|
| 698 |
+
headers=["#", "Records", "Reaction", "Detail URL"],
|
| 699 |
+
datatype=["number", "number", "str", "str"],
|
| 700 |
+
interactive=False,
|
| 701 |
+
wrap=True,
|
| 702 |
+
)
|
| 703 |
+
|
| 704 |
+
# Tab 2: Reaction Detail (Original functionality)
|
| 705 |
+
with gr.TabItem("Reaction Detail"):
|
| 706 |
+
selection = gr.Dropdown(
|
| 707 |
+
label="Select a reaction from the latest search",
|
| 708 |
+
choices=[],
|
| 709 |
+
interactive=False,
|
| 710 |
+
)
|
| 711 |
+
manual_url = gr.Textbox(
|
| 712 |
+
label="Or paste a NIST detail URL",
|
| 713 |
+
placeholder="https://kinetics.nist.gov/kinetics/ReactionSearch?....",
|
| 714 |
+
)
|
| 715 |
+
detail_button = gr.Button("Fetch Reaction Detail")
|
| 716 |
+
|
| 717 |
+
# Reaction metadata and details
|
| 718 |
+
detail_markdown = gr.Markdown()
|
| 719 |
+
|
| 720 |
+
# Kinetics data table
|
| 721 |
+
dataset_table = gr.Dataframe(
|
| 722 |
+
headers=["Section", "Squib", "Temp [K]", "A", "n", "Ea [J/mole]", "k(298 K)", "Order", "Squib URL"],
|
| 723 |
+
datatype=["str"] * 9,
|
| 724 |
+
interactive=False,
|
| 725 |
+
wrap=True,
|
| 726 |
+
)
|
| 727 |
+
|
| 728 |
+
# Reaction SVG visualization
|
| 729 |
+
with gr.Row():
|
| 730 |
+
gr.Markdown("### Reaction Structure")
|
| 731 |
+
reaction_svg = gr.HTML()
|
| 732 |
+
|
| 733 |
+
# Arrhenius plot
|
| 734 |
+
with gr.Row():
|
| 735 |
+
gr.Markdown("### Arrhenius Plot")
|
| 736 |
+
reaction_plot = gr.Plot()
|
| 737 |
+
|
| 738 |
+
# Tab 3: Reaction SVG (Original functionality)
|
| 739 |
+
with gr.TabItem("Reaction SVG"):
|
| 740 |
+
gr.Markdown(
|
| 741 |
+
"Render an RDKit reaction sketch from reaction SMILES/SMARTS. "
|
| 742 |
+
"Example: `CCO.O=C=O>>CC(=O)O` or `[CH3:1].[Cl:2][C@@H](F)[Br]>>[CH3:1][C@@H](F)[Cl]`."
|
| 743 |
+
)
|
| 744 |
+
|
| 745 |
+
# Common reaction examples
|
| 746 |
+
common_reactions = [
|
| 747 |
+
("Ethanol esterification", "CCO.CC(=O)O>>CC(=O)OCC.O"),
|
| 748 |
+
("Methane combustion", "C.O>>CO2"),
|
| 749 |
+
("Ethylene hydration", "C=C.O>>CCO"),
|
| 750 |
+
("Acetylene + HBr", "C#C.Br>>C=CBr"),
|
| 751 |
+
("Benzene nitration", "c1ccccc1.O=N(=O)O>>c1ccc(cc1)[N+](=O)[O-].O"),
|
| 752 |
+
("Methyl radical + Ethane", "[CH3].CC>>[CH4].C"),
|
| 753 |
+
("Chlorine + Hydrogen", "Cl.C>>CCl"),
|
| 754 |
+
("Propane oxidation", "CCC.O>>CC(C)=O"),
|
| 755 |
+
]
|
| 756 |
+
|
| 757 |
+
with gr.Row():
|
| 758 |
+
reaction_preset = gr.Dropdown(
|
| 759 |
+
label="Common Reactions",
|
| 760 |
+
choices=[label for label, _ in common_reactions],
|
| 761 |
+
interactive=True
|
| 762 |
+
)
|
| 763 |
+
preset_dict = {label: smiles for label, smiles in common_reactions}
|
| 764 |
+
|
| 765 |
+
reaction_input = gr.Textbox(
|
| 766 |
+
label="Reaction SMILES/SMARTS",
|
| 767 |
+
placeholder="Reactant1.Reactant2>>Product1.Product2",
|
| 768 |
+
lines=2,
|
| 769 |
+
)
|
| 770 |
+
|
| 771 |
+
def populate_from_preset(preset_name):
|
| 772 |
+
if preset_name and preset_name in preset_dict:
|
| 773 |
+
return preset_dict[preset_name]
|
| 774 |
+
return ""
|
| 775 |
+
|
| 776 |
+
reaction_preset.change(
|
| 777 |
+
fn=populate_from_preset,
|
| 778 |
+
inputs=reaction_preset,
|
| 779 |
+
outputs=reaction_input,
|
| 780 |
+
)
|
| 781 |
+
|
| 782 |
+
render_button = gr.Button("Render Reaction", variant="secondary")
|
| 783 |
+
reaction_svg_output = gr.HTML()
|
| 784 |
+
render_status = gr.Markdown()
|
| 785 |
+
|
| 786 |
+
render_button.click(
|
| 787 |
+
fn=render_reaction_svg,
|
| 788 |
+
inputs=reaction_input,
|
| 789 |
+
outputs=[reaction_svg_output, render_status],
|
| 790 |
+
)
|
| 791 |
+
|
| 792 |
+
# Tab 4: Kinetics Plotter
|
| 793 |
with gr.TabItem("Kinetics Plotter"):
|
| 794 |
with gr.Row():
|
| 795 |
with gr.Column():
|
|
|
|
| 815 |
outputs=[plot_output, thermo_table, info_output]
|
| 816 |
)
|
| 817 |
|
| 818 |
+
# Tabs 5-16: One per NIST database
|
| 819 |
for db_name in DB_TABS.keys():
|
| 820 |
with gr.TabItem(db_name):
|
| 821 |
gr.Markdown(f"### {db_name}\n{DB_TABS[db_name]['summary']}")
|
|
|
|
| 846 |
return fetch_specific_db(db_name, formula)
|
| 847 |
fetch_btn.click(wrapped_fetch, inputs=[formula_input], outputs=[output_md, output_df, output_plot])
|
| 848 |
|
| 849 |
+
# Event handlers for original functionality
|
| 850 |
+
search_button.click(
|
| 851 |
+
fn=perform_search,
|
| 852 |
+
inputs=[simple_search, decomp, category, units],
|
| 853 |
+
outputs=[result_table, search_status, selection, results_state],
|
| 854 |
+
)
|
| 855 |
+
|
| 856 |
+
detail_button.click(
|
| 857 |
+
fn=fetch_detail,
|
| 858 |
+
inputs=[selection, manual_url],
|
| 859 |
+
outputs=[detail_markdown, dataset_table, reaction_plot, reaction_svg],
|
| 860 |
+
)
|
| 861 |
+
|
| 862 |
+
# Auto-render SVG when selection changes
|
| 863 |
+
selection.change(
|
| 864 |
+
fn=fetch_detail,
|
| 865 |
+
inputs=[selection, manual_url],
|
| 866 |
+
outputs=[detail_markdown, dataset_table, reaction_plot, reaction_svg],
|
| 867 |
+
)
|
| 868 |
+
|
| 869 |
# Examples (global or per-tab)
|
| 870 |
gr.Examples(
|
| 871 |
examples=[
|