Update to use consolidated sema-utils models with new API
Browse filesFiles changed (9)
- DockerfileAdded+26
- README.md+96-6
- deploy_to_hf.mdAdded+138
- docs/app.pyAdded+248
- docs/current-state.mdAdded+255
- requirements.txtAdded+8
- sema_translation_api.pyAdded+277
- test_api_client.pyAdded+201
- test_model_download.pyAdded+198
1234567891011121314151617181920212223242526# Dockerfile for Sema Translation API on HuggingFace Spaces
# Use an official Python runtime as a parent imageFROM python:3.10-slim
# Set the working directory in the containerWORKDIR /code
# Copy the requirements file into the container at /codeCOPY ./requirements.txt /code/requirements.txt
# Install any needed packages specified in requirements.txt# --no-cache-dir reduces image size# --upgrade pip ensures we have the latest versionRUN pip install --no-cache-dir --upgrade pipRUN pip install --no-cache-dir -r /code/requirements.txt
# Copy the application code to the working directoryCOPY ./sema_translation_api.py /code/sema_translation_api.py
# Expose port 7860 (HuggingFace Spaces standard)EXPOSE 7860
# Tell uvicorn to run on port 7860, which is the standard for HF Spaces# Use 0.0.0.0 to make it accessible from outside the containerCMD ["uvicorn", "sema_translation_api:app", "--host", "0.0.0.0", "--port", "7860"]123456789101112---title: Sema Apiemoji: πcolorFrom: indigocolorTo: pinksdk: dockerpinned: falselicense: mitshort_description: Translation api---Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102---title: Sema Translation APIemoji: πcolorFrom: bluecolorTo: greensdk: dockerpinned: falselicense: mitshort_description: Translation API using consolidated sema-utils models---# Sema Translation API πA powerful translation API that supports multiple African languages using the consolidated `sematech/sema-utils` model repository.## Features- **Automatic Language Detection**: Detects source language automatically if not provided- **Multi-language Support**: Supports 200+ languages via FLORES-200 codes- **Fast Translation**: Uses CTranslate2 for optimized inference- **RESTful API**: Clean FastAPI interface with automatic documentation- **Consolidated Models**: Uses models from the unified `sematech/sema-utils` repository## API Endpoints### `GET /`Health check endpoint that returns API status and version information.### `POST /translate`Main translation endpoint that accepts:**Request Body:**```json{"text": "Habari ya asubuhi","target_language": "eng_Latn","source_language": "swh_Latn"}```**Response:**```json{"translated_text": "Good morning","source_language": "swh_Latn","target_language": "eng_Latn","inference_time": 0.234,"timestamp": "Monday | 2024-06-21 | 14:30:25"}```## Language CodesThis API uses FLORES-200 language codes. Some common examples:- `eng_Latn` - English- `swh_Latn` - Swahili- `kik_Latn` - Kikuyu- `luo_Latn` - Luo- `fra_Latn` - French- `spa_Latn` - Spanish## Usage Examples### Python```pythonimport requestsresponse = requests.post("https://your-space-url/translate", json={"text": "Habari ya asubuhi","target_language": "eng_Latn"})print(response.json())```### cURL```bashcurl -X POST "https://your-space-url/translate" \-H "Content-Type: application/json" \-d '{"text": "WΔ© mwega?","source_language": "kik_Latn","target_language": "eng_Latn"}'```## Model InformationThis API uses models from the consolidated `sematech/sema-utils` repository:- **Translation Model**: `sematrans-3.3B` (CTranslate2 optimized)- **Language Detection**: `lid218e.bin` (FastText)- **Tokenization**: `spm.model` (SentencePiece)## API DocumentationOnce the Space is running, visit `/docs` for interactive API documentation.---Created by Lewis Kamau Kimaru | Sema AI
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138# Deployment Instructions for HuggingFace Spaces
## Files Ready for Deployment
Your HuggingFace Space needs these files (all created and ready):
1. **`sema_translation_api.py`** - Main API application2. **`requirements.txt`** - Python dependencies3. **`Dockerfile`** - Container configuration4. **`README.md`** - Space documentation and metadata
## Deployment Steps
### Option 1: Using Git (Recommended)
1. **Navigate to your existing HF Space repository:** ```bash cd backend/sema-api ```
2. **The files are ready to deploy as-is:** ```bash # All files are ready: # - sema_translation_api.py (main application) # - requirements.txt # - Dockerfile # - README.md ```
3. **Commit and push to HuggingFace:** ```bash git add . git commit -m "Update to use consolidated sema-utils models with new API" git push origin main ```
### Option 2: Using HuggingFace Web Interface
1. Go to your Space: `https://huggingface.co/spaces/sematech/sema-api`2. Click on "Files" tab3. Upload/replace these files: - Upload `sema_translation_api.py` - Replace `requirements.txt` - Replace `Dockerfile` - Replace `README.md`
## What Happens After Deployment
1. **Automatic Build**: HF Spaces will automatically start building your Docker container2. **Model Download**: During build, the app will download models from `sematech/sema-utils`: - `spm.model` (SentencePiece tokenizer) - `lid218e.bin` (Language detection) - `translation_models/sematrans-3.3B/` (Translation model)3. **API Startup**: Once built, your API will be available at the Space URL
## Testing Your Deployed API
### 1. Health Check```bashcurl https://sematech-sema-api.hf.space/```
### 2. Translation with Auto-Detection```bashcurl -X POST "https://sematech-sema-api.hf.space/translate" \ -H "Content-Type: application/json" \ -d '{ "text": "Habari ya asubuhi", "target_language": "eng_Latn" }'```
### 3. Translation with Source Language```bashcurl -X POST "https://sematech-sema-api.hf.space/translate" \ -H "Content-Type: application/json" \ -d '{ "text": "WΔ© mwega?", "source_language": "kik_Latn", "target_language": "eng_Latn" }'```
### 4. Interactive DocumentationVisit: `https://sematech-sema-api.hf.space/docs`
## Expected Build Time
- **First build**: 10-15 minutes (downloading models ~5GB)- **Subsequent builds**: 2-5 minutes (models cached)
## Monitoring the Build
1. Go to your Space page2. Click on "Logs" tab to see build progress3. Look for these key messages: - "π₯ Downloading models from sematech/sema-utils..." - "β
All models loaded successfully!" - "π API started successfully!"
## Troubleshooting
### If Build Fails:1. Check the logs for specific error messages2. Common issues: - Model download timeout (retry build) - Memory issues (models are large) - Network connectivity issues
### If API Doesn't Respond:1. Check if the Space is "Running" (green status)2. Try the health check endpoint first3. Check logs for runtime errors
## Key Improvements in This Version
1. **Consolidated Models**: Uses your unified `sema-utils` repository2. **Better Error Handling**: Clear error messages and validation3. **Performance Monitoring**: Tracks inference time4. **Clean API Design**: Follows FastAPI best practices5. **Automatic Documentation**: Built-in OpenAPI docs6. **Flexible Input**: Auto-detection or manual source language
## Next Steps After Deployment
1. **Test the API** with various language pairs2. **Monitor performance** and response times3. **Update documentation** with your actual Space URL4. **Consider adding rate limiting** for production use5. **Add authentication** if needed for private use
## Important Note About File Structure
The Dockerfile correctly references `sema_translation_api:app` (not `app:app`) since our main file is `sema_translation_api.py`. No need to rename files - deploy as-is!
---
Your new API is ready to deploy! π123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248''' Created By Lewis Kamau Kimaru Sema translator fastapi implementation January 2024 Docker deployment'''
from fastapi import FastAPI, HTTPException, Request, Dependsfrom fastapi.middleware.cors import CORSMiddlewarefrom fastapi.responses import HTMLResponseimport uvicorn
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipelineimport ctranslate2import sentencepiece as spmimport fasttextimport torch
from datetime import datetimeimport pytzimport timeimport os
app = FastAPI()
origins = ["*"]
app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=False, allow_methods=["*"], allow_headers=["*"],)
# set this key as an environment variablehf_read_key = os.environ.get('huggingface_token')os.environ["HUGGINGFACEHUB_API_TOKEN"] = hf_read_key
fasttext.FastText.eprint = lambda x: None
# User interfacetemplates_folder = os.path.join(os.path.dirname(__file__), "templates") # Get time of request
def get_time(): nairobi_timezone = pytz.timezone('Africa/Nairobi') current_time_nairobi = datetime.now(nairobi_timezone) curr_day = current_time_nairobi.strftime('%A') curr_date = current_time_nairobi.strftime('%Y-%m-%d') curr_time = current_time_nairobi.strftime('%H:%M:%S') full_date = f"{curr_day} | {curr_date} | {curr_time}" return full_date, curr_time
def load_models(): # build model and tokenizer model_name_dict = { #'nllb-distilled-600M': 'facebook/nllb-200-distilled-600M', #'nllb-1.3B': 'facebook/nllb-200-1.3B', #'nllb-distilled-1.3B': 'facebook/nllb-200-distilled-1.3B', #'nllb-3.3B': 'facebook/nllb-200-3.3B', #'nllb-moe-54b': 'facebook/nllb-moe-54b', }
model_dict = {}
for call_name, real_name in model_name_dict.items(): print('\tLoading model: %s' % call_name) model = AutoModelForSeq2SeqLM.from_pretrained(real_name) tokenizer = AutoTokenizer.from_pretrained(real_name) model_dict[call_name+'_model'] = model model_dict[call_name+'_tokenizer'] = tokenizer
return model_dict
# Load the model and tokenizer ..... only once!beam_size = 1 # change to a smaller value for faster inferencedevice = "cpu" # or "cuda"
print('(note-to-self)..... I play the Orchestraπ¦.......')
# Language Prediction modelprint("\n1οΈβ£importing Language Prediction model")lang_model_file = "lid218e.bin"lang_model_full_path = os.path.join(os.path.dirname(__file__), lang_model_file)lang_model = fasttext.load_model(lang_model_full_path)
# Load the source SentencePiece modelprint("\n2οΈβ£importing SentencePiece model")sp_model_file = "spm.model"sp_model_full_path = os.path.join(os.path.dirname(__file__), sp_model_file)sp = spm.SentencePieceProcessor()sp.load(sp_model_full_path)
# Import The Translator model
print("\n3οΈβ£importing Translator model")ct_model_file = "sematrans-3.3B"ct_model_full_path = os.path.join(os.path.dirname(__file__), ct_model_file)translator = ctranslate2.Translator(ct_model_full_path, device)
#model_dict = load_models()
print('\nDone importing models π\n')
def translate_detect(userinput: str, target_lang: str): source_sents = [userinput] source_sents = [sent.strip() for sent in source_sents] target_prefix = [[target_lang]] * len(source_sents)
# Predict the source language predictions = lang_model.predict(source_sents[0], k=1) source_lang = predictions[0][0].replace('__label__', '')
# Subword the source sentences source_sents_subworded = sp.encode(source_sents, out_type=str) source_sents_subworded = [[source_lang] + sent + ["</s>"] for sent in source_sents_subworded]
# Translate the source sentences translations = translator.translate_batch( source_sents_subworded, batch_type="tokens", max_batch_size=2024, beam_size=beam_size, target_prefix=target_prefix, ) translations = [translation[0]['tokens'] for translation in translations]
# Desubword the target sentences translations_desubword = sp.decode(translations) translations_desubword = [sent[len(target_lang):] for sent in translations_desubword]
# Return the source language and the translated text return source_lang, translations_desubword
def translate_enter(userinput: str, source_lang: str, target_lang: str): source_sents = [userinput] source_sents = [sent.strip() for sent in source_sents] target_prefix = [[target_lang]] * len(source_sents)
# Subword the source sentences source_sents_subworded = sp.encode(source_sents, out_type=str) source_sents_subworded = [[source_lang] + sent + ["</s>"] for sent in source_sents_subworded]
# Translate the source sentences translations = translator.translate_batch(source_sents_subworded, batch_type="tokens", max_batch_size=2024, beam_size=beam_size, target_prefix=target_prefix) translations = [translation[0]['tokens'] for translation in translations]
# Desubword the target sentences translations_desubword = sp.decode(translations) translations_desubword = [sent[len(target_lang):] for sent in translations_desubword]
# Return the source language and the translated text return translations_desubword[0]
def translate_faster(userinput3: str, source_lang3: str, target_lang3: str): if len(model_dict) == 2: model_name = 'nllb-moe-54b' start_time = time.time() model = model_dict[model_name + '_model'] tokenizer = model_dict[model_name + '_tokenizer']
translator = pipeline('translation', model=model, tokenizer=tokenizer, src_lang=source_lang3, tgt_lang=target_lang3) output = translator(userinput3, max_length=400) end_time = time.time()
output = output[0]['translation_text'] result = {'inference_time': end_time - start_time, 'source': source, 'target': target, 'result': output} return result @app.get("/", response_class=HTMLResponse)async def read_root(request: Request): return HTMLResponse(content=open(os.path.join(templates_folder, "translator.html"), "r").read(), status_code=200)
@app.post("/translate_detect/")async def translate_detect_endpoint(request: Request): datad = await request.json() userinputd = datad.get("userinput") target_langd = datad.get("target_lang") dfull_date = get_time()[0] print(f"\nrequest: {dfull_date}\nTarget Language; {target_langd}, User Input: {userinputd}\n")
if not userinputd or not target_langd: raise HTTPException(status_code=422, detail="Both 'userinput' and 'target_lang' are required.")
source_langd, translated_text_d = translate_detect(userinputd, target_langd) dcurrent_time = get_time()[1] print(f"\nresponse: {dcurrent_time}; ... Source_language: {source_langd}, Translated Text: {translated_text_d}\n\n") return { "source_language": source_langd, "translated_text": translated_text_d[0], }
@app.post("/translate_enter/")async def translate_enter_endpoint(request: Request): datae = await request.json() userinpute = datae.get("userinput") source_lange = datae.get("source_lang") target_lange = datae.get("target_lang") efull_date = get_time()[0] print(f"\nrequest: {efull_date}\nSource_language; {source_lange}, Target Language; {target_lange}, User Input: {userinpute}\n")
if not userinpute or not target_lange: raise HTTPException(status_code=422, detail="'userinput' 'sourc_lang'and 'target_lang' are required.")
translated_text_e = translate_enter(userinpute, source_lange, target_lange) ecurrent_time = get_time()[1] print(f"\nresponse: {ecurrent_time}; ... Translated Text: {translated_text_e}\n\n") return { "translated_text": translated_text_e, }
@app.post("/translate_faster/")async def translate_faster_endpoint(request: Request): dataf = await request.json() userinputf = datae.get("userinput") source_langf = datae.get("source_lang") target_langf = datae.get("target_lang") ffull_date = get_time()[0] print(f"\nrequest: {ffull_date}\nSource_language; {source_langf}, Target Language; {target_langf}, User Input: {userinputf}\n")
if not userinputf or not target_langf: raise HTTPException(status_code=422, detail="'userinput' 'sourc_lang'and 'target_lang' are required.")
translated_text_f = translate_faster(userinputf, source_langf, target_langf) fcurrent_time = get_time()[1] print(f"\nresponse: {fcurrent_time}; ... Translated Text: {translated_text_f}\n\n") return { "translated_text": translated_text_f, } print("\nAPI started successfully π\n")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255# Current State of the Sema API### Analysis of the Current `app.py`
You're right to want to refactor this. While it works, it has several issues that make it difficult to maintain and scale:
1. **Global Scope:** All the models (`lang_model`, `sp`, `translator`) are loaded in the global scope of the script. This works for simple scripts but is bad practice in applications. It makes the code hard to test and can lead to unexpected side effects.2. **Redundant Code:** The functions `translate_detect` and `translate_enter` are almost identical. The only difference is that one detects the source language and the other takes it as an argument. This can be combined into a single, more flexible function.3. **Unused/Confusing Code:** The `load_models` function, `model_name_dict`, and the entire `translate_faster` endpoint seem to be remnants of a different implementation (likely using standard `transformers` pipelines). This code is not being used by the main translation endpoints and adds confusion.4. **Hardcoded Paths:** Model file paths are constructed using `os.path.join` relative to the script's location (`__file__`). This is not ideal for deployment, especially with Docker, where file paths need to be explicit and predictable.5. **Direct Request Handling:** The endpoints use `await request.json()` to parse the request body. This works, but using Pydantic models (as shown in the previous example) is the standard FastAPI way, providing automatic validation, type hints, and documentation.6. **Mixing Concerns:** The script mixes model loading, business logic (translation), utility functions (`get_time`), and API endpoint definitions all in one file. The previous folder structure we discussed solves this by separating these concerns.
### A Simple, Straightforward Dockerized Script for HF Spaces
Given your goalβa simple, straightforward script that runs in Docker on HF Spaces and uses your centralized modelsβwe can create a much cleaner version.
This setup will:1. Define the necessary files for a Hugging Face Space.2. Automatically download your models from `sematech/sema-utils` when the Space builds.3. Provide a single, clear translation endpoint.4. Be easy to understand and maintain.
---
#### Step 1: Create the Project Folder and Files
Create a new folder for your Hugging Face Space. Let's call it `sema_api_space`. Inside, create the following files:
```sema_api_space/βββ app.py <-- The simplified FastAPI appβββ requirements.txt <-- Python dependenciesβββ Dockerfile <-- Instructions to build the Docker image```
---
#### Step 2: Write the Code for Each File
##### **`requirements.txt`**
This file lists the libraries that `pip` will install.
```text# requirements.txtfastapiuvicorn[standard]ctranslate2sentencepiecefasttext-wheelhuggingface_hubpydantic```
##### **`Dockerfile`**
This file tells Hugging Face Spaces how to build your application environment. It will copy your code, install dependencies, and define the command to run the server.
```dockerfile# Dockerfile
# Use an official Python runtime as a parent imageFROM python:3.10-slim
# Set the working directory in the containerWORKDIR /code
# Copy the requirements file into the container at /codeCOPY ./requirements.txt /code/requirements.txt
# Install any needed packages specified in requirements.txt# --no-cache-dir reduces image size# --upgrade pip ensures we have the latest versionRUN pip install --no-cache-dir --upgrade pipRUN pip install --no-cache-dir -r /code/requirements.txt
# Copy the rest of your application code to the working directoryCOPY ./app.py /code/app.py
# Tell uvicorn to run on port 7860, which is the standard for HF Spaces# Use 0.0.0.0 to make it accessible from outside the containerCMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]```
##### **`app.py`**
This is the heart of your application. It's a heavily simplified and cleaned-up version of your original script. It uses the best practices we discussed.
```python# app.py
import osfrom fastapi import FastAPI, HTTPExceptionfrom pydantic import BaseModel, Fieldfrom huggingface_hub import hf_hub_downloadimport ctranslate2import sentencepiece as spmimport fasttext
# --- 1. Define Data Schemas (for validation and documentation) ---class TranslationRequest(BaseModel): text: str = Field(..., example="WΔ© mwega?") target_language: str = Field(..., example="eng_Latn", description="FLORES-200 code for the target language.") source_language: str | None = Field(None, example="kik_Latn", description="Optional FLORES-200 code for the source language.")
class TranslationResponse(BaseModel): translated_text: str detected_source_language: str
# --- 2. Model Loading ---# This section runs only ONCE when the application starts.print("Downloading and loading models...")
# Define the Hugging Face repo and the files to downloadREPO_ID = "sematech/sema-utils"MODELS_DIR = "hf_models" # A local directory to store the models
# Ensure the local directory existsos.makedirs(MODELS_DIR, exist_ok=True)
# Download each file and get its local pathtry: # Note: hf_hub_download automatically handles caching. # It won't re-download if the file is already there. spm_path = hf_hub_download(repo_id=REPO_ID, filename="spm.model", local_dir=MODELS_DIR) ft_path = hf_hub_download(repo_id=REPO_ID, filename="lid218e.bin", local_dir=MODELS_DIR) # For CTranslate2 models, it's often better to download the whole directory. # We specify the subfolder where the model lives in the repo. # The actual model path will be inside the returned directory. ct_model_dir = hf_hub_download( repo_id=REPO_ID, filename="sematrans-3.3B/model.bin", # A file inside the dir to trigger download local_dir=MODELS_DIR ) # The actual path to the CTranslate2 model directory ct_path = os.path.dirname(ct_model_dir)
except Exception as e: print(f"Error downloading models: {e}") # In a real app, you might want to exit or handle this more gracefully. exit()
# Suppress the fasttext warningfasttext.FastText.eprint = lambda x: None
# Load the models into memorysp_model = spm.SentencePieceProcessor(spm_path)lang_model = fasttext.load_model(ft_path)translator = ctranslate2.Translator(ct_path, device="cpu") # Use "cuda" if your Space has a GPU
print("All models loaded successfully!")
# --- 3. FastAPI Application ---app = FastAPI( title="Sema Simple Translation API", description="A simple API using models from sematech/sema-utils on Hugging Face Hub.", version="1.0.0")
@app.get("/")def root(): return {"status": "ok", "message": "Sema Translation API is running."}
@app.post("/translate", response_model=TranslationResponse)async def translate_endpoint(request: TranslationRequest): """ Performs translation. Detects source language if not provided. """ if not request.text.strip(): raise HTTPException(status_code=400, detail="Input text cannot be empty.")
# A single function handles both cases (with or without source_language) try: # Detect language if not provided source_lang = request.source_language if not source_lang: # Replace newlines for better language detection predictions = lang_model.predict(request.text.replace('\n', ' '), k=1) source_lang = predictions[0][0].replace('__label__', '')
# Prepare for translation source_tokenized = sp_model.encode(request.text, out_type=str) source_tokenized = [[source_lang] + sent + ["</s>"] for sent in source_tokenized] target_prefix = [[request.target_language]]
# Perform translation results = translator.translate_batch( source_tokenized, batch_type="tokens", max_batch_size=2048, beam_size=2, target_prefix=target_prefix, ) translated_tokens = results[0].hypotheses[0][1:] # Exclude target language token translated_text = sp_model.decode(translated_tokens)
return TranslationResponse( translated_text=translated_text, detected_source_language=source_lang, ) except Exception as e: print(f"An error occurred during translation: {e}") raise HTTPException(status_code=500, detail="An internal error occurred during translation.")
```
---
#### Step 3: Create and Deploy the Hugging Face Space
1. **Go to Hugging Face** and click on your profile, then "New Space".2. **Choose a name** for your Space (e.g., `sema-translation-api`).3. **Select "Docker"** as the Space SDK.4. **Choose a template** (e.g., "Blank").5. Click "Create Space".6. **Upload the files:** * Click on the "Files" tab in your new Space. * Click "Add file" -> "Upload files". * Upload the three files you created: `app.py`, `requirements.txt`, and `Dockerfile`.
The Space will automatically start building the Docker image. You can watch the progress in the "Logs" tab. It will download the models from `sematech/sema-utils` during this build process. Once it's running, you'll have a public API endpoint.
---
#### Step 4: Test the Deployed API
Once your Space is running, you can test it with a simple `curl` command or any HTTP client.
1. **Find your Space URL:** It will be something like `https://your-username-your-space-name.hf.space`.2. **Run a `curl` command** from your terminal:
```bashcurl -X POST "https://your-username-your-space-name.hf.space/translate" \-H "Content-Type: application/json" \-d '{ "text": "Habari ya asubuhi, ulimwengu", "target_language": "eng_Latn"}'```
**Expected Output:**
You should receive a JSON response like this:
```json{ "translated_text": "Good morning, world.", "detected_source_language": "swh_Latn"}```12345678fastapiuvicorn[standard]ctranslate2sentencepiecefasttext-wheelhuggingface_hubpydanticpytz123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277"""Sema Translation API - New ImplementationCreated for testing consolidated sema-utils repositoryUses HuggingFace Hub for model downloading"""
import osimport timefrom datetime import datetimeimport pytzfrom typing import Optional
from fastapi import FastAPI, HTTPException, Requestfrom fastapi.middleware.cors import CORSMiddlewarefrom pydantic import BaseModel, Fieldfrom huggingface_hub import hf_hub_download, snapshot_downloadimport ctranslate2import sentencepiece as spmimport fasttext
# --- Data Models ---class TranslationRequest(BaseModel): text: str = Field(..., example="Habari ya asubuhi", description="Text to translate") target_language: str = Field(..., example="eng_Latn", description="FLORES-200 target language code") source_language: Optional[str] = Field(None, example="swh_Latn", description="Optional FLORES-200 source language code")
class TranslationResponse(BaseModel): translated_text: str source_language: str target_language: str inference_time: float timestamp: str
# --- FastAPI App Setup ---app = FastAPI( title="Sema Translation API", description="Translation API using consolidated sema-utils models from HuggingFace", version="2.0.0")
# CORS middlewareapp.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=False, allow_methods=["*"], allow_headers=["*"],)
# --- Global Variables ---REPO_ID = "sematech/sema-utils"MODELS_DIR = "hf_models"beam_size = 1device = "cpu"
# Model instances (will be loaded on startup)lang_model = Nonesp_model = Nonetranslator = None
def get_nairobi_time(): """Get current time in Nairobi timezone""" nairobi_timezone = pytz.timezone('Africa/Nairobi') current_time_nairobi = datetime.now(nairobi_timezone)
curr_day = current_time_nairobi.strftime('%A') curr_date = current_time_nairobi.strftime('%Y-%m-%d') curr_time = current_time_nairobi.strftime('%H:%M:%S')
full_date = f"{curr_day} | {curr_date} | {curr_time}" return full_date, curr_time
def download_models(): """Download models from HuggingFace Hub""" print("π Downloading models from sematech/sema-utils...")
# Ensure models directory exists os.makedirs(MODELS_DIR, exist_ok=True)
try: # Download individual files from root print("π₯ Downloading SentencePiece model...") spm_path = hf_hub_download( repo_id=REPO_ID, filename="spm.model", local_dir=MODELS_DIR )
print("π₯ Downloading language detection model...") ft_path = hf_hub_download( repo_id=REPO_ID, filename="lid218e.bin", local_dir=MODELS_DIR )
# Download translation model (3.3B) from subfolder print("π₯ Downloading translation model (3.3B)...") ct_model_path = snapshot_download( repo_id=REPO_ID, allow_patterns="translation_models/sematrans-3.3B/*", local_dir=MODELS_DIR )
# Construct paths ct_model_full_path = os.path.join(MODELS_DIR, "translation_models", "sematrans-3.3B")
return spm_path, ft_path, ct_model_full_path
except Exception as e: print(f"β Error downloading models: {e}") raise e
def load_models(): """Load all models into memory""" global lang_model, sp_model, translator
print("π Loading models into memory...")
# Download models first spm_path, ft_path, ct_model_path = download_models()
# Suppress fasttext warnings fasttext.FastText.eprint = lambda x: None
try: # Load language detection model print("1οΈβ£ Loading language detection model...") lang_model = fasttext.load_model(ft_path)
# Load SentencePiece model print("2οΈβ£ Loading SentencePiece model...") sp_model = spm.SentencePieceProcessor() sp_model.load(spm_path)
# Load translation model print("3οΈβ£ Loading translation model...") translator = ctranslate2.Translator(ct_model_path, device)
print("β
All models loaded successfully!")
except Exception as e: print(f"β Error loading models: {e}") raise e
def translate_with_detection(text: str, target_lang: str): """Translate text with automatic source language detection""" start_time = time.time()
# Prepare input source_sents = [text.strip()] target_prefix = [[target_lang]]
# Detect source language predictions = lang_model.predict(text.replace('\n', ' '), k=1) source_lang = predictions[0][0].replace('__label__', '')
# Tokenize source text source_sents_subworded = sp_model.encode(source_sents, out_type=str) source_sents_subworded = [[source_lang] + sent + ["</s>"] for sent in source_sents_subworded]
# Translate translations = translator.translate_batch( source_sents_subworded, batch_type="tokens", max_batch_size=2048, beam_size=beam_size, target_prefix=target_prefix, )
# Decode translation translations = [translation[0]['tokens'] for translation in translations] translations_desubword = sp_model.decode(translations) translated_text = translations_desubword[0][len(target_lang):]
inference_time = time.time() - start_time
return source_lang, translated_text, inference_time
def translate_with_source(text: str, source_lang: str, target_lang: str): """Translate text with provided source language""" start_time = time.time()
# Prepare input source_sents = [text.strip()] target_prefix = [[target_lang]]
# Tokenize source text source_sents_subworded = sp_model.encode(source_sents, out_type=str) source_sents_subworded = [[source_lang] + sent + ["</s>"] for sent in source_sents_subworded]
# Translate translations = translator.translate_batch( source_sents_subworded, batch_type="tokens", max_batch_size=2048, beam_size=beam_size, target_prefix=target_prefix )
# Decode translation translations = [translation[0]['tokens'] for translation in translations] translations_desubword = sp_model.decode(translations) translated_text = translations_desubword[0][len(target_lang):]
inference_time = time.time() - start_time
return translated_text, inference_time
# --- API Endpoints ---
@app.get("/")async def root(): """Health check endpoint""" return { "status": "ok", "message": "Sema Translation API is running", "version": "2.0.0", "models_loaded": all([lang_model, sp_model, translator]) }
@app.post("/translate", response_model=TranslationResponse)async def translate_endpoint(request: TranslationRequest): """ Main translation endpoint. Automatically detects source language if not provided. """ if not request.text.strip(): raise HTTPException(status_code=400, detail="Input text cannot be empty")
full_date, current_time = get_nairobi_time() print(f"\nπ Request: {full_date}") print(f"Target: {request.target_language}, Text: {request.text[:50]}...")
try: if request.source_language: # Use provided source language translated_text, inference_time = translate_with_source( request.text, request.source_language, request.target_language ) source_lang = request.source_language else: # Auto-detect source language source_lang, translated_text, inference_time = translate_with_detection( request.text, request.target_language )
_, response_time = get_nairobi_time() print(f"β
Response: {response_time}") print(f"Source: {source_lang}, Translation: {translated_text[:50]}...\n")
return TranslationResponse( translated_text=translated_text, source_language=source_lang, target_language=request.target_language, inference_time=inference_time, timestamp=full_date )
except Exception as e: print(f"β Translation error: {e}") raise HTTPException(status_code=500, detail=f"Translation failed: {str(e)}")
# --- Startup Event ---@app.on_event("startup")async def startup_event(): """Load models when the application starts""" print("\nπ΅ Starting Sema Translation API...") print("πΌ Loading the Orchestra... π¦") load_models() print("π API started successfully!\n")
if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201"""Test client for the Sema Translation API"""
import requestsimport jsonimport time
def test_api_endpoint(base_url="http://localhost:8000"): """Test the translation API endpoints""" print("π§ͺ Testing Sema Translation API\n") # Test 1: Health check print("1οΈβ£ Testing health check endpoint...") try: response = requests.get(f"{base_url}/") if response.status_code == 200: data = response.json() print(f"β
Health check passed: {data}") else: print(f"β Health check failed: {response.status_code}") return False except Exception as e: print(f"β Health check error: {e}") return False # Test 2: Translation with auto-detection print("\n2οΈβ£ Testing translation with auto-detection...") test_data = { "text": "Habari ya asubuhi, ulimwengu", "target_language": "eng_Latn" } try: response = requests.post( f"{base_url}/translate", headers={"Content-Type": "application/json"}, data=json.dumps(test_data) ) if response.status_code == 200: data = response.json() print(f"β
Auto-detection translation successful:") print(f" π Original: {test_data['text']}") print(f" π Detected source: {data['source_language']}") print(f" π― Target: {data['target_language']}") print(f" β¨ Translation: {data['translated_text']}") print(f" β±οΈ Inference time: {data['inference_time']:.3f}s") else: print(f"β Auto-detection translation failed: {response.status_code}") print(f" Error: {response.text}") return False except Exception as e: print(f"β Auto-detection translation error: {e}") return False # Test 3: Translation with specified source language print("\n3οΈβ£ Testing translation with specified source language...") test_data_with_source = { "text": "WΔ© mwega?", "source_language": "kik_Latn", "target_language": "eng_Latn" } try: response = requests.post( f"{base_url}/translate", headers={"Content-Type": "application/json"}, data=json.dumps(test_data_with_source) ) if response.status_code == 200: data = response.json() print(f"β
Specified source translation successful:") print(f" π Original: {test_data_with_source['text']}") print(f" π Source: {data['source_language']}") print(f" π― Target: {data['target_language']}") print(f" β¨ Translation: {data['translated_text']}") print(f" β±οΈ Inference time: {data['inference_time']:.3f}s") else: print(f"β Specified source translation failed: {response.status_code}") print(f" Error: {response.text}") return False except Exception as e: print(f"β Specified source translation error: {e}") return False # Test 4: Error handling - empty text print("\n4οΈβ£ Testing error handling (empty text)...") test_data_empty = { "text": "", "target_language": "eng_Latn" } try: response = requests.post( f"{base_url}/translate", headers={"Content-Type": "application/json"}, data=json.dumps(test_data_empty) ) if response.status_code == 400: print("β
Empty text error handling works correctly") else: print(f"β Empty text error handling failed: {response.status_code}") return False except Exception as e: print(f"β Empty text error handling error: {e}") return False # Test 5: Multiple translations for performance print("\n5οΈβ£ Testing multiple translations for performance...") test_texts = [ {"text": "Jambo", "target_language": "eng_Latn"}, {"text": "Asante sana", "target_language": "eng_Latn"}, {"text": "Karibu", "target_language": "eng_Latn"}, {"text": "Pole sana", "target_language": "eng_Latn"}, {"text": "Tutaonana", "target_language": "eng_Latn"} ] total_time = 0 successful_translations = 0 for i, test_data in enumerate(test_texts, 1): try: start_time = time.time() response = requests.post( f"{base_url}/translate", headers={"Content-Type": "application/json"}, data=json.dumps(test_data) ) end_time = time.time() if response.status_code == 200: data = response.json() request_time = end_time - start_time total_time += request_time successful_translations += 1 print(f" {i}. '{test_data['text']}' β '{data['translated_text']}' " f"({request_time:.3f}s)") else: print(f" {i}. Failed: {response.status_code}") except Exception as e: print(f" {i}. Error: {e}") if successful_translations > 0: avg_time = total_time / successful_translations print(f"\nπ Performance Summary:") print(f" β
Successful translations: {successful_translations}/{len(test_texts)}") print(f" β±οΈ Average request time: {avg_time:.3f}s") print(f" π Total time: {total_time:.3f}s") return True
def test_api_documentation(base_url="http://localhost:8000"): """Test API documentation endpoints""" print("\nπ Testing API documentation...") # Test OpenAPI docs try: response = requests.get(f"{base_url}/docs") if response.status_code == 200: print("β
OpenAPI docs accessible at /docs") else: print(f"β OpenAPI docs failed: {response.status_code}") except Exception as e: print(f"β OpenAPI docs error: {e}") # Test OpenAPI JSON try: response = requests.get(f"{base_url}/openapi.json") if response.status_code == 200: print("β
OpenAPI JSON accessible at /openapi.json") else: print(f"β OpenAPI JSON failed: {response.status_code}") except Exception as e: print(f"β OpenAPI JSON error: {e}")
if __name__ == "__main__": import sys # Allow custom base URL base_url = "http://localhost:8000" if len(sys.argv) > 1: base_url = sys.argv[1] print(f"π― Testing API at: {base_url}") print("β οΈ Make sure the API server is running before running this test!\n") # Run tests success = test_api_endpoint(base_url) test_api_documentation(base_url) if success: print("\nπ All API tests passed!") else: print("\nβ Some API tests failed!") sys.exit(1)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198"""Test script to verify model downloading and loading from sema-utils repository"""
import osimport sysfrom huggingface_hub import hf_hub_download, snapshot_downloadimport ctranslate2import sentencepiece as spmimport fasttext
def test_model_download(): """Test downloading models from sematech/sema-utils""" REPO_ID = "sematech/sema-utils" MODELS_DIR = "test_models" print("π§ͺ Testing model download from sematech/sema-utils...") # Create test directory os.makedirs(MODELS_DIR, exist_ok=True) try: # Test 1: Download SentencePiece model print("\n1οΈβ£ Testing SentencePiece model download...") smp_path = hf_hub_download( repo_id=REPO_ID, filename="spm.model", local_dir=MODELS_DIR ) print(f"β
SentencePiece model downloaded to: {smp_path}") # Test 2: Download language detection model print("\n2οΈβ£ Testing language detection model download...") ft_path = hf_hub_download( repo_id=REPO_ID, filename="lid218e.bin", local_dir=MODELS_DIR ) print(f"β
Language detection model downloaded to: {ft_path}") # Test 3: Download translation model print("\n3οΈβ£ Testing translation model download...") ct_model_path = snapshot_download( repo_id=REPO_ID, allow_patterns="translation_models/sematrans-3.3B/*", local_dir=MODELS_DIR ) print(f"β
Translation model downloaded to: {ct_model_path}") # Verify file structure ct_model_full_path = os.path.join(MODELS_DIR, "translation_models", "sematrans-3.3B") print(f"\nπ Translation model directory: {ct_model_full_path}") if os.path.exists(ct_model_full_path): files = os.listdir(ct_model_full_path) print(f"π Files in translation model directory: {files}") else: print("β Translation model directory not found!") return False return smp_path, ft_path, ct_model_full_path except Exception as e: print(f"β Error during download: {e}") return False
def test_model_loading(smp_path, ft_path, ct_model_path): """Test loading the downloaded models""" print("\nπ Testing model loading...") try: # Suppress fasttext warnings fasttext.FastText.eprint = lambda x: None # Test 1: Load language detection model print("\n1οΈβ£ Testing language detection model loading...") lang_model = fasttext.load_model(ft_path) print("β
Language detection model loaded successfully") # Test language detection test_text = "Habari ya asubuhi" predictions = lang_model.predict(test_text, k=1) detected_lang = predictions[0][0].replace('__label__', '') print(f"π Detected language for '{test_text}': {detected_lang}") # Test 2: Load SentencePiece model print("\n2οΈβ£ Testing SentencePiece model loading...") sp_model = spm.SentencePieceProcessor() sp_model.load(smp_path) print("β
SentencePiece model loaded successfully") # Test tokenization tokens = sp_model.encode(test_text, out_type=str) print(f"π€ Tokenized '{test_text}': {tokens}") # Test 3: Load translation model print("\n3οΈβ£ Testing translation model loading...") translator = ctranslate2.Translator(ct_model_path, device="cpu") print("β
Translation model loaded successfully") return lang_model, sp_model, translator except Exception as e: print(f"β Error during model loading: {e}") return False
def test_translation(lang_model, sp_model, translator): """Test the complete translation pipeline""" print("\nπ Testing complete translation pipeline...") test_text = "Habari ya asubuhi, ulimwengu" target_lang = "eng_Latn" try: # Step 1: Detect source language predictions = lang_model.predict(test_text.replace('\n', ' '), k=1) source_lang = predictions[0][0].replace('__label__', '') print(f"π Detected source language: {source_lang}") # Step 2: Tokenize source_sents = [test_text.strip()] source_sents_subworded = sp_model.encode(source_sents, out_type=str) source_sents_subworded = [[source_lang] + sent + ["</s>"] for sent in source_sents_subworded] print(f"π€ Tokenized input: {source_sents_subworded[0][:10]}...") # Step 3: Translate target_prefix = [[target_lang]] translations = translator.translate_batch( source_sents_subworded, batch_type="tokens", max_batch_size=2048, beam_size=1, target_prefix=target_prefix, ) # Step 4: Decode translations = [translation[0]['tokens'] for translation in translations] translations_desubword = sp_model.decode(translations) translated_text = translations_desubword[0][len(target_lang):] print(f"\nπ Translation successful!") print(f"π Original: {test_text}") print(f"π Source language: {source_lang}") print(f"π― Target language: {target_lang}") print(f"β¨ Translation: {translated_text}") return True except Exception as e: print(f"β Error during translation: {e}") return False
def cleanup_test_files(): """Clean up test files""" import shutil test_dir = "test_models" if os.path.exists(test_dir): print(f"\nπ§Ή Cleaning up test directory: {test_dir}") shutil.rmtree(test_dir) print("β
Cleanup complete")
if __name__ == "__main__": print("π Starting Sema Utils Model Test\n") # Test model download download_result = test_model_download() if not download_result: print("β Model download test failed!") sys.exit(1) smp_path, ft_path, ct_model_path = download_result # Test model loading loading_result = test_model_loading(smp_path, ft_path, ct_model_path) if not loading_result: print("β Model loading test failed!") sys.exit(1) lang_model, sp_model, translator = loading_result # Test translation translation_result = test_translation(lang_model, sp_model, translator) if not translation_result: print("β Translation test failed!") sys.exit(1) print("\nπ All tests passed! Your sema-utils repository is working correctly.") # Ask user if they want to clean up response = input("\nπ§Ή Do you want to clean up test files? (y/n): ") if response.lower() in ['y', 'yes']: cleanup_test_files() print("\nβ
Test complete!")DockerfileAdded+26
README.md+96-6
deploy_to_hf.mdAdded+138
docs/app.pyAdded+248
docs/current-state.mdAdded+255
requirements.txtAdded+8
sema_translation_api.pyAdded+277
test_api_client.pyAdded+201
test_model_download.pyAdded+198