omi icon indicating copy to clipboard operation
omi copied to clipboard

feat(backend): improve speaker detection with regex + name gazetteer

Open Iansabia opened this issue 9 months ago โ€ข 5 comments

Summary

Improves speaker detection in detect_speaker_from_text() by expanding regex patterns and adding an embedded name gazetteer -- zero new dependencies.

Addresses feedback from @beastoin on the original spaCy approach: no container bloat, no model downloads, no per-worker memory overhead.

What changed:

  • Expanded regex patterns: 5 new English introduction patterns ("This is Bob", "Hey it's Charlie", "Call me Dave", "You're speaking with Sarah", "The name's Bond") + multi-word name support
  • Name gazetteer: ~5K common first names as a frozenset for O(1) lookup, used to handle lowercased ASR output ("this is bob") and ambiguous patterns ("bob here")
  • Two-phase detection: Phase 1 (regex, fast, 34 languages) -> Phase 2 (gazetteer + context patterns, handles edge cases)

Detection Coverage

Pattern Phase 1 (Regex) Phase 2 (Gazetteer)
"I'm Alice" Yes --
"This is Bob" (capitalized) Yes (new) --
"Hey, it's Charlie" Yes (new) --
"Call me Dave" Yes (new) --
"You're speaking with Sarah" Yes (new) --
"this is bob" (lowercased ASR) -- Yes
"bob here" -- Yes
"This is important" No (correct) No (correct)

Performance

Metric Value
New dependencies 0
Memory overhead <1MB (frozenset)
Speed <0.05ms per call
Language support 34 languages (unchanged)

Files Changed

File Change
backend/utils/speaker_identification.py Expanded regex + gazetteer Phase 2
backend/utils/speaker_identification_names.py New -- ~5K name frozenset
backend/tests/unit/test_speaker_identification.py New -- 27 tests (Phase 1 + Phase 2 + negatives)
backend/test.sh Added speaker identification test

Test Plan

  • [x] 27 unit tests pass (14 regex, 7 gazetteer, 6 negative cases)
  • [x] All existing tests unaffected
  • [x] black --check passes
  • [x] No spaCy references remain

Closes #3039

Iansabia avatar Feb 10 '26 00:02 Iansabia

Great feedback, totally agree on the dependency weight. I have sort of a revised approach

Phase 1: Expanded regex -- Add 5 new English patterns ("This is X", "Call me X", "Hey it's X", "You're speaking with X", "Name's X") with multi-word name support. Catches 6/9 patterns from the table at zero cost.

Phase 2: Name gazetteer -- Instead of spaCy for name disambiguation, embed a frozenset of ~5,000 common first names directly in a Python file. This lets us:

  • Handle lowercased ASR output ("this is bob" โ†’ "Bob") by checking if the word after an intro phrase is a known name
  • Safely catch "Bob here" without false-positiving on "Monday here"
  • Zero new dependencies

Basically the gazetteer does the same job as spaCy's PERSON entity detection (is this word a name?) but as an O(1) set lookup.

I can try and do both phases, or would you prefer just the expanded regex for now?

Iansabia avatar Feb 10 '26 05:02 Iansabia

Updated โ€” pushed both phases. spaCy is fully removed. The PR now uses:

  • 7 expanded English regex patterns (multi-word name support)
  • ~5,000 name gazetteer as a plain Python frozenset (O(1) lookup, zero deps)
  • Pre-compiled context patterns for lowercased ASR

No new dependencies added. Ready for re-review.

Iansabia avatar Feb 11 '26 02:02 Iansabia

Hey ๐Ÿ‘‹ โ€” great work on the unit tests (27 passing)! One more thing before review: could you share a quick live demo showing this working with real audio/transcription data? A screen recording or terminal output from a local or dev environment would be perfect.

In the AI era, writing code is the easy part โ€” what really makes a PR stand out is proof that it works end-to-end beyond unit tests. Thanks! ๐Ÿ™

beastoin avatar Feb 19 '26 09:02 beastoin

Live Demo โ€” Speaker Detection on Realistic ASR Data

Demo script: backend/demo_speaker_detection.py (cd backend && python3 demo_speaker_detection.py)

Results

Metric Value
ASR transcript detection 27/27 correct
Edge cases 10/10 pass
Unit tests 27/27 pass
Throughput ~90K calls/sec
Latency ~11ฮผs per call

Coverage

  • Capitalized ASR: "I'm Sarah", "This is David", "You're speaking with Jennifer"
  • First + last names: "I'm John Smith", "This is Michael Chen", "My name is Sarah Connor"
  • Lowercased ASR (raw Deepgram/Whisper): "hey this is bob", "i'm alice", "sarah here"
  • Negative cases: "this is important", "monday here we go again" โ€” correctly rejected
  • Multi-language: French, German, Spanish
  • Simulated live meeting: 4 speakers auto-identified from SPEAKER_00โ€“03 โ†’ Sarah, Bob, Charlie, Dave
  • Performance: 270K calls in ~3s, zero dependencies

