Spaces:
Sleeping
Sleeping
Download app.py from gnai-creator/sage-two-decoder: direct link, hf CLI and curl.
- Browser
- Download file 1.34 kB
-
https://huggingface.co/spaces/gnai-creator/sage-two-decoder/resolve/main/app.py
- Command line
-
hf download hf://spaces/gnai-creator/sage-two-decoder/app.py
-
curl -L -o app.py https://huggingface.co/spaces/gnai-creator/sage-two-decoder/resolve/main/app.py
1.34 kB
| 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] | |
| 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)} | |