Spaces:
Running
Running
Enzo Reis de Oliveira
commited on
Commit
·
075858d
1
Parent(s):
79a99bb
Fixing csv
Browse files- app.py +15 -34
- requirements.txt +2 -1
app.py
CHANGED
@@ -1,11 +1,9 @@
|
|
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 |
# 1) Ajusta o path para o inference do SMI-TED
|
11 |
BASE_DIR = os.path.dirname(__file__)
|
@@ -24,38 +22,22 @@ model = load_smi_ted(
|
|
24 |
|
25 |
# 4) Função utilizada pela interface Gradio
|
26 |
def gerar_embedding(smiles: str):
|
27 |
-
"""
|
28 |
-
Recebe uma string SMILES e devolve:
|
29 |
-
- embedding (lista de 768 floats)
|
30 |
-
- caminho para um CSV com esse embedding, pronto para download
|
31 |
-
Em caso de erro, devolve um dicionário com a mensagem e nenhum arquivo.
|
32 |
-
"""
|
33 |
smiles = smiles.strip()
|
34 |
if not smiles:
|
35 |
return {"erro": "digite uma sequência SMILES primeiro"}, None
|
36 |
-
|
37 |
try:
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
# Cria um CSV temporário com uma única linha (o embedding)
|
43 |
df = pd.DataFrame([embedding])
|
44 |
-
tmp = tempfile.NamedTemporaryFile(
|
45 |
-
|
46 |
-
suffix=".csv",
|
47 |
-
prefix="embedding_",
|
48 |
-
)
|
49 |
-
csv_path = tmp.name
|
50 |
-
df.to_csv(csv_path, index=False)
|
51 |
tmp.close()
|
52 |
-
|
53 |
-
return embedding, csv_path
|
54 |
-
|
55 |
except Exception as e:
|
56 |
return {"erro": str(e)}, None
|
57 |
|
58 |
-
# 5)
|
59 |
demo = gr.Interface(
|
60 |
fn=gerar_embedding,
|
61 |
inputs=gr.Textbox(label="SMILES", placeholder="Ex.: CCO"),
|
@@ -66,11 +48,10 @@ demo = gr.Interface(
|
|
66 |
title="SMI-TED Embedding Generator",
|
67 |
description=(
|
68 |
"Cole uma sequência SMILES e receba o embedding gerado pelo modelo "
|
69 |
-
"SMI-TED Light
|
70 |
-
"Você também pode baixar o embedding em CSV."
|
71 |
),
|
72 |
)
|
73 |
|
74 |
-
|
75 |
if __name__ == "__main__":
|
76 |
-
demo.launch(show_api=False)
|
|
|
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) Ajusta o path para o inference do SMI-TED
|
9 |
BASE_DIR = os.path.dirname(__file__)
|
|
|
22 |
|
23 |
# 4) Função utilizada pela interface Gradio
|
24 |
def gerar_embedding(smiles: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
smiles = smiles.strip()
|
26 |
if not smiles:
|
27 |
return {"erro": "digite uma sequência SMILES primeiro"}, None
|
|
|
28 |
try:
|
29 |
+
vetor = model.encode(smiles, return_torch=True)[0]
|
30 |
+
embedding = vetor.tolist()
|
31 |
+
# monta um CSV temporário
|
|
|
|
|
32 |
df = pd.DataFrame([embedding])
|
33 |
+
tmp = tempfile.NamedTemporaryFile(delete=False, suffix=".csv", prefix="embedding_")
|
34 |
+
df.to_csv(tmp.name, index=False)
|
|
|
|
|
|
|
|
|
|
|
35 |
tmp.close()
|
36 |
+
return embedding, tmp.name
|
|
|
|
|
37 |
except Exception as e:
|
38 |
return {"erro": str(e)}, None
|
39 |
|
40 |
+
# 5) Interface com JSON + botão de download
|
41 |
demo = gr.Interface(
|
42 |
fn=gerar_embedding,
|
43 |
inputs=gr.Textbox(label="SMILES", placeholder="Ex.: CCO"),
|
|
|
48 |
title="SMI-TED Embedding Generator",
|
49 |
description=(
|
50 |
"Cole uma sequência SMILES e receba o embedding gerado pelo modelo "
|
51 |
+
"SMI-TED Light. Você também pode baixar o embedding em CSV."
|
|
|
52 |
),
|
53 |
)
|
54 |
|
55 |
+
|
56 |
if __name__ == "__main__":
|
57 |
+
demo.launch(show_api=False)
|
requirements.txt
CHANGED
@@ -5,5 +5,6 @@ numpy==1.26.4
|
|
5 |
pandas==1.4.0
|
6 |
tqdm>=4.66.4
|
7 |
rdkit>=2024.3.5
|
8 |
-
gradio
|
|
|
9 |
huggingface-hub
|
|
|
5 |
pandas==1.4.0
|
6 |
tqdm>=4.66.4
|
7 |
rdkit>=2024.3.5
|
8 |
+
gradio==4.32.0
|
9 |
+
gradio_client==0.17.0
|
10 |
huggingface-hub
|