anaghanagesh commited on
Commit
4717bae
Β·
verified Β·
1 Parent(s): 65e3e3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -70
app.py CHANGED
@@ -1,86 +1,66 @@
1
- import gradio as gr
2
  from transformers import pipeline
3
  from rdkit import Chem
4
- from rdkit.Chem import AllChem, Draw
5
- from rdkit.Chem import Descriptors
 
6
  import base64
7
  from io import BytesIO
 
8
 
9
- # Load models
10
- bio_gpt = pipeline("text-generation", model="microsoft/BioGPT-Large")
11
- chemberta = pipeline("text-classification", model="seyonec/ChemBERTa-zinc-base-chemprop")
12
- fda_gpt = pipeline("text-generation", model="EleutherAI/gpt-neo-1.3B")
13
 
14
- def mol_to_svg(smiles):
15
- mol = Chem.MolFromSmiles(smiles)
16
- mol = Chem.AddHs(mol)
17
- AllChem.EmbedMolecule(mol, AllChem.ETKDG())
18
- AllChem.UFFOptimizeMolecule(mol)
19
- drawer = Draw.MolDraw2DSVG(300, 300)
20
- drawer.DrawMolecule(mol)
21
- drawer.FinishDrawing()
22
- return drawer.GetDrawingText()
23
 
24
- def mol_to_3d_html(smiles):
25
- try:
26
- mol = Chem.MolFromSmiles(smiles)
27
- mol = Chem.AddHs(mol)
28
- AllChem.EmbedMolecule(mol, AllChem.ETKDG())
29
- AllChem.UFFOptimizeMolecule(mol)
30
- mol_block = Chem.MolToMolBlock(mol)
31
- mol_block = mol_block.replace("\n", "\\n")
32
 
33
- html = f"""
34
- <div id=\"viewer\" style=\"width: 400px; height: 400px; position: relative;\"></div>
35
- <script src=\"https://3Dmol.org/build/3Dmol-min.js\"></script>
36
- <script>
37
- let viewer = $3Dmol.createViewer("viewer", {{ backgroundColor: "white" }});
38
- let molBlock = `{mol_block}`;
39
- viewer.addModel(molBlock, "mol");
40
- viewer.setStyle({{}}, {{stick:{{}}}});
41
- viewer.zoomTo();
42
- viewer.animate({{loop: "backAndForth"}});
43
- viewer.render();
44
- </script>
45
- """
46
- return html
47
- except Exception as e:
48
- return f"<p>Error rendering 3D molecule: {str(e)}</p>"
49
 
50
- def drug_discovery(disease, symptoms):
51
- prompt = f"Disease: {disease}\nSymptoms: {symptoms}\nLiterature Review:"
52
- literature = bio_gpt(prompt, max_length=250)[0]['generated_text']
53
 
54
- smiles = "CC(C)CC" # Just an example SMILES string, normally generated by a molecule generator
55
- property_score = chemberta(smiles)[0]['score']
56
- svg = mol_to_svg(smiles)
57
- html_3d = mol_to_3d_html(smiles)
58
 
59
- fda_prompt = f"A drug with SMILES {smiles} is under review. List FDA regulatory issues."
60
- fda_output = fda_gpt(fda_prompt, max_length=100)[0]['generated_text']
 
 
 
 
 
 
 
61
 
62
- return literature, smiles, f"ChemBERTa Property Score: {property_score:.3f}", svg, html_3d, fda_output
 
 
 
63
 
64
- inputs = [
65
- gr.Textbox(label="🦠 Disease", placeholder="e.g. lung cancer"),
66
- gr.Textbox(label="🧾 Symptoms", placeholder="e.g. shortness of breath, weight loss")
67
- ]
68
 
69
- outputs = [
70
- gr.Textbox(label="πŸ“œ Literature Insights"),
71
- gr.Textbox(label="πŸ§ͺ SMILES String"),
72
- gr.Textbox(label="🧬 Property Score"),
73
- gr.HTML(label="🧫 2D Molecule Structure"),
74
- gr.HTML(label="πŸ”¬ 3D Molecule Viewer"),
75
- gr.Textbox(label="βš–οΈ FDA Compliance Summary")
76
- ]
77
 