speaker_demo

Iansabia avatar Feb 20 '26 15:02 Iansabia

Hey @beastoin @kodjima33, rebased onto the latest main and cleaned up the conflicts. The stale merge commit is gone, so it's a clean linear history now.

Where it stands:

  • 4 commits on top of current main. The test.sh conflict is resolved, and speaker_identification.py merged cleanly alongside the recent executors/logging refactor (none of that work is touched).
  • 27/27 unit tests pass, black --check is clean.
  • Net change is identical to the version you approved: expanded English regex plus a ~5K-name gazetteer, zero new dependencies.

I saw #3039 got closed as "not planned" during the board cleanup. Happy to keep this PR linked to it or re-point it wherever works best for you. Let me know if there's anything else you'd like before merge.

Iansabia avatar May 30 '26 15:05 Iansabia

Thanks for the thorough review @Git-on-my-level, this was genuinely helpful. Both points are fixed:

1. Stopword guard restored in Phase 1. Phase 1 again carries the ... and name.lower() not in SPEAKER_NAME_STOPWORDS condition, so pronouns and fillers from run-on transcripts no longer leak through as speaker names before Phase 2 can weigh in. All 7 test_pronoun_stopwords_rejected cases pass, and I added the same run-on cases to this PR's own test_speaker_identification.py so the behavior is locked in here too:

  • "And I am It was great" -> None
  • "My name is It" -> None
  • "I am Sorry about that" -> None
  • ...and genuine intros still detect ("I am John" -> "John").

2. Multi-word names normalized. Phase 1 now uses the existing _normalize_name helper instead of name.capitalize(), so "I'm John Smith" returns "John Smith" (matching how the Person is stored) rather than "John smith".

While I was in here I also rebased onto the latest main to clear the conflicts and added explicit tests for both of the above. The full file is green (37 passed):

Also, I noticed #3039 was closed as "not planned" during the board cleanup. When I first picked this up it was listed as a bounty, so I wanted to ask where that stands now.

Thanks again for pushing on the end-to-end correctness, it made the change better.

Iansabia avatar Jun 27 '26 12:06 Iansabia

omi_speaker_tests

Iansabia avatar Jun 27 '26 12:06 Iansabia

Maintainer Review โ€” Omi PR #4708 (attempt 2, glm-5.2)

feat(backend): improve speaker detection with regex + name gazetteer URL: https://github.com/BasedHardware/omi/pull/4708 Head SHA: 64e65a7106cb49fa4a7b92b4564fbfcc95c5b67d Author: @Iansabia (FIRST_TIME_CONTRIBUTOR) ยท Tier: suspicious Generated: 2026-06-27T13:22:55Z

Recommendation: request_changes ยท Confidence: 0.82 ยท Not approved (gate disallows)


TL;DR

Good direction that resolves the earlier spaCy dependency-weight concern with a zero-dependency two-phase design. The 5996-line gazetteer data file is clean. But this is the second review round, and two correctness regressions in the regex phase are still open from the prior CHANGES_REQUESTED โ€” both independently reproduced against the current head SHA. Requesting changes; not approving (gate disallows anyway).

No security/supply-chain concern. No escalation.

Approval gate

Formal approval is blocked for multiple independent reasons, so gh pr review --approve was not issued:

  • must_not_approve: true in context
  • Large diff (4 files, 6202 changed lines)
  • Nontrivial feature PR โ†’ human product review required
  • Blocking labels present: needs-maintainer-review, needs-tests
  • No CI checks configured on the branch (cannot verify green)

