Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,136 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from rdkit import Chem
|
4 |
+
from rdkit.Chem import AllChem
|
5 |
+
from rdkit.Chem.Draw import rdMolDraw2D
|
6 |
+
import base64
|
7 |
+
import re
|
8 |
+
import py3Dmol
|
9 |
+
|
10 |
+
# Drug discovery function
|
11 |
+
|
12 |
+
def drug_discovery(disease, symptoms):
|
13 |
+
bio_gpt = pipeline("text-generation", model="microsoft/BioGPT-Large")
|
14 |
+
|
15 |
+
# Detailed medical prompt
|
16 |
+
prompt = (
|
17 |
+
f"Act as a biomedical researcher. For the disease '{disease}' with symptoms '{symptoms}', provide a detailed summary of:\n"
|
18 |
+
"- Causes\n- Diagnosis methods\n- Treatment options\n- Common medications (include drug names)\n"
|
19 |
+
"- Any FDA-approved therapies or hospital protocols\n\nKeep it concise but detailed."
|
20 |
+
)
|
21 |
+
|
22 |
+
try:
|
23 |
+
result = bio_gpt(prompt, max_length=512, do_sample=True, temperature=0.7)[0]['generated_text']
|
24 |
+
except Exception as e:
|
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 |
+
smiles_result = "C1=CC=CC=C1"
|
36 |
+
|
37 |
+
smiles_matches = re.findall(r"(?<![A-Za-z0-9])[A-Za-z0-9@+\-\[\]\(\)=#$]{5,}(?![A-Za-z0-9])", smiles_result)
|
38 |
+
smiles = None
|
39 |
+
for match in smiles_matches:
|
40 |
+
mol_test = Chem.MolFromSmiles(match)
|
41 |
+
if mol_test:
|
42 |
+
smiles = match
|
43 |
+
break
|
44 |
+
if not smiles:
|
45 |
+
smiles = "C1=CC=CC=C1"
|
46 |
+
|
47 |
+
mol = Chem.MolFromSmiles(smiles)
|
48 |
+
if not mol:
|
49 |
+
return "Invalid SMILES generated", smiles, "", ""
|
50 |
+
|
51 |
+
AllChem.Compute2DCoords(mol)
|
52 |
+
drawer = rdMolDraw2D.MolDraw2DCairo(300, 300)
|
53 |
+
drawer.DrawMolecule(mol)
|
54 |
+
drawer.FinishDrawing()
|
55 |
+
img_data = drawer.GetDrawingText()
|
56 |
+
img_base64 = base64.b64encode(img_data).decode("utf-8")
|
57 |
+
img_html = f'''<div style="text-align:center; margin-top: 10px; animation: fadeIn 2s ease-in-out;">
|
58 |
+
<img src="data:image/png;base64,{img_base64}" alt="2D Molecule"
|
59 |
+
style="border-radius: 16px; box-shadow: 0 6px 20px rgba(0,255,255,0.3); border: 1px solid #444;">
|
60 |
+
<div style='font-family: Arial, sans-serif; color: #eeeeee; margin-top: 8px; animation: slideUp 1.5s ease-in-out;'>π Visualized Drug Molecule (2D)</div>
|
61 |
+
</div>'''
|
62 |
+
|
63 |
+
mol3d = Chem.AddHs(mol)
|
64 |
+
AllChem.EmbedMolecule(mol3d)
|
65 |
+
AllChem.UFFOptimizeMolecule(mol3d)
|
66 |
+
mb = Chem.MolToMolBlock(mol3d)
|
67 |
+
|
68 |
+
viewer = py3Dmol.view(width=420, height=420)
|
69 |
+
viewer.addModel(mb, "mol")
|
70 |
+
viewer.setStyle({"stick": {"colorscheme": "cyanCarbon"}})
|
71 |
+
viewer.setBackgroundColor("black")
|
72 |
+
viewer.zoomTo()
|
73 |
+
viewer.spin(True)
|
74 |
+
viewer_html_raw = viewer._make_html()
|
75 |
+
|
76 |
+
viewer_html = f'''
|
77 |
+
<div style="text-align:center; margin-top: 20px; animation: zoomIn 2s ease-in-out;">
|
78 |
+
<iframe srcdoc="{viewer_html_raw.replace('"', '"')}"
|
79 |
+
width="440" height="440" frameborder="0"
|
80 |
+
style="border-radius: 16px; box-shadow: 0 8px 30px rgba(0,255,255,0.35);"></iframe>
|
81 |
+
<div style='font-family: Arial, sans-serif; color: #eeeeee; margin-top: 8px; animation: slideUp 1.5s ease-in-out;'>𧬠Animated 3D Molecule (Stick View)</div>
|
82 |
+
</div>'''
|
83 |
+
|
84 |
+
return result.strip(), smiles, img_html, viewer_html
|
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=[disease_input, symptom_input],
|
129 |
+
outputs=[lit_output, smiles_output, img_output, viewer_output],
|
130 |
+
title="π₯ AI-Powered Drug Discovery for Hospitals",
|
131 |
+
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.",
|
132 |
+
theme="default",
|
133 |
+
css=custom_css
|
134 |
+
)
|
135 |
+
|
136 |
+
iface.launch(share=True)
|