CoolFace
Apppublic

ramouch/English-Encoder

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py41 linesDownload Raw Back to root
1from fastapi import FastAPI, HTTPException2from pydantic import BaseModel3from encoder import EnglishSentenceEncoder4import logging5import asyncio6 7# Initialize FastAPI and logging8logging.basicConfig(level=logging.INFO)9app = FastAPI()10model = None11 12class TextData(BaseModel):13    text: str14 15# Health Check Endpoint16@app.get("/")17async def health_check():18    """Health check for Hugging Face Spaces."""19    return {"status": "ok"}20 21# Asynchronous Model Loading22async def get_model():23    global model24    if model is None:25        logging.info("๐Ÿ”ฅ Loading the Arabic BERT model...")26        model = EnglishSentenceEncoder()27        logging.info("โœ… Model loaded successfully.")28    return model29 30# Endpoint for Encoding Text31@app.post("/encode")32async def encode_text(data: TextData):33    try:34        logging.info(f"๐Ÿ“ Received text: {data.text}")35        encoder = await get_model()36        embedding = encoder.encode(data.text)37        logging.info("โœ… Embedding created successfully.")38        return {"embedding": embedding.tolist()[0]}39    except Exception as e:40        logging.error(f"โŒ Error during embedding: {e}")41        raise HTTPException(status_code=500, detail=str(e))