anaghanagesh commited on
Commit
e1228a0
·
verified ·
1 Parent(s): 4717bae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -46
app.py CHANGED
@@ -1,4 +1,4 @@
1
- import streamlit as st
2
  from transformers import pipeline
3
  from rdkit import Chem
4
  from rdkit.Chem import AllChem
@@ -8,59 +8,60 @@ 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.")
 
1
+ import gradio as gr
2
  from transformers import pipeline
3
  from rdkit import Chem
4
  from rdkit.Chem import AllChem
 
8
  from io import BytesIO
9
  import py3Dmol
10
 
11
+ # Function to generate literature and 3D molecule view
12
+ def drug_discovery(disease, symptoms):
13
+ # BioGPT pipeline
14
+ bio_gpt = pipeline("text-generation", model="microsoft/BioGPT-Large")
15
+ prompt = f"Recent treatments for {disease} with symptoms: {symptoms}."
16
+ literature = bio_gpt(prompt, max_length=200)[0]['generated_text']
17
 
18
+ # Generate SMILES (placeholder)
19
+ smiles = "CC(C)CC" # Example: isopentane
20
 
21
+ # Generate RDKit molecule
22
+ mol = Chem.MolFromSmiles(smiles)
23
+ AllChem.Compute2DCoords(mol)
 
 
24
 
25
+ # Draw 2D image
26
+ drawer = rdMolDraw2D.MolDraw2DCairo(300, 300)
27
+ drawer.DrawMolecule(mol)
28
+ drawer.FinishDrawing()
29
+ img_data = drawer.GetDrawingText()
 
30
 
31
+ # Convert binary to base64
32
+ img_base64 = base64.b64encode(img_data).decode("utf-8")
33
+ img_html = f'<img src="data:image/png;base64,{img_base64}" alt="2D Molecule">'
34
 
35
+ # 3D molecule
36
+ mol3d = Chem.AddHs(mol)
37
+ AllChem.EmbedMolecule(mol3d)
38
+ AllChem.UFFOptimizeMolecule(mol3d)
39
+ mb = Chem.MolToMolBlock(mol3d)
40
 
41
+ viewer = py3Dmol.view(width=400, height=400)
42
+ viewer.addModel(mb, "mol")
43
+ viewer.setStyle({"stick": {}})
44
+ viewer.setBackgroundColor("white")
45
+ viewer.zoomTo()
46
+ viewer_html = viewer._make_html()
 
 
 
47
 
48
+ return literature, smiles, img_html, viewer_html
 
 
 
49
 
50
+ # Gradio UI
51
+ disease_input = gr.Textbox(label="Enter Disease", value="lung cancer")
52
+ symptom_input = gr.Textbox(label="Enter Symptoms", value="shortness of breath, weight loss")
53
+ lit_output = gr.Textbox(label="Literature Insights")
54
+ smiles_output = gr.Textbox(label="SMILES String")
55
+ img_output = gr.HTML(label="2D Molecule")
56
+ viewer_output = gr.HTML(label="3D Molecule Viewer")
57
 
58
+ iface = gr.Interface(
59
+ fn=drug_discovery,
60
+ inputs=[disease_input, symptom_input],
61
+ outputs=[lit_output, smiles_output, img_output, viewer_output],
62
+ title="🧬 Drug Discovery using LLMs",
63
+ description="Enter a disease and symptoms to generate literature insights and view the molecule in 2D and interactive 3D."
64
+ )
 
65
 
66
+ iface.launch()
67