-
-
Notifications
You must be signed in to change notification settings - Fork 182
feat: implement automatic driver radio transcription #124
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kyujin-cho
wants to merge
17
commits into
slowlydev:develop
Choose a base branch
from
kyujin-cho:feature/radio-speech-recognition
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
18103ae
implement automatic driver radio transcription
kyujin-cho 7810853
fix indent
kyujin-cho fb92d8f
Merge branch 'develop' into feature/radio-speech-recognition
kyujin-cho 9bf0ed2
Update dash/src/components/TeamRadioMessage.tsx
kyujin-cho 6698a9c
fix lint
kyujin-cho 0f52157
remove console.log
kyujin-cho 74e9585
remove reference to webkitAudioContext
kyujin-cho d7a4f6d
import constant directly
kyujin-cho d7d4716
use headless' select API
kyujin-cho 33f9955
update response code
kyujin-cho 813a48b
Merge branch 'develop' into feature/radio-speech-recognition
kyujin-cho 6b289c8
Merge branch 'develop' into feature/radio-speech-recognition
slowlydev 60d6a62
Merge branch 'develop' of github.com:slowlydev/f1-dash into feature/r…
slowlydev 00649d8
chore: merge leftovers
slowlydev a0afa0f
fix: double clsx import
slowlydev 98ff2b3
refactor: use the transcription store
slowlydev 6dcd8a0
refactor(live): audio endpoint
slowlydev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| use axum::{extract::Query, http::StatusCode, response::IntoResponse}; | ||
| use serde::Deserialize; | ||
| use std::env; | ||
| use tracing::error; | ||
|
|
||
| #[derive(Deserialize)] | ||
| pub struct Params { | ||
| path: String, | ||
| } | ||
|
|
||
| pub async fn get_audio(Query(params): Query<Params>) -> Result<impl IntoResponse, StatusCode> { | ||
| let Ok(_) = env::var("ENABLE_AUDIO_FETCH") else { | ||
| return Err(StatusCode::NOT_IMPLEMENTED); | ||
| }; | ||
|
|
||
| let audio_url = format!("https://livetiming.formula1.com/static/{}", params.path); | ||
|
|
||
| let Ok(response) = reqwest::get(&audio_url).await else { | ||
| error!("Failed to retrieve audio data from {}", audio_url); | ||
| return Err(StatusCode::INTERNAL_SERVER_ERROR); | ||
| }; | ||
|
|
||
| let Ok(bytes) = response.bytes().await else { | ||
| error!("Failed to decode response from {}", audio_url); | ||
| return Err(StatusCode::INTERNAL_SERVER_ERROR); | ||
| }; | ||
|
|
||
| Ok(bytes.as_ref().to_vec()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* eslint-disable camelcase */ | ||
| // from https://github.com/xenova/whisper-web/blob/main/src/worker.js | ||
| import { pipeline, env } from "@xenova/transformers"; | ||
|
|
||
| // Disable local models | ||
| env.allowLocalModels = false; | ||
|
|
||
| // Define model factories | ||
| // Ensures only one model is created of each type | ||
| class PipelineFactory { | ||
| static task = null; | ||
| static model = null; | ||
| static quantized = null; | ||
| static instance = null; | ||
|
|
||
| constructor(model, quantized) { | ||
| this.model = model; | ||
| this.quantized = quantized; | ||
| } | ||
|
|
||
| static async getInstance(progress_callback = null) { | ||
| if (this.instance === null) { | ||
| this.instance = pipeline(this.task, this.model, { | ||
| quantized: this.quantized, | ||
| progress_callback, | ||
| // For medium models, we need to load the `no_attentions` revision to avoid running out of memory | ||
| revision: this.model.includes("/whisper-medium") ? "no_attentions" : "main", | ||
| }); | ||
| } | ||
|
|
||
| return this.instance; | ||
| } | ||
| } | ||
|
|
||
| self.addEventListener("message", async (event) => { | ||
| const message = event.data; | ||
|
|
||
| // Do some work... | ||
| // TODO use message data | ||
| let transcript; | ||
| try { | ||
| transcript = await transcribe( | ||
| message.audio, | ||
| message.model, | ||
| message.multilingual, | ||
| message.quantized, | ||
| message.subtask, | ||
| message.language, | ||
| ); | ||
| } catch (e) { | ||
| console.warn("Error while transcribing: " + e); | ||
| transcript = { | ||
| text: "", | ||
| chunks: [], | ||
| }; | ||
| } | ||
|
|
||
| // Send the result back to the main thread | ||
| self.postMessage({ | ||
| status: "complete", | ||
| task: "automatic-speech-recognition", | ||
| key: message.key, | ||
| data: transcript, | ||
| }); | ||
| }); | ||
|
|
||
| class AutomaticSpeechRecognitionPipelineFactory extends PipelineFactory { | ||
| static task = "automatic-speech-recognition"; | ||
| static model = null; | ||
| static quantized = null; | ||
| } | ||
|
|
||
| const transcribe = async (audio, model, multilingual, quantized, subtask, language) => { | ||
| const isDistilWhisper = model.startsWith("distil-whisper/"); | ||
|
|
||
| let modelName = model; | ||
| if (!isDistilWhisper && !multilingual) { | ||
| modelName += ".en"; | ||
| } | ||
|
|
||
| const p = AutomaticSpeechRecognitionPipelineFactory; | ||
| if (p.model !== modelName || p.quantized !== quantized) { | ||
| // Invalidate model if different | ||
| p.model = modelName; | ||
| p.quantized = quantized; | ||
|
|
||
| if (p.instance !== null) { | ||
| (await p.getInstance()).dispose(); | ||
| p.instance = null; | ||
| } | ||
| } | ||
|
|
||
| // Load transcriber model | ||
| let transcriber = await p.getInstance((data) => { | ||
| self.postMessage(data); | ||
| }); | ||
|
|
||
| // Actually run transcription | ||
| let output = await transcriber(audio, { | ||
| // Greedy | ||
| top_k: 0, | ||
| do_sample: false, | ||
|
|
||
| // Sliding window | ||
| chunk_length_s: isDistilWhisper ? 20 : 30, | ||
| stride_length_s: isDistilWhisper ? 3 : 5, | ||
|
|
||
| // Language and task | ||
| language: language, | ||
| task: subtask, | ||
|
|
||
| // Return timestamps | ||
| return_timestamps: true, | ||
| force_full_sequences: false, | ||
| }); | ||
|
|
||
| return output; | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| import { useRef, useState } from "react"; | ||
| import { useMemo, useRef, useState } from "react"; | ||
| import { motion } from "framer-motion"; | ||
| import { utc } from "moment"; | ||
| import clsx from "clsx"; | ||
|
|
||
| import { useSettingsStore } from "@/stores/useSettingsStore"; | ||
|
|
||
|
|
@@ -9,22 +10,36 @@ import PlayControls from "./PlayControls"; | |
| import AudioProgress from "./AudioProgress"; | ||
|
|
||
| import { Driver, RadioCapture } from "@/types/state.type"; | ||
| import clsx from "clsx"; | ||
|
|
||
| type Props = { | ||
| driver: Driver; | ||
| capture: RadioCapture; | ||
| basePath: string; | ||
| transcription?: string; | ||
| }; | ||
|
|
||
| export default function TeamRadioMessage({ driver, capture, basePath }: Props) { | ||
| export default function TeamRadioMessage({ driver, capture, basePath, transcription }: Props) { | ||
| const audioRef = useRef<HTMLAudioElement | null>(null); | ||
| const intervalRef = useRef<NodeJS.Timeout | null>(null); | ||
|
|
||
| const [playing, setPlaying] = useState<boolean>(false); | ||
| const [duration, setDuration] = useState<number>(10); | ||
| const [progress, setProgress] = useState<number>(0); | ||
|
|
||
| const transcriptionElement = useMemo(() => { | ||
| if (transcription === undefined) { | ||
| return <></>; | ||
| } else if (transcription === "") { | ||
| return <SkeletonTranscription />; | ||
| } else { | ||
| return ( | ||
| <p className="font-small text-sm" style={{ whiteSpace: "pre-wrap" }}> | ||
| {transcription} | ||
| </p> | ||
| ); | ||
| } | ||
| }, [transcription]); | ||
|
|
||
| const loadMeta = () => { | ||
| if (!audioRef.current) return; | ||
| setDuration(audioRef.current.duration); | ||
|
|
@@ -106,6 +121,13 @@ export default function TeamRadioMessage({ driver, capture, basePath }: Props) { | |
| /> | ||
| </div> | ||
| </div> | ||
| <div className="gap-1">{transcriptionElement}</div> | ||
| </motion.li> | ||
| ); | ||
| } | ||
|
|
||
| const SkeletonTranscription = () => { | ||
| const animateClass = "h-6 animate-pulse rounded-md bg-zinc-800"; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems a bit tall, either do one or two thinner ones please |
||
|
|
||
| return <div className={clsx(animateClass, "!h-8 w-80")} />; | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.