Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import openai
|
3 |
+
import requests
|
4 |
+
from transformers import pipeline
|
5 |
+
from langchain.llms import OpenAI as LangOpenAI
|
6 |
+
from langchain.chains import LLMChain
|
7 |
+
from langchain.prompts import PromptTemplate
|
8 |
+
from rdkit import Chem
|
9 |
+
from rdkit.Chem import AllChem, Draw
|
10 |
+
from rdkit.Chem.Draw import rdMolDraw2D
|
11 |
+
import base64
|
12 |
+
from io import BytesIO
|
13 |
+
import py3Dmol
|
14 |
+
import re
|
15 |
+
|
16 |
+
# === Advanced LLM Configs ===
|
17 |
+
openai.api_key = "your-openai-api-key"
|
18 |
+
|
19 |
+
# BioGPT and OpenAI Coordinated Agents
|
20 |
+
def get_literature_insights(disease, symptoms):
|
21 |
+
bio_gpt = pipeline("text-generation", model="microsoft/BioGPT-Large")
|
22 |
+
prompt = f"Recent drug research for {disease} with symptoms: {symptoms}."
|
23 |
+
return bio_gpt(prompt, max_length=200)[0]['generated_text']
|
24 |
+
|
25 |
+
def get_openai_smiles(disease, symptoms):
|
26 |
+
prompt = f"Suggest 3 valid, drug-like SMILES strings that can potentially treat {disease} (symptoms: {symptoms}). Return only SMILES strings separated by space."
|
27 |
+
response = openai.Completion.create(
|
28 |
+
engine="text-davinci-003",
|
29 |
+
prompt=prompt,
|
30 |
+
max_tokens=100
|
31 |
+
)
|
32 |
+
return response.choices[0].text.strip()
|
33 |
+
|
34 |
+
# === Multi-agent Molecular Pipeline ===
|
35 |
+
def drug_discovery(disease, symptoms):
|
36 |
+
# Agent 1: Literature from BioGPT
|
37 |
+
literature = get_literature_insights(disease, symptoms)
|
38 |
+
|
39 |
+
# Agent 2: Molecule SMILES from OpenAI
|
40 |
+
smiles_result = get_openai_smiles(disease, symptoms)
|
41 |
+
smiles_matches = re.findall(r"(?<![A-Za-z0-9])[A-Za-z0-9@+\-\[\]\(\)=#$]{5,}(?![A-Za-z0-9])", smiles_result)
|
42 |
+
|
43 |
+
smiles = None
|
44 |
+
for match in smiles_matches:
|
45 |
+
if Chem.MolFromSmiles(match):
|
46 |
+
smiles = match
|
47 |
+
break
|
48 |
+
if not smiles:
|
49 |
+
smiles = "C1=CC=CC=C1" # fallback
|
50 |
+
|
51 |
+
# RDKit 2D Drawing
|
52 |
+
mol = Chem.MolFromSmiles(smiles)
|
53 |
+
AllChem.Compute2DCoords(mol)
|
54 |
+
drawer = rdMolDraw2D.MolDraw2DCairo(300, 300)
|
55 |
+
drawer.DrawMolecule(mol)
|
56 |
+
drawer.FinishDrawing()
|
57 |
+
img_data = drawer.GetDrawingText()
|
58 |
+
img_base64 = base64.b64encode(img_data).decode("utf-8")
|
59 |
+
img_html = f'''<div style="text-align:center; margin-top: 10px; animation: fadeIn 2s ease-in-out;">
|
60 |
+
<img src="data:image/png;base64,{img_base64}" alt="2D Molecule"
|
61 |
+
style="border-radius: 16px; box-shadow: 0 6px 20px rgba(0,255,255,0.3);">
|
62 |
+
<div style='color: #eee; margin-top: 8px;'>π 2D Drug Structure</div></div>'''
|
63 |
+
|
64 |
+
# RDKit 3D Molecule + py3Dmol
|
65 |
+
mol3d = Chem.AddHs(mol)
|
66 |
+
AllChem.EmbedMolecule(mol3d)
|
67 |
+
AllChem.UFFOptimizeMolecule(mol3d)
|
68 |
+
molblock = Chem.MolToMolBlock(mol3d)
|
69 |
+
viewer = py3Dmol.view(width=420, height=420)
|
70 |
+
viewer.addModel(molblock, "mol")
|
71 |
+
viewer.setStyle({"stick": {"colorscheme": "cyanCarbon"}})
|
72 |
+
viewer.setBackgroundColor("black")
|
73 |
+
viewer.zoomTo()
|
74 |
+
viewer.spin(True)
|
75 |
+
viewer_html_raw = viewer._make_html()
|
76 |
+
viewer_html = f'''<div style="text-align:center; margin-top: 20px; animation: zoomIn 2s ease-in-out;">
|
77 |
+
<iframe srcdoc="{viewer_html_raw.replace('"', '"')}" width="440" height="440" frameborder="0"></iframe>
|
78 |
+
<div style='color: #eee; margin-top: 8px;'>𧬠3D Molecule</div></div>'''
|
79 |
+
|
80 |
+
return literature, smiles, img_html, viewer_html
|
81 |
+
|
82 |
+
# === Gradio UI ===
|
83 |
+
disease_input = gr.Textbox(label="𧬠Disease (e.g., glioblastoma)", value="glioblastoma")
|
84 |
+
symptom_input = gr.Textbox(label="π©Έ Symptoms (e.g., seizures, nausea)", value="seizures, nausea")
|
85 |
+
lit_output = gr.Textbox(label="π Literature from BioGPT")
|
86 |
+
smiles_output = gr.Textbox(label="π§ͺ SMILES Representation")
|
87 |
+
img_output = gr.HTML(label="π¬ 2D Structure")
|
88 |
+
viewer_output = gr.HTML(label="𧬠3D Molecule")
|
89 |
+
|
90 |
+
custom_css = """
|
91 |
+
@keyframes fadeIn {
|
92 |
+
from {opacity: 0;}
|
93 |
+
to {opacity: 1;}
|
94 |
+
}
|
95 |
+
@keyframes zoomIn {
|
96 |
+
from {transform: scale(0.5); opacity: 0;}
|
97 |
+
to {transform: scale(1); opacity: 1;}
|
98 |
+
}
|
99 |
+
body {
|
100 |
+
background: linear-gradient(to right, #141e30, #243b55);
|
101 |
+
color: #ffffff;
|
102 |
+
font-family: 'Segoe UI', sans-serif;
|
103 |
+
}
|
104 |
+
.gradio-container {
|
105 |
+
animation: fadeIn 2s ease-in-out;
|
106 |
+
}
|
107 |
+
"""
|
108 |
+
|
109 |
+
iface = gr.Interface(
|
110 |
+
fn=drug_discovery,
|
111 |
+
inputs=[disease_input, symptom_input],
|
112 |
+
outputs=[lit_output, smiles_output, img_output, viewer_output],
|
113 |
+
title="π AI-Powered Drug Discovery System (CS3235 Project)",
|
114 |
+
description="This real-time LLM-based platform suggests drugs for diseases without known treatments, generates 2D/3D molecules, and provides literature justifications using BioGPT + OpenAI + RDKit.",
|
115 |
+
css=custom_css
|
116 |
+
)
|
117 |
+
|
118 |
+
iface.launch(share=True)
|
119 |
+
|