minhpng commited on
Commit
270736b
·
verified ·
1 Parent(s): 3816bf9

Upload 7 files

Browse files
app/__init__.py ADDED
File without changes
app/libs/generate_api_key.py ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ import secrets
2
+
3
+ def generate_api_key():
4
+ print([secrets.token_urlsafe(40) for _ in range(3)])
5
+
6
+ generate_api_key()
app/libs/header_api_auth.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import Depends, HTTPException, status
2
+ from fastapi.security import APIKeyHeader
3
+
4
+ API_KEYS = ['Fo0nxlisbbKOlZfnir6_5Tv4hHyNF8sv94f7lxA9iK3ApUuT3wmGuQ', 'wsAlgiZ_Bvncop-BGlua2FqsrKj7Ex8mgDN2F-UsfJ2yQpnwHODg9Q', 'wIjqpBXazXUZmqJ8vTKF0yUXjNihX9JSEfk3-3aFQbM-DBtu2ccnVQ']
5
+
6
+ api_key_header = APIKeyHeader(name="x-api-key", auto_error=False)
7
+
8
+ def get_api_key(api_key_header: str = Depends(api_key_header)):
9
+ if api_key_header in API_KEYS:
10
+ return api_key_header
11
+ raise HTTPException(
12
+ status_code=status.HTTP_401_UNAUTHORIZED,
13
+ detail="Invalid or missing API Key",
14
+ )
15
+
app/main.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import uvicorn
2
+ from fastapi import FastAPI
3
+ from fastapi.middleware.cors import CORSMiddleware
4
+
5
+ from routers import get_transcript
6
+
7
+
8
+ app = FastAPI()
9
+
10
+ app.add_middleware(CORSMiddleware, allow_origins=["http://localhost:3000"], allow_credentials=True, allow_methods=["*"], allow_headers=["Content-Type", "Authorization", "x-api-key"])
11
+
12
+ app.include_router(get_transcript.router)
13
+
14
+ @app.get("/")
15
+ def read_root():
16
+ return {"message": "This is from python backend cache"}
17
+
18
+ if __name__ == '__main__':
19
+ uvicorn.run(
20
+ "main:app", reload=True)
21
+
app/routers/get_transcript.py ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import time
2
+ from fastapi import APIRouter, Depends, HTTPException, status
3
+ from faster_whisper import WhisperModel
4
+
5
+ from libs.header_api_auth import get_api_key
6
+
7
+ router = APIRouter(prefix="/get-transcript", tags=["transcript"])
8
+
9
+ # model_size: distil-large-v2
10
+ # model_size: distil-large-v3
11
+
12
+ @router.get("/")
13
+ def get_transcript(audio_path: str, model_size: str = "distil-large-v3", api_key: str = Depends(get_api_key)):
14
+ # Run on GPU with FP16
15
+ # model = WhisperModel(model_size, device="cuda", compute_type="float16")
16
+
17
+ # or run on GPU with INT8
18
+ # model = WhisperModel(model_size, device="cuda", cosmpute_type="int8_float16")
19
+ # or run on CPU with INT8
20
+ # model_run = WhisperModel(model_size, device="cpu", compute_type="int8")
21
+
22
+
23
+ print(f"model>>>: {model_size}")
24
+
25
+ st = time.time()
26
+
27
+
28
+ try:
29
+ model_run = WhisperModel(model_size, device="cpu", compute_type="int8")
30
+ segments, info = model_run.transcribe(
31
+ audio_path,
32
+ beam_size=16,
33
+ language="en",
34
+ condition_on_previous_text=False,
35
+ )
36
+ except Exception as error:
37
+ raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail=f"error>>>: {error}")
38
+
39
+ text = ""
40
+
41
+ for segment in segments:
42
+ text += segment.text
43
+
44
+ et = time.time()
45
+ elapsed_time = et - st
46
+ return {"text": text}
47
+
dockerfile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python 3.10.9 image
2
+ FROM python:3.12.1
3
+
4
+ # Copy the current directory contents into the container at .
5
+ COPY . .
6
+
7
+ # Set the working directory to /
8
+ WORKDIR /
9
+
10
+ # Install requirements.txt
11
+ RUN pip install --no-cache-dir --upgrade -r /requirements.txt
12
+
13
+ # Start the FastAPI app on port 7860, the default port expected by Spaces
14
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
requirements.txt ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ annotated-types==0.7.0
2
+ anyio==4.5.0
3
+ av==12.3.0
4
+ certifi==2024.8.30
5
+ charset-normalizer==3.3.2
6
+ click==8.1.7
7
+ coloredlogs==15.0.1
8
+ ctranslate2==4.4.0
9
+ fastapi==0.115.0
10
+ faster-whisper==1.0.3
11
+ filelock==3.16.1
12
+ flatbuffers==24.3.25
13
+ fsspec==2024.9.0
14
+ h11==0.14.0
15
+ huggingface-hub==0.25.1
16
+ humanfriendly==10.0
17
+ idna==3.10
18
+ mpmath==1.3.0
19
+ numpy==2.1.2
20
+ onnxruntime==1.19.2
21
+ packaging==24.1
22
+ protobuf==5.28.2
23
+ pydantic==2.9.2
24
+ pydantic_core==2.23.4
25
+ PyYAML==6.0.2
26
+ requests==2.32.3
27
+ setuptools==75.1.0
28
+ sniffio==1.3.1
29
+ starlette==0.38.5
30
+ sympy==1.13.3
31
+ tokenizers==0.20.0
32
+ tqdm==4.66.5
33
+ typing_extensions==4.12.2
34
+ urllib3==2.2.3
35
+ uvicorn==0.30.6