File size: 4,113 Bytes
e1228a0 65e3e3f 4717bae 000942b 4717bae 72b3c2c e1228a0 72b3c2c e1228a0 72b3c2c e1228a0 8ecb2fa e1228a0 000942b e1228a0 72b3c2c e1228a0 2da9c47 000942b e1228a0 04d92a2 2da9c47 e1228a0 2da9c47 e1228a0 2da9c47 8ecb2fa 2da9c47 65e3e3f e1228a0 65e3e3f e1228a0 2da9c47 65e3e3f e1228a0 2da9c47 e1228a0 72b3c2c 8ecb2fa 72b3c2c 2da9c47 |
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
import gradio as gr
from transformers import pipeline
from rdkit import Chem
from rdkit.Chem import AllChem
from rdkit.Chem.Draw import rdMolDraw2D
from rdkit.Chem import rdDepictor
import base64
from io import BytesIO
import py3Dmol
# Function to generate literature and 3D molecule view
def drug_discovery(disease, symptoms):
# BioGPT pipeline
bio_gpt = pipeline("text-generation", model="microsoft/BioGPT-Large")
prompt = f"Recent treatments for {disease} with symptoms: {symptoms}."
literature = bio_gpt(prompt, max_length=200)[0]['generated_text']
# Generate SMILES (placeholder)
smiles = "CC(C)CC" # Example: isopentane
# Generate RDKit molecule
mol = Chem.MolFromSmiles(smiles)
if not mol:
return "Invalid SMILES generated", "", "", ""
AllChem.Compute2DCoords(mol)
# Draw 2D image
drawer = rdMolDraw2D.MolDraw2DCairo(300, 300)
drawer.DrawMolecule(mol)
drawer.FinishDrawing()
img_data = drawer.GetDrawingText()
# Convert binary to base64
img_base64 = base64.b64encode(img_data).decode("utf-8")
img_html = f'''<div style="text-align:center; margin-top: 10px; animation: fadeIn 2s ease-in-out;">
<img src="data:image/png;base64,{img_base64}" alt="2D Molecule"
style="border-radius: 16px; box-shadow: 0 6px 20px rgba(0,128,128,0.2); border: 1px solid #ccc;">
<div style='font-family: Arial, sans-serif; color: #2c3e50; margin-top: 8px; animation: slideUp 1.5s ease-in-out;'>π Visualized Drug Molecule (2D)</div>
</div>'''
# 3D molecule
mol3d = Chem.AddHs(mol)
AllChem.EmbedMolecule(mol3d)
AllChem.UFFOptimizeMolecule(mol3d)
mb = Chem.MolToMolBlock(mol3d)
viewer = py3Dmol.view(width=420, height=420)
viewer.addModel(mb, "mol")
viewer.setStyle({"stick": {"colorscheme": "tealCarbon"}})
viewer.setBackgroundColor("white")
viewer.zoomTo()
viewer.rotate(1)
viewer.spin(True)
viewer_html_raw = viewer._make_html()
viewer_html = f'''
<div style="text-align:center; margin-top: 20px; animation: zoomIn 2s ease-in-out;">
<iframe srcdoc="{viewer_html_raw.replace('"', '"')}"
width="440" height="440" frameborder="0"
style="border-radius: 16px; box-shadow: 0 8px 30px rgba(0,128,128,0.25);"></iframe>
<div style='font-family: Arial, sans-serif; color: #2c3e50; margin-top: 8px; animation: slideUp 1.5s ease-in-out;'>𧬠Animated 3D Molecule (Stick View)</div>
</div>'''
return literature, smiles, img_html, viewer_html
# Gradio UI
disease_input = gr.Textbox(label="π₯ Enter Disease (e.g., lung cancer)", value="lung cancer")
symptom_input = gr.Textbox(label="π Enter Symptoms (e.g., cough, weight loss)", value="shortness of breath, weight loss")
lit_output = gr.Textbox(label="π° Literature Insights from BioGPT")
smiles_output = gr.Textbox(label="π§ͺ SMILES Representation")
img_output = gr.HTML(label="πΌοΈ Molecule 2D Visualization")
viewer_output = gr.HTML(label="π¬ 3D Drug Molecule Animation")
custom_css = """
@keyframes fadeIn {
from {opacity: 0;}
to {opacity: 1;}
}
@keyframes slideUp {
from {transform: translateY(40px); opacity: 0;}
to {transform: translateY(0); opacity: 1;}
}
@keyframes zoomIn {
from {transform: scale(0.5); opacity: 0;}
to {transform: scale(1); opacity: 1;}
}
body {
background: linear-gradient(to right, #e0f7fa, #ffffff);
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.gradio-container {
animation: fadeIn 1.5s ease-in-out;
}
"""
iface = gr.Interface(
fn=drug_discovery,
inputs=[disease_input, symptom_input],
outputs=[lit_output, smiles_output, img_output, viewer_output],
title="π₯ AI-Powered Drug Discovery for Hospitals",
description="This hospital-themed platform takes a disease and symptoms as input, retrieves biomedical insights using BioGPT, and visualizes potential drug molecules in 2D and animated 3D. Ideal for clinical research and pharma innovation.",
theme="default",
css=custom_css
)
iface.launch(share=True)
|