Spaces:
Sleeping
Sleeping
Felipe Maya Muniz commited on
Commit ·
4a6cfc9
1
Parent(s): 5985d55
Deploy FastAPI decoder with model
Browse filesFiles changed (6)
- .gitattributes+1
- DockerfileAdded+20
- app.pyAdded+45
- decoder_model.h5Added+3
- requirements.txtAdded+3
- tokenizer_utils.pyAdded+12
32 unmodified lines3334353632 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text*.keras filter=lfs diff=lfs merge=lfs -text1234567891011121314151617181920# Usa uma imagem leve com Python 3.10FROM python:3.10-slim
# Define diretório de trabalho dentro do containerWORKDIR /app
# Copia arquivos de dependênciaCOPY requirements.txt .
# Instala dependênciasRUN pip install --no-cache-dir -r requirements.txt
# Copia o restante do códigoCOPY . .
# Expõe a porta padrão usada pelo SpacesEXPOSE 7860
# Comando para rodar o app com UvicornCMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]123456789101112131415161718192021222324252627282930313233343536373839404142434445from fastapi import FastAPIfrom pydantic import BaseModelimport tensorflow as tfimport numpy as npfrom tokenizer_utils import load_index_to_word
app = FastAPI()
# Carrega o modelo apenas uma vezdecoder_model = tf.keras.models.load_model("decoder_model.h5")
# Pydantic model para a requisiçãoclass 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)}123version https://git-lfs.github.com/spec/v1oid sha256:cac6aa6c5cef86e36ff0d8a5e7aeafc7d417bf8d960467b2eb7141b77bc8f8ecsize 27866032123fastapiuvicorntensorflow123456789101112### 📄 tokenizer_utils.pydef load_index_to_word(): # Mock de exemplo. Substitua por loading real do seu JSON ou pickle. return { 0: "<PAD>", 1: "hello", 2: "world", 3: "sage", 4: "is", 5: "thinking", }.gitattributes+1
DockerfileAdded+20
app.pyAdded+45
decoder_model.h5Added+3
requirements.txtAdded+3
tokenizer_utils.pyAdded+12