File size: 3,192 Bytes
411663c db8f05b 411663c cefc489 411663c db8f05b 411663c 5f39686 db8f05b 411663c cfcf72b 857f8ba cfcf72b 411663c cfcf72b 411663c 857f8ba 222f5de bbdcedf 5482655 bbdcedf 59ba94e bbdcedf 7dda106 8a22f44 bbdcedf 222f5de 857f8ba 222f5de |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
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
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 UI
with gr.Blocks(title="ChemQuery: Learn Molecules Easily") as demo:
gr.Markdown("## ChemQuery: Learn Molecules Easily")
gr.Markdown(
"""
Enter a chemical name to explore its molecular formula, synonyms, atomic structure, and bonding details.
ChemQuery is Powered by PubChem & PubChemPy — designed to make chemistry learning simple and fun for students, teachers, and curious minds alike.
If you find this initiative helpful, please:
- ❤️ Give it a like
- 🔁 Share it with your friends, classmates, or educators
- ⭐ Star the GitHub repo: [ChemQuery](https://github.com/RezuwanHassan262/ChemQuery/tree/main)
> _“ChemQuery is a free and open-source chemistry learning tool built as part of an educational initiative to support students worldwide.”_
Let's spread the love for science and open knowledge! 🌍🧪✨
"""
)
with gr.Row():
chemical_input = gr.Textbox(label="Enter Chemical Name", lines=1, scale=5)
submit_btn = gr.Button("Submit", scale=1, elem_id="submit-btn")
gr.Markdown(
"""
<style>
#submit-btn button {
background-color: #48CAE4 !important;
height: 48px !important;
font-weight: bold;
}
</style>
"""
)
output_text = gr.Textbox(label="Compound Information", lines=25)
download_file = gr.File(label="Download as TXT")
submit_btn.click(get_chemical_info, inputs=chemical_input, outputs=[output_text, download_file])
demo.launch()
|