vumichien commited on
Commit
e2badff
1 Parent(s): 4647a68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py CHANGED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import from_pretrained_keras
2
+ import gradio as gr
3
+ from rdkit import Chem, RDLogger
4
+ from rdkit.Chem.Draw import IPythonConsole, MolsToGridImage
5
+ import numpy as np
6
+ import tensorflow as tf
7
+ from tensorflow import keras
8
+
9
+ RDLogger.DisableLog("rdApp.*")
10
+
11
+ def graph_to_molecule(graph):
12
+ # Unpack graph
13
+ adjacency, features = graph
14
+
15
+ # RWMol is a molecule object intended to be edited
16
+ molecule = Chem.RWMol()
17
+
18
+ # Remove "no atoms" & atoms with no bonds
19
+ keep_idx = np.where(
20
+ (np.argmax(features, axis=1) != ATOM_DIM - 1)
21
+ & (np.sum(adjacency[:-1], axis=(0, 1)) != 0)
22
+ )[0]
23
+ features = features[keep_idx]
24
+ adjacency = adjacency[:, keep_idx, :][:, :, keep_idx]
25
+
26
+ # Add atoms to molecule
27
+ for atom_type_idx in np.argmax(features, axis=1):
28
+ atom = Chem.Atom(atom_mapping[atom_type_idx])
29
+ _ = molecule.AddAtom(atom)
30
+
31
+ # Add bonds between atoms in molecule; based on the upper triangles
32
+ # of the [symmetric] adjacency tensor
33
+ (bonds_ij, atoms_i, atoms_j) = np.where(np.triu(adjacency) == 1)
34
+ for (bond_ij, atom_i, atom_j) in zip(bonds_ij, atoms_i, atoms_j):
35
+ if atom_i == atom_j or bond_ij == BOND_DIM - 1:
36
+ continue
37
+ bond_type = bond_mapping[bond_ij]
38
+ molecule.AddBond(int(atom_i), int(atom_j), bond_type)
39
+
40
+ # Sanitize the molecule; for more information on sanitization, see
41
+ # https://www.rdkit.org/docs/RDKit_Book.html#molecular-sanitization
42
+ flag = Chem.SanitizeMol(molecule, catchErrors=True)
43
+ # Let's be strict. If sanitization fails, return None
44
+ if flag != Chem.SanitizeFlags.SANITIZE_NONE:
45
+ return None
46
+
47
+ return molecule
48
+
49
+ generator = from_pretrained_keras("keras-io/wgan-molecular-graphs")
50
+
51
+ def predict(num_mol):
52
+ samples = num_mol*2
53
+ z = tf.random.normal((samples, 64))
54
+ graph = generator.predict(z)
55
+ # obtain one-hot encoded adjacency tensor
56
+ adjacency = tf.argmax(graph[0], axis=1)
57
+ adjacency = tf.one_hot(adjacency, depth=BOND_DIM, axis=1)
58
+ # Remove potential self-loops from adjacency
59
+ adjacency = tf.linalg.set_diag(adjacency, tf.zeros(tf.shape(adjacency)[:-1]))
60
+ # obtain one-hot encoded feature tensor
61
+ features = tf.argmax(graph[1], axis=2)
62
+ features = tf.one_hot(features, depth=5, axis=2)
63
+ molecules = [
64
+ graph_to_molecule([adjacency[i].numpy(), features[i].numpy()])
65
+ for i in range(samples)
66
+ ]
67
+ MolsToGridImage(
68
+ [m for m in molecules if m is not None][:num_mol], molsPerRow=5, subImgSize=(150, 150), returnPNG=False,
69
+ ).save("img.png")
70
+ return 'img.png'
71
+
72
+ gr.Interface(
73
+ predict,
74
+ inputs=[
75
+ gr.inputs.Slider(5, 50, label='Number of Molecular Graphs', step=5, default=10),
76
+ ],
77
+ outputs="image",
78
+ ).launch(debug=True)