Author SHA1 Message Date
ahp a29dae7f56 fix(punctuation): keep thousands separators out of the comma mapping
The Latin-to-Persian mapping turned every comma into ، and the spacing
rule then added a space after it, so "1,000" came out as "1، 000": one
number split into two.

- a comma between a digit and exactly three more digits is a thousands
  separator and is kept as is (Latin and Persian digits)
- a comma in a list of numbers (4090,4080) still becomes ، as before
2026-11-26 18:02:08 +03:30
ahp d254527881 test(punctuation): pin the post-pass on real-world edge cases
Documents what the rule-based pass does today, case by case, before any
rule changes (#30):

- slash dates and fractions (1404/09/02, 2026/11/23, 1/2) stay intact
- Latin digits and words inside Persian sentences, decimals, version
  numbers and clock times are left alone
- abbreviation dots (Dr.) count as terminal punctuation; a dot mid
  sentence is untouched
- ellipses (… and ...) are terminal and tightened, so a trailing-off
  question keeps its ellipsis instead of gaining a question mark
- repeated and mixed marks (!!!, ؟؟, ?!) are preserved, since they carry
  the speaker's emphasis
2026-11-26 18:02:07 +03:30
8 changed files with 84 additions and 33 deletions

No files matched your search

-1
View File
@@ -1,6 +1,5 @@
# Sokhan configuration
# Copy this file to .env and adjust the values.
# For docker compose, copy it to deploy/.env instead.
# Every variable below has a working default for local development.
# --- Core ---
+9 -13
View File
@@ -5,19 +5,6 @@ The format follows [Keep a Changelog](https://keepachangelog.com/) and the proje
## [Unreleased]
### Fixed
- Compose: the Whisper model cache now lives on the `sokhan-data` volume
(`HF_HOME=/data/huggingface`). Previously it sat in the container's own
filesystem, so every recreate (`down`/`up`, `--build`, a new image)
re-downloaded the weights — and transcription failed outright where the
Hugging Face Hub is unreachable.
- Compose: the postgres healthcheck probes the configured `POSTGRES_USER` and
`POSTGRES_DB` instead of a hardcoded `sokhan`.
- Quickstart (both READMEs) and `.env.example` now say to copy the env file to
`deploy/.env`; the missing-password error names that path too. Copying it to
the repo root left `POSTGRES_PASSWORD` unset for compose.
### Added
- **Persian accuracy stack (v2)** — measured on the 12-clip Common Voice
@@ -87,6 +74,15 @@ The format follows [Keep a Changelog](https://keepachangelog.com/) and the proje
pyproject.toml files, web/package.json, the landing site copy, the demo
Space metadata and the fine-tuned model card.
### Fixed
- The punctuation post-pass no longer splits numbers written with a
thousands separator: `1,000` stayed `1، 000` (Persian comma plus a
space). A comma between digits followed by exactly three digits is now
kept as is; a comma in a list of numbers (`4090,4080`) still becomes `،`.
The edge cases from #30 (slash dates, Latin digits in Persian sentences,
abbreviations, ellipses, repeated marks) are pinned by tests.
## [1.4.1] - 2026-11-26
### Security
+1 -5
View File
@@ -145,8 +145,6 @@ faster-whisper، یک ورکر Celery، یک داشبورد Next.js که فار
```bash
git clone https://github.com/Abolfazlrwm/sokhan.git
cd sokhan
cp .env.example deploy/.env # compose فایل .env کنار فایل compose را می‌خواند
# در deploy/.env مقدار POSTGRES_PASSWORD را یک رمز طولانی و تصادفی بگذارید
docker compose -f deploy/docker-compose.yml up -d --build
```
@@ -154,9 +152,7 @@ docker compose -f deploy/docker-compose.yml up -d --build
- API و مستندات: http://localhost:8000/docs
ورکر مدل Whisper تنظیم‌شده را در اولین رونویسی دانلود می‌کند
(`WHISPER_MODEL_SIZE`، پیش‌فرض `small`) و آن را روی ولوم `sokhan-data`
(`HF_HOME=/data/huggingface`) نگه می‌دارد، پس ساخت دوبارهٔ کانتینرها مدل را
دوباره دانلود نمی‌کند. برای فعال‌کردن خلاصه‌سازی،
(`WHISPER_MODEL_SIZE`، پیش‌فرض `small`). برای فعال‌کردن خلاصه‌سازی،
`SUMMARY_ENABLED=true` و متغیرهای LLM خود را در فایل `.env` کنار
`docker-compose.yml` بگذارید. برای اعمال مهاجرت‌های اسکیما به‌صورت دستی:
+1 -5
View File
@@ -143,8 +143,6 @@ default — a GPU is not required.
```bash
git clone https://github.com/Abolfazlrwm/sokhan.git
cd sokhan
cp .env.example deploy/.env # compose reads the .env next to the compose file
# edit deploy/.env and set POSTGRES_PASSWORD to a long random value
docker compose -f deploy/docker-compose.yml up -d --build
```
@@ -152,9 +150,7 @@ docker compose -f deploy/docker-compose.yml up -d --build
- API and docs: http://localhost:8000/docs
The worker downloads the configured Whisper model on first transcription
(`WHISPER_MODEL_SIZE`, default `small`) and caches it on the `sokhan-data`
volume (`HF_HOME=/data/huggingface`), so recreating the containers does not
download it again. To enable summaries, set
(`WHISPER_MODEL_SIZE`, default `small`). To enable summaries, set
`SUMMARY_ENABLED=true` and your LLM variables in a `.env` file next to
`docker-compose.yml`. To apply schema migrations manually:
+3 -3
View File
@@ -12,9 +12,9 @@ COPY alembic ./alembic
RUN pip install --no-cache-dir .
RUN useradd --system --uid 1000 --create-home appuser \
&& mkdir -p /data/audio \
&& chown -R appuser:appuser /data
# Non-root runtime user; uvicorn binds 8000 (unprivileged) and the app keeps
# no local writable state (postgres via DATABASE_URL).
RUN useradd --system --uid 1000 --create-home appuser
USER appuser
EXPOSE 8000
+10 -2
View File
@@ -5,7 +5,8 @@ puts the transcript back into publishable shape with three conservative,
deterministic rule families — no model call, no LLM dependency:
1. Script fix: the Latin comma, question mark and semicolon become their
Persian counterparts (، ؟ ؛).
Persian counterparts (، ؟ ؛). A comma that groups digits ("1,000",
"12,500,000") is a thousands separator, not punctuation, and is kept.
2. Spacing: no space *before* punctuation, exactly one space *after* clause
and sentence punctuation. `.` and `:` are deliberately left alone so
decimals ("3.5") and clock times ("12:30") survive.
@@ -26,6 +27,12 @@ import re
# Latin forms that should not survive in a Persian transcript.
_LATIN_TO_PERSIAN = str.maketrans({",": "،", "?": "؟", ";": "؛"})
# A comma between a digit and exactly three more digits groups a number.
# \d matches Persian digits too, so "۱,۲۰۰" is covered.
_THOUSANDS_COMMA = re.compile(r"(?<=\d),(?=\d{3}(?!\d))")
# Stands in for a thousands comma while the Latin marks are translated.
_KEPT_COMMA = "\x00"
_CLAUSE = "،؛"
_SENTENCE_END = "؟!…."
_PUNCT_CHARS = _CLAUSE + _SENTENCE_END + ":."
@@ -73,7 +80,8 @@ def _has_terminal(text: str) -> bool:
def restore_punctuation(text: str) -> str:
"""Return `text` with Persian punctuation restored; safe to re-apply."""
cleaned = text.translate(_LATIN_TO_PERSIAN).strip()
cleaned = _THOUSANDS_COMMA.sub(_KEPT_COMMA, text)
cleaned = cleaned.translate(_LATIN_TO_PERSIAN).replace(_KEPT_COMMA, ",").strip()
cleaned = _SPACE_BEFORE.sub(r"\1", cleaned)
cleaned = _SPACE_AFTER.sub(r"\1 ", cleaned)
cleaned = _WS_RUNS.sub(" ", cleaned).strip()
+57
View File
@@ -51,3 +51,60 @@ def test_empty_and_whitespace_input_stay_empty():
def test_statement_with_colon_gets_no_double_mark():
assert restore_punctuation("این است") == "این است."
assert restore_punctuation("این است:") == "این است:"
# Edge cases from #30. Each test pins the behavior on real-world input; the
# comments say why that behavior is the right one for a transcript.
def test_slash_dates_are_left_intact():
# `/` is not punctuation to this pass, so date groups never get spaced
# or split, in Persian and in Latin digits alike.
assert restore_punctuation("تاریخ ۱۴۰۴/۰۹/۰۲ است") == "تاریخ ۱۴۰۴/۰۹/۰۲ است."
assert restore_punctuation("جلسه 2026/11/23 برگزار شد") == "جلسه 2026/11/23 برگزار شد."
assert restore_punctuation("نسبت ۱/۲ است") == "نسبت ۱/۲ است."
def test_latin_digits_and_words_inside_persian_sentences():
assert restore_punctuation("۳ دستگاه RTX 4090 خریدیم") == "۳ دستگاه RTX 4090 خریدیم."
assert restore_punctuation("قیمت ۳.۵ میلیون شد") == "قیمت ۳.۵ میلیون شد."
assert restore_punctuation("نسخه 1.4.1 منتشر شد") == "نسخه 1.4.1 منتشر شد."
assert restore_punctuation("ساعت ۱۲:۳۰ بیا") == "ساعت ۱۲:۳۰ بیا."
def test_thousands_separators_are_not_turned_into_persian_commas():
# A Latin comma followed by exactly three digits groups a number; turning
# it into ، and spacing it ("1، 000") would split one number into two.
assert restore_punctuation("قیمت 1,000 دلار شد") == "قیمت 1,000 دلار شد."
assert restore_punctuation("جمعیت 12,500,000 نفر") == "جمعیت 12,500,000 نفر."
assert restore_punctuation("مبلغ ۱,۲۰۰ تومان") == "مبلغ ۱,۲۰۰ تومان."
def test_comma_between_numbers_that_is_not_grouping_stays_a_list_separator():
assert restore_punctuation("RTX 4090,4080 و 3090") == "RTX 4090، 4080 و 3090."
assert restore_punctuation("اعداد 1,2 و 3") == "اعداد 1، 2 و 3."
def test_abbreviation_dots_are_kept_and_end_the_segment():
# A trailing dot already counts as terminal punctuation, so nothing is
# appended; a dot in the middle is left alone like a decimal point.
assert restore_punctuation("Dr.") == "Dr."
assert restore_punctuation("با Dr. Smith صحبت کردم") == "با Dr. Smith صحبت کردم."
assert restore_punctuation("با دکتر. احمدی") == "با دکتر. احمدی."
def test_ellipsis_forms_are_terminal_and_tightened():
assert restore_punctuation("صبر کن…") == "صبر کن…"
assert restore_punctuation("نمیدونم...") == "نمیدونم..."
assert restore_punctuation("صبر کن ... بعد") == "صبر کن... بعد."
# A trailing-off question keeps its ellipsis instead of gaining a ؟.
assert restore_punctuation("کی میای...") == "کی میای..."
def test_repeated_and_mixed_marks_are_preserved():
# Repetition carries the speaker's emphasis; the pass only converts the
# script of Latin marks and never collapses them.
assert restore_punctuation("وای!!!") == "وای!!!"
assert restore_punctuation("چرا!!!") == "چرا!!!"
assert restore_punctuation("چی؟؟") == "چی؟؟"
assert restore_punctuation("واقعا?!") == "واقعا؟!"
+3 -4
View File
@@ -5,12 +5,12 @@ services:
POSTGRES_USER: ${POSTGRES_USER:-sokhan}
# Required on purpose: shipping a default credential means every
# deployment that forgets the .env runs an open database.
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD must be set (cp .env.example deploy/.env and edit it)}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?POSTGRES_PASSWORD must be set (see .env.example)}
POSTGRES_DB: ${POSTGRES_DB:-sokhan}
volumes:
- postgres-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
test: ["CMD-SHELL", "pg_isready -U sokhan"]
interval: 5s
timeout: 3s
retries: 10
@@ -33,11 +33,10 @@ services:
build:
context: ../backend
environment: &backend-env
DATABASE_URL: postgresql+psycopg://${POSTGRES_USER:-sokhan}:${POSTGRES_PASSWORD:?POSTGRES_PASSWORD must be set (cp .env.example deploy/.env and edit it)}@postgres:5432/${POSTGRES_DB:-sokhan}
DATABASE_URL: postgresql+psycopg://${POSTGRES_USER:-sokhan}:${POSTGRES_PASSWORD:?POSTGRES_PASSWORD must be set (see .env.example)}@postgres:5432/${POSTGRES_DB:-sokhan}
REDIS_URL: redis://redis:6379/0
CELERY_ENABLED: "true"
DATA_DIR: /data
HF_HOME: /data/huggingface
SOKHAN_CORS_ORIGINS: ${SOKHAN_CORS_ORIGINS:-}
WHISPER_MODEL_SIZE: ${WHISPER_MODEL_SIZE:-medium}
WHISPER_DEVICE: ${WHISPER_DEVICE:-cpu}