Spaces:
Sleeping
Sleeping
Put basic transcription in app.py
#2
by
Petita
- opened
app.py
CHANGED
@@ -1,7 +1,43 @@
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from whisper_online import *
|
3 |
+
import librosa
|
4 |
import gradio as gr
|
5 |
|
6 |
+
@lru_cache
|
7 |
+
def load_audio(fname):
|
8 |
+
a, _ = librosa.load(fname, sr=16000, dtype=np.float32)
|
9 |
+
return a
|
10 |
|
11 |
+
|
12 |
+
asr = FasterWhisperASR("en", "large-v2") # loads and wraps Whisper model
|
13 |
+
online = OnlineASRProcessor(asr) # create processing object with default buffer trimming option
|
14 |
+
|
15 |
+
def transcribe(audio_file)
|
16 |
+
try:
|
17 |
+
audio_data = load_audio(audio_file)
|
18 |
+
|
19 |
+
# Insert audio chunk into the ASR processor
|
20 |
+
online.insert_audio_chunk(audio_data)
|
21 |
+
|
22 |
+
# Process the audio chunk
|
23 |
+
o = online.process_iter()
|
24 |
+
|
25 |
+
# Send current partial output
|
26 |
+
return o
|
27 |
+
|
28 |
+
except Exception as e:
|
29 |
+
return str(e)
|
30 |
+
|
31 |
+
def finish_stream():
|
32 |
+
try:
|
33 |
+
# Finish audio processing
|
34 |
+
o = online.finish()
|
35 |
+
|
36 |
+
# Send last output
|
37 |
+
return o
|
38 |
+
|
39 |
+
except Exception as e:
|
40 |
+
return str(e)
|
41 |
+
|
42 |
+
iface = gr.Interface(fn=transcribe,inputs = "text", outputs = "text")
|
43 |
+
iface.launch()
|