Spaces:
Sleeping
Sleeping
Commit
·
03485f2
1
Parent(s):
6d38cdf
Add task
Browse files
app.py
CHANGED
|
@@ -4,11 +4,14 @@ import numpy as np
|
|
| 4 |
from ipywebrtc import AudioRecorder, CameraStream, AudioStream
|
| 5 |
from tempfile import NamedTemporaryFile
|
| 6 |
from pywhispercpp.model import Model
|
|
|
|
| 7 |
|
| 8 |
-
whisper_models = [
|
| 9 |
whisper_model = solara.reactive("tiny.en-q8_0")
|
|
|
|
| 10 |
transcription = solara.reactive("")
|
| 11 |
generation_time = solara.reactive("")
|
|
|
|
| 12 |
@solara.component
|
| 13 |
def Page():
|
| 14 |
with solara.Sidebar():
|
|
@@ -19,29 +22,39 @@ def Page():
|
|
| 19 |
solara.Markdown(f"#{title}")
|
| 20 |
solara.Markdown("## Send a voice message")
|
| 21 |
solara.Markdown("### Recorder")
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
camera = CameraStream(constraints={'audio': True,'video':False})
|
| 24 |
recorder = AudioRecorder(stream=camera)
|
|
|
|
| 25 |
display(recorder)
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
with
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
if generation_time.value != "":
|
| 47 |
-
solara.Text(f"Generation time: {generation_time.value} seconds", style="color: blue; position: fixed; bottom: 8rem")
|
|
|
|
| 4 |
from ipywebrtc import AudioRecorder, CameraStream, AudioStream
|
| 5 |
from tempfile import NamedTemporaryFile
|
| 6 |
from pywhispercpp.model import Model
|
| 7 |
+
from solara.lab import use_task, Task
|
| 8 |
|
| 9 |
+
whisper_models = ["tiny.en-q5_1", "tiny.en-q8_0","tiny.en", "base.en-q5_1", "base.en", "small.en-q5_1", "small.en"]
|
| 10 |
whisper_model = solara.reactive("tiny.en-q8_0")
|
| 11 |
+
current_whisper_model = solara.reactive("tiny.en-q8_0")
|
| 12 |
transcription = solara.reactive("")
|
| 13 |
generation_time = solara.reactive("")
|
| 14 |
+
w = Model('tiny.en-q8_0')
|
| 15 |
@solara.component
|
| 16 |
def Page():
|
| 17 |
with solara.Sidebar():
|
|
|
|
| 22 |
solara.Markdown(f"#{title}")
|
| 23 |
solara.Markdown("## Send a voice message")
|
| 24 |
solara.Markdown("### Recorder")
|
| 25 |
+
with solara.Row():
|
| 26 |
+
def load_model():
|
| 27 |
+
w = Model(whisper_model.value)
|
| 28 |
+
current_whisper_model.value = whisper_model.value
|
| 29 |
+
return 1
|
| 30 |
+
solara.Select(label="Select model:", value=whisper_model, values=whisper_models, style="width: 10%")
|
| 31 |
+
#solara.Button("Load model", on_click=load_model)
|
| 32 |
+
result : Task[int] = use_task(load_model, dependencies=[whisper_model.value])
|
| 33 |
+
if result.finished:
|
| 34 |
+
solara.Success(f"Current model: {current_whisper_model.value}")
|
| 35 |
+
else:
|
| 36 |
+
solara.ProgressLinear(result.pending)
|
| 37 |
camera = CameraStream(constraints={'audio': True,'video':False})
|
| 38 |
recorder = AudioRecorder(stream=camera)
|
| 39 |
+
recorder.playing = False
|
| 40 |
display(recorder)
|
| 41 |
+
def transcribe_voice():
|
| 42 |
+
transcription.value = ""
|
| 43 |
+
generation_time.value = ""
|
| 44 |
+
with NamedTemporaryFile(suffix=".webm") as temp:
|
| 45 |
+
with open(f"{temp.name}", 'wb') as f:
|
| 46 |
+
f.write(recorder.audio.value)
|
| 47 |
+
start_time = time.time()
|
| 48 |
+
segments = w.transcribe(f"{temp.name}")
|
| 49 |
+
for segment in segments:
|
| 50 |
+
transcription.value += segment.text
|
| 51 |
+
end_time = time.time()
|
| 52 |
+
generation_time.value = np.round(end_time - start_time, 2)
|
| 53 |
+
transcription.value += " "
|
| 54 |
+
with solara.Row():
|
| 55 |
+
solara.Button("Send voice message", on_click=transcribe_voice)
|
| 56 |
+
with solara.Column(style="padding: 50px"):
|
| 57 |
+
solara.Markdown(f"### Transcription:")
|
| 58 |
+
solara.Text(f"{transcription.value}", style="color: blue; font-size: 1.5rem")
|
| 59 |
+
if generation_time.value != "":
|
| 60 |
+
solara.Text(f"Generation time: {generation_time.value} seconds", style="color: blue; position: fixed; bottom: 8rem")
|
|
|
|
|
|