Update app.py
Browse files
app.py
CHANGED
@@ -6,131 +6,96 @@ from rdkit.Chem.Draw import rdMolDraw2D
|
|
6 |
import base64
|
7 |
import re
|
8 |
import py3Dmol
|
|
|
9 |
|
10 |
-
#
|
|
|
11 |
|
12 |
def drug_discovery(disease, symptoms):
|
13 |
-
|
14 |
-
|
15 |
-
# Detailed medical prompt
|
16 |
prompt = (
|
17 |
-
f"
|
18 |
-
"
|
19 |
-
"
|
|
|
|
|
20 |
)
|
21 |
|
22 |
try:
|
23 |
-
|
24 |
-
|
25 |
-
result = f"Could not generate literature due to an error: {e}"
|
26 |
-
|
27 |
-
result = re.sub(r"<\s*/?\s*(TITLE|FREETEXT)\s*>", "", result)
|
28 |
-
result = re.sub(r"^.*?(?=Causes|Diagnosis|Treatment|Common medications)", "", result, flags=re.IGNORECASE)
|
29 |
-
|
30 |
-
# Generate SMILES
|
31 |
-
molecule_prompt = f"Give 5 different valid drug-like SMILES strings that can treat {disease} with symptoms: {symptoms}. Only list SMILES separated by spaces."
|
32 |
-
try:
|
33 |
-
smiles_result = bio_gpt(molecule_prompt, max_length=100)[0]['generated_text']
|
34 |
except Exception as e:
|
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 |
-
<div style=
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
return
|
85 |
-
|
86 |
-
# Gradio UI
|
87 |
-
|
88 |
-
disease_input = gr.Textbox(label="π₯ Enter Disease (e.g., lung cancer)", value="lung cancer")
|
89 |
-
symptom_input = gr.Textbox(label="π Enter Symptoms (e.g., cough, weight loss)", value="shortness of breath, weight loss")
|
90 |
-
lit_output = gr.Textbox(label="π Literature Insights from BioGPT")
|
91 |
-
smiles_output = gr.Textbox(label="π§ͺ SMILES Representation")
|
92 |
-
img_output = gr.HTML(label="πΌοΈ Molecule 2D Visualization")
|
93 |
-
viewer_output = gr.HTML(label="π¬ 3D Drug Molecule Animation")
|
94 |
-
|
95 |
-
custom_css = """
|
96 |
-
@keyframes fadeIn {
|
97 |
-
from {opacity: 0;}
|
98 |
-
to {opacity: 1;}
|
99 |
-
}
|
100 |
-
|
101 |
-
@keyframes slideUp {
|
102 |
-
from {transform: translateY(40px); opacity: 0;}
|
103 |
-
to {transform: translateY(0); opacity: 1;}
|
104 |
-
}
|
105 |
-
|
106 |
-
@keyframes zoomIn {
|
107 |
-
from {transform: scale(0.5); opacity: 0;}
|
108 |
-
to {transform: scale(1); opacity: 1;}
|
109 |
-
}
|
110 |
-
|
111 |
-
body {
|
112 |
-
background: linear-gradient(to right, #0f0f0f, #1a1a1a, #000000);
|
113 |
-
color: #eeeeee;
|
114 |
-
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
115 |
-
}
|
116 |
-
|
117 |
-
.gradio-container {
|
118 |
-
animation: fadeIn 1.5s ease-in-out;
|
119 |
-
}
|
120 |
-
|
121 |
-
.gradio-container .block-label {
|
122 |
-
color: #ffffff;
|
123 |
-
}
|
124 |
-
"""
|
125 |
|
126 |
iface = gr.Interface(
|
127 |
fn=drug_discovery,
|
128 |
-
inputs=[
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
134 |
)
|
135 |
|
136 |
-
iface.launch(share=True)
|
|
|
6 |
import base64
|
7 |
import re
|
8 |
import py3Dmol
|
9 |
+
import time
|
10 |
|
11 |
+
# Load model once
|
12 |
+
bio_gpt = pipeline("text-generation", model="microsoft/BioGPT-Large")
|
13 |
|
14 |
def drug_discovery(disease, symptoms):
|
15 |
+
# Simplified and efficient medical prompt
|
|
|
|
|
16 |
prompt = (
|
17 |
+
f"You're a biomedical AI. A new disease shows symptoms: '{symptoms}'. "
|
18 |
+
f"Suggest 5 generic drug names and 5 SMILES strings that could help treat this. "
|
19 |
+
f"List drug names first, then SMILES strings in separate lines like:\n"
|
20 |
+
f"Drugs: Aspirin, Ibuprofen, Paracetamol, ...\n"
|
21 |
+
f"SMILES: C1=CC=CC=C1 C(C(=O)O)N ..."
|
22 |
)
|
23 |
|
24 |
try:
|
25 |
+
start = time.time()
|
26 |
+
result = bio_gpt(prompt, max_length=150, do_sample=True, temperature=0.6)[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
except Exception as e:
|
28 |
+
return f"BioGPT error: {e}", "", "", ""
|
29 |
+
|
30 |
+
# Extract drug names and SMILES
|
31 |
+
drugs_match = re.search(r"Drugs:\s*(.+)", result)
|
32 |
+
smiles_match = re.search(r"SMILES:\s*(.+)", result)
|
33 |
+
|
34 |
+
drug_names = drugs_match.group(1).strip() if drugs_match else "Unknown"
|
35 |
+
raw_smiles = smiles_match.group(1).strip() if smiles_match else "C1=CC=CC=C1"
|
36 |
+
|
37 |
+
smiles_list = re.findall(r"(?<![A-Za-z0-9])[A-Za-z0-9@+\-\[\]\(\)=#$]{5,}(?![A-Za-z0-9])", raw_smiles)
|
38 |
+
smiles_list = list({sm for sm in smiles_list if Chem.MolFromSmiles(sm)})[:3]
|
39 |
+
|
40 |
+
if not smiles_list:
|
41 |
+
smiles_list = ["C1=CC=CC=C1"]
|
42 |
+
|
43 |
+
img_html, viewer_htmls = "", ""
|
44 |
+
for smiles in smiles_list:
|
45 |
+
mol = Chem.MolFromSmiles(smiles)
|
46 |
+
AllChem.Compute2DCoords(mol)
|
47 |
+
drawer = rdMolDraw2D.MolDraw2DCairo(250, 250)
|
48 |
+
drawer.DrawMolecule(mol)
|
49 |
+
drawer.FinishDrawing()
|
50 |
+
img_data = drawer.GetDrawingText()
|
51 |
+
img_base64 = base64.b64encode(img_data).decode("utf-8")
|
52 |
+
img_html += f'''
|
53 |
+
<div style="display:inline-block; margin:10px;">
|
54 |
+
<img src="data:image/png;base64,{img_base64}" width="120" height="120">
|
55 |
+
<p style="color:white; font-size:12px;">{smiles}</p>
|
56 |
+
</div>'''
|
57 |
+
|
58 |
+
# 3D View
|
59 |
+
mol3d = Chem.AddHs(mol)
|
60 |
+
AllChem.EmbedMolecule(mol3d, randomSeed=42)
|
61 |
+
AllChem.UFFOptimizeMolecule(mol3d)
|
62 |
+
mb = Chem.MolToMolBlock(mol3d)
|
63 |
+
viewer = py3Dmol.view(width=240, height=240)
|
64 |
+
viewer.addModel(mb, "mol")
|
65 |
+
viewer.setStyle({"stick": {"colorscheme": "cyanCarbon"}})
|
66 |
+
viewer.setBackgroundColor("black")
|
67 |
+
viewer.zoomTo()
|
68 |
+
viewer_html_raw = viewer._make_html()
|
69 |
+
viewer_htmls += f'''
|
70 |
+
<div style="display:inline-block; margin:10px;">
|
71 |
+
<iframe srcdoc="{viewer_html_raw.replace('"', '"')}" width="240" height="240" frameborder="0"></iframe>
|
72 |
+
</div>'''
|
73 |
+
|
74 |
+
duration = round(time.time() - start, 2)
|
75 |
+
literature_summary = f"π Drug candidates (auto-generated in {duration}s):\n{drug_names}"
|
76 |
+
|
77 |
+
return literature_summary, ", ".join(smiles_list), img_html, viewer_htmls
|
78 |
+
|
79 |
+
# Gradio UI setup
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
|
81 |
iface = gr.Interface(
|
82 |
fn=drug_discovery,
|
83 |
+
inputs=[
|
84 |
+
gr.Textbox(label="𧬠Enter Unknown Disease or Name", value="X-disease"),
|
85 |
+
gr.Textbox(label="π Symptoms", value="fever, joint pain")
|
86 |
+
],
|
87 |
+
outputs=[
|
88 |
+
gr.Textbox(label="π AI Literature Summary"),
|
89 |
+
gr.Textbox(label="π§ͺ SMILES List"),
|
90 |
+
gr.HTML(label="πΌοΈ 2D Molecules"),
|
91 |
+
gr.HTML(label="π¬ 3D Molecules")
|
92 |
+
],
|
93 |
+
title="π§ͺ Drug Discovery for Unknown Diseases",
|
94 |
+
description="BioGPT + RDKit-powered system to suggest potential drug molecules for unknown or rare diseases.",
|
95 |
+
css="""
|
96 |
+
body { background-color: #111; color: #eee; }
|
97 |
+
.gradio-container { animation: fadeIn 1.5s ease-in-out; }
|
98 |
+
"""
|
99 |
)
|
100 |
|
101 |
+
iface.launch(share=True)
|