Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
import
|
| 2 |
from transformers import pipeline
|
| 3 |
from rdkit import Chem
|
| 4 |
from rdkit.Chem import AllChem
|
|
@@ -8,59 +8,60 @@ import base64
|
|
| 8 |
from io import BytesIO
|
| 9 |
import py3Dmol
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
st.header("🧾 Symptoms")
|
| 19 |
-
symptoms = st.text_input("Enter symptoms", "shortness of breath, weight loss")
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
literature = bio_gpt(prompt, max_length=200)[0]['generated_text']
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
|
|
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
drawer.FinishDrawing()
|
| 43 |
-
img_data = drawer.GetDrawingText()
|
| 44 |
-
st.image(img_data, caption="2D Structure", use_column_width=False)
|
| 45 |
|
| 46 |
-
|
| 47 |
-
mol3d = Chem.AddHs(mol)
|
| 48 |
-
AllChem.EmbedMolecule(mol3d)
|
| 49 |
-
AllChem.UFFOptimizeMolecule(mol3d)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
st.components.v1.html(xyzview._make_html(), height=400)
|
| 62 |
|
| 63 |
-
|
| 64 |
|
| 65 |
-
else:
|
| 66 |
-
st.info("👈 Enter disease and symptoms, then hit Submit to discover a molecule.")
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
from rdkit import Chem
|
| 4 |
from rdkit.Chem import AllChem
|
|
|
|
| 8 |
from io import BytesIO
|
| 9 |
import py3Dmol
|
| 10 |
|
| 11 |
+
# Function to generate literature and 3D molecule view
|
| 12 |
+
def drug_discovery(disease, symptoms):
|
| 13 |
+
# BioGPT pipeline
|
| 14 |
+
bio_gpt = pipeline("text-generation", model="microsoft/BioGPT-Large")
|
| 15 |
+
prompt = f"Recent treatments for {disease} with symptoms: {symptoms}."
|
| 16 |
+
literature = bio_gpt(prompt, max_length=200)[0]['generated_text']
|
| 17 |
|
| 18 |
+
# Generate SMILES (placeholder)
|
| 19 |
+
smiles = "CC(C)CC" # Example: isopentane
|
| 20 |
|
| 21 |
+
# Generate RDKit molecule
|
| 22 |
+
mol = Chem.MolFromSmiles(smiles)
|
| 23 |
+
AllChem.Compute2DCoords(mol)
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
# Draw 2D image
|
| 26 |
+
drawer = rdMolDraw2D.MolDraw2DCairo(300, 300)
|
| 27 |
+
drawer.DrawMolecule(mol)
|
| 28 |
+
drawer.FinishDrawing()
|
| 29 |
+
img_data = drawer.GetDrawingText()
|
|
|
|
| 30 |
|
| 31 |
+
# Convert binary to base64
|
| 32 |
+
img_base64 = base64.b64encode(img_data).decode("utf-8")
|
| 33 |
+
img_html = f'<img src="data:image/png;base64,{img_base64}" alt="2D Molecule">'
|
| 34 |
|
| 35 |
+
# 3D molecule
|
| 36 |
+
mol3d = Chem.AddHs(mol)
|
| 37 |
+
AllChem.EmbedMolecule(mol3d)
|
| 38 |
+
AllChem.UFFOptimizeMolecule(mol3d)
|
| 39 |
+
mb = Chem.MolToMolBlock(mol3d)
|
| 40 |
|
| 41 |
+
viewer = py3Dmol.view(width=400, height=400)
|
| 42 |
+
viewer.addModel(mb, "mol")
|
| 43 |
+
viewer.setStyle({"stick": {}})
|
| 44 |
+
viewer.setBackgroundColor("white")
|
| 45 |
+
viewer.zoomTo()
|
| 46 |
+
viewer_html = viewer._make_html()
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
return literature, smiles, img_html, viewer_html
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
+
# Gradio UI
|
| 51 |
+
disease_input = gr.Textbox(label="Enter Disease", value="lung cancer")
|
| 52 |
+
symptom_input = gr.Textbox(label="Enter Symptoms", value="shortness of breath, weight loss")
|
| 53 |
+
lit_output = gr.Textbox(label="Literature Insights")
|
| 54 |
+
smiles_output = gr.Textbox(label="SMILES String")
|
| 55 |
+
img_output = gr.HTML(label="2D Molecule")
|
| 56 |
+
viewer_output = gr.HTML(label="3D Molecule Viewer")
|
| 57 |
|
| 58 |
+
iface = gr.Interface(
|
| 59 |
+
fn=drug_discovery,
|
| 60 |
+
inputs=[disease_input, symptom_input],
|
| 61 |
+
outputs=[lit_output, smiles_output, img_output, viewer_output],
|
| 62 |
+
title="🧬 Drug Discovery using LLMs",
|
| 63 |
+
description="Enter a disease and symptoms to generate literature insights and view the molecule in 2D and interactive 3D."
|
| 64 |
+
)
|
|
|
|
| 65 |
|
| 66 |
+
iface.launch()
|
| 67 |
|
|
|
|
|
|