Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,7 +1,44 @@
|
|
1 |
-
from fastapi import FastAPI
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
app= FastAPI()
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, WebSocket
|
2 |
+
from transformers import pipeline
|
3 |
+
import torchaudio.transforms as T
|
4 |
+
import soundfile as sf
|
5 |
+
import io
|
6 |
|
7 |
+
app = FastAPI()
|
8 |
|
9 |
+
pipe = pipeline(model="AsemBadr/whisper-small-final-v3", task="automatic-speech-recognition")
|
10 |
+
TARGET_SAMPLE_RATE = 16000
|
11 |
+
|
12 |
+
@app.get("/hi")
|
13 |
+
async def root():
|
14 |
+
return {"message": "Eid Mubarek"}
|
15 |
+
|
16 |
+
def preprocess_audio(audio_data: bytes):
|
17 |
+
audio_file = io.BytesIO(audio_data)
|
18 |
+
audio, sample_rate = sf.read(audio_file, dtype='float32')
|
19 |
+
|
20 |
+
if sample_rate != TARGET_SAMPLE_RATE:
|
21 |
+
resampler = T.Resample(sample_rate, TARGET_SAMPLE_RATE)
|
22 |
+
audio = resampler(audio)
|
23 |
+
|
24 |
+
return audio
|
25 |
+
|
26 |
+
def transcribe_audio(waveform):
|
27 |
+
result = pipe(waveform)
|
28 |
+
return result['text']
|
29 |
+
|
30 |
+
@app.websocket("/transcribe")
|
31 |
+
async def transcribe(websocket: WebSocket):
|
32 |
+
await websocket.accept()
|
33 |
+
while True:
|
34 |
+
data = await websocket.receive_bytes()
|
35 |
+
if data:
|
36 |
+
waveform = preprocess_audio(data)
|
37 |
+
transcription = transcribe_audio(waveform)
|
38 |
+
await websocket.send_text(transcription)
|
39 |
+
else:
|
40 |
+
break
|
41 |
+
|
42 |
+
if __name__ == "__main__":
|
43 |
+
import uvicorn
|
44 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|