Update app.py
Browse files
app.py
CHANGED
|
@@ -1,40 +1,25 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
|
|
|
|
|
|
|
|
|
| 3 |
import base64
|
| 4 |
from io import BytesIO
|
| 5 |
-
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 6 |
-
from rdkit import Chem
|
| 7 |
-
from rdkit.Chem import Draw, AllChem
|
| 8 |
-
import torch
|
| 9 |
|
| 10 |
# Load models
|
| 11 |
bio_gpt = pipeline("text-generation", model="microsoft/BioGPT-Large")
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
compliance_qa = pipeline("question-answering", model="nlpaueb/legal-bert-base-uncased")
|
| 15 |
-
|
| 16 |
-
# --- Functions ---
|
| 17 |
-
def extract_insights(disease, symptoms):
|
| 18 |
-
prompt = f"Recent treatments for {disease} with symptoms: {symptoms}"
|
| 19 |
-
result = bio_gpt(prompt, max_length=200, do_sample=True)
|
| 20 |
-
return result[0]['generated_text']
|
| 21 |
-
|
| 22 |
-
def generate_molecule():
|
| 23 |
-
sample_smiles = ["CCO", "CCN", "C1=CC=CC=C1", "C(C(=O)O)N", "CC(C)CC"]
|
| 24 |
-
return random.choice(sample_smiles)
|
| 25 |
|
| 26 |
-
def
|
| 27 |
-
inputs = chemberta_tokenizer(smiles, return_tensors="pt")
|
| 28 |
-
with torch.no_grad():
|
| 29 |
-
outputs = chemberta_model(**inputs)
|
| 30 |
-
return round(outputs.logits.mean().item(), 3)
|
| 31 |
-
|
| 32 |
-
def visualize_2d(smiles):
|
| 33 |
mol = Chem.MolFromSmiles(smiles)
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def mol_to_3d_html(smiles):
|
| 40 |
try:
|
|
@@ -43,65 +28,59 @@ def mol_to_3d_html(smiles):
|
|
| 43 |
AllChem.EmbedMolecule(mol, AllChem.ETKDG())
|
| 44 |
AllChem.UFFOptimizeMolecule(mol)
|
| 45 |
mol_block = Chem.MolToMolBlock(mol)
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
<
|
| 50 |
-
<script src="https://3Dmol.org/build/3Dmol-min.js"></script>
|
| 51 |
<script>
|
| 52 |
-
let
|
| 53 |
-
let
|
| 54 |
-
|
| 55 |
-
let molData = `{encoded_block}`;
|
| 56 |
-
viewer.addModel(molData, "mol");
|
| 57 |
viewer.setStyle({{}}, {{stick:{{}}}});
|
| 58 |
viewer.zoomTo();
|
|
|
|
| 59 |
viewer.render();
|
| 60 |
</script>
|
| 61 |
"""
|
| 62 |
-
return
|
| 63 |
except Exception as e:
|
| 64 |
-
return f"<p>Error
|
| 65 |
|
| 66 |
-
def
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
return compliance_qa(question=question, context=context)['answer']
|
| 70 |
|
| 71 |
-
#
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
smiles = generate_molecule()
|
| 75 |
-
prop_score = predict_properties(smiles)
|
| 76 |
-
img_base64 = visualize_2d(smiles)
|
| 77 |
-
compliance = check_compliance()
|
| 78 |
html_3d = mol_to_3d_html(smiles)
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
demo = gr.Interface(
|
| 90 |
-
fn=
|
| 91 |
-
inputs=
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
outputs=[
|
| 96 |
-
gr.Textbox(label="📜 Literature Insights"),
|
| 97 |
-
gr.Textbox(label="🧪 SMILES String"),
|
| 98 |
-
gr.Textbox(label="🧬 Property Score"),
|
| 99 |
-
gr.HTML(label="🧫 2D Molecule Structure"),
|
| 100 |
-
gr.HTML(label="🔬 3D Molecule Viewer"),
|
| 101 |
-
gr.Textbox(label="⚖️ FDA Compliance Summary")
|
| 102 |
-
],
|
| 103 |
-
title="🧬 AI-Driven Drug Discovery System",
|
| 104 |
-
description="Enter disease and symptoms to generate drug candidates using BioGPT, ChemBERTa, and LegalBERT. View 2D and animated 3D molecules!"
|
| 105 |
)
|
| 106 |
|
| 107 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from rdkit import Chem
|
| 4 |
+
from rdkit.Chem import AllChem, Draw
|
| 5 |
+
from rdkit.Chem import Descriptors
|
| 6 |
import base64
|
| 7 |
from io import BytesIO
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Load models
|
| 10 |
bio_gpt = pipeline("text-generation", model="microsoft/BioGPT-Large")
|
| 11 |
+
chemberta = pipeline("text-classification", model="seyonec/ChemBERTa-zinc-base-chemprop")
|
| 12 |
+
fda_gpt = pipeline("text-generation", model="EleutherAI/gpt-neo-1.3B")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
def mol_to_svg(smiles):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
mol = Chem.MolFromSmiles(smiles)
|
| 16 |
+
mol = Chem.AddHs(mol)
|
| 17 |
+
AllChem.EmbedMolecule(mol, AllChem.ETKDG())
|
| 18 |
+
AllChem.UFFOptimizeMolecule(mol)
|
| 19 |
+
drawer = Draw.MolDraw2DSVG(300, 300)
|
| 20 |
+
drawer.DrawMolecule(mol)
|
| 21 |
+
drawer.FinishDrawing()
|
| 22 |
+
return drawer.GetDrawingText()
|
| 23 |
|
| 24 |
def mol_to_3d_html(smiles):
|
| 25 |
try:
|
|
|
|
| 28 |
AllChem.EmbedMolecule(mol, AllChem.ETKDG())
|
| 29 |
AllChem.UFFOptimizeMolecule(mol)
|
| 30 |
mol_block = Chem.MolToMolBlock(mol)
|
| 31 |
+
mol_block = mol_block.replace("\n", "\\n")
|
| 32 |
|
| 33 |
+
html = f"""
|
| 34 |
+
<div id=\"viewer\" style=\"width: 400px; height: 400px; position: relative;\"></div>
|
| 35 |
+
<script src=\"https://3Dmol.org/build/3Dmol-min.js\"></script>
|
|
|
|
| 36 |
<script>
|
| 37 |
+
let viewer = $3Dmol.createViewer("viewer", {{ backgroundColor: "white" }});
|
| 38 |
+
let molBlock = `{mol_block}`;
|
| 39 |
+
viewer.addModel(molBlock, "mol");
|
|
|
|
|
|
|
| 40 |
viewer.setStyle({{}}, {{stick:{{}}}});
|
| 41 |
viewer.zoomTo();
|
| 42 |
+
viewer.animate({{loop: "backAndForth"}});
|
| 43 |
viewer.render();
|
| 44 |
</script>
|
| 45 |
"""
|
| 46 |
+
return html
|
| 47 |
except Exception as e:
|
| 48 |
+
return f"<p>Error rendering 3D molecule: {str(e)}</p>"
|
| 49 |
|
| 50 |
+
def drug_discovery(disease, symptoms):
|
| 51 |
+
prompt = f"Disease: {disease}\nSymptoms: {symptoms}\nLiterature Review:"
|
| 52 |
+
literature = bio_gpt(prompt, max_length=250)[0]['generated_text']
|
|
|
|
| 53 |
|
| 54 |
+
smiles = "CC(C)CC" # Just an example SMILES string, normally generated by a molecule generator
|
| 55 |
+
property_score = chemberta(smiles)[0]['score']
|
| 56 |
+
svg = mol_to_svg(smiles)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
html_3d = mol_to_3d_html(smiles)
|
| 58 |
|
| 59 |
+
fda_prompt = f"A drug with SMILES {smiles} is under review. List FDA regulatory issues."
|
| 60 |
+
fda_output = fda_gpt(fda_prompt, max_length=100)[0]['generated_text']
|
| 61 |
+
|
| 62 |
+
return literature, smiles, f"ChemBERTa Property Score: {property_score:.3f}", svg, html_3d, fda_output
|
| 63 |
+
|
| 64 |
+
inputs = [
|
| 65 |
+
gr.Textbox(label="🦠 Disease", placeholder="e.g. lung cancer"),
|
| 66 |
+
gr.Textbox(label="🧾 Symptoms", placeholder="e.g. shortness of breath, weight loss")
|
| 67 |
+
]
|
| 68 |
+
|
| 69 |
+
outputs = [
|
| 70 |
+
gr.Textbox(label="📜 Literature Insights"),
|
| 71 |
+
gr.Textbox(label="🧪 SMILES String"),
|
| 72 |
+
gr.Textbox(label="🧬 Property Score"),
|
| 73 |
+
gr.HTML(label="🧫 2D Molecule Structure"),
|
| 74 |
+
gr.HTML(label="🔬 3D Molecule Viewer"),
|
| 75 |
+
gr.Textbox(label="⚖️ FDA Compliance Summary")
|
| 76 |
+
]
|
| 77 |
|
| 78 |
demo = gr.Interface(
|
| 79 |
+
fn=drug_discovery,
|
| 80 |
+
inputs=inputs,
|
| 81 |
+
outputs=outputs,
|
| 82 |
+
title="💊 AI-Driven Drug Discovery using LLMs",
|
| 83 |
+
description="Enter a disease and its symptoms. This app generates literature insights, a possible molecule in SMILES format, scores its drug-likeness, and shows 2D & 3D views."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 84 |
)
|
| 85 |
|
| 86 |
demo.launch()
|