File size: 1,337 Bytes
4a6cfc9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI
from pydantic import BaseModel
import tensorflow as tf
import numpy as np
from tokenizer_utils import load_index_to_word

app = FastAPI()

# Carrega o modelo apenas uma vez
decoder_model = tf.keras.models.load_model("decoder_model.h5")

# Pydantic model para a requisição
class EmbeddingRequest(BaseModel):
    embedding: list[float]

@app.post("/decode")
async def decode(req: EmbeddingRequest):
    try:
        # Prepara tensor com shape (1, embedding_dim)
        input_tensor = np.array([req.embedding], dtype=np.float32)

        # Faz a previsão
        prediction = decoder_model.predict(input_tensor)

        # Pega os índices com maior probabilidade
        token_ids = prediction.argmax(axis=-1)

        # Normaliza para lista de inteiros
        if isinstance(token_ids, np.ndarray):
            token_ids = token_ids[0] if token_ids.ndim > 1 else token_ids
            token_ids = token_ids.tolist()

        if isinstance(token_ids, (np.integer, int)):
            token_ids = [int(token_ids)]

        # Carrega dicionário index → word
        index_to_word = load_index_to_word()

        # Decodifica
        decoded = " ".join(index_to_word.get(i, "") for i in token_ids).strip()

        return {"decoded": decoded}
    
    except Exception as e:
        return {"error": str(e)}