import gradio as gr import requests from rdkit import Chem from rdkit.Chem import Draw def get_smiles(name): if not name or len(name) < 3: return "" try: url = f"https://cactus.nci.nih.gov/chemical/structure/{name}/smiles" r = requests.get(url, timeout=5) return r.text.strip() if r.status_code == 200 else "Not found" except: return "Connection Error" def analyze_mechanism(name, reagent): smiles = get_smiles(name) if not smiles or "not" in smiles.lower(): return None, "## ❌ Error: Molecule Not Found\nPlease try 'Propene', 'Cyclohexene', or '2-Butene'." mol = Chem.MolFromSmiles(smiles) matches = mol.GetSubstructMatches(Chem.MolFromSmarts("[C]=[C]")) if not matches: return None, "## ❌ Error: No C=C Bond\nAddition requires an alkene." c1_idx, c2_idx = matches[0] prod = Chem.RWMol(mol) prod.RemoveBond(c1_idx, c2_idx) prod.AddBond(c1_idx, c2_idx, Chem.rdchem.BondType.SINGLE) h1, h2 = mol.GetAtomWithIdx(c1_idx).GetTotalNumHs(), mol.GetAtomWithIdx(c2_idx).GetTotalNumHs() target_idx = c1_idx if h1 < h2 else c2_idx new_atom = prod.AddAtom(Chem.Atom(reagent.replace("H", ""))) prod.AddBond(target_idx, new_atom, Chem.rdchem.BondType.SINGLE) img = Draw.MolsToGridImage( [mol, prod], molsPerRow=2, subImgSize=(500, 500), legends=["1. REACTANT", "2. FINAL PRODUCT"], highlightAtomLists=[[c1_idx, c2_idx], [target_idx]], useSVG=False ) explanation = f""" # 🧪 REACTION INTERPRETATION ### 1️⃣ THE ELECTROPHILIC ATTACK The high-electron density pi-bond attacks the **H+** electrophile. ### 2️⃣ CARBOCATION STABILITY The positive charge forms on the carbon that is more stable. **Stability:** Tertiary (3°) > Secondary (2°) > Primary (1°). ### 3️⃣ NUCLEOPHILIC COMPLETION The **{reagent[1:]}-** ion bonds to the carbocation to form the final product. """ return img, explanation # Deep Dark Theme CSS dark_css = """ body, .gradio-container { background-color: #0f172a !important; color: #f8fafc !important; } .variant-panel { background-color: #1e293b !important; border: 1px solid #334155 !important; border-radius: 8px !important; } h1, h2, h3 { color: #38bdf8 !important; } span, p, label { color: #f8fafc !important; } .gr-button-primary { background-color: #0284c7 !important; border: none !important; color: white !important; } .gr-textbox input, .gr-dropdown select { background-color: #0f172a !important; color: white !important; border: 1px solid #334155 !important; } """ with gr.Blocks() as demo: gr.Markdown("# ⚗️ MARKOVNIKOV LAB", elem_id="main-title") with gr.Row(): with gr.Column(scale=1): with gr.Column(variant="panel"): gr.Markdown("### 📥 INPUT DATA") chem_input = gr.Textbox(label="Alkene Name", placeholder="e.g. 2-Butene") reagent_input = gr.Dropdown(["HCl", "HBr", "HI"], label="Reagent", value="HCl") submit_btn = gr.Button("RUN ANALYSIS", variant="primary") with gr.Column(variant="panel"): gr.Markdown("### 🎓 EXAM TIPS") gr.Markdown("• **Markovnikov's Rule:** Hydrogen goes to the carbon with more H's.") gr.Markdown("• **Stability:** The intermediate carbocation determines the major product.") with gr.Column(scale=2): with gr.Column(variant="panel"): res_img = gr.Image(label="Reaction Visualization", type="pil", interactive=False) res_text = gr.Markdown("Results will appear here in high-contrast text.") submit_btn.click(analyze_mechanism, [chem_input, reagent_input], [res_img, res_text]) if __name__ == "__main__": demo.launch(theme=gr.themes.Default(), css=dark_css)