CoolFace
Apppublic

ramouch/English-Encoder

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
App README

๐Ÿ”ค English Sentence Encoder API (FastAPI + SentenceTransformers)

This repository provides an easy-to-use English text embedding API using FastAPI and the high-performance all-MiniLM-L6-v2 model from SentenceTransformers.

Perfect for semantic similarity, search, and clustering tasks where compact sentence embeddings are needed.


๐Ÿš€ Features

  • โ€”โœจ Uses all-MiniLM-L6-v2: Fast & accurate transformer-based model
  • โ€”โšก Lightweight, suitable for CPU
  • โ€”๐Ÿ”Œ RESTful API powered by FastAPI
  • โ€”๐Ÿ” Accepts single or batch sentence inputs
  • โ€”๐Ÿงช Local testing & container-friendly

๐Ÿง  Model Details

PropertyDescription
Modelsentence-transformers/all-MiniLM-L6-v2
ArchitectureMiniLM (distilled Transformer)
Embedding Size384
Inference Speedโšก Very fast on CPU
Training ObjectiveContrastive + Multiple Negatives (SBERT)
Use CasesSemantic Search, Clustering, Similarity Scoring
โœ… Ideal for production systems needing fast, low-latency sentence embeddings.

๐Ÿ“ฆ API Endpoint

POST /encode

Input: Raw text or list of sentences Output: List of float embeddings

โœ… Example Request:

bash
curl -X POST http://localhost:7860/encode \
  -H "Content-Type: application/json" \
  -d '{"text": "How are you today?"}'

๐Ÿ“ค Example Response:

json
{
  "embedding": [
    -0.0212, 0.0543, -0.0746, ...
  ]
}

๐Ÿงช Local Development

1. Install dependencies

bash
pip install -r requirements.txt

2. Run the API

bash
python app.py

3. Test the endpoint

bash
curl -X POST http://localhost:7860/encode \
  -H "Content-Type: application/json" \
  -d '{"text": "Example sentence."}'

๐Ÿณ Dockerfile

dockerfile
# Use a minimal base image
FROM python:3.9-slim

# Create a non-root user for security
RUN useradd -m user
USER user

# Set environment variables
ENV HOME=/home/user \
    PATH=/home/user/.local/bin:$PATH \
    PORT=7860

# Set the working directory
WORKDIR $HOME/app

# Copy requirements and install dependencies in a single RUN command
COPY --chown=user requirements.txt ./
RUN pip install --upgrade pip && \
    pip install -r requirements.txt

# Copy application files and the model
COPY --chown=user ./ $HOME/app

# Expose the correct port for Hugging Face Spaces
EXPOSE 7860

# Run the FastAPI app with uvicorn directly for lightweight deployment
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]

๐Ÿ“ Project Structure

.
โ”œโ”€โ”€ app.py                  # FastAPI app with /encode endpoint
โ”œโ”€โ”€ encoder.py              # Model wrapper for MiniLM encoder
โ”œโ”€โ”€ requirements.txt        # All required Python libraries
โ”œโ”€โ”€ Dockerfile              # For deployment
โ””โ”€โ”€ README.md               # Documentation

๐Ÿงพ Requirements

txt
fastapi
uvicorn
sentence-transformers
numpy

๐Ÿ“‚ Use in Python

python
import requests

text = "What time is the meeting?"
response = requests.post("http://localhost:7860/encode", json={"text": text})
embedding = response.json()["embedding"]
print(embedding)

๐Ÿ‘ฉโ€๐Ÿ’ป Maintainer

Inherited Games Studio ๐Ÿ“ง contact@inheritedgames.com ๐Ÿ”— github.com/inheritedgames ๐Ÿ”— github.com/RAMA012001


๐Ÿ“„ License

MIT License


๐Ÿ™ Credits