Kavin1701 commited on
Commit
5541632
·
verified ·
1 Parent(s): 3de8e0a

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +14 -0
  2. fastapi_app.py +49 -0
  3. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ WORKDIR /python-docker
4
+
5
+ COPY requirements.txt requirements.txt
6
+ RUN apt-get update && apt-get install git -y
7
+ RUN pip3 install -r requirements.txt
8
+ RUN apt-get update && apt-get install -y ffmpeg
9
+ RUN python -c "from transformers import pipeline; pipeline('automatic-speech-recognition', model='openai/whisper-small')"
10
+ COPY . .
11
+
12
+ EXPOSE 8001
13
+
14
+ CMD ["uvicorn", "fastapi_app:app", "--host", "0.0.0.0", "--port", "8001"]
fastapi_app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, File, UploadFile, HTTPException
2
+ from fastapi.responses import JSONResponse, RedirectResponse
3
+ from tempfile import NamedTemporaryFile
4
+ from typing import List
5
+ from transformers import pipeline
6
+
7
+ # Specify the model name
8
+ model_name = "openai/whisper-small"
9
+
10
+ whisper_asr = pipeline(
11
+ "automatic-speech-recognition",
12
+ model = model_name,
13
+ chunk_length_s = 30,
14
+ device = "cpu"
15
+ )
16
+
17
+
18
+ app = FastAPI()
19
+
20
+ @app.post("/whisper/")
21
+ async def handler(files: List[UploadFile] = File(...)):
22
+ if not files:
23
+ raise HTTPException(status_code=400, detail="No files were provided")
24
+
25
+ # For each file, let's store the results in a list of dictionaries.
26
+ results = []
27
+
28
+ for file in files:
29
+ # Create a temporary file.
30
+ with NamedTemporaryFile(delete=True) as temp:
31
+ # Write the user's uploaded file to the temporary file.
32
+ with open(temp.name, "wb") as temp_file:
33
+ temp_file.write(file.file.read())
34
+
35
+ # Let's get the transcript of the temporary file.
36
+ result = whisper_asr(temp.name, return_timestamps=False)
37
+
38
+ # Now we can store the result object for this file.
39
+ results.append({
40
+ 'filename': file.filename,
41
+ 'transcript': result['text'],
42
+ })
43
+
44
+ return JSONResponse(content={'results': results})
45
+
46
+
47
+ @app.get("/", response_class=RedirectResponse)
48
+ async def redirect_to_docs():
49
+ return "/docs"
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ fastapi
2
+ aiofiles
3
+ python-multipart
4
+ uvicorn
5
+ transformers
6
+ torch