Spaces:
Running
Running
Enzo Reis de Oliveira
commited on
Commit
·
e1e6b13
1
Parent(s):
843425c
Error fix
Browse files
app.py
CHANGED
@@ -1,24 +1,18 @@
|
|
1 |
import os
|
2 |
import sys
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
# 1) Ajuste de path
|
5 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
6 |
INFERENCE_PATH = os.path.join(BASE_DIR, "smi-ted", "inference")
|
7 |
-
|
8 |
-
if not os.path.isdir(INFERENCE_PATH):
|
9 |
-
raise RuntimeError(f"Caminho de inference não encontrado: {INFERENCE_PATH}")
|
10 |
-
|
11 |
-
# Insere no início do sys.path para ter precedência
|
12 |
sys.path.insert(0, INFERENCE_PATH)
|
13 |
|
14 |
-
# 2) Agora sim importamos o loader do modelo
|
15 |
from smi_ted_light.load import load_smi_ted
|
16 |
|
17 |
-
|
18 |
-
import pandas as pd
|
19 |
-
import gradio as gr
|
20 |
-
|
21 |
-
# 3) Carrega o modelo SMI-TED Light
|
22 |
MODEL_DIR = os.path.join(INFERENCE_PATH, "smi_ted_light")
|
23 |
model = load_smi_ted(
|
24 |
folder=MODEL_DIR,
|
@@ -26,43 +20,53 @@ model = load_smi_ted(
|
|
26 |
vocab_filename="bert_vocab_curated.txt",
|
27 |
)
|
28 |
|
29 |
-
#
|
30 |
def gerar_embedding_e_csv(smiles: str):
|
31 |
smiles = smiles.strip()
|
32 |
if not smiles:
|
33 |
-
|
|
|
34 |
|
35 |
try:
|
36 |
vetor = model.encode(smiles, return_torch=True)[0].tolist()
|
|
|
37 |
df = pd.DataFrame([vetor])
|
38 |
tmp = tempfile.NamedTemporaryFile(suffix=".csv", delete=False)
|
39 |
df.to_csv(tmp.name, index=False)
|
40 |
tmp.close()
|
41 |
-
|
|
|
42 |
except Exception as e:
|
43 |
-
|
|
|
44 |
|
45 |
-
#
|
46 |
with gr.Blocks() as demo:
|
47 |
gr.Markdown(
|
48 |
"""
|
49 |
## SMI-TED Embedding Generator
|
50 |
Cole uma sequência SMILES e receba:
|
51 |
-
1.
|
52 |
-
2. Um
|
53 |
"""
|
54 |
)
|
|
|
55 |
with gr.Row():
|
56 |
inp_smiles = gr.Textbox(label="SMILES", placeholder="Ex.: CCO")
|
57 |
btn = gr.Button("Gerar Embedding")
|
58 |
with gr.Row():
|
59 |
-
|
|
|
|
|
|
|
|
|
|
|
60 |
out_file = gr.File(label="Download do CSV", visible=False)
|
61 |
|
62 |
btn.click(
|
63 |
fn=gerar_embedding_e_csv,
|
64 |
inputs=inp_smiles,
|
65 |
-
outputs=[
|
66 |
)
|
67 |
|
68 |
if __name__ == "__main__":
|
|
|
1 |
import os
|
2 |
import sys
|
3 |
+
import json
|
4 |
+
import tempfile
|
5 |
+
import pandas as pd
|
6 |
+
import gradio as gr
|
7 |
|
8 |
+
# 1) Ajuste de path ANTES de importar smi_ted_light
|
9 |
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
10 |
INFERENCE_PATH = os.path.join(BASE_DIR, "smi-ted", "inference")
|
|
|
|
|
|
|
|
|
|
|
11 |
sys.path.insert(0, INFERENCE_PATH)
|
12 |
|
|
|
13 |
from smi_ted_light.load import load_smi_ted
|
14 |
|
15 |
+
# 2) Carrega o modelo
|
|
|
|
|
|
|
|
|
16 |
MODEL_DIR = os.path.join(INFERENCE_PATH, "smi_ted_light")
|
17 |
model = load_smi_ted(
|
18 |
folder=MODEL_DIR,
|
|
|
20 |
vocab_filename="bert_vocab_curated.txt",
|
21 |
)
|
22 |
|
23 |
+
# 3) Função que retorna STRING JSON + gr.update para o CSV
|
24 |
def gerar_embedding_e_csv(smiles: str):
|
25 |
smiles = smiles.strip()
|
26 |
if not smiles:
|
27 |
+
erro = {"erro": "digite uma sequência SMILES primeiro"}
|
28 |
+
return json.dumps(erro), gr.update(visible=False)
|
29 |
|
30 |
try:
|
31 |
vetor = model.encode(smiles, return_torch=True)[0].tolist()
|
32 |
+
# monta CSV
|
33 |
df = pd.DataFrame([vetor])
|
34 |
tmp = tempfile.NamedTemporaryFile(suffix=".csv", delete=False)
|
35 |
df.to_csv(tmp.name, index=False)
|
36 |
tmp.close()
|
37 |
+
# retorna JSON-string e torna o link visível
|
38 |
+
return json.dumps(vetor), gr.update(value=tmp.name, visible=True)
|
39 |
except Exception as e:
|
40 |
+
erro = {"erro": str(e)}
|
41 |
+
return json.dumps(erro), gr.update(visible=False)
|
42 |
|
43 |
+
# 4) Interface Blocks
|
44 |
with gr.Blocks() as demo:
|
45 |
gr.Markdown(
|
46 |
"""
|
47 |
## SMI-TED Embedding Generator
|
48 |
Cole uma sequência SMILES e receba:
|
49 |
+
1. Uma **string JSON** com o vetor (Textbox)
|
50 |
+
2. Um link para **baixar o CSV** (File)
|
51 |
"""
|
52 |
)
|
53 |
+
|
54 |
with gr.Row():
|
55 |
inp_smiles = gr.Textbox(label="SMILES", placeholder="Ex.: CCO")
|
56 |
btn = gr.Button("Gerar Embedding")
|
57 |
with gr.Row():
|
58 |
+
out_text = gr.Textbox(
|
59 |
+
label="Embedding (JSON)",
|
60 |
+
interactive=False,
|
61 |
+
lines=4,
|
62 |
+
placeholder='Vai aparecer aqui o vetor como JSON...'
|
63 |
+
)
|
64 |
out_file = gr.File(label="Download do CSV", visible=False)
|
65 |
|
66 |
btn.click(
|
67 |
fn=gerar_embedding_e_csv,
|
68 |
inputs=inp_smiles,
|
69 |
+
outputs=[out_text, out_file]
|
70 |
)
|
71 |
|
72 |
if __name__ == "__main__":
|