Spaces:
Sleeping
Sleeping
Hugo Rodrigues commited on
Commit ·
ac9e33e
1
Parent(s): 33fb059
translate model facebook/seamless-m4t-v2-large
Browse files- .gitignore +22 -0
- Dockerfile +45 -0
- README.md +16 -0
- compose.yaml +54 -0
- main.py +65 -0
- packages.txt +1 -0
- requirements.txt +10 -0
.gitignore
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Build Artifacts
|
| 2 |
+
build/
|
| 3 |
+
|
| 4 |
+
# Core Dumps
|
| 5 |
+
core
|
| 6 |
+
|
| 7 |
+
# Byte-Compiled Modules
|
| 8 |
+
__pycache__/
|
| 9 |
+
|
| 10 |
+
# Extension Modules
|
| 11 |
+
*.so
|
| 12 |
+
|
| 13 |
+
# Packaging Artifacts
|
| 14 |
+
*.egg-info
|
| 15 |
+
*.whl
|
| 16 |
+
|
| 17 |
+
# IDEs and Tools
|
| 18 |
+
.idea/
|
| 19 |
+
.gdb_history
|
| 20 |
+
.vscode/
|
| 21 |
+
# Other
|
| 22 |
+
.DS_Store
|
Dockerfile
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM nvidia/cuda:12.1.0-cudnn8-devel-ubuntu20.04
|
| 2 |
+
LABEL maintainer="Hugging Face"
|
| 3 |
+
|
| 4 |
+
ARG DEBIAN_FRONTEND=noninteractive
|
| 5 |
+
|
| 6 |
+
RUN apt update
|
| 7 |
+
RUN apt install -y git libsndfile1-dev tesseract-ocr espeak-ng python3 python3-pip ffmpeg
|
| 8 |
+
RUN python3 -m pip install --no-cache-dir --upgrade pip
|
| 9 |
+
# RUN apt-get install -y git libsndfile1-dev tesseract-ocr espeak-ng ffmpeg
|
| 10 |
+
|
| 11 |
+
# Prevents Python from writing pyc files.
|
| 12 |
+
ENV PYTHONDONTWRITEBYTECODE=1
|
| 13 |
+
# Open MP threads. It may need to change in production env.
|
| 14 |
+
ENV OMP_NUM_THREADS=1
|
| 15 |
+
|
| 16 |
+
# Keeps Python from buffering stdout and stderr to avoid situations where
|
| 17 |
+
# the application crashes without emitting any logs due to buffering.
|
| 18 |
+
ENV PYTHONUNBUFFERED=1
|
| 19 |
+
|
| 20 |
+
ENV HF_HUB_CACHE="/hub"
|
| 21 |
+
|
| 22 |
+
WORKDIR /app
|
| 23 |
+
|
| 24 |
+
# Create a non-privileged user that the app will run under.
|
| 25 |
+
# See https://docs.docker.com/go/dockerfile-user-best-practices/
|
| 26 |
+
RUN useradd -m -u 1000 user
|
| 27 |
+
# Download dependencies as a separate step to take advantage of Docker's caching.
|
| 28 |
+
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
|
| 29 |
+
# Leverage a bind mount to requirements.txt to avoid having to copy them into
|
| 30 |
+
# into this layer.
|
| 31 |
+
RUN --mount=type=cache,target=/root/.cache/pip \
|
| 32 |
+
--mount=type=bind,source=requirements.txt,target=requirements.txt \
|
| 33 |
+
python3 -m pip install -r requirements.txt
|
| 34 |
+
|
| 35 |
+
# Switch to the non-privileged user to run the application.
|
| 36 |
+
USER user
|
| 37 |
+
|
| 38 |
+
# Copy the source code into the container.
|
| 39 |
+
COPY . .
|
| 40 |
+
|
| 41 |
+
# Expose the port that the application listens on.
|
| 42 |
+
EXPOSE 8088
|
| 43 |
+
|
| 44 |
+
# Run the application.
|
| 45 |
+
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -9,3 +9,19 @@ license: cc-by-4.0
|
|
| 9 |
---
|
| 10 |
|
| 11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
---
|
| 10 |
|
| 11 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 12 |
+
|
| 13 |
+
# Translate
|
| 14 |
+
|
| 15 |
+
## Usage
|
| 16 |
+
|
| 17 |
+
```
|
| 18 |
+
conda activate hf
|
| 19 |
+
```
|
| 20 |
+
|
| 21 |
+
VS Code Python select interpreter hf
|
| 22 |
+
|
| 23 |
+
## Composa
|
| 24 |
+
|
| 25 |
+
```
|
| 26 |
+
docker compose up --build
|
| 27 |
+
```
|
compose.yaml
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Comments are provided throughout this file to help you get started.
|
| 2 |
+
# If you need more help, visit the Docker compose reference guide at
|
| 3 |
+
# https://docs.docker.com/go/compose-spec-reference/
|
| 4 |
+
|
| 5 |
+
# Here the instructions define your application as a service called "server".
|
| 6 |
+
# This service is built from the Dockerfile in the current directory.
|
| 7 |
+
# You can add other services your application may depend on here, such as a
|
| 8 |
+
# database or a cache. For examples, see the Awesome Compose repository:
|
| 9 |
+
# https://github.com/docker/awesome-compose
|
| 10 |
+
services:
|
| 11 |
+
server:
|
| 12 |
+
build:
|
| 13 |
+
context: .
|
| 14 |
+
ports:
|
| 15 |
+
- 8089:7860
|
| 16 |
+
volumes:
|
| 17 |
+
# - ${PWD}:/app:rw
|
| 18 |
+
- /Users/hugorodrigues/.cache/huggingface/hub:/hub:rw
|
| 19 |
+
environment:
|
| 20 |
+
- HF_HUB_CACHE=/hub
|
| 21 |
+
|
| 22 |
+
# The commented out section below is an example of how to define a PostgreSQL
|
| 23 |
+
# database that your application can use. `depends_on` tells Docker Compose to
|
| 24 |
+
# start the database before your application. The `db-data` volume persists the
|
| 25 |
+
# database data between container restarts. The `db-password` secret is used
|
| 26 |
+
# to set the database password. You must create `db/password.txt` and add
|
| 27 |
+
# a password of your choosing to it before running `docker compose up`.
|
| 28 |
+
# depends_on:
|
| 29 |
+
# db:
|
| 30 |
+
# condition: service_healthy
|
| 31 |
+
# db:
|
| 32 |
+
# image: postgres
|
| 33 |
+
# restart: always
|
| 34 |
+
# user: postgres
|
| 35 |
+
# secrets:
|
| 36 |
+
# - db-password
|
| 37 |
+
# volumes:
|
| 38 |
+
# - db-data:/var/lib/postgresql/data
|
| 39 |
+
# environment:
|
| 40 |
+
# - POSTGRES_DB=example
|
| 41 |
+
# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password
|
| 42 |
+
# expose:
|
| 43 |
+
# - 5432
|
| 44 |
+
# healthcheck:
|
| 45 |
+
# test: [ "CMD", "pg_isready" ]
|
| 46 |
+
# interval: 10s
|
| 47 |
+
# timeout: 5s
|
| 48 |
+
# retries: 5
|
| 49 |
+
# volumes:
|
| 50 |
+
# db-data:
|
| 51 |
+
# secrets:
|
| 52 |
+
# db-password:
|
| 53 |
+
# file: db/password.txt
|
| 54 |
+
|
main.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
|
| 3 |
+
# from typing import Union
|
| 4 |
+
# from pydantic import BaseModel
|
| 5 |
+
from fastapi import FastAPI
|
| 6 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 7 |
+
|
| 8 |
+
# from fastapi.staticfiles import StaticFiles
|
| 9 |
+
# from fastapi.responses import FileResponse
|
| 10 |
+
|
| 11 |
+
import torch
|
| 12 |
+
# from transformers import pipeline
|
| 13 |
+
|
| 14 |
+
from transformers import SeamlessM4Tv2Model
|
| 15 |
+
from transformers import AutoProcessor
|
| 16 |
+
|
| 17 |
+
processor = AutoProcessor.from_pretrained("facebook/seamless-m4t-v2-large")
|
| 18 |
+
model = SeamlessM4Tv2Model.from_pretrained("facebook/seamless-m4t-v2-large")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 22 |
+
# torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 23 |
+
|
| 24 |
+
model.to(device)
|
| 25 |
+
|
| 26 |
+
app = FastAPI(docs_url="/api/docs")
|
| 27 |
+
|
| 28 |
+
app.add_middleware(
|
| 29 |
+
CORSMiddleware,
|
| 30 |
+
allow_origins=["*"],
|
| 31 |
+
allow_methods=["*"],
|
| 32 |
+
allow_headers=["*"],
|
| 33 |
+
allow_credentials=True,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
BATCH_SIZE = 8
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@app.get("/device")
|
| 40 |
+
def getDevice():
|
| 41 |
+
start_time = time.time()
|
| 42 |
+
print("Time took to process the request and return response is {} sec".format(
|
| 43 |
+
time.time() - start_time))
|
| 44 |
+
return device
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
@app.get("/translate")
|
| 48 |
+
def transcribe(inputs, src_lang="eng", tgt_lang="por"):
|
| 49 |
+
start_time = time.time()
|
| 50 |
+
|
| 51 |
+
if inputs is None:
|
| 52 |
+
raise "No audio file submitted! Please upload or record an audio file before submitting your request."
|
| 53 |
+
|
| 54 |
+
text_inputs = processor(text=inputs,
|
| 55 |
+
src_lang=src_lang, return_tensors="pt").to(device)
|
| 56 |
+
|
| 57 |
+
output_tokens = model.generate(
|
| 58 |
+
**text_inputs, tgt_lang=tgt_lang, generate_speech=False)
|
| 59 |
+
|
| 60 |
+
translated_text_from_text = processor.decode(
|
| 61 |
+
output_tokens[0].tolist()[0], skip_special_tokens=True)
|
| 62 |
+
|
| 63 |
+
print("Time took to process the request and return response is {} sec".format(
|
| 64 |
+
time.time() - start_time))
|
| 65 |
+
return translated_text_from_text
|
packages.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
ffmpeg
|
requirements.txt
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
pydantic
|
| 3 |
+
typing
|
| 4 |
+
transformers
|
| 5 |
+
python-multipart
|
| 6 |
+
sentencepiece
|
| 7 |
+
protobuf
|
| 8 |
+
torch
|
| 9 |
+
uvicorn[standard]
|
| 10 |
+
ffmpeg
|