Spaces:
Runtime error
Runtime error
Merge branch 'main' of https://huggingface.co/spaces/EntrepreneurFirst/team3
Browse files- Dockerfile +1 -1
- app/main.py +49 -1
Dockerfile
CHANGED
@@ -4,7 +4,7 @@ FROM nvidia/cuda:12.4.1-runtime-ubuntu22.04
|
|
4 |
USER root
|
5 |
|
6 |
# Install python and pip
|
7 |
-
RUN apt-get update && apt-get install -y python3 python3-pip
|
8 |
&& apt-get install -y git
|
9 |
|
10 |
# The commands as another prerequisite
|
|
|
4 |
USER root
|
5 |
|
6 |
# Install python and pip
|
7 |
+
RUN apt-get update && apt-get install -y python3 python3-pip \
|
8 |
&& apt-get install -y git
|
9 |
|
10 |
# The commands as another prerequisite
|
app/main.py
CHANGED
@@ -1,7 +1,55 @@
|
|
1 |
from fastapi import FastAPI
|
|
|
|
|
2 |
|
3 |
app = FastAPI()
|
4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
@app.get("/")
|
6 |
def root():
|
7 |
-
return {"Hello": "World"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI
|
2 |
+
import torch
|
3 |
+
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
4 |
|
5 |
app = FastAPI()
|
6 |
|
7 |
+
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
8 |
+
torch_dtype = torch.float16 if torch.cuda.is_available() else torch.float32
|
9 |
+
|
10 |
+
model_id = "openai/whisper-large-v3"
|
11 |
+
|
12 |
+
model = AutoModelForSpeechSeq2Seq.from_pretrained(
|
13 |
+
model_id, torch_dtype=torch_dtype, low_cpu_mem_usage=True, use_safetensors=True
|
14 |
+
)
|
15 |
+
model.to(device)
|
16 |
+
|
17 |
+
processor = AutoProcessor.from_pretrained(model_id)
|
18 |
+
|
19 |
+
pipe = pipeline(
|
20 |
+
"automatic-speech-recognition",
|
21 |
+
model=model,
|
22 |
+
tokenizer=processor.tokenizer,
|
23 |
+
feature_extractor=processor.feature_extractor,
|
24 |
+
max_new_tokens=128,
|
25 |
+
chunk_length_s=30,
|
26 |
+
batch_size=16,
|
27 |
+
return_timestamps=True,
|
28 |
+
torch_dtype=torch_dtype,
|
29 |
+
device=device,
|
30 |
+
)
|
31 |
+
|
32 |
+
def speech_to_text(path_to_file: str) -> str:
|
33 |
+
"""
|
34 |
+
Given the path to the .wav file, it returns the transcription.
|
35 |
+
"""
|
36 |
+
result = pipe(path_to_file)
|
37 |
+
|
38 |
+
return result["text"]
|
39 |
+
|
40 |
+
|
41 |
@app.get("/")
|
42 |
def root():
|
43 |
+
return {"Hello": "World"}
|
44 |
+
|
45 |
+
@app.post("/upload/")
|
46 |
+
async def create_upload_file(file: UploadFile = File(...)):
|
47 |
+
|
48 |
+
# Process the file here
|
49 |
+
content = await file.read()
|
50 |
+
|
51 |
+
with open("tempfile.wav", "wb") as f:
|
52 |
+
f.write(contents)
|
53 |
+
response_text = speech_to_text("tempfile.wav")
|
54 |
+
|
55 |
+
return PlainTextResponse(content=response_text, status_code=200)
|