anaghanagesh commited on
Commit
ac09baf
Β·
verified Β·
1 Parent(s): ffb73c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -115
app.py CHANGED
@@ -6,131 +6,96 @@ 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('"', '&quot;')}"
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)
 
6
  import base64
7
  import re
8
  import py3Dmol
9
+ import time
10
 
11
+ # Load model once
12
+ bio_gpt = pipeline("text-generation", model="microsoft/BioGPT-Large")
13
 
14
  def drug_discovery(disease, symptoms):
15
+ # Simplified and efficient medical prompt
 
 
16
  prompt = (
17
+ f"You're a biomedical AI. A new disease shows symptoms: '{symptoms}'. "
18
+ f"Suggest 5 generic drug names and 5 SMILES strings that could help treat this. "
19
+ f"List drug names first, then SMILES strings in separate lines like:\n"
20
+ f"Drugs: Aspirin, Ibuprofen, Paracetamol, ...\n"
21
+ f"SMILES: C1=CC=CC=C1 C(C(=O)O)N ..."
22
  )
23
 
24
  try:
25
+ start = time.time()
26
+ result = bio_gpt(prompt, max_length=150, do_sample=True, temperature=0.6)[0]['generated_text']
 
 
 
 
 
 
 
 
 
27
  except Exception as e:
28
+ return f"BioGPT error: {e}", "", "", ""
29
+
30
+ # Extract drug names and SMILES
31
+ drugs_match = re.search(r"Drugs:\s*(.+)", result)
32
+ smiles_match = re.search(r"SMILES:\s*(.+)", result)
33
+
34
+ drug_names = drugs_match.group(1).strip() if drugs_match else "Unknown"
35
+ raw_smiles = smiles_match.group(1).strip() if smiles_match else "C1=CC=CC=C1"
36
+
37
+ smiles_list = re.findall(r"(?<![A-Za-z0-9])[A-Za-z0-9@+\-\[\]\(\)=#$]{5,}(?![A-Za-z0-9])", raw_smiles)
38
+ smiles_list = list({sm for sm in smiles_list if Chem.MolFromSmiles(sm)})[:3]
39
+
40
+ if not smiles_list:
41
+ smiles_list = ["C1=CC=CC=C1"]
42
+
43
+ img_html, viewer_htmls = "", ""
44
+ for smiles in smiles_list:
45
+ mol = Chem.MolFromSmiles(smiles)
46
+ AllChem.Compute2DCoords(mol)
47
+ drawer = rdMolDraw2D.MolDraw2DCairo(250, 250)
48
+ drawer.DrawMolecule(mol)
49
+ drawer.FinishDrawing()
50
+ img_data = drawer.GetDrawingText()
51
+ img_base64 = base64.b64encode(img_data).decode("utf-8")
52
+ img_html += f'''
53
+ <div style="display:inline-block; margin:10px;">
54
+ <img src="data:image/png;base64,{img_base64}" width="120" height="120">
55
+ <p style="color:white; font-size:12px;">{smiles}</p>
56
+ </div>'''
57
+
58
+ # 3D View
59
+ mol3d = Chem.AddHs(mol)
60
+ AllChem.EmbedMolecule(mol3d, randomSeed=42)
61
+ AllChem.UFFOptimizeMolecule(mol3d)
62
+ mb = Chem.MolToMolBlock(mol3d)
63
+ viewer = py3Dmol.view(width=240, height=240)
64
+ viewer.addModel(mb, "mol")
65
+ viewer.setStyle({"stick": {"colorscheme": "cyanCarbon"}})
66
+ viewer.setBackgroundColor("black")
67
+ viewer.zoomTo()
68
+ viewer_html_raw = viewer._make_html()
69
+ viewer_htmls += f'''
70
+ <div style="display:inline-block; margin:10px;">
71
+ <iframe srcdoc="{viewer_html_raw.replace('"', '&quot;')}" width="240" height="240" frameborder="0"></iframe>
72
+ </div>'''
73
+
74
+ duration = round(time.time() - start, 2)
75
+ literature_summary = f"πŸ“‹ Drug candidates (auto-generated in {duration}s):\n{drug_names}"
76
+
77
+ return literature_summary, ", ".join(smiles_list), img_html, viewer_htmls
78
+
79
+ # Gradio UI setup
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
  iface = gr.Interface(
82
  fn=drug_discovery,
83
+ inputs=[
84
+ gr.Textbox(label="🧬 Enter Unknown Disease or Name", value="X-disease"),
85
+ gr.Textbox(label="πŸ“ Symptoms", value="fever, joint pain")
86
+ ],
87
+ outputs=[
88
+ gr.Textbox(label="πŸ”– AI Literature Summary"),
89
+ gr.Textbox(label="πŸ§ͺ SMILES List"),
90
+ gr.HTML(label="πŸ–ΌοΈ 2D Molecules"),
91
+ gr.HTML(label="πŸ”¬ 3D Molecules")
92
+ ],
93
+ title="πŸ§ͺ Drug Discovery for Unknown Diseases",
94
+ description="BioGPT + RDKit-powered system to suggest potential drug molecules for unknown or rare diseases.",
95
+ css="""
96
+ body { background-color: #111; color: #eee; }
97
+ .gradio-container { animation: fadeIn 1.5s ease-in-out; }
98
+ """
99
  )
100
 
101
+ iface.launch(share=True)