smitathkr1 commited on
Commit
97034ae
·
verified ·
1 Parent(s): 0b1ad4b

Upload requirements.txt with huggingface_hub

Browse files
Files changed (1) hide show
  1. requirements.txt +158 -5
requirements.txt CHANGED
@@ -1,5 +1,158 @@
1
- # RDKit dependencies
2
- rdkit
3
- gradio==4.44.1
4
- huggingface_hub==0.19.4
5
- cirpy
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from rdkit import Chem
3
+ from rdkit.Chem import Descriptors, Draw, AllChem
4
+ import cirpy
5
+ import py3Dmol
6
+
7
+
8
+ def _mol_from_smiles(smiles: str):
9
+ mol = Chem.MolFromSmiles(smiles)
10
+ if mol is None:
11
+ raise gr.Error("Invalid SMILES string.")
12
+ return mol
13
+
14
+
15
+ def smiles_to_canonical(smiles: str) -> str:
16
+ mol = _mol_from_smiles(smiles)
17
+ return Chem.MolToSmiles(mol)
18
+
19
+
20
+ def molecular_weight(smiles: str) -> float:
21
+ mol = _mol_from_smiles(smiles)
22
+ return float(Descriptors.MolWt(mol))
23
+
24
+
25
+ def logp(smiles: str) -> float:
26
+ mol = _mol_from_smiles(smiles)
27
+ return float(Descriptors.MolLogP(mol))
28
+
29
+
30
+ def tpsa(smiles: str) -> float:
31
+ mol = _mol_from_smiles(smiles)
32
+ return float(Descriptors.TPSA(mol))
33
+
34
+
35
+ def mol_image(smiles: str):
36
+ mol = _mol_from_smiles(smiles)
37
+ return Draw.MolToImage(mol)
38
+
39
+
40
+ def name_to_smiles(name: str) -> str:
41
+ """Convert chemical name to SMILES using Chemical Identifier Resolver (CIR)"""
42
+ try:
43
+ smiles = cirpy.resolve(name, 'smiles')
44
+ if smiles is None:
45
+ raise gr.Error(f"Could not find SMILES for chemical name: {name}")
46
+ return smiles
47
+ except Exception as e:
48
+ raise gr.Error(f"Error converting name to SMILES: {str(e)}")
49
+
50
+
51
+ def name_to_3d_molecule(name: str) -> str:
52
+ """Convert chemical name to 3D molecule visualization"""
53
+ try:
54
+ # Convert name to SMILES
55
+ smiles = cirpy.resolve(name, 'smiles')
56
+ if smiles is None:
57
+ raise gr.Error(f"Could not find SMILES for chemical name: {name}")
58
+
59
+ # Create molecule from SMILES
60
+ mol = Chem.MolFromSmiles(smiles)
61
+ if mol is None:
62
+ raise gr.Error(f"Could not create molecule from SMILES: {smiles}")
63
+
64
+ # Add hydrogens for better 3D structure
65
+ mol = Chem.AddHs(mol)
66
+
67
+ # Generate 3D coordinates
68
+ success = AllChem.EmbedMolecule(mol, AllChem.ETKDG())
69
+ if success == -1:
70
+ raise gr.Error(f"Could not generate 3D coordinates for: {name}")
71
+
72
+ # Optimize geometry
73
+ AllChem.MMFFOptimizeMolecule(mol)
74
+
75
+ # Convert to PDB format for py3Dmol
76
+ pdb_block = Chem.MolToPDBBlock(mol)
77
+
78
+ # Create 3D viewer
79
+ viewer = py3Dmol.view(width=400, height=400)
80
+ viewer.addModel(pdb_block, 'pdb')
81
+ viewer.setStyle({'stick': {}})
82
+ viewer.zoomTo()
83
+
84
+ # Return HTML representation
85
+ return viewer._repr_html_()
86
+
87
+ except Exception as e:
88
+ raise gr.Error(f"Error creating 3D molecule: {str(e)}")
89
+
90
+
91
+ smiles_interface = gr.Interface(
92
+ fn=smiles_to_canonical,
93
+ inputs=gr.Textbox(label="SMILES"),
94
+ outputs=gr.Textbox(label="Canonical SMILES"),
95
+ api_name="smiles_to_mol",
96
+ description="Convert an input SMILES string to its canonical form.",
97
+ )
98
+
99
+ name_interface = gr.Interface(
100
+ fn=name_to_smiles,
101
+ inputs=gr.Textbox(label="Chemical Name", placeholder="e.g., aspirin, caffeine, benzene"),
102
+ outputs=gr.Textbox(label="SMILES"),
103
+ api_name="name_to_smiles",
104
+ description="Convert a chemical name to SMILES notation.",
105
+ examples=[["aspirin"], ["caffeine"], ["benzene"], ["ethanol"]],
106
+ )
107
+
108
+ mw_interface = gr.Interface(
109
+ fn=molecular_weight,
110
+ inputs=gr.Textbox(label="SMILES"),
111
+ outputs=gr.Number(label="Molecular Weight (g/mol)"),
112
+ api_name="molecular_weight",
113
+ description="Compute the molecular weight from a SMILES string.",
114
+ )
115
+
116
+ logp_interface = gr.Interface(
117
+ fn=logp,
118
+ inputs=gr.Textbox(label="SMILES"),
119
+ outputs=gr.Number(label="logP"),
120
+ api_name="logp",
121
+ description="Calculate the octanol/water partition coefficient (logP).",
122
+ )
123
+
124
+ tpsa_interface = gr.Interface(
125
+ fn=tpsa,
126
+ inputs=gr.Textbox(label="SMILES"),
127
+ outputs=gr.Number(label="TPSA"),
128
+ api_name="tpsa",
129
+ description="Calculate the topological polar surface area (TPSA).",
130
+ )
131
+
132
+ molecule_3d_interface = gr.Interface(
133
+ fn=name_to_3d_molecule,
134
+ inputs=gr.Textbox(label="Chemical Name", placeholder="e.g., benzene, aspirin, caffeine"),
135
+ outputs=gr.HTML(label="3D Molecule Viewer"),
136
+ api_name="name_to_3d_molecule",
137
+ description="Convert a chemical name to an interactive 3D molecule visualization.",
138
+ examples=[["benzene"], ["aspirin"], ["caffeine"], ["ethanol"]],
139
+ )
140
+
141
+
142
+ demo = gr.TabbedInterface(
143
+ [name_interface, molecule_3d_interface, smiles_interface, mw_interface, logp_interface, tpsa_interface],
144
+ [
145
+ "Name to SMILES",
146
+ "3D Molecule Viewer",
147
+ "SMILES to Canonical",
148
+ "Molecular Weight",
149
+ "LogP",
150
+ "TPSA",
151
+ ],
152
+ title="RDKit API",
153
+ css=".gradio-container {max-width: 800px; margin: auto;}",
154
+ )
155
+
156
+
157
+ if __name__ == "__main__":
158
+ demo.queue().launch(server_name="0.0.0.0", server_port=7860)