Spaces:
Running
Running
Enzo Reis de Oliveira
commited on
Commit
·
843425c
1
Parent(s):
4d799f2
Fixing path again
Browse files
app.py
CHANGED
|
@@ -1,45 +1,48 @@
|
|
| 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 |
-
#
|
| 14 |
-
MODEL_DIR = os.path.join(
|
| 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 |
-
#
|
| 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 |
-
#
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
gr.Markdown(
|
| 45 |
"""
|
|
@@ -49,7 +52,6 @@ with gr.Blocks() as demo:
|
|
| 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")
|
|
@@ -57,7 +59,6 @@ with gr.Blocks() as demo:
|
|
| 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,
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
|
| 4 |
+
# 1) Ajuste de path antes de qualquer import de smi_ted_light
|
| 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 |
import tempfile
|
| 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,
|
| 25 |
ckpt_filename="smi-ted-Light_40.pt",
|
| 26 |
vocab_filename="bert_vocab_curated.txt",
|
| 27 |
)
|
| 28 |
|
| 29 |
+
# 4) Função que gera embedding e disponibiliza CSV
|
| 30 |
def gerar_embedding_e_csv(smiles: str):
|
| 31 |
smiles = smiles.strip()
|
| 32 |
if not smiles:
|
|
|
|
| 33 |
return {"erro": "digite uma sequência SMILES primeiro"}, gr.update(visible=False)
|
| 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 |
return vetor, gr.update(value=tmp.name, visible=True)
|
| 42 |
except Exception as e:
|
|
|
|
| 43 |
return {"erro": str(e)}, gr.update(visible=False)
|
| 44 |
|
| 45 |
+
# 5) Montagem da interface Gradio Blocks
|
| 46 |
with gr.Blocks() as demo:
|
| 47 |
gr.Markdown(
|
| 48 |
"""
|
|
|
|
| 52 |
2. Um botão para baixar esse vetor em CSV
|
| 53 |
"""
|
| 54 |
)
|
|
|
|
| 55 |
with gr.Row():
|
| 56 |
inp_smiles = gr.Textbox(label="SMILES", placeholder="Ex.: CCO")
|
| 57 |
btn = gr.Button("Gerar Embedding")
|
|
|
|
| 59 |
out_json = gr.JSON(label="Embedding (lista de floats)")
|
| 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,
|