Spaces:
Running
Running
Enzo Reis de Oliveira
commited on
Commit
·
4d799f2
1
Parent(s):
f63af71
CSV fixed
Browse files- app.py +49 -35
- requirements.txt +5 -6
app.py
CHANGED
@@ -1,54 +1,68 @@
|
|
1 |
-
import os
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
sys.path.append(INFERENCE_DIR)
|
6 |
-
|
7 |
import gradio as gr
|
8 |
from smi_ted_light.load import load_smi_ted
|
9 |
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
# 2)
|
12 |
MODEL_DIR = os.path.join("smi-ted", "inference", "smi_ted_light")
|
13 |
-
|
14 |
-
# 3) Carrega o modelo SMI‑TED (Light)
|
15 |
-
# Se você renomeou o .pt ou o vocab, ajuste aqui.
|
16 |
model = load_smi_ted(
|
17 |
folder=MODEL_DIR,
|
18 |
ckpt_filename="smi-ted-Light_40.pt",
|
19 |
vocab_filename="bert_vocab_curated.txt",
|
20 |
)
|
21 |
|
22 |
-
#
|
23 |
-
def
|
24 |
-
"""
|
25 |
-
Recebe uma string SMILES e devolve o embedding (lista de 768 floats).
|
26 |
-
Em caso de erro, devolve um dicionário com a mensagem.
|
27 |
-
"""
|
28 |
smiles = smiles.strip()
|
29 |
if not smiles:
|
30 |
-
|
|
|
31 |
|
32 |
try:
|
33 |
-
#
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
except Exception as e:
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
#
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
"
|
49 |
-
)
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
-
# 6) Roda localmente ou no Hugging Face Space
|
53 |
if __name__ == "__main__":
|
54 |
demo.launch()
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import tempfile
|
4 |
+
import pandas as pd
|
|
|
|
|
5 |
import gradio as gr
|
6 |
from smi_ted_light.load import load_smi_ted
|
7 |
|
8 |
+
# 1) Ajuste de paths para encontrar o inference
|
9 |
+
BASE_DIR = os.path.dirname(__file__)
|
10 |
+
INFERENCE_DIR = os.path.join(BASE_DIR, "smi-ted", "inference")
|
11 |
+
sys.path.append(INFERENCE_DIR)
|
12 |
|
13 |
+
# 2) Carregando o modelo
|
14 |
MODEL_DIR = os.path.join("smi-ted", "inference", "smi_ted_light")
|
|
|
|
|
|
|
15 |
model = load_smi_ted(
|
16 |
folder=MODEL_DIR,
|
17 |
ckpt_filename="smi-ted-Light_40.pt",
|
18 |
vocab_filename="bert_vocab_curated.txt",
|
19 |
)
|
20 |
|
21 |
+
# 3) Função única que gera embedding E CSV
|
22 |
+
def gerar_embedding_e_csv(smiles: str):
|
|
|
|
|
|
|
|
|
23 |
smiles = smiles.strip()
|
24 |
if not smiles:
|
25 |
+
# Se não digitou nada, retorna erro e esconde o botão de download
|
26 |
+
return {"erro": "digite uma sequência SMILES primeiro"}, gr.update(visible=False)
|
27 |
|
28 |
try:
|
29 |
+
# Gera o embedding
|
30 |
+
vetor = model.encode(smiles, return_torch=True)[0].tolist()
|
31 |
+
# Cria DataFrame e escreve CSV num arquivo temporário
|
32 |
+
df = pd.DataFrame([vetor])
|
33 |
+
tmp = tempfile.NamedTemporaryFile(suffix=".csv", delete=False)
|
34 |
+
df.to_csv(tmp.name, index=False)
|
35 |
+
tmp.close()
|
36 |
+
# Retorna: 1) JSON, 2) update para o File (path + visível)
|
37 |
+
return vetor, gr.update(value=tmp.name, visible=True)
|
38 |
except Exception as e:
|
39 |
+
# Em caso de erro interno, mostra mensagem e esconde o botão
|
40 |
+
return {"erro": str(e)}, gr.update(visible=False)
|
41 |
+
|
42 |
+
# 4) Montando a interface Blocks
|
43 |
+
with gr.Blocks() as demo:
|
44 |
+
gr.Markdown(
|
45 |
+
"""
|
46 |
+
## SMI-TED Embedding Generator
|
47 |
+
Cole uma sequência SMILES e receba:
|
48 |
+
1. O vetor embedding (768 floats) em JSON
|
49 |
+
2. Um botão para baixar esse vetor em CSV
|
50 |
+
"""
|
51 |
+
)
|
52 |
+
|
53 |
+
with gr.Row():
|
54 |
+
inp_smiles = gr.Textbox(label="SMILES", placeholder="Ex.: CCO")
|
55 |
+
btn = gr.Button("Gerar Embedding")
|
56 |
+
with gr.Row():
|
57 |
+
out_json = gr.JSON(label="Embedding (lista de floats)")
|
58 |
+
out_file = gr.File(label="Download do CSV", visible=False)
|
59 |
+
|
60 |
+
# 5) Ligando o botão à função única (dois outputs)
|
61 |
+
btn.click(
|
62 |
+
fn=gerar_embedding_e_csv,
|
63 |
+
inputs=inp_smiles,
|
64 |
+
outputs=[out_json, out_file]
|
65 |
+
)
|
66 |
|
|
|
67 |
if __name__ == "__main__":
|
68 |
demo.launch()
|
requirements.txt
CHANGED
@@ -1,10 +1,9 @@
|
|
1 |
-
torch>=2.1.0
|
2 |
transformers>=4.40.0
|
3 |
-
regex
|
4 |
-
numpy==1.26.4
|
5 |
-
pandas==1.4.0
|
6 |
tqdm>=4.66.4
|
7 |
rdkit>=2024.3.5
|
8 |
-
gradio
|
9 |
-
gradio_client==0.17.0
|
10 |
huggingface-hub
|
|
|
1 |
+
torch>=2.1.0
|
2 |
transformers>=4.40.0
|
3 |
+
regex
|
4 |
+
numpy==1.26.4
|
5 |
+
pandas==1.4.0
|
6 |
tqdm>=4.66.4
|
7 |
rdkit>=2024.3.5
|
8 |
+
gradio>=4.32.0
|
|
|
9 |
huggingface-hub
|