What's good

  • Zero new dependencies โ€” spaCy was removed in response to @beastoin's dependency-weight feedback. Exactly the right call.
  • Gazetteer data is clean. Static sweep of speaker_identification_names.py (5996 lines): parses as valid Python, COMMON_FIRST_NAMES is a pure frozenset of 4994 lowercase string names, no import/exec/eval/compile/open/network/subprocess/os/sys in the file body (only false-positive hits were the word "from" inside the module docstring and the legitimate frozenset( line).
  • Feature fit is sound โ€” better speaker-detection robustness (lowercased ASR, more intro patterns) without heavy ML deps aligns with Omi's transcription/persona surface. No product-direction misalignment.
  • Test coverage and the live demo are strong (27 unit tests, reproducible benchmark).
  • Contributor has been responsive across multiple review rounds.

Two reproduced regressions (why request_changes)

Both are in the phantom-speaker area, both have concrete repros, and both feed bad speaker names into get_person_by_name / create_person.

1. Multi-word capture group bypasses the stopword filter (Phase 1)

Phase 1's new optional second-word group ([A-Z][a-zA-Z]*(?:\s+[A-Z][a-zA-Z]*)?) is checked against SPEAKER_NAME_STOPWORDS only as a whole string. Since the stopword set holds single words (it, you, important, ...), a two-word capture is never rejected even when the first word is a stopword โ€” reopening the run-on-transcript phantom-speaker path.

Reproduced against head:

  • "This is It Works" โ†’ "It Works" (expected None; it is a stopword)
  • "This is Important Information" โ†’ "Important Information"
  • "I'm You Know" โ†’ "You Know"

Fix: also reject when any whitespace-separated token of the captured name is a stopword, e.g. any(t in SPEAKER_NAME_STOPWORDS for t in name.lower().split()).

2. Phase 2 (gazetteer) has no stopword check, and my is in both sets (verified)

Phase 2 only tests cleaned not in COMMON_FIRST_NAMES and never consults SPEAKER_NAME_STOPWORDS. The pronoun my is present in the gazetteer (verified at runtime), so lowercased ASR resolves to a speaker named "My":

  • "this is my" โ†’ "My" (expected None)
  • "this is my friend" โ†’ "My"
  • "hey it's my turn" โ†’ "My"

Fix: add the stopword check to Phase 2 as well, or drop my from the gazetteer (it's a rare name and directly conflicts with the pronoun).

Neither case is covered by the current tests. Adding the repros above as negative cases would lock both fixes in.

Notes

  • No CI checks are configured on the contributor branch, so I cannot verify CI green; this alone is an approval-blocker.
  • One commit carries Co-Authored-By: Claude Opus 4.6 โ€” cosmetic AI-assist attribution, not a security concern.
  • Mergeable mechanically (MERGEABLE), but mergeStateStatus: BLOCKED.
  • @beastoin is a requested reviewer; human maintainer should confirm direction and re-review after fixes.

Actions taken

  • Posted a kind, maintainer-voiced comment with the two repros + localized fix suggestions.
  • Preserved existing labels (needs-maintainer-review, needs-tests); no new labels needed.
  • Did not approve, did not merge, did not push to the contributor branch.

Git-on-my-level avatar Jun 27 '26 13:06 Git-on-my-level

Thanks @Git-on-my-level. Both issues are fixed, and I did a deeper self-audit of the regex phase to catch anything else in the same phantom-speaker area before re-review.

Your two issues

  1. Multi-word capture bypassed the stopword filter. Phase 1 now rejects a capture when its leading token is a stopword (or every token is), so "This is It Works", "I'm Not Sure", and "I'm You Know" all return None. I used the leading-token rule rather than any(token in SPEAKER_NAME_STOPWORDS ...) on purpose: good, well, right, and fine are in the stopword set but are also real surnames, and any(...) would drop legitimate names like "DeShawn Good". Leading-token catches every run-on case while preserving real surnames.

  2. Phase 2 had no stopword check (my). Phase 2 now consults the stopword set too, so "this is my", "this is my friend", and "hey it's my turn" return None.

Both are locked in with negative tests.

Extra hardening from the self-audit (same phantom-speaker class)

  • Honorifics: "I'm Dr. Smith" -> "Smith" (was "Dr").
  • Suffix cues gated by context: "is bob here?" / "no bob here" -> None, while genuine intros like "bob here" and "dave calling from the office" still detect (the question/negation guard is scoped to the local clause, so run-on ASR like "is this thing on? sarah speaking" still resolves to "Sarah").
  • Possessives: "This is John's car" -> None.
  • Trailing pronoun: "I'm Anna I will call you" -> "Anna" (was "Anna I").
  • Normalization preserves interior capitals and short acronyms, so "DeShawn" / "McKenzie" / "DJ" round-trip correctly for get_person_by_name / create_person.

Recall preserved (deliberate)

I did not denylist words that are both common words and real first names (Friday, Will, Grace, June, May, Mark, Hope, ...), because blocking them would drop real users with those names. There is a class of ambiguous capitalized words ("It's Friday" -> "Friday") that I left as-is for that reason. If you'd prefer a curated denylist of true non-names (weekdays, nationalities, brands), I'm happy to add one, but that is a precision/recall call I wanted to leave to the maintainers. Non-English pattern issues are out of scope for this PR.

Size

I packed the name gazetteer from one-name-per-line into a dense split-string: 5,996 -> 330 lines, identical frozenset (verified by round-trip). That should bring the PR well under the size flag.

63 unit tests pass, black clean:

Iansabia avatar Jun 27 '26 14:06 Iansabia

omi_speaker_tests

Iansabia avatar Jun 27 '26 14:06 Iansabia