Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from typing import List
|
3 |
+
from utils import DbProcessor
|
4 |
+
import os
|
5 |
+
|
6 |
+
db_processor = DbProcessor()
|
7 |
+
|
8 |
+
def get_data(
|
9 |
+
smiles: str, iupac: str, name: str, inchl: str, inchl_key: str, return_fields: List[str]
|
10 |
+
):
|
11 |
+
smiles = smiles.strip()
|
12 |
+
iupac = iupac.strip()
|
13 |
+
name = name.strip()
|
14 |
+
inchl = inchl.strip()
|
15 |
+
inchl_key = inchl_key.strip()
|
16 |
+
|
17 |
+
inputs = {
|
18 |
+
"SMILES": smiles,
|
19 |
+
"IUPAC": iupac,
|
20 |
+
"NAME": name,
|
21 |
+
"InChI": inchl,
|
22 |
+
"InChIKey": inchl_key
|
23 |
+
}
|
24 |
+
|
25 |
+
count = 0
|
26 |
+
query_type = None
|
27 |
+
for k, v in inputs.items():
|
28 |
+
if v:
|
29 |
+
count += 1
|
30 |
+
query_type = k
|
31 |
+
|
32 |
+
if count == 0 or count > 1:
|
33 |
+
return [f"For search, you should specify only one field, but you specified {count} fields"] * 5
|
34 |
+
|
35 |
+
result_template = {
|
36 |
+
'SMILES': "",
|
37 |
+
'IUPAC': "",
|
38 |
+
'InChI': "",
|
39 |
+
'InChIKey': "",
|
40 |
+
'synonyms': ""
|
41 |
+
}
|
42 |
+
|
43 |
+
try:
|
44 |
+
result = db_processor.request_data(text_input=inputs[query_type], query_type=query_type, return_fields=return_fields)
|
45 |
+
for key, value in result.items():
|
46 |
+
if key in result_template:
|
47 |
+
result_template[key] = value
|
48 |
+
if "synonyms" in return_fields:
|
49 |
+
result_template["synonyms"] = str(result["synonyms"])[1:-1]
|
50 |
+
|
51 |
+
return [result_template['SMILES'], result_template['IUPAC'], result_template['synonyms'], result_template['InChI'], result_template['InChIKey']]
|
52 |
+
|
53 |
+
except Exception as e:
|
54 |
+
print(e)
|
55 |
+
return ["Compound not found"] * 5
|
56 |
+
|
57 |
+
main_interface = gr.Interface(
|
58 |
+
fn=get_data,
|
59 |
+
allow_flagging='never',
|
60 |
+
inputs=[
|
61 |
+
gr.Textbox(label="Search from SMILES", placeholder="Enter SMILES name here"),
|
62 |
+
gr.Textbox(label="Search from IUPAC", placeholder="Enter IUPAC name here"),
|
63 |
+
gr.Textbox(label="Search from trivial name", placeholder="Enter name here"),
|
64 |
+
gr.Textbox(label="Search from InChI", placeholder="Enter InChI here"),
|
65 |
+
gr.Textbox(label="Search from InChIKey", placeholder="Enter InChIKey here"),
|
66 |
+
gr.CheckboxGroup(choices=["IUPAC", "SMILES", "InChI", "InChIKey", "synonyms"], label="Choose data fields to request")
|
67 |
+
],
|
68 |
+
outputs=[
|
69 |
+
gr.Textbox(label="SMILES name"),
|
70 |
+
gr.Textbox(label="IUPAC name"),
|
71 |
+
gr.Textbox(label="Synonyms"),
|
72 |
+
gr.Textbox(label="InChI"),
|
73 |
+
gr.Textbox(label="InChIKey")
|
74 |
+
],
|
75 |
+
examples=[
|
76 |
+
["", "", "(+)-PSI reagent", "", "", ["IUPAC", "SMILES", "InChI", "InChIKey", "synonyms"]],
|
77 |
+
["CCCCO", "", "", "", "", ["IUPAC", "synonyms"]],
|
78 |
+
["C[C@@H](C1=CC=C(C=C1)CC(C)C)C(=O)O", "", "", "", "", ["IUPAC", "SMILES", "InChI", "InChIKey", "synonyms"]],
|
79 |
+
],
|
80 |
+
theme=gr.themes.Base()
|
81 |
+
)
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
84 |
+
main_interface.launch(share=True, auth=(os.getenv('user'), os.getenv('password')))
|