|
|
import gradio as gr |
|
|
import pubchempy as pcp |
|
|
|
|
|
def get_chemical_info(chemical_name): |
|
|
try: |
|
|
compound = pcp.get_compounds(chemical_name, 'name')[0] |
|
|
|
|
|
output = [] |
|
|
output.append(f"IUPAC Name: {compound.iupac_name}") |
|
|
output.append(f"Common Name: {compound.synonyms[0]}") |
|
|
output.append(f"Synonyms: {', '.join(compound.synonyms[:4])}") |
|
|
output.append(f"Formula: {compound.molecular_formula}") |
|
|
output.append(f"Molecular Weight: {compound.molecular_weight}") |
|
|
output.append(f"Exact Molecular Weight: {compound.exact_mass}") |
|
|
output.append(f"Isotope Atom Count: {compound.isotope_atom_count}") |
|
|
output.append(f"Charge: {compound.charge}") |
|
|
|
|
|
|
|
|
output.append("\nAtoms:") |
|
|
for i, atom in enumerate(compound.atoms, start=1): |
|
|
output.append(f" Atom {i}: Element = {atom.element}") |
|
|
|
|
|
|
|
|
output.append("\nBonds:") |
|
|
for bond in compound.bonds: |
|
|
atom1 = bond.atom1.element |
|
|
atom2 = bond.atom2.element |
|
|
output.append(f" Bond between {atom1} and {atom2}, Order: {bond.order}") |
|
|
|
|
|
return "\n".join(output) |
|
|
|
|
|
except IndexError: |
|
|
return f"No information found for '{chemical_name}'. Please try a more precise name." |
|
|
|
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
|
fn=get_chemical_info, |
|
|
inputs=gr.Textbox(label="Enter Chemical Name"), |
|
|
outputs=gr.Textbox(label="Compound Information", lines=20), |
|
|
title="ChemQuery: Learn Molecules Easily", |
|
|
description="Enter a chemical name to get formula, synonyms, atomic structure, and more. Powered by PubChem & PubChemPy. Designed for student use in chemistry learning." |
|
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch() |
|
|
|