Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- app.py +53 -2
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -8,6 +8,33 @@ from rdkit import Chem
|
|
| 8 |
from rdkit.Chem import Draw
|
| 9 |
from PIL import Image
|
| 10 |
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
ckpt_path = hf_hub_download('yujieq/MolScribe', 'swin_base_char_aux_1m.pth')
|
| 13 |
model = MolScribe(ckpt_path, device=torch.device('cpu'))
|
|
@@ -21,7 +48,10 @@ def make_smiles(img):
|
|
| 21 |
filename = "chat_image.png"
|
| 22 |
new_img_raw.save(filename)
|
| 23 |
new_img = Image.open(filename)
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
demo = gr.Interface(
|
| 27 |
fn = make_smiles,
|
|
@@ -30,4 +60,25 @@ demo = gr.Interface(
|
|
| 30 |
title="MolScribe",
|
| 31 |
)
|
| 32 |
|
| 33 |
-
demo.launch(mcp_server=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
from rdkit.Chem import Draw
|
| 9 |
from PIL import Image
|
| 10 |
import io
|
| 11 |
+
import pubchempy as pcp
|
| 12 |
+
|
| 13 |
+
def name_node(smiles: str) -> (str):
|
| 14 |
+
'''
|
| 15 |
+
Queries Pubchem for the name of the molecule based on the smiles string.
|
| 16 |
+
Args:
|
| 17 |
+
smiles: the input smiles string
|
| 18 |
+
Returns:
|
| 19 |
+
names_list: the list of names of the molecules
|
| 20 |
+
name_string: a string of the tool results
|
| 21 |
+
'''
|
| 22 |
+
print("name tool")
|
| 23 |
+
print('===================================================')
|
| 24 |
+
|
| 25 |
+
name_string = ''
|
| 26 |
+
try:
|
| 27 |
+
res = pcp.get_compounds(smiles, "smiles")
|
| 28 |
+
name = res[0].iupac_name
|
| 29 |
+
name_string += f'{smiles}: IUPAC molecule name: {name}\n'
|
| 30 |
+
syn_list = pcp.get_synonyms(res[0].cid)
|
| 31 |
+
for alt_name in syn_list[0]['Synonym'][:5]:
|
| 32 |
+
name_string += f'{smiles}: alternative or common name: {alt_name}\n'
|
| 33 |
+
except:
|
| 34 |
+
name = "unknown"
|
| 35 |
+
name_string += f'{smiles}: Fail\n'
|
| 36 |
+
|
| 37 |
+
return name_string
|
| 38 |
|
| 39 |
ckpt_path = hf_hub_download('yujieq/MolScribe', 'swin_base_char_aux_1m.pth')
|
| 40 |
model = MolScribe(ckpt_path, device=torch.device('cpu'))
|
|
|
|
| 48 |
filename = "chat_image.png"
|
| 49 |
new_img_raw.save(filename)
|
| 50 |
new_img = Image.open(filename)
|
| 51 |
+
|
| 52 |
+
smiles = output['smiles']
|
| 53 |
+
name_string = name_node(smiles)
|
| 54 |
+
return name_string, new_img
|
| 55 |
|
| 56 |
demo = gr.Interface(
|
| 57 |
fn = make_smiles,
|
|
|
|
| 60 |
title="MolScribe",
|
| 61 |
)
|
| 62 |
|
| 63 |
+
demo.launch(mcp_server=True)
|
| 64 |
+
|
| 65 |
+
with gr.Blocks() as modrag:
|
| 66 |
+
top = gr.Markdown(
|
| 67 |
+
"""
|
| 68 |
+
# Convert a molecule image to a SMILES string and name with MolScribe
|
| 69 |
+
- Black on white iamges work best
|
| 70 |
+
""")
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
inputs=gr.Image(type="filepath")
|
| 74 |
+
with gr.Column():
|
| 75 |
+
text_out = gr.Textbox(lines=2, label="SMILES")
|
| 76 |
+
img_out = gr.Image(label="New Image")
|
| 77 |
+
|
| 78 |
+
submit_button = gr.Button("Submit")
|
| 79 |
+
clear_button = gr.ClearButton("Clear")
|
| 80 |
+
|
| 81 |
+
submit_button.click(make_smiles, [inputs], [text_out, img_out])
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
modrag.launch(mcp_server=True, share=True)
|
requirements.txt
CHANGED
|
@@ -2,4 +2,5 @@ git+https://github.com/thomas0809/MolScribe.git
|
|
| 2 |
rdkit
|
| 3 |
torch
|
| 4 |
gradio
|
| 5 |
-
pillow
|
|
|
|
|
|
| 2 |
rdkit
|
| 3 |
torch
|
| 4 |
gradio
|
| 5 |
+
pillow
|
| 6 |
+
pubchempy
|