78
- demo = gr.Interface(
79
- fn=drug_discovery,
80
- inputs=inputs,
81
- outputs=outputs,
82
- title="πŸ’Š AI-Driven Drug Discovery using LLMs",
83
- description="Enter a disease and its symptoms. This app generates literature insights, a possible molecule in SMILES format, scores its drug-likeness, and shows 2D & 3D views."
84
- )
85
 
86
- demo.launch()
 
 
1
+ import streamlit as st
2
  from transformers import pipeline
3
  from rdkit import Chem
4
+ from rdkit.Chem import AllChem
5
+ from rdkit.Chem.Draw import rdMolDraw2D
6
+ from rdkit.Chem import rdDepictor
7
  import base64
8
  from io import BytesIO
9
+ import py3Dmol
10
 
11
+ st.set_page_config(page_title="Drug Discovery using LLMs", layout="wide")
 
 
 
12
 
13
+ st.title("🧬 Drug Discovery using LLMs")
 
 
 
 
 
 
 
 
14
 
15
+ with st.sidebar:
16
+ st.header("🦠 Disease")
17
+ disease = st.text_input("Enter disease", "lung cancer")
18
+ st.header("🧾 Symptoms")
19
+ symptoms = st.text_input("Enter symptoms", "shortness of breath, weight loss")
 
 
 
20
 
21
+ if st.button("Submit"):
22
+ with st.spinner("πŸ”Ž Discovering insights and potential drugs..."):
23
+ # BioGPT pipeline
24
+ bio_gpt = pipeline("text-generation", model="microsoft/BioGPT-Large")
25
+ prompt = f"Recent treatments for {disease} with symptoms: {symptoms}."
26
+ literature = bio_gpt(prompt, max_length=200)[0]['generated_text']
 
 
 
 
 
 
 
 
 
 
27
 
28
+ st.subheader("πŸ“š Literature Insights")
29
+ st.write(literature)
 
30
 
31
+ # Generate a dummy SMILES for demo (could replace with MoLeR/Diffusion)
32
+ st.subheader("πŸ§ͺ SMILES String")
33
+ smiles = "CC(C)CC" # Isopentane (dummy drug structure)
34
+ st.write(f"SMILES: {smiles}")
35
 
36
+ # Draw 2D molecule (optional, not shown if 3D works)
37
+ mol = Chem.MolFromSmiles(smiles)
38
+ AllChem.Compute2DCoords(mol)
39
+ rdDepictor.Compute2DCoords(mol)
40
+ drawer = rdMolDraw2D.MolDraw2DCairo(300, 300)
41
+ drawer.DrawMolecule(mol)
42
+ drawer.FinishDrawing()
43
+ img_data = drawer.GetDrawingText()
44
+ st.image(img_data, caption="2D Structure", use_column_width=False)
45
 
46
+ # Generate 3D coordinates
47
+ mol3d = Chem.AddHs(mol)
48
+ AllChem.EmbedMolecule(mol3d)
49
+ AllChem.UFFOptimizeMolecule(mol3d)
50
 
51
+ # Extract 3D coordinates for py3Dmol
52
+ mb = Chem.MolToMolBlock(mol3d)
 
 
53
 
54
+ st.subheader("πŸ”¬ 3D Structure Viewer")
55
+ xyzview = py3Dmol.view(width=400, height=400)
56
+ xyzview.addModel(mb, "mol")
57
+ xyzview.setStyle({"stick": {}})
58
+ xyzview.setBackgroundColor("white")
59
+ xyzview.zoomTo()
60
+ xyzview.show()
61
+ st.components.v1.html(xyzview._make_html(), height=400)
62
 
63
+ st.success("βœ… Done")
 
 
 
 
 
 
64
 
65
+ else:
66
+ st.info("πŸ‘ˆ Enter disease and symptoms, then hit Submit to discover a molecule.")