cafierom commited on
Commit
385239a
·
verified ·
1 Parent(s): 2b31e2e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -2
app.py CHANGED
@@ -28,8 +28,6 @@ import pubchempy as pcp
28
  import gradio as gr
29
  from PIL import Image
30
 
31
- from chem_nodes import *
32
-
33
  device = "cuda" if torch.cuda.is_available() else "cpu"
34
 
35
  hf = HuggingFacePipeline.from_model_id(
@@ -39,6 +37,141 @@ hf = HuggingFacePipeline.from_model_id(
39
 
40
  chat_model = ChatHuggingFace(llm=hf)
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def first_node(state: State) -> State:
43
  '''
44
  The first node of the agent. This node receives the input and asks the LLM
 
28
  import gradio as gr
29
  from PIL import Image
30
 
 
 
31
  device = "cuda" if torch.cuda.is_available() else "cpu"
32
 
33
  hf = HuggingFacePipeline.from_model_id(
 
37
 
38
  chat_model = ChatHuggingFace(llm=hf)
39
 
40
+ class State(TypedDict):
41
+ '''
42
+ The state of the agent.
43
+ '''
44
+ messages: Annotated[list, add_messages]
45
+ query_smiles: str
46
+ query_task: str
47
+ query_name: str
48
+ query_reference: str
49
+ tool_choice: tuple
50
+ which_tool: int
51
+ props_string: str
52
+ similars_img: str
53
+ loop_again: str
54
+
55
+ def name_node(state: State) -> State:
56
+ '''
57
+ Queries Pubchem for the name of the molecule based on the smiles string.
58
+ Args:
59
+ smiles: the input smiles string
60
+ Returns:
61
+ name: the name of the molecule
62
+ props_string: a string of the tool results
63
+ '''
64
+ print("name tool")
65
+ print('===================================================')
66
+ current_props_string = state["props_string"]
67
+
68
+ try:
69
+ smiles = state["query_smiles"]
70
+ res = pcp.get_compounds(smiles, "smiles")
71
+ name = res[0].iupac_name
72
+ name_string = f'IUPAC molecule name: {name}\n'
73
+ #print(smiles, name)
74
+ syn_list = pcp.get_synonyms(res[0].cid)
75
+ for alt_name in syn_list[0]['Synonym'][:5]:
76
+ name_string += f'alternative or common name: {alt_name}\n'
77
+ except:
78
+ name = "unknown"
79
+ name_string = 'Could not find name for molecule'
80
+
81
+ state["query_name"] = name
82
+
83
+ current_props_string += name_string
84
+ state["props_string"] = current_props_string
85
+ state["which_tool"] += 1
86
+ return state
87
+
88
+ def smiles_node(state: State) -> State:
89
+ '''
90
+ Queries Pubchem for the smiles string of the molecule based on the name.
91
+ Args:
92
+ smiles: the molecule name
93
+ Returns:
94
+ smiles: the smiles string of the molecule
95
+ props_string: a string of the tool results
96
+ '''
97
+ print("smiles tool")
98
+ print('===================================================')
99
+ current_props_string = state["props_string"]
100
+
101
+ try:
102
+ name = state["query_name"]
103
+ res = pcp.get_compounds(name, "name")
104
+ smiles = res[0].smiles
105
+ smiles_string = f'molecule SMILES: {smiles}\n'
106
+ except:
107
+ smiles = "unknown"
108
+ smiles_string = 'Could not find smiles for molecule'
109
+
110
+ state["query_smiles"] = smiles
111
+
112
+ current_props_string += smiles_string
113
+ state["props_string"] = current_props_string
114
+ state["which_tool"] += 1
115
+ return state
116
+
117
+ def similars_node(state: State) -> State:
118
+ '''
119
+ Queries Pubchem for similar molecules based on the smiles string or name
120
+ Args:
121
+ smiles: the input smiles string, OR
122
+ name: the molecule name
123
+ Returns:
124
+ props_string: a string of the tool results.
125
+ '''
126
+ print("similars tool")
127
+ print('===================================================')
128
+ current_props_string = state["props_string"]
129
+
130
+ try:
131
+ if state['query_smiles'] != None:
132
+ smiles = state["query_smiles"]
133
+ res = pcp.get_compounds(smiles, "smiles", searchtype="similarity",listkey_count=20)
134
+ elif state['query_name'] != None:
135
+ name = state["query_name"]
136
+ res = pcp.get_compounds(name, "name", searchtype="similarity",listkey_count=20)
137
+ else:
138
+ print('Not enough information to run similars tool')
139
+ return state
140
+
141
+ props_string = 'Found Similar compounds: \n'
142
+ sub_smiles = []
143
+
144
+ i = 0
145
+ for compound in res:
146
+ if i == 0:
147
+ print(compound.iupac_name)
148
+ i+=1
149
+ sub_smiles.append(compound.smiles)
150
+ props_string += f'Name: {compound.iupac_name}\n'
151
+ props_string += f'SMILES: {compound.smiles}\n'
152
+ props_string += f'Molecular Weight: {compound.molecular_weight}\n'
153
+ props_string += f'LogP: {compound.xlogp}\n'
154
+ props_string += '==================='
155
+
156
+ sub_mols = [Chem.MolFromSmiles(smile) for smile in sub_smiles]
157
+ legend = [str(compound.smiles) for compound in res]
158
+
159
+ img = Draw.MolsToGridImage(sub_mols, legends=legend, molsPerRow=4, subImgSize=(250, 250))
160
+ pic = img.data
161
+
162
+ filename = "Similars_image"
163
+ with open(filename+".png",'wb+') as outf:
164
+ outf.write(pic)
165
+ except:
166
+ props_string = 'Could not find similar molecules'
167
+ filename = None
168
+
169
+ current_props_string += props_string
170
+ state["props_string"] = current_props_string
171
+ state['similars_img'] = filename
172
+ state["which_tool"] += 1
173
+ return state
174
+
175
  def first_node(state: State) -> State:
176
  '''
177
  The first node of the agent. This node receives the input and asks the LLM