ChemQuery / app.py
Rezuwan's picture
Update app.py
cfcf72b verified
raw
history blame
2.15 kB
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}")
# Atoms
output.append("\nAtoms:")
atom_dict = {i + 1: atom.element for i, atom in enumerate(compound.atoms)}
for idx, element in atom_dict.items():
output.append(f" Atom {idx}: Element = {element}")
# Bonds
output.append("\nBonds:")
for bond in compound.bonds:
atom1 = atom_dict.get(bond.aid1, f"Atom {bond.aid1}")
atom2 = atom_dict.get(bond.aid2, f"Atom {bond.aid2}")
output.append(f" Bond between {atom1} and {atom2}, Order: {bond.order}")
final_text = "\n".join(output)
# Save to a file for download
file_path = f"/tmp/{chemical_name.replace(' ', '_')}_info.txt"
with open(file_path, "w") as f:
f.write(final_text)
return final_text, file_path
except IndexError:
return f"No information found for '{chemical_name}'. Please try a more precise name.", None
# Gradio Interface
demo = gr.Interface(
fn=get_chemical_info,
inputs=gr.Textbox(label="Enter Chemical Name"),
outputs=[
gr.Textbox(label="Compound Information", lines=20),
gr.File(label="Download as TXT")
],
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()