Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import random
|
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 |
+
import py3Dmol
|
10 |
+
|
11 |
+
# Load NLP & ML models
|
12 |
+
bio_gpt = pipeline("text-generation", model="microsoft/BioGPT-Large")
|
13 |
+
chemberta_tokenizer = AutoTokenizer.from_pretrained("seyonec/ChemBERTa-zinc-base-v1")
|
14 |
+
chemberta_model = AutoModelForCausalLM.from_pretrained("seyonec/ChemBERTa-zinc-base-v1")
|
15 |
+
compliance_qa = pipeline("question-answering", model="nlpaueb/legal-bert-base-uncased")
|
16 |
+
|
17 |
+
def extract_insights(disease, symptoms):
|
18 |
+
prompt = f"Recent treatments for {disease} with symptoms: {symptoms}"
|
19 |
+
try:
|
20 |
+
result = bio_gpt(prompt, max_length=200, do_sample=True)
|
21 |
+
return result[0]['generated_text']
|
22 |
+
except Exception as e:
|
23 |
+
return f"Error extracting insights: {str(e)}"
|
24 |
+
|
25 |
+
def generate_molecule():
|
26 |
+
sample_smiles = ["CCO", "CCN", "C1=CC=CC=C1", "C(C(=O)O)N", "CC(C)CC"]
|
27 |
+
return random.choice(sample_smiles)
|
28 |
+
|
29 |
+
def predict_properties(smiles):
|
30 |
+
try:
|
31 |
+
inputs = chemberta_tokenizer(smiles, return_tensors="pt")
|
32 |
+
with torch.no_grad():
|
33 |
+
outputs = chemberta_model(**inputs)
|
34 |
+
return round(outputs.logits.mean().item(), 3)
|
35 |
+
except Exception as e:
|
36 |
+
return f"Error predicting properties: {str(e)}"
|
37 |
+
|
38 |
+
def visualize_molecule(smiles):
|
39 |
+
try:
|
40 |
+
mol = Chem.MolFromSmiles(smiles)
|
41 |
+
img = Draw.MolToImage(mol, size=(300, 300))
|
42 |
+
buf = BytesIO()
|
43 |
+
img.save(buf, format="PNG")
|
44 |
+
return buf.getvalue()
|
45 |
+
except Exception as e:
|
46 |
+
return f"Error visualizing molecule: {str(e)}"
|
47 |
+
|
48 |
+
def generate_3d_structure(smiles):
|
49 |
+
try:
|
50 |
+
mol = Chem.MolFromSmiles(smiles)
|
51 |
+
mol = Chem.AddHs(mol)
|
52 |
+
AllChem.EmbedMolecule(mol, AllChem.ETKDG())
|
53 |
+
AllChem.UFFOptimizeMolecule(mol)
|
54 |
+
mol_block = Chem.MolToMolBlock(mol)
|
55 |
+
viewer = py3Dmol.view(width=400, height=400)
|
56 |
+
viewer.addModel(mol_block, "mol")
|
57 |
+
viewer.setStyle({"stick": {}})
|
58 |
+
viewer.zoomTo()
|
59 |
+
return viewer._make_html()
|
60 |
+
except Exception as e:
|
61 |
+
return f"Error generating 3D molecule: {str(e)}"
|
62 |
+
|
63 |
+
def check_compliance():
|
64 |
+
try:
|
65 |
+
question = "What does FDA require for drug testing?"
|
66 |
+
context = "FDA requires extensive testing for new drug candidates including Phase I, II, and III clinical trials."
|
67 |
+
return compliance_qa(question=question, context=context)['answer']
|
68 |
+
except Exception as e:
|
69 |
+
return f"Error checking compliance: {str(e)}"
|
70 |
+
|
71 |
+
def full_pipeline(disease, symptoms):
|
72 |
+
insights = extract_insights(disease, symptoms)
|
73 |
+
smiles = generate_molecule()
|
74 |
+
mol_img = visualize_molecule(smiles)
|
75 |
+
score = predict_properties(smiles)
|
76 |
+
mol_3d_html = generate_3d_structure(smiles)
|
77 |
+
compliance = check_compliance()
|
78 |
+
return insights, mol_img, f"{smiles} | Score: {score}", mol_3d_html, compliance
|
79 |
+
|
80 |
+
demo = gr.Interface(
|
81 |
+
fn=full_pipeline,
|
82 |
+
inputs=[
|
83 |
+
gr.Textbox(label="Disease", value="lung cancer"),
|
84 |
+
gr.Textbox(label="Symptoms", value="shortness of breath, weight loss")
|
85 |
+
],
|
86 |
+
outputs=[
|
87 |
+
gr.Textbox(label="Literature Insights"),
|
88 |
+
gr.Image(label="2D Molecule"),
|
89 |
+
gr.Textbox(label="Molecule Info"),
|
90 |
+
gr.HTML(label="3D Molecule Structure"),
|
91 |
+
gr.Textbox(label="Compliance Info")
|
92 |
+
],
|
93 |
+
title="🧬 AI-Driven Drug Discovery",
|
94 |
+
description="Enter a disease and symptoms to generate and analyze potential drug candidates using AI."
|
95 |
+
)
|
96 |
+
|
97 |
+
if __name__ == "__main__":
|
98 |
+
demo.launch()
|