File size: 2,167 Bytes
4d799f2
 
843425c
 
 
 
 
 
 
 
 
 
 
 
 
 
4d799f2
 
2af5b7d
 
843425c
 
64428bf
 
 
 
 
 
843425c
4d799f2
64428bf
 
4d799f2
f63af71
64428bf
4d799f2
 
 
 
 
 
fe92162
4d799f2
 
843425c
4d799f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f63af71
64428bf
f63af71
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import sys

# 1) Ajuste de path antes de qualquer import de smi_ted_light
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
INFERENCE_PATH = os.path.join(BASE_DIR, "smi-ted", "inference")

if not os.path.isdir(INFERENCE_PATH):
    raise RuntimeError(f"Caminho de inference não encontrado: {INFERENCE_PATH}")

# Insere no início do sys.path para ter precedência
sys.path.insert(0, INFERENCE_PATH)

# 2) Agora sim importamos o loader do modelo
from smi_ted_light.load import load_smi_ted

import tempfile
import pandas as pd
import gradio as gr

# 3) Carrega o modelo SMI-TED Light
MODEL_DIR = os.path.join(INFERENCE_PATH, "smi_ted_light")
model = load_smi_ted(
    folder=MODEL_DIR,
    ckpt_filename="smi-ted-Light_40.pt",
    vocab_filename="bert_vocab_curated.txt",
)

# 4) Função que gera embedding e disponibiliza CSV
def gerar_embedding_e_csv(smiles: str):
    smiles = smiles.strip()
    if not smiles:
        return {"erro": "digite uma sequência SMILES primeiro"}, gr.update(visible=False)

    try:
        vetor = model.encode(smiles, return_torch=True)[0].tolist()
        df = pd.DataFrame([vetor])
        tmp = tempfile.NamedTemporaryFile(suffix=".csv", delete=False)
        df.to_csv(tmp.name, index=False)
        tmp.close()
        return vetor, gr.update(value=tmp.name, visible=True)
    except Exception as e:
        return {"erro": str(e)}, gr.update(visible=False)

# 5) Montagem da interface Gradio Blocks
with gr.Blocks() as demo:
    gr.Markdown(
        """
        ## SMI-TED Embedding Generator  
        Cole uma sequência SMILES e receba:
        1. O vetor embedding (768 floats) em JSON  
        2. Um botão para baixar esse vetor em CSV  
        """
    )
    with gr.Row():
        inp_smiles = gr.Textbox(label="SMILES", placeholder="Ex.: CCO")
        btn = gr.Button("Gerar Embedding")
    with gr.Row():
        out_json = gr.JSON(label="Embedding (lista de floats)")
        out_file = gr.File(label="Download do CSV", visible=False)

    btn.click(
        fn=gerar_embedding_e_csv,
        inputs=inp_smiles,
        outputs=[out_json, out_file]
    )

if __name__ == "__main__":
    demo.launch()