Compare commits
87 Commits
574124558b
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 49f3cdc407 | |||
| 2c0998579c | |||
| 327c05ea16 | |||
| dabb5970ba | |||
| 36b0b6f241 | |||
| 6640bc050d | |||
| 59363c5dcd | |||
| 0e27537a68 | |||
| 0947e91f15 | |||
| 1d447f2836 | |||
| 49e607e1e1 | |||
| bd4393addc | |||
| f5836d83f3 | |||
| b2dce9e048 | |||
| 4d9414fee9 | |||
| d4ed84f68d | |||
| eb83a37f02 | |||
| e7aa5ebf25 | |||
| 1265a664cd | |||
| 83f3c09218 | |||
| d828a91bf3 | |||
| 670c6d3e2b | |||
| f20102d564 | |||
| 0e6bc53cf8 | |||
| c43076efd4 | |||
| 03d66219d9 | |||
| 0c0e52dfb8 | |||
| 604bfa3f41 | |||
| 8ff473f3e6 | |||
| 0b3f737e5b | |||
| 598f8630de | |||
| 7fac0e7d9c | |||
| 5dd56a3368 | |||
| 7364d572d5 | |||
| d51b006a19 | |||
| ea5a0752df | |||
| b0a1bc059b | |||
| e27e5b8522 | |||
| 6233a41f61 | |||
| 237bd4b37c | |||
| 7ece1a50c2 | |||
| 46fbcf80af | |||
| 42a155aeaa | |||
| b0a23b32e1 | |||
| 2e2bc3fb29 | |||
| 2f9299389b | |||
| e0d2fd6963 | |||
| 4651c5f8b2 | |||
| 6c11a8f19a | |||
| 2a2a5e024c | |||
| 7adca3d921 | |||
| efb34dd9ff | |||
| 11e5309a8e | |||
| a3ca1f3505 | |||
| 154cac6c7b | |||
| 18f4a4e8de | |||
| 2f304e3ed1 | |||
| fd94e2daa0 | |||
| e74bc04cb3 | |||
| c792fa17e8 | |||
| e55f36a131 | |||
| 572587bb85 | |||
| cfc38b21ed | |||
| 1582b90ddb | |||
| 9ec4c4ccba | |||
| 8ecae8f648 | |||
| 49e999f0ee | |||
| eb9b2f9126 | |||
| 50c7ec90a0 | |||
| f7c9c70bfc | |||
| a8f48b9e58 | |||
| 2dce9b43c9 | |||
| 1dea51f1f9 | |||
| 63cd620b79 | |||
| dc20e9cff0 | |||
| fb1dc3324d | |||
| 917a7b8f2f | |||
| 85cdd9216a | |||
| 2803c81b44 | |||
| b9d25a39dd | |||
| f6db48b1d0 | |||
| 37d30e0ee2 | |||
| d854d498cd | |||
| 1eb88d27ba | |||
| 2ea46ada42 | |||
| 47b3304297 | |||
| 4822ef28e8 |
+29
-13
@@ -5,39 +5,55 @@ FROM python:3.11-slim
|
||||
LABEL maintainer="Jacob Schmieder"
|
||||
LABEL email="Jacob.Schmieder@dbfz.de"
|
||||
LABEL version="0.1.1.dev"
|
||||
LABEL description="Scraibe: LocalAI-backed transcription and diarization client with summarization. \
|
||||
LABEL description="Scraibe: LocalAI-backed transcription and diarization client with summarization and custom Web GUI. \
|
||||
Sends audio to a LocalAI server running vibevoice.cpp and uses a second LLM for summarization."
|
||||
LABEL url="https://github.com/JSchmie/ScrAIbe"
|
||||
LABEL url="https://git.optimex.systems/admin/scribe"
|
||||
|
||||
# Install system dependencies (ffmpeg required)
|
||||
# Install system dependencies (ffmpeg, redis)
|
||||
RUN apt update -y && \
|
||||
apt install -y --no-install-recommends ffmpeg && \
|
||||
apt install -y --no-install-recommends ffmpeg redis-server && \
|
||||
apt clean && \
|
||||
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
|
||||
|
||||
# Working directory
|
||||
WORKDIR /app
|
||||
WORKDIR /app/src
|
||||
|
||||
# Environment variables for LocalAI (transcription/diarization)
|
||||
# Set these via docker run -e or docker-compose
|
||||
ENV LOCALAI_API_URL=http://localhost:8080
|
||||
ENV LOCALAI_API_KEY=
|
||||
ENV LOCALAI_MODEL=vibevoice-diarize
|
||||
ENV LOCALAI_MODEL=vibevoice-cpp-asr
|
||||
|
||||
# Environment variables for Summarizer LLM
|
||||
ENV SUMMARIZER_API_URL=http://localhost:8080
|
||||
ENV SUMMARIZER_API_KEY=
|
||||
ENV SUMMARIZER_MODEL=llama-3.1-8b-instruct
|
||||
ENV SUMMARIZER_MODEL=qwen3-14b
|
||||
|
||||
# Gradio / Web GUI
|
||||
ENV GRADIO_SERVER_NAME=0.0.0.0
|
||||
|
||||
# Async processing (Celery + Redis)
|
||||
ENV CELERY_BROKER_URL=redis://localhost:6379/0
|
||||
ENV CELERY_RESULT_BACKEND=redis://localhost:6379/0
|
||||
ENV SCRAIBE_UPLOAD_DIR=/tmp/scraibe_uploads
|
||||
|
||||
# Email and template configuration
|
||||
ENV EMAIL_CONTACT_ADDRESS=support@example.com
|
||||
ENV EMAIL_CSS_PATH=
|
||||
ENV SCRAIBE_TEMPLATES_DIR=/app/src/misc
|
||||
ENV SCRABIE_VERSION=0.1.1.dev
|
||||
|
||||
# Copy and install Python dependencies
|
||||
COPY requirements.txt /app/requirements.txt
|
||||
COPY requirements.txt /app/src/requirements.txt
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
|
||||
# Copy application code
|
||||
COPY scraibe /app/scraibe
|
||||
COPY scraibe /app/src/scraibe
|
||||
|
||||
# Expose port (if UI is served)
|
||||
# Copy custom Web GUI assets (header, footer, templates, logos, config)
|
||||
COPY misc /app/src/misc
|
||||
|
||||
# Expose ports
|
||||
EXPOSE 7860
|
||||
|
||||
# Run the application
|
||||
ENTRYPOINT ["python3", "-m", "scraibe.cli"]
|
||||
# Run the Web GUI and Celery worker (with Redis) by default
|
||||
CMD ["/bin/bash", "-c", "redis-server --daemonize yes && celery -A scraibe.celery_app worker -Q transcription -l info & python3 -m scraibe"]
|
||||
|
||||
@@ -1,173 +1,293 @@
|
||||
# `ScrAIbe: Streamlined Conversation Recording with Automated Intelligence Based Environment` 🎙️🧠
|
||||
# ScrAIbe – LocalAI-Backed Transcription and Summarization
|
||||
|
||||
ScrAIbe is a transcription and summarization service that:
|
||||
|
||||
- Sends audio to a LocalAI server running vibevoice.cpp for transcription and speaker diarization.
|
||||
- Optionally uses a second LLM to generate a structured summary.
|
||||
- Provides:
|
||||
- A web GUI for uploading audio and receiving transcripts via email.
|
||||
- A CLI and Python API for direct integration.
|
||||
|
||||
No local speech models or heavy dependencies are required. ScrAIbe is designed as a thin client in front of your own AI services.
|
||||
|
||||
For more information: https://apstrom.ca
|
||||
|
||||
## Features
|
||||
|
||||
- Transcription with speaker diarization via LocalAI:
|
||||
- Uses the /v1/audio/diarization endpoint.
|
||||
- Compatible with vibevoice.cpp and other diarization-capable backends.
|
||||
- Optional AI-powered summarization:
|
||||
- Task: transcript_and_summarize
|
||||
- Highlights:
|
||||
- Main topics and discussion points
|
||||
- Key decisions and outcomes
|
||||
- Action items and responsibilities
|
||||
- Open issues and risks
|
||||
- Async web GUI:
|
||||
- Upload audio via browser.
|
||||
- Jobs are queued and processed in the background (Celery + Redis).
|
||||
- Emails:
|
||||
- Immediate confirmation with queue position.
|
||||
- Final transcript (MD + DOCX + JSON) when ready.
|
||||
- Summary as MD + DOCX (if requested).
|
||||
- Error notification if processing fails.
|
||||
- File formats:
|
||||
- Transcript: .md and .docx (line-numbered, no cover page)
|
||||
- Summary (if requested): .md and .docx (no line numbering, no cover page)
|
||||
- Full structured output: .json
|
||||
- Customizable branding:
|
||||
- Web GUI title, logo, and accent color via environment variables.
|
||||
- Email logo, accent color, and subject lines via environment variables.
|
||||
- CLI and Python API:
|
||||
- Simple command-line interface.
|
||||
- Drop-in Scraibe class for integration into other tools.
|
||||
- Docker-ready:
|
||||
- Lightweight container, configured via environment variables.
|
||||
|
||||
## Architecture
|
||||
|
||||
- LocalAI (vibevoice.cpp):
|
||||
- Handles audio → transcript + speaker segments.
|
||||
- Summarizer LLM (OpenAI-compatible chat endpoint):
|
||||
- Handles transcript → structured summary.
|
||||
- ScrAIbe:
|
||||
- Orchestrates:
|
||||
- File upload to LocalAI
|
||||
- Transcript assembly
|
||||
- Chunked summarization
|
||||
- Output formatting (e.g., .md with transcript + summary)
|
||||
- Runs:
|
||||
- Web GUI (Gradio)
|
||||
- Celery worker (async processing)
|
||||
- Redis (in-container by default)
|
||||
|
||||
## Quick Start (Web GUI in Docker)
|
||||
|
||||
Run the container with your LocalAI and summarizer endpoints:
|
||||
|
||||
- docker run -d \
|
||||
-p 7860:7860 \
|
||||
-e LOCALAI_API_URL=http://localai:8080 \
|
||||
-e SUMMARIZER_API_URL=http://llm:8080 \
|
||||
-e EMAIL_SMTP_HOST=smtp.your-domain.com \
|
||||
-e EMAIL_SMTP_PORT=587 \
|
||||
-e EMAIL_SMTP_USER=transcribe@your-domain.com \
|
||||
-e EMAIL_SMTP_PASSWORD=your_password \
|
||||
-e EMAIL_FROM_ADDRESS="ScrAIbe <transcribe@your-domain.com>" \
|
||||
-e EMAIL_CONTACT_ADDRESS=support@your-domain.com \
|
||||
-e WEBUI_TITLE="Your Transcription Service" \
|
||||
-e WEBUI_LOGO_URL="https://your-domain.com/logo.png" \
|
||||
-e EMAIL_LOGO_URL="https://your-domain.com/logo.png" \
|
||||
-e EMAIL_ACCENT_COLOR="#7C6DA0" \
|
||||
scraibe:latest
|
||||
|
||||
Then open: http://<host>:7860
|
||||
|
||||
## Quick Start (CLI)
|
||||
|
||||
Basic usage:
|
||||
|
||||
- Transcribe:
|
||||
|
||||
- python3 -m scraibe.cli -f "audio.wav" -o "./output" -of txt
|
||||
|
||||
- Transcribe and summarize:
|
||||
|
||||
- python3 -m scraibe.cli -f "audio.wav" -o "./output" --task transcript_and_summarize
|
||||
|
||||
Environment variables must be set to point to your LocalAI and summarizer LLM.
|
||||
|
||||
## Python API
|
||||
|
||||
Example: transcribe only
|
||||
|
||||
- from scraibe import Scraibe
|
||||
|
||||
- client = Scraibe()
|
||||
- text = client.transcribe("audio.wav")
|
||||
- print(text)
|
||||
|
||||
Example: transcribe and summarize
|
||||
|
||||
- from scraibe import Scraibe
|
||||
|
||||
- client = Scraibe()
|
||||
- result = client.transcript_and_summarize("audio.wav")
|
||||
- transcript = result["transcript"]
|
||||
- summary = result["summary"]
|
||||
|
||||
You can override endpoints and models via environment variables or constructor parameters if needed.
|
||||
|
||||
## Command-Line Options
|
||||
|
||||
Run:
|
||||
|
||||
- python3 -m scraibe.cli -h
|
||||
|
||||
Key options:
|
||||
|
||||
- -f / --audio-files:
|
||||
- One or more audio files to process.
|
||||
- --task:
|
||||
- transcribe (default)
|
||||
- transcript_and_summarize
|
||||
- -o / --output-directory:
|
||||
- Output folder for generated files.
|
||||
- -of / --output-format:
|
||||
- txt, json, md, html
|
||||
- For transcript_and_summarize, output is always saved as .md with:
|
||||
- # Transcript
|
||||
- # Summary
|
||||
|
||||
Other options (e.g., --language, --num-speakers) are accepted and forwarded where applicable; many legacy Whisper/Pyannote flags are kept for compatibility but ignored.
|
||||
|
||||
## Docker Usage
|
||||
|
||||
ScrAIbe is designed to run in Docker as a client to your LocalAI and summarizer LLM.
|
||||
|
||||
### Basic run (transcribe via CLI)
|
||||
|
||||
- docker run -it \
|
||||
-e LOCALAI_API_URL=http://localai:8080 \
|
||||
-v /path/to/audio:/audio \
|
||||
scraibe:latest \
|
||||
-f /audio/meeting.wav -o /audio/output -of txt
|
||||
|
||||
### Basic run (transcribe + summarize via CLI)
|
||||
|
||||
- docker run -it \
|
||||
-e LOCALAI_API_URL=http://localai:8080 \
|
||||
-e SUMMARIZER_API_URL=http://llm:8080 \
|
||||
-v /path/to/audio:/audio \
|
||||
scraibe:latest \
|
||||
-f /audio/meeting.wav -o /audio/output --task transcript_and_summarize
|
||||
|
||||
### Docker Environment Variables
|
||||
|
||||
The following environment variables configure ScrAIbe in Docker.
|
||||
|
||||
Transcription / Diarization (LocalAI):
|
||||
|
||||
- LOCALAI_API_URL:
|
||||
- Required.
|
||||
- Base URL of the LocalAI server.
|
||||
- Example: http://localai:8080
|
||||
- LOCALAI_API_KEY:
|
||||
- Optional.
|
||||
- API key for LocalAI, if configured.
|
||||
- LOCALAI_MODEL:
|
||||
- Optional (default: vibevoice-diarize).
|
||||
- Model name used for transcription/diarization.
|
||||
|
||||
Welcome to `ScrAIbe`, a state-of-the-art, [PyTorch](https://pytorch.org/) based multilingual speech-to-text framework designed to generate fully automated transcriptions.
|
||||
Summarization LLM:
|
||||
|
||||
Beyond transcription, ScrAIbe supports advanced functions such as speaker diarization and speaker recognition. 🚀
|
||||
- SUMMARIZER_API_URL:
|
||||
- Required when using --task transcript_and_summarize.
|
||||
- Base URL of the summarization LLM (OpenAI-compatible /v1/chat/completions).
|
||||
- Example: http://llm:8080
|
||||
- SUMMARIZER_API_KEY:
|
||||
- Optional.
|
||||
- API key for the summarization LLM, if required.
|
||||
- SUMMARIZER_MODEL:
|
||||
- Optional (default: llama-3.1-8b-instruct).
|
||||
- Model name used for summarization.
|
||||
|
||||
Designed as a comprehensive AI toolkit, it uses multiple powerful AI models:
|
||||
Web GUI and branding:
|
||||
|
||||
- **[Whisper](https://github.com/openai/whisper)**: A general-purpose speech recognition model.
|
||||
- **[WhisperX](https://github.com/m-bain/whisperX)**: A faster, quantized version of Whisper for enhanced performance on CPU. ⚡
|
||||
- **[Pyannote-Audio](https://github.com/pyannote/pyannote-audio)**: An open-source toolkit for speaker diarization. 🗣️
|
||||
|
||||
The framework utilizes a PyanNet-inspired pipeline, with the `Pyannote` library for speaker diarization and `VoxCeleb` for speaker embedding.
|
||||
|
||||
During post-diarization, each audio segment is processed by the OpenAI `Whisper` model in a transformer encoder-decoder structure. Initially, a CNN mitigates noise and enhances speech. Before transcription, `VoxLingua` identifies the language segment, facilitating Whisper's role in both transcription and text translation. 🌍✨
|
||||
|
||||
The following graphic illustrates the whole pipeline:
|
||||
|
||||
<div style="text-align:center;">
|
||||
<img src="./Pictures/pipeline.png#gh-dark-mode-only" style="width: 60%;" />
|
||||
<img src="./Pictures/pipeline_light.png#gh-light-mode-only" style="width: 60%;" />
|
||||
</div>
|
||||
|
||||
## Getting Started 🚀
|
||||
|
||||
### Prerequisites
|
||||
|
||||
Before installing ScrAIbe, ensure you have the following prerequisites:
|
||||
|
||||
- **Python**: Version 3.9 or later.
|
||||
- **PyTorch**: Version 2.0 or later.
|
||||
- **CUDA**: A compatible version with your PyTorch Version if you want to use GPU acceleration.
|
||||
|
||||
**Note:** PyTorch should be automatically installed with the pip installer. However, if you encounter any issues, you should consider installing it manually by following the instructions on the [PyTorch website](https://pytorch.org/get-started/locally/).
|
||||
|
||||
### Install ScrAIbe
|
||||
|
||||
Install ScrAIbe on your local machine with ease using PyPI.
|
||||
|
||||
```bash
|
||||
pip install scraibe
|
||||
```
|
||||
|
||||
If you want to install the development version, you can do so by installing it from GitHub:
|
||||
|
||||
```bash
|
||||
pip install git+https://github.com/JSchmie/ScrAIbe.git@develop
|
||||
```
|
||||
|
||||
or from PyPI using our latest pre-release:
|
||||
|
||||
```bash
|
||||
pip install --pre scraibe
|
||||
```
|
||||
|
||||
Get started with ScrAIbe today and experience seamless, automated transcription and diarization.
|
||||
|
||||
## Usage
|
||||
|
||||
We've developed ScrAIbe with several access points to cater to diverse user needs.
|
||||
|
||||
### Python Usage
|
||||
|
||||
Gain full control over the functionalities as well as process customization.
|
||||
|
||||
```python
|
||||
from scraibe import Scraibe
|
||||
|
||||
model = Scraibe()
|
||||
|
||||
text = model.autotranscribe("audio.wav")
|
||||
|
||||
print(f"Transcription: \n{text}")
|
||||
```
|
||||
|
||||
The `Scraibe` class ensures the models are properly loaded. You can customize the models with various keywords:
|
||||
|
||||
- **Whisper Models**: Use the `whisper_model` keyword to specify models like `tiny`, `base`, `small`, `medium`, or `large` (`large-v2`, `large-v3`) depending on your accuracy and speed needs.
|
||||
- **Pyannote Diarization Model**: Use the `dia_model` keyword to change the diarization model.
|
||||
- **WhisperX**: Set the `whisper_type` to `"whisperX"` for enhanced performance on CPU and use their enhanced models. (Model names are the same)
|
||||
- **Keyword Arguments**: A variety of different `kwargs` are available:
|
||||
- `use_auth_token`: Pass a Hugging Face token to the Pyannote backend if you want to use one of the models hosted on their Hugging Face.
|
||||
- `verbose`: Enable this to add an additional level of verbosity.
|
||||
|
||||
In general, you should be able to input any `kwargs` that you can input in the original Whisper (WhisperX) and Pyannote Python APIs.
|
||||
|
||||
As input, `autotranscribe` accepts every format compatible with [FFmpeg](https://ffmpeg.org/ffmpeg-formats.html). Examples include `.mp4`, `.mp3`, `.wav`, `.ogg`, `.flac`, and many more.
|
||||
|
||||
To further control the pipeline of `ScrAIbe`, you can pass almost any keyword argument that is accepted by `Whisper` or `Pyannote`. For more options, refer to the documentation of these frameworks, as their keywords are likely to work here as well.
|
||||
|
||||
Here are some examples regarding `diarization` (which relies on the `pyannote` pipeline):
|
||||
|
||||
- `num_speakers`: Number of speakers in the audio file
|
||||
- `min_speakers`: Minimum number of speakers in the audio file
|
||||
- `max_speakers`: Maximum number of speakers in the audio file
|
||||
|
||||
Then there are arguments for the transcription process, which uses the "Whisper" model:
|
||||
|
||||
- `language`: Specify the language ([list of supported languages](https://github.com/openai/whisper/blob/main/language-breakdown.svg))
|
||||
- `task`: Can be either `transcribe` or `translate`. If `translate` is selected, the transcribed audio will be translated to English.
|
||||
|
||||
For example:
|
||||
|
||||
```python
|
||||
text = model.autotranscribe("audio.wav", language="german", num_speakers = 2)
|
||||
```
|
||||
|
||||
`Scraibe` also contains the option to just do a transcription:
|
||||
|
||||
```python
|
||||
transcription = model.transcribe("audio.wav")
|
||||
```
|
||||
|
||||
or just do a diarization:
|
||||
|
||||
```python
|
||||
diarization = model.diarization("audio.wav")
|
||||
```
|
||||
|
||||
Start exploring the powerful features of ScrAIbe and customize it to fit your specific transcription and diarization needs!
|
||||
|
||||
### Command-line usage
|
||||
|
||||
Next to the Pyhton interface, you can also run ScrAIbe using the command-line interface:
|
||||
|
||||
```bash
|
||||
scraibe -f "audio.wav" --language "german" --num_speakers 2
|
||||
```
|
||||
|
||||
For the full list of options, run:
|
||||
|
||||
```bash
|
||||
scraibe -h
|
||||
```
|
||||
|
||||
This will display a comprehensive list of all command-line options, allowing you to tailor ScrAIbe’s functionality to your specific needs.
|
||||
|
||||
## Gradio App 🌐
|
||||
|
||||
The Gradio App is now part of ScrAIbe-WebUI! This user-friendly interface enables you to run the model without any coding knowledge. You can easily run the app in your browser and upload your audio files, or make the framework available on your network and run it on your local machine. 🚀
|
||||
|
||||
All functionalities previously available in the Gradio App are now part of the ScrAIbe-WebUI. For more information and detailed instructions, visit the [ScrAIbe-WebUI GitHub repository](https://github.com/JSchmie/ScrAIbe-WebUI).
|
||||
|
||||
## Docker Container 🐳
|
||||
|
||||
ScrAIbe's Docker containers have also moved to ScrAIbe-WebUI! This option is especially useful if you want to run the model on a server or if you would like to use the GPU without dealing with CUDA.
|
||||
|
||||
All Docker container functionalities are now part of ScrAIbe-WebUI. For more information and detailed instructions on how to use the Docker containers, please visit the [ScrAIbe-WebUI GitHub repository](https://github.com/JSchmie/ScrAIbe-WebUI).
|
||||
|
||||
---
|
||||
|
||||
With these changes, ScrAIbe focuses on its core functionalities while the enhanced Gradio App and related Docker containers are now part of ScrAIbe-WebUI. Enjoy a more streamlined and powerful transcription experience! 🎉
|
||||
|
||||
## Documentation 📚
|
||||
|
||||
For comprehensive guides, detailed instructions, and advanced usage tips, visit our [documentation page](https://jschmie.github.io/ScrAIbe/). Here, you will find everything you need to make the most out of ScrAIbe.
|
||||
|
||||
### Contributions 🤝
|
||||
|
||||
We warmly welcome contributions from the community! Whether you’re fixing bugs, adding new features, or improving documentation, your help is invaluable. Please see our [Contributing Guidelines](./CONTRIBUTING.md) for more information on how to get involved and make your mark on ScrAIbe-WebUI.
|
||||
|
||||
|
||||
### License 📜
|
||||
|
||||
ScrAIbe-WebUI is proudly open source and licensed under the GPL-3.0 license. This promotes a collaborative and transparent development process. For more details, see the [LICENSE](./LICENSE) file in this repository.
|
||||
|
||||
## Acknowledgments
|
||||
|
||||
Special thanks go to the [KIDA](https://www.kida-bmel.de/) project and the [BMEL (Bundesministerium für Ernährung und Landwirtschaft)](https://www.bmel.de/EN/Home/home_node.html), especially to the AI Consultancy Team.
|
||||
|
||||
---
|
||||
|
||||
Join us in making ScrAIbe even better! 🚀
|
||||
- WEBUI_TITLE:
|
||||
- Title shown in the web GUI (default: A.P.Strom Transcription).
|
||||
- WEBUI_LOGO_URL:
|
||||
- URL of the logo displayed in the web GUI header.
|
||||
- Example: https://your-domain.com/logo.png
|
||||
|
||||
Accent color (UI and emails):
|
||||
|
||||
- EMAIL_ACCENT_COLOR:
|
||||
- Accent color used in:
|
||||
- Web GUI buttons and accents
|
||||
- Email headings, links, and email addresses
|
||||
- Default: #7C6DA0
|
||||
|
||||
Async processing (Celery + Redis):
|
||||
|
||||
- CELERY_BROKER_URL:
|
||||
- Redis broker URL (default: redis://localhost:6379/0).
|
||||
- CELERY_RESULT_BACKEND:
|
||||
- Redis backend URL (default: redis://localhost:6379/0).
|
||||
- SCRAIBE_UPLOAD_DIR:
|
||||
- Directory where uploaded audio is stored (default: /tmp/scraibe_uploads).
|
||||
|
||||
Email configuration:
|
||||
|
||||
- EMAIL_SMTP_HOST:
|
||||
- SMTP server host.
|
||||
- EMAIL_SMTP_PORT:
|
||||
- SMTP server port (e.g., 587).
|
||||
- EMAIL_SMTP_USER:
|
||||
- SMTP username.
|
||||
- EMAIL_SMTP_PASSWORD:
|
||||
- SMTP password.
|
||||
- EMAIL_SMTP_USE_TLS:
|
||||
- Use TLS (true/false; default: true).
|
||||
- EMAIL_FROM_ADDRESS:
|
||||
- Sender address (e.g., "ScrAIbe <transcribe@your-domain.com>").
|
||||
- EMAIL_CONTACT_ADDRESS:
|
||||
- Support contact address shown in email templates.
|
||||
- EMAIL_LOGO_URL:
|
||||
- URL of the logo used in emails (preferred).
|
||||
- EMAIL_LOGO_PATH:
|
||||
- Fallback local path for email logo (default: /app/src/misc/logo1.png).
|
||||
- EMAIL_CSS_PATH:
|
||||
- Path to the CSS used in emails (default: /app/src/misc/mail_style.css).
|
||||
|
||||
Email subject lines (customizable):
|
||||
|
||||
- EMAIL_SUBJECT_UPLOAD:
|
||||
- Subject for upload confirmation email.
|
||||
- Default: "ScrAIbe: Your transcription request has been received"
|
||||
- EMAIL_SUBJECT_SUCCESS:
|
||||
- Subject for transcript-ready email.
|
||||
- Default: "ScrAIbe: Your transcript is ready"
|
||||
- EMAIL_SUBJECT_ERROR:
|
||||
- Subject for error notification email.
|
||||
- Default: "ScrAIbe: Error with your transcription request"
|
||||
|
||||
Output files (async web GUI):
|
||||
|
||||
When a job completes, the user receives:
|
||||
|
||||
- Transcript:
|
||||
- .md file
|
||||
- .docx file (line-numbered, no cover page)
|
||||
- Summary (if requested):
|
||||
- .md file
|
||||
- .docx file (no line numbering, no cover page)
|
||||
- JSON:
|
||||
- Structured transcript with diarization and metadata
|
||||
|
||||
All of these can also be overridden from the CLI when needed (e.g., --localai-api-url, --summarizer-api-url).
|
||||
|
||||
## Dependencies
|
||||
|
||||
Core runtime dependencies:
|
||||
|
||||
- Python 3.9+
|
||||
- httpx
|
||||
- numpy
|
||||
- tqdm
|
||||
- gradio
|
||||
- celery[redis]
|
||||
- redis
|
||||
- python-docx
|
||||
- ffmpeg (for audio preprocessing)
|
||||
|
||||
No local Whisper, PyTorch, or Pyannote models are required.
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions are welcome. Please refer to CONTRIBUTING.md for guidelines.
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under GPL-3.0. See LICENSE for details.
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
## Custom configuration for A.P.Strom Transcription (LocalAI-backed)
|
||||
## Lines that start with ## are comment lines.
|
||||
|
||||
interface_type: async # async or simple (one does transcriptions, requires Email setup)
|
||||
|
||||
launch:
|
||||
## Gradio launch options (if using WebUI)
|
||||
# server_port: null
|
||||
# server_name: "A.P.Strom Transcription"
|
||||
# inline: false
|
||||
# inbrowser: false
|
||||
# share: false
|
||||
# debug: false
|
||||
max_threads: 18
|
||||
# quiet: false
|
||||
# auth: null # tuple of username and password
|
||||
# auth_message: null
|
||||
# prevent_thread_lock: false
|
||||
# show_error: false
|
||||
# height: 500
|
||||
# width: 100%
|
||||
favicon_path: /app/src/misc/logo.png
|
||||
# ssl_keyfile: null
|
||||
# ssl_certfile: null
|
||||
# ssl_keyfile_password: null
|
||||
# ssl_verify: false
|
||||
# show_api: false
|
||||
# allowed_paths:
|
||||
# blocked_paths: null
|
||||
# root_path: null
|
||||
# app_kwargs: null
|
||||
# state_session_capacity: 10000
|
||||
# share_server_address: null
|
||||
# share_server_protocol: null
|
||||
# max_file_size: null
|
||||
# enable_monitoring: null
|
||||
|
||||
queue:
|
||||
## Queue configuration
|
||||
# status_update_rate: 'auto'
|
||||
# api_open: null
|
||||
max_size: 10
|
||||
# default_concurrency_limit:
|
||||
|
||||
layout:
|
||||
show_settings: false
|
||||
header: /app/src/misc/header.html
|
||||
header_format_options:
|
||||
header_logo_url: https://apstrom.ca/
|
||||
header_logo_src: /app/src/misc/logo.png
|
||||
footer: /app/src/misc/footer.html
|
||||
footer_format_options:
|
||||
# footer_css_path: /app/src/misc/footer_style.css
|
||||
footer_scraibe_webui_version: "0.1.1-dev"
|
||||
|
||||
scraibe_params:
|
||||
## LocalAI (transcription + diarization)
|
||||
localai_api_url: http://localhost:8080
|
||||
localai_api_key: ""
|
||||
localai_model: vibevoice-cpp-asr
|
||||
|
||||
## Summarizer LLM (for transcript_and_summarize)
|
||||
summarizer_api_url: http://localhost:8080
|
||||
summarizer_api_key: ""
|
||||
summarizer_model: qwen3-14b
|
||||
|
||||
## Legacy Whisper/Pyannote fields (ignored by LocalAI client; kept for compatibility)
|
||||
whisper_model: large-v3
|
||||
whisper_type: whisper
|
||||
dia_model: null
|
||||
use_auth_token: null
|
||||
device: cpu
|
||||
num_threads: 18
|
||||
|
||||
mail:
|
||||
sender_email: scribe@apstrom.ca
|
||||
smtp_server: mail.apstrom.ca
|
||||
smtp_port: 587
|
||||
sender_password: ""
|
||||
connection_type: TLS # 'SSL', 'TLS', or 'PLAIN'
|
||||
context: default
|
||||
default_subject: "A.P.Strom audio transcription"
|
||||
error_template: /app/src/misc/error_notification_template.html
|
||||
error_subject: "error"
|
||||
error_format_options:
|
||||
## exception is mandatory for your html; will be set to the related exception in the Code
|
||||
contact_email: support@apstrom.ca
|
||||
success_template: /app/src/misc/success_template.html
|
||||
success_subject: "ready"
|
||||
success_format_options:
|
||||
contact_email: info@apstrom.ca
|
||||
upload_notification_template: /app/src/misc/upload_notification_template.html
|
||||
upload_subject: "upload successful"
|
||||
upload_notification_format_options:
|
||||
queue_position: null
|
||||
contact_email: info@apstrom.ca
|
||||
# mail_css_path: /app/src/misc/mail_style.css
|
||||
|
||||
advanced:
|
||||
keep_model_alive: false # for sync interface only; keeps the model alive during a session
|
||||
concurrent_workers_async: 2 # number of concurrent working threads in the async interface
|
||||
@@ -0,0 +1,31 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Error Notification</title>
|
||||
<style>
|
||||
{email_css}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1 style="color:{accent_color};">Error Notification</h1>
|
||||
<p>Dear user,</p>
|
||||
<p>An error occurred while processing your audio file. This means that something went wrong during the processing of your file, and it could not be completed successfully.</p>
|
||||
<p class="error-message">Error details: {exception}</p>
|
||||
<p>Please check the file and try again. If the problem persists, our support team is here to help.</p>
|
||||
<div class="contact">
|
||||
<p>You can contact our support team at <a href="mailto:{contact_email}" style="color:{accent_color};">{contact_email}</a>. They are available to assist with any questions or issues you may have.</p>
|
||||
</div>
|
||||
<div class="disclaimer">
|
||||
<p>Please note that our support team does not have the ability to fix processing errors directly or access the files you have uploaded. They can provide guidance and help troubleshoot any issues you may encounter.</p>
|
||||
</div>
|
||||
<div class="signature">
|
||||
<p>Thank you for using our transcription service!</p>
|
||||
<p>A.P.Strom</p>
|
||||
</div>
|
||||
{email_logo}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,119 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Footer</title>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
|
||||
|
||||
<style>
|
||||
/* Styles from footer_style.css */
|
||||
|
||||
/* Resetting margins and paddings */
|
||||
html, body {{
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}}
|
||||
body {{
|
||||
font-family: Arial, sans-serif;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: space-between; /* Ensures footer stays at the bottom */
|
||||
}}
|
||||
.footer {{
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 20px;
|
||||
/* Removed background-color to inherit from parent */
|
||||
font-size: 16px;
|
||||
color: #333;
|
||||
width: 100%; /* Ensure footer is full width */
|
||||
box-sizing: border-box; /* Padding is included in the width */
|
||||
}}
|
||||
.footer > div:first-child {{
|
||||
margin-left: -20px;
|
||||
padding-left: 0; /* Adjust if necessary */
|
||||
}}
|
||||
.footer div, .footer a {{
|
||||
margin: 0 5px;
|
||||
}}
|
||||
.footer div {{
|
||||
text-align: left;
|
||||
}}
|
||||
.footer a {{
|
||||
color: {accent_color};
|
||||
transition: color 0.3s ease;
|
||||
}}
|
||||
.footer a:hover {{
|
||||
color: #50AF31;
|
||||
}}
|
||||
.foot-text {{
|
||||
text-align: center;
|
||||
width: 80%;
|
||||
margin-bottom: 15px;
|
||||
font-size: 14px;
|
||||
color: #333;
|
||||
}}
|
||||
.brand-section {{
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
text-align: center;
|
||||
}}
|
||||
.brand-icon a {{
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 50px;
|
||||
height: 50px;
|
||||
border-radius: 50%;
|
||||
background-color: transparent; /* Ensure transparency */
|
||||
text-decoration: none !important;
|
||||
color: white;
|
||||
transition: background-color 0.3s ease, transform 0.3s ease;
|
||||
}}
|
||||
.brand-icon i {{
|
||||
font-size: 24px;
|
||||
}}
|
||||
.brand-icon a:hover, .brand-icon a:focus {{
|
||||
background-color: {accent_color};
|
||||
transform: scale(1.1);
|
||||
text-decoration: none;
|
||||
}}
|
||||
.brand-icon a, .brand-icon a:hover, .brand-icon a:active, .brand-icon a:visited {{
|
||||
text-decoration: none;
|
||||
}}
|
||||
.build-version {{
|
||||
margin-top: 8px;
|
||||
color: white; /* Adjust as needed */
|
||||
font-size: 12px;
|
||||
}}
|
||||
/* Removed dark mode media query to let Gradio handle theming */
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<div class="footer">
|
||||
<div class="foot-text">
|
||||
<h2 style="font-weight: bold; color: {accent_color};">Disclaimer</h2>
|
||||
<p>The transcription completed by this application may contain errors.</p>
|
||||
<p>Users must take care to review transcripts before circulating to ensure that they are error-free and complete.</p>
|
||||
<p>The transcripts produced by this application do not replace a court reporter's transcription. The transcripts completed by this application are for the user's convenience only.</p>
|
||||
<h2 style="font-weight: bold; color: {accent_color};">Data retention</h2>
|
||||
<p>Audio or video files uploaded to this application are only retained for the time that it takes to complete the transcription. All transcripts are deleted after they are transmitted to the user.</p>
|
||||
</div>
|
||||
<div class="brand-section">
|
||||
<div class="brand-icon">
|
||||
<a href="https://apstrom.ca" aria-label="A.P.Strom">
|
||||
<i class="fas fa-globe"></i>
|
||||
</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="build-version">Build-Version: {footer_scraibe_webui_version}</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,100 @@
|
||||
/* footer_style.css */
|
||||
|
||||
/* Resetting margins and paddings */
|
||||
html, body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: space-between; /* Ensures footer stays at the bottom */
|
||||
}
|
||||
.footer {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 20px; /* Adjusted for demonstration */
|
||||
background-color: #F9FAFB; /* Fixed background color */
|
||||
font-size: 16px;
|
||||
color: #333;
|
||||
width: 100%; /* Ensure footer is full width */
|
||||
box-sizing: border-box; /* Padding is included in the width */
|
||||
}
|
||||
.footer > div:first-child {
|
||||
margin-left: -20px;
|
||||
padding-left: 0; /* Reducing or eliminating left padding if it's causing the shift */
|
||||
}
|
||||
.footer div, .footer a {
|
||||
margin: 0 5px;
|
||||
}
|
||||
.footer div {
|
||||
text-align: left;
|
||||
}
|
||||
.footer a {
|
||||
color: #333;
|
||||
transition: color 0.3s ease;
|
||||
}
|
||||
.footer a:hover {
|
||||
color: #50AF31;
|
||||
}
|
||||
.github-section {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
text-align: center;
|
||||
}
|
||||
.github-icon a {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 50px;
|
||||
height: 50px;
|
||||
border-radius: 50%;
|
||||
background-color: none;
|
||||
text-decoration: none !important; /* Removes underline */
|
||||
color: white;
|
||||
transition: background-color 0.3s ease, transform 0.3s ease;
|
||||
}
|
||||
.github-icon i {
|
||||
font-size: 24px;
|
||||
}
|
||||
.github-icon a:hover, .github-icon a:focus {
|
||||
background-color: #50AF31;
|
||||
transform: scale(1.1);
|
||||
text-decoration: none; /* Removes underline */
|
||||
}
|
||||
.github-icon a, .github-icon a:hover, .github-icon a:active, .github-icon a:visited {
|
||||
text-decoration: none;
|
||||
}
|
||||
.build-version {
|
||||
margin-top: 8px; /* Adjust spacing between the icon and the text as needed */
|
||||
color: white; /* Adjust text color as needed */
|
||||
font-size: 12px; /* Adjust font size as needed */
|
||||
}
|
||||
|
||||
/* Dark mode styles */
|
||||
@media (prefers-color-scheme: dark) {
|
||||
body {
|
||||
background-color: #121212;
|
||||
color: #FFFFFF;
|
||||
}
|
||||
.footer {
|
||||
background-color: transparent; /* Make footer background transparent */
|
||||
color: #FFFFFF;
|
||||
}
|
||||
.footer a {
|
||||
color: #FFFFFF;
|
||||
}
|
||||
.footer a:hover {
|
||||
color: #50AF31;
|
||||
}
|
||||
.build-version {
|
||||
color: #CCCCCC; /* Adjust text color for better contrast in dark mode */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>{webui_title}</title>
|
||||
|
||||
<!-- Importing Cormorant Garamond font from Google Fonts -->
|
||||
<link href="https://fonts.googleapis.com/css2?family=Cormorant+Garamond:wght@400;700&display=swap" rel="stylesheet">
|
||||
|
||||
<style>
|
||||
.header-wrapper {{
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
padding: 20px 20px 0;
|
||||
box-sizing: border-box;
|
||||
}}
|
||||
|
||||
.logo-container {{
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin-bottom: 10px;
|
||||
}}
|
||||
|
||||
.logo {{
|
||||
width: 75px;
|
||||
height: auto;
|
||||
display: block;
|
||||
}}
|
||||
|
||||
.header-title {{
|
||||
font-family: 'Cormorant Garamond', serif;
|
||||
font-size: 45px;
|
||||
font-weight: bold;
|
||||
color: {accent_color};
|
||||
margin: 0;
|
||||
position: relative;
|
||||
padding: 0.4em 0;
|
||||
text-align: center;
|
||||
max-width: 90%;
|
||||
}}
|
||||
|
||||
.header-title::before,
|
||||
.header-title::after {{
|
||||
content: "";
|
||||
position: absolute;
|
||||
height: 2px;
|
||||
width: 80%;
|
||||
background-color: {accent_color};
|
||||
left: 10%;
|
||||
}}
|
||||
|
||||
.header-title::before {{
|
||||
top: 0.4em;
|
||||
}}
|
||||
|
||||
.header-title::after {{
|
||||
bottom: 0.4em;
|
||||
}}
|
||||
|
||||
.header-description {{
|
||||
text-align: center;
|
||||
padding: 10px 40px 20px;
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
}}
|
||||
|
||||
.header-description p,
|
||||
.header-description h2 {{
|
||||
font-size: 15px;
|
||||
margin: 8px 0;
|
||||
line-height: 1.5;
|
||||
}}
|
||||
|
||||
.header-description h2 {{
|
||||
font-weight: bold;
|
||||
color: {accent_color};
|
||||
}}
|
||||
|
||||
@media (max-width: 768px) {{
|
||||
.header-title {{
|
||||
font-size: 31px;
|
||||
}}
|
||||
|
||||
.header-title::before,
|
||||
.header-title::after {{
|
||||
width: 80%;
|
||||
left: 10%;
|
||||
}}
|
||||
|
||||
.logo {{
|
||||
width: 50px;
|
||||
}}
|
||||
|
||||
.header-description {{
|
||||
padding: 10px 20px 15px;
|
||||
}}
|
||||
}}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="header-wrapper">
|
||||
<div class="logo-container">
|
||||
<a href="{header_logo_url}">
|
||||
<img src="{header_logo_src}" alt="{webui_title}" class="logo">
|
||||
</a>
|
||||
</div>
|
||||
<h1 class="header-title">{webui_title}</h1>
|
||||
</div>
|
||||
<div class="header-description">
|
||||
<p>
|
||||
Upload, record, or provide a video with audio for transcription. Our toolkit is designed to transcribe content from multiple languages accurately. The integrated speaker diarisation feature identifies different speakers, ensuring a smooth transcription experience. For optimal results, indicate the number of speakers and the original language of the content.
|
||||
</p>
|
||||
<h2>Start your transcription below.</h2>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,58 @@
|
||||
/* header_style.css */
|
||||
|
||||
/* Importing Cormorant Garamond font from Google Fonts */
|
||||
@import url('https://fonts.googleapis.com/css2?family=Cormorant+Garamond:wght@400;700&display=swap');
|
||||
|
||||
.header-container {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
position: relative;
|
||||
padding-top: 30px;
|
||||
}
|
||||
|
||||
.logo-container {
|
||||
position: absolute;
|
||||
top: 50%;
|
||||
right: 20px;
|
||||
transform: translateY(-50%);
|
||||
width: 300px;
|
||||
}
|
||||
|
||||
.logo {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-family: 'Cormorant Garamond', serif;
|
||||
font-size: 50px !important; /* Increased font size */
|
||||
font-weight: bold;
|
||||
color: #50AF31;
|
||||
margin: 0;
|
||||
position: relative;
|
||||
padding: 0.5em 0;
|
||||
}
|
||||
|
||||
h1::before, h1::after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
height: 2px;
|
||||
width: 80%;
|
||||
background-color: #50AF31;
|
||||
left: 10%;
|
||||
}
|
||||
|
||||
h1::before {
|
||||
top: 0.5em;
|
||||
}
|
||||
|
||||
h1::after {
|
||||
bottom: 0.5em;
|
||||
}
|
||||
|
||||
p, h2 {
|
||||
font-size: 16px;
|
||||
margin: 10px 0;
|
||||
line-height: 1.4;
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 152 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 111 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 111 KiB |
@@ -0,0 +1,61 @@
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
line-height: 1.5;
|
||||
background-color: #ffffff;
|
||||
color: #333;
|
||||
margin: 0;
|
||||
padding: 20px;
|
||||
}
|
||||
.container {
|
||||
width: 100%;
|
||||
max-width: 600px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
border: 1px solid #ddd;
|
||||
border-radius: 5px;
|
||||
}
|
||||
h1, h2, h3 {
|
||||
font-size: 1.5em;
|
||||
margin-top: 0;
|
||||
color: {accent_color};
|
||||
}
|
||||
p {
|
||||
margin: 10px 0;
|
||||
font-size: 1em;
|
||||
}
|
||||
.error-message, .success-message {
|
||||
padding: 10px 0;
|
||||
margin-bottom: 15px;
|
||||
font-size: 1em;
|
||||
}
|
||||
.error-message {
|
||||
color: #721c24;
|
||||
}
|
||||
.success-message {
|
||||
color: #155724;
|
||||
}
|
||||
.contact {
|
||||
margin-top: 15px;
|
||||
font-size: 0.9em;
|
||||
color: #555;
|
||||
}
|
||||
.contact a {
|
||||
color: {accent_color};
|
||||
text-decoration: none;
|
||||
}
|
||||
.contact a:hover {
|
||||
text-decoration: underline;
|
||||
}
|
||||
a {
|
||||
color: {accent_color};
|
||||
}
|
||||
.disclaimer {
|
||||
margin-top: 20px;
|
||||
font-size: 0.8em;
|
||||
color: #777;
|
||||
}
|
||||
.signature {
|
||||
margin-top: 20px;
|
||||
font-size: 0.8em;
|
||||
color: #555;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Transcript Ready</title>
|
||||
<style>
|
||||
{email_css}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1 style="color:{accent_color};">Transcript Ready</h1>
|
||||
<p>Dear user,</p>
|
||||
<p>Your file has been successfully processed, and the transcript is now ready. The transcript of your audio or video file is attached to this email.</p>
|
||||
<p>We hope you find the transcript useful. If you have any questions or need further assistance, please do not hesitate to contact our support team.</p>
|
||||
<div class="contact">
|
||||
<p>You can reach our support team at <a href="mailto:{contact_email}" style="color:{accent_color};">{contact_email}</a>. They are available to help with any questions or issues you may have.</p>
|
||||
</div>
|
||||
<div class="disclaimer">
|
||||
<p>Please note that our support team cannot modify the content of the transcript. They can assist with any other questions or concerns you may have.</p>
|
||||
</div>
|
||||
<div class="signature">
|
||||
<p>Thank you for using our transcription service!</p>
|
||||
<p>A.P.Strom</p>
|
||||
</div>
|
||||
{email_logo}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,30 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Upload Successful</title>
|
||||
<style>
|
||||
{email_css}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1 style="color:{accent_color};">Upload Successful</h1>
|
||||
<p>Dear user,</p>
|
||||
<p>Your file has been successfully uploaded and is now in our processing queue. This means that our system has received your file, and it is waiting to be processed. We will handle your file as soon as possible.</p>
|
||||
<p>We will notify you once your file has been processed. If you have any urgent needs or further questions, feel free to reach out to our support team.</p>
|
||||
<div class="contact">
|
||||
<p>You can contact our support team at <a href="mailto:{contact_email}" style="color:{accent_color};">{contact_email}</a>. Please note that our support team is here to help with any questions or issues you might have.</p>
|
||||
</div>
|
||||
<div class="disclaimer">
|
||||
<p>Please note that our support team does not have the ability to change your position in the queue or access the files you have uploaded. They are here to provide assistance and answer any questions you might have about the process.</p>
|
||||
</div>
|
||||
<div class="signature">
|
||||
<p>Thank you for using our transcription service!</p>
|
||||
<p>A.P.Strom</p>
|
||||
</div>
|
||||
{email_logo}
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
@@ -72,6 +72,9 @@ scraibe = "scraibe.cli:cli"
|
||||
[tool.poetry.extras]
|
||||
app = ["scraibe-webui"]
|
||||
|
||||
[tool.ruff]
|
||||
line-length = 58
|
||||
|
||||
[tool.ruff.lint.extend-per-file-ignores]
|
||||
"__init__.py" = ["E402", "F403", "F401"]
|
||||
"scraibe/misc.py" = ["E722"]
|
||||
|
||||
@@ -1,3 +1,8 @@
|
||||
tqdm>=4.66.5
|
||||
numpy>=1.26.4
|
||||
httpx>=0.28.0
|
||||
gradio>=5.0.0
|
||||
PyYAML>=6.0
|
||||
celery[redis]>=5.3.0
|
||||
redis>=5.0.0
|
||||
python-docx>=1.1.0
|
||||
|
||||
@@ -5,6 +5,4 @@ from .audio import AudioProcessor
|
||||
from .transcript_exporter import Transcript
|
||||
from .misc import set_threads, ParseKwargs
|
||||
|
||||
from .cli import cli
|
||||
|
||||
from ._version import __version__
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
"""
|
||||
Entrypoint for running ScrAIbe as a module:
|
||||
|
||||
python -m scraibe
|
||||
|
||||
Always launches the Web GUI (Gradio), never the CLI.
|
||||
"""
|
||||
|
||||
from .webui import create_app
|
||||
|
||||
if __name__ == "__main__":
|
||||
create_app()
|
||||
@@ -7,13 +7,21 @@ Simplified audio processor for ScrAIbe.
|
||||
Previously this used torch and pyannote-style processing. In the LocalAI-backed
|
||||
version, we primarily pass files to the API, but we keep a lightweight helper
|
||||
for backward compatibility.
|
||||
|
||||
Now also includes utilities for chunking long audio into smaller segments
|
||||
to avoid GPU memory limits when using vibevoice-cpp on LocalAI.
|
||||
"""
|
||||
|
||||
import json
|
||||
import os
|
||||
import tempfile
|
||||
from subprocess import CalledProcessError, run
|
||||
import numpy as np
|
||||
|
||||
SAMPLE_RATE = 16000
|
||||
NORMALIZATION_FACTOR = 32768.0
|
||||
DEFAULT_CHUNK_DURATION = 180.0 # seconds
|
||||
DEFAULT_CHUNK_OVERLAP = 2.0 # seconds
|
||||
|
||||
|
||||
class AudioProcessor:
|
||||
@@ -106,3 +114,109 @@ class AudioProcessor:
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"AudioProcessor(waveform_len={len(self.waveform)}, sr={self.sr})"
|
||||
|
||||
|
||||
def get_audio_duration(file_path: str) -> float:
|
||||
"""
|
||||
Get the duration of an audio file in seconds using ffprobe.
|
||||
|
||||
Args:
|
||||
file_path: Path to the audio file.
|
||||
|
||||
Returns:
|
||||
Duration in seconds as a float.
|
||||
|
||||
Raises:
|
||||
RuntimeError: If ffprobe fails.
|
||||
"""
|
||||
cmd = [
|
||||
"ffprobe",
|
||||
"-v", "error",
|
||||
"-show_entries", "format=duration",
|
||||
"-of", "json",
|
||||
file_path,
|
||||
]
|
||||
try:
|
||||
result = run(cmd, capture_output=True, text=True, check=True)
|
||||
data = json.loads(result.stdout)
|
||||
return float(data["format"]["duration"])
|
||||
except (CalledProcessError, json.JSONDecodeError, KeyError) as e:
|
||||
raise RuntimeError(f"Failed to get audio duration for {file_path}: {e}")
|
||||
|
||||
|
||||
def split_audio_into_chunks(
|
||||
input_path: str,
|
||||
max_duration: float = DEFAULT_CHUNK_DURATION,
|
||||
overlap: float = DEFAULT_CHUNK_OVERLAP,
|
||||
output_format: str = "wav",
|
||||
sample_rate: int = 24000,
|
||||
) -> list:
|
||||
"""
|
||||
Split a long audio file into overlapping chunks using ffmpeg.
|
||||
|
||||
Args:
|
||||
input_path: Path to the input audio file.
|
||||
max_duration: Maximum duration of each chunk in seconds.
|
||||
overlap: Overlap duration in seconds between consecutive chunks.
|
||||
output_format: Output format (e.g., 'wav').
|
||||
sample_rate: Sample rate for output chunks.
|
||||
|
||||
Returns:
|
||||
List of dicts:
|
||||
[{"path": "chunk.wav", "start": 0.0, "end": 180.0}, ...]
|
||||
Files must be cleaned up by the caller.
|
||||
"""
|
||||
duration = get_audio_duration(input_path)
|
||||
|
||||
# If file is shorter than max_duration, no need to split
|
||||
if duration <= max_duration:
|
||||
return [{"path": input_path, "start": 0.0, "end": duration}]
|
||||
|
||||
chunks = []
|
||||
start = 0.0
|
||||
chunk_id = 0
|
||||
|
||||
while start < duration:
|
||||
chunk_end = min(start + max_duration, duration)
|
||||
chunk_duration = chunk_end - start
|
||||
|
||||
tmp = tempfile.NamedTemporaryFile(
|
||||
delete=False,
|
||||
suffix=f".{output_format}",
|
||||
prefix="scraibe_chunk_",
|
||||
)
|
||||
chunk_path = tmp.name
|
||||
tmp.close()
|
||||
|
||||
cmd = [
|
||||
"ffmpeg",
|
||||
"-y",
|
||||
"-nostdin",
|
||||
"-ss", str(start),
|
||||
"-i", input_path,
|
||||
"-t", str(chunk_duration),
|
||||
"-ar", str(sample_rate),
|
||||
"-ac", "1",
|
||||
"-c:a", "pcm_s16le",
|
||||
chunk_path,
|
||||
]
|
||||
try:
|
||||
run(cmd, capture_output=True, check=True)
|
||||
except CalledProcessError as e:
|
||||
# Clean up on error
|
||||
if os.path.exists(chunk_path):
|
||||
os.remove(chunk_path)
|
||||
raise RuntimeError(
|
||||
f"Failed to create audio chunk {chunk_id} for {input_path}: {e.stderr.decode()}"
|
||||
)
|
||||
|
||||
chunks.append({
|
||||
"path": chunk_path,
|
||||
"start": start,
|
||||
"end": chunk_end,
|
||||
})
|
||||
|
||||
start += max_duration - overlap
|
||||
chunk_id += 1
|
||||
|
||||
return chunks
|
||||
|
||||
+98
-17
@@ -16,12 +16,15 @@ but ignored when not relevant.
|
||||
"""
|
||||
|
||||
import os
|
||||
from typing import Union, Optional
|
||||
import logging
|
||||
from typing import Union, Optional, Dict, Any
|
||||
|
||||
from .localai_client import LocalAIClient, LocalAIError
|
||||
from .summarizer import SummarizerClient, SummarizerError
|
||||
from .transcript_exporter import Transcript
|
||||
|
||||
logger = logging.getLogger("scraibe.autotranscript")
|
||||
|
||||
|
||||
class Scraibe:
|
||||
"""
|
||||
@@ -68,6 +71,8 @@ class Scraibe:
|
||||
"""
|
||||
self.verbose = verbose or kwargs.get("verbose", False)
|
||||
|
||||
logger.info("Initializing Scraibe.")
|
||||
|
||||
try:
|
||||
self.client = LocalAIClient(
|
||||
api_url=api_url,
|
||||
@@ -75,6 +80,7 @@ class Scraibe:
|
||||
model=model,
|
||||
)
|
||||
except LocalAIError as e:
|
||||
logger.error("Failed to initialize LocalAI client: %s", e)
|
||||
raise LocalAIError(f"Failed to initialize LocalAI client: {e}")
|
||||
|
||||
# Summarizer is lazy-initialized if needed
|
||||
@@ -95,6 +101,7 @@ class Scraibe:
|
||||
if self._summarizer is not None:
|
||||
return self._summarizer
|
||||
|
||||
logger.info("Initializing SummarizerClient (lazy).")
|
||||
try:
|
||||
self._summarizer = SummarizerClient(
|
||||
api_url=api_url,
|
||||
@@ -102,6 +109,7 @@ class Scraibe:
|
||||
model=model,
|
||||
)
|
||||
except SummarizerError as e:
|
||||
logger.error("Failed to initialize Summarizer client: %s", e)
|
||||
raise SummarizerError(f"Failed to initialize Summarizer client: {e}")
|
||||
|
||||
return self._summarizer
|
||||
@@ -112,21 +120,21 @@ class Scraibe:
|
||||
|
||||
def transcribe(
|
||||
self,
|
||||
audio_file: Union[str],
|
||||
audio_file: str,
|
||||
*,
|
||||
for_export: bool = False,
|
||||
**kwargs,
|
||||
) -> str:
|
||||
) -> Union[str, Dict[str, Any]]:
|
||||
"""
|
||||
Transcribe the provided audio file using LocalAI.
|
||||
|
||||
Uses /v1/audio/diarization with vibevoice.cpp, then concatenates
|
||||
all segment texts.
|
||||
|
||||
Args:
|
||||
audio_file (str): Path to the audio file.
|
||||
**kwargs: Additional keyword arguments (some forwarded, others ignored).
|
||||
|
||||
Uses /v1/audio/diarization with vibevoice.cpp (verbose_json).
|
||||
Returns:
|
||||
str: The concatenated transcribed text.
|
||||
- If for_export=False: plain transcript text (str).
|
||||
- If for_export=True: dict with:
|
||||
- transcript: plain text
|
||||
- segments: list[segment] with speaker labels
|
||||
- raw_result: full verbose_json from LocalAI (if present)
|
||||
"""
|
||||
if isinstance(audio_file, str):
|
||||
if not os.path.exists(audio_file):
|
||||
@@ -136,35 +144,78 @@ class Scraibe:
|
||||
"In LocalAI mode, audio_file must be a file path (str)."
|
||||
)
|
||||
|
||||
verbose = kwargs.get("verbose", self.verbose)
|
||||
verbose = kwargs.pop("verbose", self.verbose)
|
||||
logger.info("transcribe called for: %s", audio_file)
|
||||
|
||||
try:
|
||||
result = self.client.diarize_and_transcribe(
|
||||
audio_path=audio_file,
|
||||
include_text=True,
|
||||
verbose=verbose,
|
||||
return_raw=True,
|
||||
**kwargs,
|
||||
)
|
||||
except LocalAIError as e:
|
||||
logger.error("Error during LocalAI transcription: %s", e)
|
||||
raise LocalAIError(f"Error during LocalAI transcription: {e}")
|
||||
|
||||
segments = result.get("segments", [])
|
||||
speakers = result.get("speakers", [])
|
||||
transcripts = result.get("transcripts", [])
|
||||
return " ".join(t.strip() for t in transcripts if t.strip())
|
||||
|
||||
# Build simple transcript text
|
||||
if for_export:
|
||||
# Include speaker-labeled transcript
|
||||
lines = []
|
||||
for seg, speaker, text in zip(segments, speakers, transcripts):
|
||||
start, end = seg
|
||||
ts = self._format_timestamp(start)
|
||||
line = f"[{ts}] {speaker}: {text.strip()}"
|
||||
lines.append(line)
|
||||
full_text = "\n\n".join(lines)
|
||||
else:
|
||||
# Legacy: space-joined text
|
||||
full_text = " ".join(t.strip() for t in transcripts if t.strip())
|
||||
|
||||
logger.info("transcribe completed, length=%d chars", len(full_text))
|
||||
|
||||
if for_export:
|
||||
# Return richer structure for JSON export
|
||||
raw_result = result.get("raw_result")
|
||||
return {
|
||||
"transcript": full_text,
|
||||
"segments": [
|
||||
{
|
||||
"id": i,
|
||||
"speaker": sp,
|
||||
"start": seg[0],
|
||||
"end": seg[1],
|
||||
"text": txt,
|
||||
}
|
||||
for i, (seg, sp, txt) in enumerate(
|
||||
zip(segments, speakers, transcripts)
|
||||
)
|
||||
],
|
||||
"raw_result": raw_result if raw_result is not None else None,
|
||||
}
|
||||
|
||||
return full_text
|
||||
|
||||
def transcript_and_summarize(
|
||||
self,
|
||||
audio_file: Union[str],
|
||||
audio_file: str,
|
||||
*,
|
||||
summarizer_api_url: Optional[str] = None,
|
||||
summarizer_api_key: Optional[str] = None,
|
||||
summarizer_model: Optional[str] = None,
|
||||
for_export: bool = False,
|
||||
**kwargs,
|
||||
) -> dict:
|
||||
"""
|
||||
Transcribe the audio file and generate a detailed summary.
|
||||
|
||||
Steps:
|
||||
- Transcribe via LocalAI.
|
||||
- Transcribe via LocalAI (verbose_json).
|
||||
- Build a plain-text transcript (with speaker labels).
|
||||
- Summarize the transcript using the configured LLM.
|
||||
|
||||
@@ -172,6 +223,8 @@ class Scraibe:
|
||||
dict with:
|
||||
- transcript: full transcript text (with speaker labels)
|
||||
- summary: final detailed summary (markdown-ready)
|
||||
- segments: (if for_export) list[segment] with speaker labels
|
||||
- raw_result: (if for_export) full verbose_json from LocalAI
|
||||
"""
|
||||
if isinstance(audio_file, str):
|
||||
if not os.path.exists(audio_file):
|
||||
@@ -181,7 +234,8 @@ class Scraibe:
|
||||
"In LocalAI mode, audio_file must be a file path (str)."
|
||||
)
|
||||
|
||||
verbose = kwargs.get("verbose", self.verbose)
|
||||
verbose = kwargs.pop("verbose", self.verbose)
|
||||
logger.info("transcript_and_summarize called for: %s", audio_file)
|
||||
|
||||
# 1) Get diarized + transcribed result
|
||||
try:
|
||||
@@ -189,9 +243,11 @@ class Scraibe:
|
||||
audio_path=audio_file,
|
||||
include_text=True,
|
||||
verbose=verbose,
|
||||
return_raw=True,
|
||||
**kwargs,
|
||||
)
|
||||
except LocalAIError as e:
|
||||
logger.error("Error during LocalAI transcription: %s", e)
|
||||
raise LocalAIError(f"Error during LocalAI transcription: {e}")
|
||||
|
||||
segments = result.get("segments", [])
|
||||
@@ -199,6 +255,7 @@ class Scraibe:
|
||||
transcripts = result.get("transcripts", [])
|
||||
|
||||
if not segments:
|
||||
logger.warning("No segments returned; returning empty transcript/summary.")
|
||||
return {
|
||||
"transcript": "",
|
||||
"summary": "No transcript content to summarize.",
|
||||
@@ -213,6 +270,7 @@ class Scraibe:
|
||||
lines.append(line)
|
||||
|
||||
full_transcript = "\n\n".join(lines)
|
||||
logger.info("Built full transcript, length=%d chars", len(full_transcript))
|
||||
|
||||
# 3) Summarize
|
||||
try:
|
||||
@@ -222,18 +280,41 @@ class Scraibe:
|
||||
model=summarizer_model,
|
||||
)
|
||||
except SummarizerError as e:
|
||||
logger.error("Failed to initialize summarizer: %s", e)
|
||||
raise SummarizerError(f"Failed to initialize summarizer: {e}")
|
||||
|
||||
try:
|
||||
summary = summarizer.summarize_transcript(full_transcript)
|
||||
except SummarizerError as e:
|
||||
logger.error("Error during summarization: %s", e)
|
||||
raise SummarizerError(f"Error during summarization: {e}")
|
||||
|
||||
return {
|
||||
logger.info("transcript_and_summarize completed.")
|
||||
|
||||
out = {
|
||||
"transcript": full_transcript,
|
||||
"summary": summary,
|
||||
}
|
||||
|
||||
if for_export:
|
||||
# Add segments and raw_result for JSON export
|
||||
raw_result = result.get("raw_result")
|
||||
out["segments"] = [
|
||||
{
|
||||
"id": i,
|
||||
"speaker": sp,
|
||||
"start": seg[0],
|
||||
"end": seg[1],
|
||||
"text": txt,
|
||||
}
|
||||
for i, (seg, sp, txt) in enumerate(
|
||||
zip(segments, speakers, transcripts)
|
||||
)
|
||||
]
|
||||
out["raw_result"] = raw_result if raw_result is not None else None
|
||||
|
||||
return out
|
||||
|
||||
# -----------------
|
||||
# Helpers
|
||||
# -----------------
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
"""
|
||||
Celery application for async transcription jobs.
|
||||
"""
|
||||
|
||||
import os
|
||||
from celery import Celery
|
||||
|
||||
broker_url = os.getenv("CELERY_BROKER_URL", "redis://redis:6379/0")
|
||||
result_backend = os.getenv("CELERY_RESULT_BACKEND", "redis://redis:6379/0")
|
||||
|
||||
celery_app = Celery(
|
||||
"scraibe",
|
||||
broker=broker_url,
|
||||
backend=result_backend,
|
||||
)
|
||||
|
||||
celery_app.conf.update(
|
||||
task_routes={
|
||||
"scraibe.tasks.process_transcription_task": {"queue": "transcription"},
|
||||
},
|
||||
task_serializer="json",
|
||||
result_serializer="json",
|
||||
accept_content=["json"],
|
||||
timezone="UTC",
|
||||
enable_utc=True,
|
||||
)
|
||||
|
||||
celery_app.autodiscover_tasks(["scraibe.tasks"])
|
||||
+47
-11
@@ -9,9 +9,10 @@ This version is adapted for LocalAI-based transcription and diarization.
|
||||
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
from argparse import ArgumentParser, ArgumentDefaultsHelpFormatter
|
||||
from .autotranscript import Scraibe
|
||||
from .misc import set_threads
|
||||
from .misc import set_threads, setup_logging
|
||||
|
||||
|
||||
def cli():
|
||||
@@ -20,6 +21,11 @@ def cli():
|
||||
and diarize audio files via a LocalAI server.
|
||||
"""
|
||||
|
||||
# Initialize logging (can be overridden via --log-level)
|
||||
setup_logging(level=os.getenv("LOG_LEVEL", "INFO"))
|
||||
|
||||
logger = logging.getLogger("scraibe.cli")
|
||||
|
||||
def str2bool(string):
|
||||
str2val = {"True": True, "False": False}
|
||||
if string in str2val:
|
||||
@@ -181,20 +187,41 @@ def cli():
|
||||
help="Number of speakers in the audio.",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--log-level",
|
||||
type=str,
|
||||
default=None,
|
||||
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
|
||||
help="Override LOG_LEVEL env var for logging verbosity.",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# Apply log-level override if provided
|
||||
log_level = args.log_level or os.getenv("LOG_LEVEL", "INFO")
|
||||
setup_logging(level=log_level)
|
||||
logger.info("CLI starting with log_level=%s", log_level)
|
||||
|
||||
arg_dict = vars(args)
|
||||
|
||||
# configure output
|
||||
out_folder = arg_dict.pop("output_directory")
|
||||
os.makedirs(out_folder, exist_ok=True)
|
||||
logger.info("Output directory: %s", out_folder)
|
||||
|
||||
out_format = arg_dict.pop("output_format")
|
||||
|
||||
task = arg_dict.pop("task")
|
||||
|
||||
logger.info("Task: %s", task)
|
||||
logger.info("Output format: %s", out_format)
|
||||
|
||||
set_threads(arg_dict.pop("num_threads"))
|
||||
|
||||
# Read shared values once
|
||||
verbose = arg_dict.pop("verbose_output")
|
||||
language = arg_dict.pop("language")
|
||||
num_speakers = arg_dict.pop("num_speakers")
|
||||
|
||||
# Build kwargs for Scraibe (LocalAI-backed)
|
||||
class_kwargs = {
|
||||
"api_url": arg_dict.pop("localai_api_url"),
|
||||
@@ -205,38 +232,45 @@ def cli():
|
||||
"whisper_type": arg_dict.pop("whisper_type"),
|
||||
"dia_model": arg_dict.pop("diarization_directory"),
|
||||
"use_auth_token": arg_dict.pop("hf_token"),
|
||||
"verbose": arg_dict.pop("verbose_output"),
|
||||
"verbose": verbose,
|
||||
}
|
||||
|
||||
logger.info("LocalAI API URL: %s", class_kwargs["api_url"] or os.getenv("LOCALAI_API_URL", "<not set>"))
|
||||
logger.info("LocalAI Model: %s", class_kwargs["model"] or os.getenv("LOCALAI_MODEL", "<not set>"))
|
||||
|
||||
model = Scraibe(**class_kwargs)
|
||||
|
||||
if arg_dict["audio_files"]:
|
||||
audio_files = arg_dict.pop("audio_files")
|
||||
logger.info("Audio files: %s", audio_files)
|
||||
|
||||
if task == "transcribe":
|
||||
for audio in audio_files:
|
||||
logger.info("Starting 'transcribe' for: %s", audio)
|
||||
out = model.transcribe(
|
||||
audio,
|
||||
language=arg_dict.pop("language"),
|
||||
verbose=arg_dict.pop("verbose_output"),
|
||||
num_speakers=arg_dict.pop("num_speakers"),
|
||||
language=language,
|
||||
verbose=verbose,
|
||||
num_speakers=num_speakers,
|
||||
)
|
||||
basename = audio.split("/")[-1].split(".")[0]
|
||||
path = os.path.join(out_folder, f"{basename}.{out_format}")
|
||||
print(f"Saving {basename}.{out_format} to {out_folder}")
|
||||
logger.info("Saving transcript to: %s", path)
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
f.write(out)
|
||||
logger.info("Transcript saved: %s", path)
|
||||
|
||||
elif task == "transcript_and_summarize":
|
||||
for audio in audio_files:
|
||||
logger.info("Starting 'transcript_and_summarize' for: %s", audio)
|
||||
result = model.transcript_and_summarize(
|
||||
audio,
|
||||
summarizer_api_url=arg_dict.pop("summarizer_api_url"),
|
||||
summarizer_api_key=arg_dict.pop("summarizer_api_key"),
|
||||
summarizer_model=arg_dict.pop("summarizer_model"),
|
||||
language=arg_dict.pop("language"),
|
||||
verbose=arg_dict.pop("verbose_output"),
|
||||
num_speakers=arg_dict.pop("num_speakers"),
|
||||
language=language,
|
||||
verbose=verbose,
|
||||
num_speakers=num_speakers,
|
||||
)
|
||||
|
||||
transcript_text = result.get("transcript", "")
|
||||
@@ -246,7 +280,7 @@ def cli():
|
||||
|
||||
# Always use .md for transcript_and_summarize
|
||||
md_path = os.path.join(out_folder, f"{basename}.md")
|
||||
print(f"Saving {basename}.md (transcript + summary) to {out_folder}")
|
||||
logger.info("Saving transcript + summary to: %s", md_path)
|
||||
|
||||
with open(md_path, "w", encoding="utf-8") as f:
|
||||
f.write("# Transcript\n\n")
|
||||
@@ -254,5 +288,7 @@ def cli():
|
||||
f.write("\n\n# Summary\n\n")
|
||||
f.write(summary_text)
|
||||
|
||||
logger.info("Transcript + summary saved: %s", md_path)
|
||||
|
||||
if __name__ == "__main__":
|
||||
cli()
|
||||
|
||||
@@ -0,0 +1,649 @@
|
||||
"""
|
||||
Email sender module for ScrAIbe.
|
||||
|
||||
Sends transcription outputs (TXT, JSON, etc.) via SMTP.
|
||||
All credentials are configured via environment variables.
|
||||
Supports both plain text and HTML email bodies.
|
||||
Template placeholders are primarily filled via environment variables.
|
||||
"""
|
||||
|
||||
import base64
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import smtplib
|
||||
from email import encoders
|
||||
from email.mime.base import MIMEBase
|
||||
from email.mime.multipart import MIMEMultipart
|
||||
from email.mime.text import MIMEText
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
from docx import Document
|
||||
from docx.oxml import OxmlElement
|
||||
from docx.oxml.ns import qn
|
||||
from docx.shared import Inches, Pt
|
||||
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
||||
|
||||
logger = logging.getLogger("scraibe.email_sender")
|
||||
|
||||
|
||||
class EmailError(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def get_email_config():
|
||||
"""
|
||||
Read email configuration from environment variables.
|
||||
Raises EmailError if required fields are missing.
|
||||
"""
|
||||
smtp_host = os.getenv("EMAIL_SMTP_HOST")
|
||||
smtp_port = os.getenv("EMAIL_SMTP_PORT")
|
||||
smtp_user = os.getenv("EMAIL_SMTP_USER")
|
||||
smtp_password = os.getenv("EMAIL_SMTP_PASSWORD")
|
||||
from_address = os.getenv("EMAIL_FROM_ADDRESS")
|
||||
use_tls_str = os.getenv("EMAIL_SMTP_USE_TLS", "true").strip().lower()
|
||||
use_tls = use_tls_str not in ("false", "0", "no")
|
||||
|
||||
if not all([smtp_host, smtp_port, smtp_user, smtp_password, from_address]):
|
||||
raise EmailError(
|
||||
"Email configuration incomplete. "
|
||||
"Ensure EMAIL_SMTP_HOST, EMAIL_SMTP_PORT, EMAIL_SMTP_USER, "
|
||||
"EMAIL_SMTP_PASSWORD, and EMAIL_FROM_ADDRESS are set."
|
||||
)
|
||||
|
||||
return {
|
||||
"smtp_host": smtp_host,
|
||||
"smtp_port": int(smtp_port),
|
||||
"smtp_user": smtp_user,
|
||||
"smtp_password": smtp_password,
|
||||
"from_address": from_address,
|
||||
"use_tls": use_tls,
|
||||
}
|
||||
|
||||
|
||||
def _load_css(path: str) -> str:
|
||||
"""
|
||||
Load CSS file content if it exists.
|
||||
"""
|
||||
if not path or not os.path.exists(path):
|
||||
return ""
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
return f.read()
|
||||
|
||||
|
||||
def _email_logo_html() -> str:
|
||||
"""
|
||||
Return a subtle watermark-style logo for emails.
|
||||
|
||||
- Priority:
|
||||
1) EMAIL_LOGO_URL (direct URL)
|
||||
2) EMAIL_LOGO_PATH (local file as base64)
|
||||
- Style: small, faint, bottom-right, non-intrusive.
|
||||
"""
|
||||
logo_url = os.getenv("EMAIL_LOGO_URL")
|
||||
src = logo_url
|
||||
|
||||
if not logo_url:
|
||||
logo_path = os.getenv("EMAIL_LOGO_PATH", "/app/src/misc/logo1.png")
|
||||
if os.path.exists(logo_path):
|
||||
try:
|
||||
with open(logo_path, "rb") as f:
|
||||
b64 = base64.b64encode(f.read()).decode("utf-8")
|
||||
src = f"data:image/png;base64,{b64}"
|
||||
except Exception:
|
||||
src = None
|
||||
|
||||
if not src:
|
||||
return ""
|
||||
|
||||
# Watermark: bottom-right, low opacity, compact
|
||||
return (
|
||||
f'<div style="text-align: right; margin-top: 24px; opacity: 0.15;">'
|
||||
f'<img src="{src}" alt="Logo" style="max-width: 90px; height: auto; display: inline-block;" />'
|
||||
f'</div>'
|
||||
)
|
||||
|
||||
|
||||
def _accent_color() -> str:
|
||||
"""
|
||||
Accent color for UI and emails.
|
||||
Default: #7C6DA0
|
||||
"""
|
||||
return os.getenv("EMAIL_ACCENT_COLOR", "#7C6DA0")
|
||||
|
||||
|
||||
def build_template_context(**runtime_kwargs: Any) -> Dict[str, Any]:
|
||||
"""
|
||||
Build a context dict for templates from:
|
||||
- environment variables (base, customizable)
|
||||
- runtime-provided values (override env if present)
|
||||
|
||||
Environment variables:
|
||||
- EMAIL_CONTACT_ADDRESS: value for {contact_email}
|
||||
- EMAIL_CSS_PATH: path to mail_style.css (optional; we inline it)
|
||||
- EMAIL_LOGO_URL: URL for email logo (preferred)
|
||||
- EMAIL_LOGO_PATH: fallback local path for email logo
|
||||
- EMAIL_ACCENT_COLOR: accent color (default #7C6DA0)
|
||||
"""
|
||||
# Load and inline mail_style.css for consistent email styling
|
||||
css_path = os.getenv("EMAIL_CSS_PATH", "/app/src/misc/mail_style.css")
|
||||
css_text = _load_css(css_path)
|
||||
|
||||
# Build logo HTML (URL or local fallback)
|
||||
logo_html = _email_logo_html()
|
||||
|
||||
# Accent color
|
||||
accent = _accent_color()
|
||||
|
||||
ctx: Dict[str, Any] = {
|
||||
"contact_email": os.getenv("EMAIL_CONTACT_ADDRESS", "support@example.com"),
|
||||
"email_css": css_text,
|
||||
"email_logo": logo_html,
|
||||
"accent_color": accent,
|
||||
}
|
||||
|
||||
# Runtime values override env if provided
|
||||
if runtime_kwargs:
|
||||
ctx.update(runtime_kwargs)
|
||||
|
||||
return ctx
|
||||
|
||||
|
||||
def load_template(template_name: str, **runtime_kwargs: Any) -> str:
|
||||
"""
|
||||
Load an HTML email template from misc/ and render placeholders.
|
||||
|
||||
Expects files like:
|
||||
/app/src/misc/upload_notification_template.html
|
||||
/app/src/misc/success_template.html
|
||||
/app/src/misc/error_notification_template.html
|
||||
"""
|
||||
base = os.getenv("SCRAIBE_TEMPLATES_DIR", "/app/src/misc")
|
||||
path = os.path.join(base, template_name)
|
||||
|
||||
if not os.path.exists(path):
|
||||
raise EmailError(f"Email template not found: {path}")
|
||||
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
template = f.read()
|
||||
|
||||
# Build context from env + runtime
|
||||
ctx = build_template_context(**runtime_kwargs)
|
||||
|
||||
# Replace {placeholder} style variables safely
|
||||
try:
|
||||
return template.format(**ctx)
|
||||
except KeyError as e:
|
||||
raise EmailError(f"Missing template variable: {e}")
|
||||
|
||||
|
||||
def send_email(
|
||||
to: str,
|
||||
subject: str,
|
||||
body: str,
|
||||
html: Optional[str],
|
||||
attachments: List[str],
|
||||
cc: Optional[str] = None,
|
||||
) -> bool:
|
||||
"""
|
||||
Send an email with optional HTML body and file attachments.
|
||||
|
||||
Args:
|
||||
to: Comma-separated list of recipient email addresses.
|
||||
subject: Email subject.
|
||||
body: Email body (plain text).
|
||||
html: Email body (HTML), or None.
|
||||
attachments: List of file paths to attach.
|
||||
cc: Comma-separated list of CC email addresses (optional).
|
||||
|
||||
Returns:
|
||||
True if sent successfully.
|
||||
|
||||
Raises:
|
||||
EmailError if sending fails.
|
||||
"""
|
||||
try:
|
||||
cfg = get_email_config()
|
||||
except EmailError as e:
|
||||
logger.error("Email configuration error: %s", e)
|
||||
raise
|
||||
|
||||
# Parse recipients
|
||||
to_list = [addr.strip() for addr in to.split(",") if addr.strip()]
|
||||
cc_list = [addr.strip() for addr in cc.split(",") if addr.strip()] if cc else []
|
||||
|
||||
if not to_list:
|
||||
raise EmailError("No valid 'To' email addresses provided.")
|
||||
|
||||
# Ensure subject is never blank
|
||||
if not subject or not subject.strip():
|
||||
logger.warning("Subject was blank or missing; using default subject.")
|
||||
subject = "ScrAIbe: Your transcript is ready"
|
||||
|
||||
subject = subject.strip()
|
||||
|
||||
has_attachments = bool(attachments)
|
||||
|
||||
# Build the text/HTML part (alternative)
|
||||
alt = MIMEMultipart("alternative")
|
||||
alt.attach(MIMEText(body, "plain"))
|
||||
if html:
|
||||
alt.attach(MIMEText(html, "html"))
|
||||
|
||||
if has_attachments:
|
||||
# Outer message: multipart/mixed with headers
|
||||
msg = MIMEMultipart("mixed")
|
||||
msg["From"] = cfg["from_address"]
|
||||
msg["To"] = ", ".join(to_list)
|
||||
if cc_list:
|
||||
msg["Cc"] = ", ".join(cc_list)
|
||||
msg["Subject"] = subject
|
||||
|
||||
# Attach the alternative (text/HTML) part
|
||||
msg.attach(alt)
|
||||
|
||||
# Attach files
|
||||
for file_path in attachments:
|
||||
if not os.path.isfile(file_path):
|
||||
logger.warning("Attachment file not found, skipping: %s", file_path)
|
||||
continue
|
||||
|
||||
try:
|
||||
with open(file_path, "rb") as f:
|
||||
part = MIMEBase("application", "octet-stream")
|
||||
part.set_payload(f.read())
|
||||
encoders.encode_base64(part)
|
||||
part.add_header(
|
||||
"Content-Disposition",
|
||||
"attachment",
|
||||
filename=os.path.basename(file_path),
|
||||
)
|
||||
msg.attach(part)
|
||||
except Exception as e:
|
||||
logger.warning("Failed to attach file %s: %s", file_path, e)
|
||||
else:
|
||||
# No attachments: use the alternative part as the root message
|
||||
msg = alt
|
||||
msg["From"] = cfg["from_address"]
|
||||
msg["To"] = ", ".join(to_list)
|
||||
if cc_list:
|
||||
msg["Cc"] = ", ".join(cc_list)
|
||||
msg["Subject"] = subject
|
||||
|
||||
# Connect and send
|
||||
try:
|
||||
if cfg["use_tls"]:
|
||||
server = smtplib.SMTP(cfg["smtp_host"], cfg["smtp_port"], timeout=30)
|
||||
server.ehlo()
|
||||
server.starttls()
|
||||
server.ehlo()
|
||||
else:
|
||||
server = smtplib.SMTP(cfg["smtp_host"], cfg["smtp_port"], timeout=30)
|
||||
server.ehlo()
|
||||
|
||||
server.login(cfg["smtp_user"], cfg["smtp_password"])
|
||||
server.sendmail(
|
||||
cfg["from_address"],
|
||||
to_list + cc_list,
|
||||
msg.as_string(),
|
||||
)
|
||||
server.quit()
|
||||
logger.info(
|
||||
"Email sent to %s (CC: %s) with subject: %s",
|
||||
to_list,
|
||||
cc_list or "None",
|
||||
subject,
|
||||
)
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Failed to send email: %s", e)
|
||||
raise EmailError(f"Failed to send email: {e}")
|
||||
|
||||
|
||||
# ------------ DOCX helpers ------------
|
||||
|
||||
# Namespaces
|
||||
W_NS = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
|
||||
|
||||
|
||||
def _set_element_attr(elem, attr, value):
|
||||
elem.set(f"{{{W_NS}}}{attr}", str(value))
|
||||
|
||||
|
||||
def _create_transcript_section_properties(section):
|
||||
"""
|
||||
Configure the section properties for transcript DOCX:
|
||||
- Margins: 1 inch all sides
|
||||
- Single column layout
|
||||
- No built-in line numbering (we embed line numbers as text for portability)
|
||||
- Remove document grid to avoid off-by-one line numbering
|
||||
"""
|
||||
sectPr = section._sectPr
|
||||
|
||||
# Margins: 1 inch = 1440 twips
|
||||
pgMar = sectPr.find(f"{{{W_NS}}}pgMar")
|
||||
if pgMar is None:
|
||||
pgMar = OxmlElement("w:pgMar")
|
||||
sectPr.append(pgMar)
|
||||
_set_element_attr(pgMar, "top", "1440")
|
||||
_set_element_attr(pgMar, "right", "1440")
|
||||
_set_element_attr(pgMar, "bottom", "1440")
|
||||
_set_element_attr(pgMar, "left", "1440")
|
||||
_set_element_attr(pgMar, "header", "720")
|
||||
_set_element_attr(pgMar, "footer", "720")
|
||||
_set_element_attr(pgMar, "gutter", "0")
|
||||
|
||||
# Ensure single column (no multi-column layout)
|
||||
cols = sectPr.find(f"{{{W_NS}}}cols")
|
||||
if cols is not None:
|
||||
_set_element_attr(cols, "num", "1")
|
||||
_set_element_attr(cols, "space", "720")
|
||||
|
||||
# Remove document grid entirely
|
||||
for docGrid in sectPr.findall(f"{{{W_NS}}}docGrid"):
|
||||
sectPr.remove(docGrid)
|
||||
|
||||
# Remove any built-in line numbering; we will use text-based line numbers
|
||||
for lnNumType in sectPr.findall(f"{{{W_NS}}}lnNumType"):
|
||||
sectPr.remove(lnNumType)
|
||||
|
||||
|
||||
def _add_transcript_paragraph(doc, line_text, line_number):
|
||||
"""
|
||||
Add a single transcript line as a paragraph with an embedded line number.
|
||||
Uses a left tab stop so the line number appears in the left margin area,
|
||||
independent of built-in line numbering, ensuring consistent behavior
|
||||
across Word, LibreOffice, Google Docs, etc.
|
||||
"""
|
||||
line_text = line_text.strip()
|
||||
if not line_text:
|
||||
return
|
||||
|
||||
p = doc.add_paragraph()
|
||||
|
||||
# Set up paragraph formatting:
|
||||
# - No left indent; we control spacing via tab stop
|
||||
# - Single line spacing, no extra before/after
|
||||
pPr = p._p.get_or_add_pPr()
|
||||
|
||||
# Remove any default indent
|
||||
pPr.find(f"{{{W_NS}}}ind") and pPr.remove(pPr.find(f"{{{W_NS}}}ind"))
|
||||
|
||||
# Define a left tab stop for line numbers (e.g. 360 twips ≈ 0.25")
|
||||
tabs = OxmlElement("w:tabs")
|
||||
tab = OxmlElement("w:tab")
|
||||
tab.set("{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val", "left")
|
||||
tab.set("{http://schemas.openxmlformats.org/wordprocessingml/2006/main}pos", "360")
|
||||
tabs.append(tab)
|
||||
pPr.append(tabs)
|
||||
|
||||
spacing = OxmlElement("w:spacing")
|
||||
_set_element_attr(spacing, "before", "0")
|
||||
_set_element_attr(spacing, "after", "0")
|
||||
_set_element_attr(spacing, "line", "360") # 1.5 line spacing (12pt * 1.5 = 18pt → 360 twips)
|
||||
_set_element_attr(spacing, "lineRule", "auto")
|
||||
pPr.append(spacing)
|
||||
|
||||
# Try to match: [00:00] SPEAKER 1: content
|
||||
m = re.match(r"\[(\d+:\d+(?::\d+)?)\]\s*(.+?):\s*(.*)", line_text)
|
||||
|
||||
# Line number run (no underline)
|
||||
run_ln = p.add_run(str(line_number))
|
||||
run_ln.font.name = "Courier"
|
||||
run_ln.font.size = Pt(12)
|
||||
run_ln.underline = False
|
||||
|
||||
# Tab + spaces between line number and content
|
||||
# - 2 base spaces + 7 more for first line of speaker turn
|
||||
# - 2 base spaces + 3 more for continuation lines
|
||||
if m:
|
||||
extra_spaces = " " # 7 spaces for speaker lines
|
||||
else:
|
||||
extra_spaces = " " # 3 spaces for continuation lines
|
||||
|
||||
run_tab = p.add_run("\t " + extra_spaces)
|
||||
run_tab.font.name = "Courier"
|
||||
run_tab.font.size = Pt(12)
|
||||
run_tab.underline = False
|
||||
|
||||
if m:
|
||||
ts, speaker, content = m.groups()
|
||||
label_text = f"[{ts}] {speaker.upper()}:"
|
||||
|
||||
# Label run (underline)
|
||||
run_label = p.add_run(label_text)
|
||||
run_label.underline = True
|
||||
run_label.font.name = "Courier"
|
||||
run_label.font.size = Pt(12)
|
||||
|
||||
# Space run (no underline)
|
||||
run_space = p.add_run(" ")
|
||||
run_space.underline = False
|
||||
run_space.font.name = "Courier"
|
||||
run_space.font.size = Pt(12)
|
||||
|
||||
# Content run (no underline)
|
||||
run_txt = p.add_run(content.strip())
|
||||
run_txt.underline = False
|
||||
run_txt.font.name = "Courier"
|
||||
run_txt.font.size = Pt(12)
|
||||
else:
|
||||
# Non-standard line: plain text
|
||||
run = p.add_run(line_text)
|
||||
run.underline = False
|
||||
run.font.name = "Courier"
|
||||
run.font.size = Pt(12)
|
||||
|
||||
|
||||
# ------------ Public DOCX functions ------------
|
||||
|
||||
def create_transcript_docx(text: str, filename: str):
|
||||
"""
|
||||
Create a transcript DOCX with:
|
||||
- 1" margins on all sides
|
||||
- 12pt Courier font
|
||||
- Embedded line numbers starting at 1 on the first page
|
||||
(portable across Word, LibreOffice, Google Docs)
|
||||
- Line numbers reflect visual lines on the page, not speaker turns.
|
||||
- Proper formatting for timestamps and speaker labels
|
||||
"""
|
||||
doc = Document()
|
||||
|
||||
# Set base font (Normal style)
|
||||
style = doc.styles["Normal"]
|
||||
style.font.name = "Courier"
|
||||
style.font.size = Pt(12)
|
||||
|
||||
# Remove any default paragraphs (ensure no phantom first line)
|
||||
body = doc.element.body
|
||||
for p in list(body.findall(f"{{{W_NS}}}p")):
|
||||
body.remove(p)
|
||||
|
||||
# Configure section properties (margins, no built-in line numbering)
|
||||
_create_transcript_section_properties(doc.sections[0])
|
||||
|
||||
# Max characters per visual line (content only; total line including line number and spaces <= 60)
|
||||
max_chars = 58
|
||||
|
||||
# Lines per page before restarting numbering
|
||||
lines_per_page = 29
|
||||
|
||||
# Current line counter for visual lines
|
||||
line_number = 0
|
||||
|
||||
# Split transcript into logical lines
|
||||
logical_lines = text.strip().splitlines()
|
||||
|
||||
def insert_page_break():
|
||||
nonlocal line_number
|
||||
p_break = doc.add_paragraph()
|
||||
pPr = p_break._p.get_or_add_pPr()
|
||||
for child in list(pPr):
|
||||
tag = child.tag.split("}")[-1] if "}" in child.tag else child.tag
|
||||
if tag in ("tabs", "spacing", "ind"):
|
||||
pPr.remove(child)
|
||||
page_break = OxmlElement("w:pageBreak")
|
||||
page_break.set("{http://schemas.openxmlformats.org/wordprocessingml/2006/main}val", "1")
|
||||
pPr.append(page_break)
|
||||
line_number = 0
|
||||
|
||||
for line in logical_lines:
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
|
||||
# Try to match: [00:00] SPEAKER 1: content
|
||||
m = re.match(r"\[(\d+:\d+(?::\d+)?)\]\s*(.+?):\s*(.*)", line)
|
||||
if m:
|
||||
ts, speaker, content = m.groups()
|
||||
label_text = f"[{ts}] {speaker.upper()}:"
|
||||
content = content.strip()
|
||||
else:
|
||||
label_text = ""
|
||||
content = line.strip()
|
||||
|
||||
# Wrap content into visual lines (whole words, max_chars)
|
||||
content_lines = []
|
||||
words = content.split()
|
||||
current = ""
|
||||
for w in words:
|
||||
if len(current) == 0:
|
||||
current = w
|
||||
elif len(current) + 1 + len(w) <= max_chars:
|
||||
current += " " + w
|
||||
else:
|
||||
content_lines.append(current)
|
||||
current = w
|
||||
if current:
|
||||
content_lines.append(current)
|
||||
|
||||
# Prepare first visual line with label if present
|
||||
visual_lines = []
|
||||
if content_lines:
|
||||
first_content = content_lines.pop(0)
|
||||
if label_text:
|
||||
prefix = label_text + " "
|
||||
if len(prefix) + len(first_content) > max_chars:
|
||||
allowed = max_chars - len(prefix)
|
||||
if allowed < 1:
|
||||
allowed = 1
|
||||
candidate = first_content[:allowed]
|
||||
last_space = candidate.rfind(" ")
|
||||
if last_space > 0:
|
||||
kept = candidate[:last_space]
|
||||
rest = first_content[last_space:].strip()
|
||||
else:
|
||||
kept = candidate
|
||||
rest = first_content[allowed:].strip()
|
||||
|
||||
visual_lines.append(prefix + kept)
|
||||
if rest:
|
||||
extra_words = rest.split()
|
||||
new_lines = []
|
||||
buf = ""
|
||||
for ew in extra_words:
|
||||
if len(buf) == 0:
|
||||
buf = ew
|
||||
elif len(buf) + 1 + len(ew) <= max_chars:
|
||||
buf += " " + ew
|
||||
else:
|
||||
new_lines.append(buf)
|
||||
buf = ew
|
||||
if buf:
|
||||
new_lines.append(buf)
|
||||
content_lines = new_lines + content_lines
|
||||
else:
|
||||
visual_lines.append(prefix + first_content)
|
||||
else:
|
||||
visual_lines.append(first_content)
|
||||
|
||||
visual_lines.extend(content_lines)
|
||||
else:
|
||||
# No content_lines but maybe label_text only
|
||||
if label_text:
|
||||
visual_lines.append(label_text)
|
||||
|
||||
# If adding these visual lines would exceed lines_per_page, insert page break first
|
||||
if line_number + len(visual_lines) > lines_per_page:
|
||||
insert_page_break()
|
||||
|
||||
# Write visual lines
|
||||
for vl in visual_lines:
|
||||
line_number += 1
|
||||
_add_transcript_paragraph(doc, vl, line_number=line_number)
|
||||
|
||||
# Add page numbers to footer: "X of Y" (bottom left)
|
||||
section = doc.sections[0]
|
||||
footer = section.footer
|
||||
footer.is_linked_to_previous = False
|
||||
footer_para = footer.paragraphs[0] if footer.paragraphs else footer.add_paragraph()
|
||||
footer_para.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
||||
|
||||
# Clear any existing content
|
||||
for r in footer_para.runs:
|
||||
r.text = ""
|
||||
|
||||
def add_field(run, code):
|
||||
fldChar = OxmlElement("w:fldChar")
|
||||
fldChar.set(qn("w:fldCharType"), "begin")
|
||||
run._r.append(fldChar)
|
||||
|
||||
instrText = OxmlElement("w:instrText")
|
||||
instrText.set(qn("xml:space"), "preserve")
|
||||
instrText.text = code
|
||||
run._r.append(instrText)
|
||||
|
||||
fldCharEnd = OxmlElement("w:fldChar")
|
||||
fldCharEnd.set(qn("w:fldCharType"), "end")
|
||||
run._r.append(fldCharEnd)
|
||||
|
||||
run_page = footer_para.add_run()
|
||||
add_field(run_page, " PAGE ")
|
||||
|
||||
run_of = footer_para.add_run(" of ")
|
||||
|
||||
run_total = footer_para.add_run()
|
||||
add_field(run_total, " NUMPAGES ")
|
||||
|
||||
# Save
|
||||
doc.save(filename)
|
||||
|
||||
|
||||
def create_summary_docx(text: str, filename: str):
|
||||
"""
|
||||
Create a summary DOCX with:
|
||||
- 1" margins on all sides
|
||||
- 12pt Courier font
|
||||
- No line numbering
|
||||
"""
|
||||
doc = Document()
|
||||
|
||||
# Base font
|
||||
style = doc.styles["Normal"]
|
||||
style.font.name = "Courier"
|
||||
style.font.size = Pt(12)
|
||||
|
||||
# Margins: 1 inch all sides
|
||||
for section in doc.sections:
|
||||
section.left_margin = Inches(1.0)
|
||||
section.right_margin = Inches(1.0)
|
||||
section.top_margin = Inches(1.0)
|
||||
section.bottom_margin = Inches(1.0)
|
||||
|
||||
# Remove default paragraph
|
||||
body = doc.element.body
|
||||
for p in list(body.findall(f"{{{W_NS}}}p")):
|
||||
body.remove(p)
|
||||
|
||||
# Add summary content
|
||||
lines = text.strip().splitlines()
|
||||
for line in lines:
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
p = doc.add_paragraph(line)
|
||||
p.paragraph_format.space_after = Pt(4)
|
||||
|
||||
doc.save(filename)
|
||||
+347
-18
@@ -9,20 +9,35 @@ It replaces the previous local Whisper + Pyannote pipeline by sending
|
||||
audio files to the /v1/audio/diarization endpoint and mapping the
|
||||
response into the same Transcript format used by the UI.
|
||||
|
||||
For long audio files, it can chunk the input to avoid GPU OOM errors.
|
||||
|
||||
Environment Variables:
|
||||
LOCALAI_API_URL: (required) Base URL of the LocalAI server
|
||||
(e.g., http://localhost:8080)
|
||||
LOCALAI_API_KEY: (optional) API key, if configured
|
||||
LOCALAI_MODEL: (optional) Model name to use (default: vibevoice-diarize)
|
||||
|
||||
Chunking / long audio (all optional):
|
||||
LOCALAI_CHUNK_DURATION: Max duration of each chunk in seconds
|
||||
(default: 180.0)
|
||||
LOCALAI_CHUNK_OVERLAP: Overlap between consecutive chunks in seconds
|
||||
(default: 2.0)
|
||||
LOCALAI_MAX_SINGLE_REQUEST_DURATION: If audio duration exceeds this, chunking
|
||||
is enabled automatically (default: 300.0)
|
||||
"""
|
||||
|
||||
import os
|
||||
import io
|
||||
import json
|
||||
import logging
|
||||
from typing import Dict, List, Any, Optional
|
||||
|
||||
import httpx
|
||||
|
||||
from .audio import get_audio_duration, split_audio_into_chunks
|
||||
|
||||
logger = logging.getLogger("scraibe.localai_client")
|
||||
|
||||
|
||||
class LocalAIError(Exception):
|
||||
"""Raised when the LocalAI API returns an error or unexpected response."""
|
||||
@@ -36,16 +51,22 @@ class LocalAIClient:
|
||||
Responsibilities:
|
||||
- Read configuration from environment.
|
||||
- Upload audio file as multipart/form-data.
|
||||
- Parse diarization + transcription response.
|
||||
- Parse diarization + transcription response (verbose_json).
|
||||
- Map response into the same structure expected by Scraibe's Transcript.
|
||||
- For long audio: chunk, transcribe each chunk, merge results.
|
||||
"""
|
||||
|
||||
# Default thresholds for chunking long audio to avoid GPU OOM.
|
||||
# These can be overridden via environment or at call time.
|
||||
DEFAULT_CHUNK_DURATION = 180.0 # seconds
|
||||
DEFAULT_CHUNK_OVERLAP = 2.0 # seconds
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
api_url: Optional[str] = None,
|
||||
api_key: Optional[str] = None,
|
||||
model: Optional[str] = None,
|
||||
timeout: float = 600.0,
|
||||
timeout: float = 3600.0,
|
||||
):
|
||||
"""
|
||||
Args:
|
||||
@@ -67,12 +88,67 @@ class LocalAIClient:
|
||||
"Provide the LocalAI server URL via environment or constructor."
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"Initializing LocalAIClient: url=%s model=%s",
|
||||
self.api_url,
|
||||
self.model,
|
||||
)
|
||||
|
||||
self._client = httpx.Client(
|
||||
base_url=self.api_url,
|
||||
timeout=self.timeout,
|
||||
follow_redirects=True,
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _env_float(var: str, default: float) -> float:
|
||||
"""
|
||||
Read a float from environment with a fallback default.
|
||||
"""
|
||||
val = (os.getenv(var) or "").strip()
|
||||
if val == "":
|
||||
return default
|
||||
try:
|
||||
return float(val)
|
||||
except ValueError:
|
||||
logger.warning(
|
||||
"Invalid value for %s: %s; using default %s", var, val, default
|
||||
)
|
||||
return default
|
||||
|
||||
def _effective_chunk_duration(self, provided: Optional[float]) -> float:
|
||||
"""
|
||||
Resolve chunk_duration using this precedence:
|
||||
1) provided argument
|
||||
2) LOCALAI_CHUNK_DURATION env
|
||||
3) class default
|
||||
"""
|
||||
if provided is not None:
|
||||
return provided
|
||||
return self._env_float("LOCALAI_CHUNK_DURATION", self.DEFAULT_CHUNK_DURATION)
|
||||
|
||||
def _effective_chunk_overlap(self, provided: Optional[float]) -> float:
|
||||
"""
|
||||
Resolve chunk_overlap:
|
||||
1) provided argument
|
||||
2) LOCALAI_CHUNK_OVERLAP env
|
||||
3) class default
|
||||
"""
|
||||
if provided is not None:
|
||||
return provided
|
||||
return self._env_float("LOCALAI_CHUNK_OVERLAP", self.DEFAULT_CHUNK_OVERLAP)
|
||||
|
||||
def _effective_max_single_request_duration(self, provided: Optional[float]) -> float:
|
||||
"""
|
||||
Resolve max_single_request_duration:
|
||||
1) provided argument
|
||||
2) LOCALAI_MAX_SINGLE_REQUEST_DURATION env
|
||||
3) default 300.0
|
||||
"""
|
||||
if provided is not None:
|
||||
return provided
|
||||
return self._env_float("LOCALAI_MAX_SINGLE_REQUEST_DURATION", 300.0)
|
||||
|
||||
def close(self):
|
||||
"""Close the underlying HTTP client."""
|
||||
self._client.close()
|
||||
@@ -97,20 +173,19 @@ class LocalAIClient:
|
||||
response_format: Optional[str] = None,
|
||||
include_text: Optional[bool] = None,
|
||||
verbose: bool = False,
|
||||
return_raw: bool = False,
|
||||
use_chunking: Optional[bool] = None,
|
||||
chunk_duration: Optional[float] = None,
|
||||
chunk_overlap: Optional[float] = None,
|
||||
max_single_request_duration: Optional[float] = None,
|
||||
**_ignored,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Send audio to LocalAI /v1/audio/diarization and return a dict
|
||||
in the same style as the previous internal diarization output:
|
||||
Send audio to LocalAI /v1/audio/diarization and return:
|
||||
- A normalized dict with segments, speakers, transcripts.
|
||||
- Optionally, the raw verbose_json response (for JSON export).
|
||||
|
||||
{
|
||||
"segments": [ [start, end], ... ],
|
||||
"speakers": [ "SPEAKER_00", ... ],
|
||||
"transcripts": [ "text for segment", ... ]
|
||||
}
|
||||
|
||||
Extra kwargs that the old UI used (e.g., whisper-specific) are
|
||||
accepted but ignored.
|
||||
For long audio, it can automatically chunk the file to avoid GPU OOM.
|
||||
|
||||
Args:
|
||||
audio_path: Path to the audio file.
|
||||
@@ -122,15 +197,105 @@ class LocalAIClient:
|
||||
min_duration_on: Optional min segment duration.
|
||||
min_duration_off: Optional min gap duration.
|
||||
response_format: "json", "verbose_json", or "rttm".
|
||||
Defaults to "verbose_json" if not set.
|
||||
Defaults to "verbose_json".
|
||||
include_text: Whether to request per-segment text.
|
||||
Defaults to True.
|
||||
verbose: If True, prints progress messages.
|
||||
return_raw: If True, also return the raw API response in 'raw_result'.
|
||||
use_chunking: Whether to enable chunking for long audio.
|
||||
If None, enabled automatically based on duration.
|
||||
chunk_duration: Max duration per chunk in seconds.
|
||||
Falls back to LOCALAI_CHUNK_DURATION env, then 180.0.
|
||||
chunk_overlap: Overlap between chunks in seconds.
|
||||
Falls back to LOCALAI_CHUNK_OVERLAP env, then 2.0.
|
||||
max_single_request_duration: If audio duration exceeds this, chunking
|
||||
is enabled (unless explicitly disabled).
|
||||
Falls back to LOCALAI_MAX_SINGLE_REQUEST_DURATION
|
||||
env, then 300.0.
|
||||
"""
|
||||
if verbose:
|
||||
print("Starting diarization and transcription via LocalAI.")
|
||||
|
||||
# Defaults: use verbose_json + include_text to get both diarization and transcription.
|
||||
logger.info("diarize_and_transcribe requested for: %s", audio_path)
|
||||
|
||||
# Resolve chunking parameters with environment support
|
||||
chunk_duration = self._effective_chunk_duration(chunk_duration)
|
||||
chunk_overlap = self._effective_chunk_overlap(chunk_overlap)
|
||||
max_single = self._effective_max_single_request_duration(max_single_request_duration)
|
||||
|
||||
if use_chunking is None:
|
||||
try:
|
||||
duration = get_audio_duration(audio_path)
|
||||
except RuntimeError:
|
||||
duration = None
|
||||
|
||||
use_chunking = (duration is not None and duration > max_single)
|
||||
logger.info(
|
||||
"Auto-chunking decision: duration=%s, threshold=%s, use_chunking=%s",
|
||||
duration,
|
||||
max_single,
|
||||
use_chunking,
|
||||
)
|
||||
|
||||
if use_chunking:
|
||||
return self._diarize_and_transcribe_chunked(
|
||||
audio_path=audio_path,
|
||||
language=language,
|
||||
num_speakers=num_speakers,
|
||||
min_speakers=min_speakers,
|
||||
max_speakers=max_speakers,
|
||||
clustering_threshold=clustering_threshold,
|
||||
min_duration_on=min_duration_on,
|
||||
min_duration_off=min_duration_off,
|
||||
response_format=response_format,
|
||||
include_text=include_text,
|
||||
verbose=verbose,
|
||||
return_raw=return_raw,
|
||||
chunk_duration=chunk_duration,
|
||||
chunk_overlap=chunk_overlap,
|
||||
)
|
||||
|
||||
# Single-request path (existing behavior)
|
||||
return self._diarize_and_transcribe_single(
|
||||
audio_path=audio_path,
|
||||
language=language,
|
||||
num_speakers=num_speakers,
|
||||
min_speakers=min_speakers,
|
||||
max_speakers=max_speakers,
|
||||
clustering_threshold=clustering_threshold,
|
||||
min_duration_on=min_duration_on,
|
||||
min_duration_off=min_duration_off,
|
||||
response_format=response_format,
|
||||
include_text=include_text,
|
||||
verbose=verbose,
|
||||
return_raw=return_raw,
|
||||
)
|
||||
|
||||
def _diarize_and_transcribe_single(
|
||||
self,
|
||||
audio_path: str,
|
||||
*,
|
||||
language: Optional[str] = None,
|
||||
num_speakers: Optional[int] = None,
|
||||
min_speakers: Optional[int] = None,
|
||||
max_speakers: Optional[int] = None,
|
||||
clustering_threshold: Optional[float] = None,
|
||||
min_duration_on: Optional[float] = None,
|
||||
min_duration_off: Optional[float] = None,
|
||||
response_format: Optional[str] = None,
|
||||
include_text: Optional[bool] = None,
|
||||
verbose: bool = False,
|
||||
return_raw: bool = False,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Internal: single-request diarization and transcription.
|
||||
"""
|
||||
if verbose:
|
||||
print("Starting diarization and transcription via LocalAI.")
|
||||
|
||||
logger.info("diarize_and_transcribe requested for: %s", audio_path)
|
||||
|
||||
# Always use verbose_json for diarization + speaker info
|
||||
if response_format is None:
|
||||
response_format = "verbose_json"
|
||||
if include_text is None:
|
||||
@@ -158,6 +323,8 @@ class LocalAIClient:
|
||||
if min_duration_off is not None:
|
||||
data["min_duration_off"] = str(min_duration_off)
|
||||
|
||||
logger.debug("LocalAI request params: %s", data)
|
||||
|
||||
# Open file
|
||||
if not os.path.exists(audio_path):
|
||||
raise LocalAIError(f"Audio file not found: {audio_path}")
|
||||
@@ -172,6 +339,7 @@ class LocalAIClient:
|
||||
headers["Authorization"] = f"Bearer {self.api_key}"
|
||||
|
||||
# POST /v1/audio/diarization
|
||||
logger.info("Sending request to LocalAI: /v1/audio/diarization")
|
||||
resp = self._client.post(
|
||||
"/v1/audio/diarization",
|
||||
data=data,
|
||||
@@ -179,15 +347,19 @@ class LocalAIClient:
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
logger.info("LocalAI response status: %d", resp.status_code)
|
||||
|
||||
if resp.status_code >= 400:
|
||||
body = resp.text
|
||||
logger.error("LocalAI error response: %s", body)
|
||||
raise LocalAIError(
|
||||
f"LocalAI request failed with status {resp.status_code}: {body}"
|
||||
)
|
||||
|
||||
try:
|
||||
result = resp.json()
|
||||
raw_result = resp.json()
|
||||
except json.JSONDecodeError:
|
||||
logger.error("Failed to parse LocalAI response as JSON.")
|
||||
raise LocalAIError(
|
||||
"Failed to parse LocalAI response as JSON."
|
||||
)
|
||||
@@ -195,11 +367,163 @@ class LocalAIClient:
|
||||
if verbose:
|
||||
print("Diarization and transcription finished. Starting post-processing.")
|
||||
|
||||
return self._parse_diarization_response(result)
|
||||
parsed = self._parse_diarization_response(raw_result)
|
||||
|
||||
if return_raw:
|
||||
parsed["raw_result"] = raw_result
|
||||
|
||||
return parsed
|
||||
|
||||
def _diarize_and_transcribe_chunked(
|
||||
self,
|
||||
audio_path: str,
|
||||
*,
|
||||
language: Optional[str] = None,
|
||||
num_speakers: Optional[int] = None,
|
||||
min_speakers: Optional[int] = None,
|
||||
max_speakers: Optional[int] = None,
|
||||
clustering_threshold: Optional[float] = None,
|
||||
min_duration_on: Optional[float] = None,
|
||||
min_duration_off: Optional[float] = None,
|
||||
response_format: Optional[str] = None,
|
||||
include_text: Optional[bool] = None,
|
||||
verbose: bool = False,
|
||||
return_raw: bool = False,
|
||||
chunk_duration: float = DEFAULT_CHUNK_DURATION,
|
||||
chunk_overlap: float = DEFAULT_CHUNK_OVERLAP,
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
Internal: chunked diarization and transcription for long audio.
|
||||
|
||||
- Splits audio into overlapping chunks.
|
||||
- Transcribes each chunk via /v1/audio/diarization.
|
||||
- Merges segments with adjusted timestamps.
|
||||
"""
|
||||
if verbose:
|
||||
print("Audio is long; splitting into chunks to avoid GPU memory issues.")
|
||||
|
||||
logger.info(
|
||||
"Chunked transcription: chunk_duration=%s, overlap=%s",
|
||||
chunk_duration,
|
||||
chunk_overlap,
|
||||
)
|
||||
|
||||
chunks = split_audio_into_chunks(
|
||||
input_path=audio_path,
|
||||
max_duration=chunk_duration,
|
||||
overlap=chunk_overlap,
|
||||
)
|
||||
|
||||
if len(chunks) == 1:
|
||||
# No actual split needed; fall back to single-request path
|
||||
return self._diarize_and_transcribe_single(
|
||||
audio_path=chunks[0]["path"],
|
||||
language=language,
|
||||
num_speakers=num_speakers,
|
||||
min_speakers=min_speakers,
|
||||
max_speakers=max_speakers,
|
||||
clustering_threshold=clustering_threshold,
|
||||
min_duration_on=min_duration_on,
|
||||
min_duration_off=min_duration_off,
|
||||
response_format=response_format,
|
||||
include_text=include_text,
|
||||
verbose=verbose,
|
||||
return_raw=return_raw,
|
||||
)
|
||||
|
||||
all_segments: List[List[float]] = []
|
||||
all_speakers: List[str] = []
|
||||
all_transcripts: List[str] = []
|
||||
raw_results: List[Dict[str, Any]] = []
|
||||
temp_files = [c["path"] for c in chunks]
|
||||
|
||||
try:
|
||||
for i, chunk_info in enumerate(chunks):
|
||||
chunk_path = chunk_info["path"]
|
||||
chunk_start = chunk_info["start"]
|
||||
|
||||
if verbose:
|
||||
print(
|
||||
f"Transcribing chunk {i+1}/{len(chunks)} "
|
||||
f"(start={chunk_start:.1f}s)"
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"Transcribing chunk %d/%d, start=%.1f", i + 1, len(chunks), chunk_start
|
||||
)
|
||||
|
||||
# Use single-request logic for each chunk
|
||||
chunk_result = self._diarize_and_transcribe_single(
|
||||
audio_path=chunk_path,
|
||||
language=language,
|
||||
num_speakers=num_speakers,
|
||||
min_speakers=min_speakers,
|
||||
max_speakers=max_speakers,
|
||||
clustering_threshold=clustering_threshold,
|
||||
min_duration_on=min_duration_on,
|
||||
min_duration_off=min_duration_off,
|
||||
response_format=response_format,
|
||||
include_text=include_text,
|
||||
verbose=False,
|
||||
return_raw=return_raw,
|
||||
)
|
||||
|
||||
segs = chunk_result.get("segments", [])
|
||||
spks = chunk_result.get("speakers", [])
|
||||
txts = chunk_result.get("transcripts", [])
|
||||
raw = chunk_result.get("raw_result")
|
||||
|
||||
# Adjust timestamps to global timeline
|
||||
adjusted_segs = []
|
||||
for seg, sp, txt in zip(segs, spks, txts):
|
||||
start = float(seg[0]) + chunk_start
|
||||
end = float(seg[1]) + chunk_start
|
||||
adjusted_segs.append([start, end])
|
||||
all_speakers.append(sp)
|
||||
all_transcripts.append(txt)
|
||||
all_segments.extend(adjusted_segs)
|
||||
|
||||
if return_raw and raw is not None:
|
||||
raw_results.append(raw)
|
||||
|
||||
finally:
|
||||
# Clean up temporary chunk files
|
||||
for path in temp_files:
|
||||
if path and os.path.exists(path) and path != audio_path:
|
||||
try:
|
||||
os.remove(path)
|
||||
except Exception as e:
|
||||
logger.warning("Failed to remove chunk file %s: %s", path, e)
|
||||
|
||||
# Sort segments by start time
|
||||
combined = list(zip(all_segments, all_speakers, all_transcripts))
|
||||
combined.sort(key=lambda x: x[0][0])
|
||||
all_segments = [x[0] for x in combined]
|
||||
all_speakers = [x[1] for x in combined]
|
||||
all_transcripts = [x[2] for x in combined]
|
||||
|
||||
if verbose:
|
||||
print(
|
||||
f"Chunked transcription complete. Total segments: {len(all_segments)}"
|
||||
)
|
||||
|
||||
result = {
|
||||
"segments": all_segments,
|
||||
"speakers": all_speakers,
|
||||
"transcripts": all_transcripts,
|
||||
}
|
||||
|
||||
if return_raw and raw_results:
|
||||
result["raw_result"] = {
|
||||
"chunked": True,
|
||||
"chunks": raw_results,
|
||||
}
|
||||
|
||||
return result
|
||||
|
||||
def _parse_diarization_response(self, result: Dict[str, Any]) -> Dict[str, Any]:
|
||||
"""
|
||||
Convert LocalAI response into the internal format used by Scraibe:
|
||||
Convert LocalAI verbose_json response into the internal format used by Scraibe:
|
||||
{
|
||||
"segments": [ [start, end], ... ],
|
||||
"speakers": [ "SPEAKER_00", ... ],
|
||||
@@ -209,7 +533,7 @@ class LocalAIClient:
|
||||
segments = result.get("segments", [])
|
||||
|
||||
if not segments:
|
||||
# If no segments, return empty but valid structure
|
||||
logger.warning("LocalAI returned no segments.")
|
||||
return {
|
||||
"segments": [],
|
||||
"speakers": [],
|
||||
@@ -230,6 +554,11 @@ class LocalAIClient:
|
||||
out_speakers.append(speaker)
|
||||
out_transcripts.append(text)
|
||||
|
||||
logger.info(
|
||||
"Parsed %d segments from LocalAI.",
|
||||
len(out_segments),
|
||||
)
|
||||
|
||||
return {
|
||||
"segments": out_segments,
|
||||
"speakers": out_speakers,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import os
|
||||
import logging
|
||||
from argparse import Action
|
||||
from ast import literal_eval
|
||||
|
||||
@@ -13,6 +14,25 @@ PYANNOTE_DEFAULT_PATH = os.path.join(CACHE_DIR, "pyannote")
|
||||
PYANNOTE_DEFAULT_CONFIG = os.path.join(PYANNOTE_DEFAULT_PATH, "config.yaml")
|
||||
|
||||
|
||||
def setup_logging(level: str = "INFO"):
|
||||
"""
|
||||
Configure root logger to write to stdout so Docker can capture logs.
|
||||
|
||||
Args:
|
||||
level: Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL).
|
||||
"""
|
||||
numeric_level = getattr(logging, level.upper(), logging.INFO)
|
||||
if not isinstance(numeric_level, int):
|
||||
numeric_level = logging.INFO
|
||||
|
||||
logging.basicConfig(
|
||||
level=numeric_level,
|
||||
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
||||
datefmt="%Y-%m-%dT%H:%M:%S%z",
|
||||
force=True,
|
||||
)
|
||||
|
||||
|
||||
def set_threads(parse_threads=None, yaml_threads=None):
|
||||
"""
|
||||
Configure number of threads.
|
||||
|
||||
+41
-3
@@ -6,8 +6,8 @@ Provides a client to summarize long transcripts via an LLM endpoint.
|
||||
|
||||
Behavior:
|
||||
- Chunks transcript into 10,240-character segments.
|
||||
- Generates a summary for each chunk.
|
||||
- Combines all chunk summaries and produces a final, detailed summary.
|
||||
- Summarizes each chunk.
|
||||
- Summarizes the summaries into a final, detailed summary.
|
||||
|
||||
Environment Variables:
|
||||
- SUMMARIZER_API_URL: (required) Base URL of the LLM API (e.g., http://localhost:8080)
|
||||
@@ -17,10 +17,13 @@ Environment Variables:
|
||||
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
from typing import Optional
|
||||
|
||||
import httpx
|
||||
|
||||
logger = logging.getLogger("scraibe.summarizer")
|
||||
|
||||
|
||||
class SummarizerError(Exception):
|
||||
"""Raised when the summarization API call fails."""
|
||||
@@ -40,7 +43,7 @@ class SummarizerClient:
|
||||
api_url: Optional[str] = None,
|
||||
api_key: Optional[str] = None,
|
||||
model: Optional[str] = None,
|
||||
timeout: float = 600.0,
|
||||
timeout: float = 3600.0,
|
||||
):
|
||||
self.api_url = (api_url or os.getenv("SUMMARIZER_API_URL")).strip().rstrip("/")
|
||||
self.api_key = api_key or os.getenv("SUMMARIZER_API_KEY") or None
|
||||
@@ -53,6 +56,12 @@ class SummarizerClient:
|
||||
"Provide the summarization LLM URL via environment or constructor."
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"Initializing SummarizerClient: url=%s model=%s",
|
||||
self.api_url,
|
||||
self.model,
|
||||
)
|
||||
|
||||
self._client = httpx.Client(
|
||||
base_url=self.api_url,
|
||||
timeout=self.timeout,
|
||||
@@ -84,21 +93,40 @@ class SummarizerClient:
|
||||
- Next steps / action items
|
||||
"""
|
||||
if not transcript.strip():
|
||||
logger.warning("Empty transcript provided to summarize_transcript.")
|
||||
return "No transcript provided to summarize."
|
||||
|
||||
logger.info(
|
||||
"Starting summarization for transcript length=%d chars",
|
||||
len(transcript),
|
||||
)
|
||||
|
||||
# 1) Chunk the transcript
|
||||
chunks = self._chunk_text(transcript)
|
||||
logger.info("Split transcript into %d chunks.", len(chunks))
|
||||
|
||||
# 2) Summarize each chunk
|
||||
chunk_summaries = []
|
||||
for i, chunk in enumerate(chunks):
|
||||
logger.info(
|
||||
"Summarizing chunk %d/%d (length=%d)",
|
||||
i + 1,
|
||||
len(chunks),
|
||||
len(chunk),
|
||||
)
|
||||
summary = self._summarize_chunk(chunk, i, len(chunks))
|
||||
chunk_summaries.append(summary)
|
||||
|
||||
# 3) Combine and summarize summaries
|
||||
combined = "\n\n".join(chunk_summaries)
|
||||
logger.info(
|
||||
"Combining %d chunk summaries (total length=%d) for final summary.",
|
||||
len(chunk_summaries),
|
||||
len(combined),
|
||||
)
|
||||
final_summary = self._summarize_combined(combined)
|
||||
|
||||
logger.info("Summarization completed.")
|
||||
return final_summary
|
||||
|
||||
def _chunk_text(self, text: str) -> list[str]:
|
||||
@@ -183,13 +211,18 @@ class SummarizerClient:
|
||||
if self.api_key:
|
||||
headers["Authorization"] = f"Bearer {self.api_key}"
|
||||
|
||||
logger.info("Calling summarizer endpoint: /v1/chat/completions")
|
||||
|
||||
resp = self._client.post(
|
||||
"/v1/chat/completions",
|
||||
json=payload,
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
logger.info("Summarizer response status: %d", resp.status_code)
|
||||
|
||||
if resp.status_code >= 400:
|
||||
logger.error("Summarizer error response: %s", resp.text)
|
||||
raise SummarizerError(
|
||||
f"Summarizer API error {resp.status_code}: {resp.text}"
|
||||
)
|
||||
@@ -197,6 +230,7 @@ class SummarizerClient:
|
||||
try:
|
||||
data = resp.json()
|
||||
except json.JSONDecodeError:
|
||||
logger.error("Failed to parse summarizer response as JSON.")
|
||||
raise SummarizerError(
|
||||
"Failed to parse summarizer response as JSON."
|
||||
)
|
||||
@@ -206,6 +240,10 @@ class SummarizerClient:
|
||||
content = data["choices"][0]["message"]["content"]
|
||||
return content.strip()
|
||||
except (KeyError, IndexError, TypeError):
|
||||
logger.error(
|
||||
"Unexpected summarizer response format: %s",
|
||||
json.dumps(data, indent=2),
|
||||
)
|
||||
raise SummarizerError(
|
||||
"Unexpected summarizer response format: "
|
||||
f"{json.dumps(data, indent=2)}"
|
||||
|
||||
@@ -0,0 +1,506 @@
|
||||
"""
|
||||
Celery tasks for async transcription, diarization, and email notifications.
|
||||
"""
|
||||
|
||||
import os
|
||||
import json
|
||||
import logging
|
||||
import tempfile
|
||||
from datetime import datetime
|
||||
|
||||
from .celery_app import celery_app
|
||||
from .autotranscript import Scraibe
|
||||
from .summarizer import SummarizerClient, SummarizerError
|
||||
from .misc import setup_logging
|
||||
from .email_sender import send_email, EmailError, load_template
|
||||
from .email_sender import create_transcript_docx, create_summary_docx
|
||||
|
||||
logger = logging.getLogger("scraibe.tasks")
|
||||
|
||||
|
||||
def _local_part(email: str) -> str:
|
||||
"""
|
||||
Extract the part before '@' from an email, sanitized for filenames.
|
||||
"""
|
||||
local = (email or "").split("@")[0].strip()
|
||||
local = "".join(ch if ch.isalnum() or ch in ("-", "_", ".") else "_" for ch in local)
|
||||
return local or "user"
|
||||
|
||||
|
||||
def _date_tag() -> str:
|
||||
"""
|
||||
Date tag in DD-MON-YYYY format (e.g. 01-JAN-2025).
|
||||
"""
|
||||
return datetime.utcnow().strftime("%d-%b-%Y").upper()
|
||||
|
||||
|
||||
def _safe_filename(base: str, local: str, date_tag: str, ext: str) -> str:
|
||||
"""
|
||||
Create a temp file with the requested logical name.
|
||||
Uses mktemp for uniqueness but keeps the desired name pattern.
|
||||
"""
|
||||
name = f"{base}-{local}-{date_tag}{ext}"
|
||||
return tempfile.mktemp(prefix=name.replace(".", ""), suffix=ext)
|
||||
|
||||
|
||||
def _remove_file(path: str):
|
||||
"""
|
||||
Remove a file if it exists. Best-effort; logs but never raises.
|
||||
"""
|
||||
if not path:
|
||||
return
|
||||
try:
|
||||
if os.path.exists(path):
|
||||
os.remove(path)
|
||||
except Exception as e:
|
||||
logger.warning("Failed to remove file %s: %s", path, e)
|
||||
|
||||
|
||||
def _get_subject(env_var: str, default: str) -> str:
|
||||
"""
|
||||
Safely read an email subject from an environment variable.
|
||||
Uses default if unset or blank. Logs the final value.
|
||||
"""
|
||||
value = (os.getenv(env_var) or "").strip()
|
||||
subject = value or default
|
||||
logger.info("Email subject [%s] = %s", env_var, subject)
|
||||
return subject
|
||||
|
||||
|
||||
def get_queue_position(task_id: str) -> int:
|
||||
"""
|
||||
Estimate the job's position in the queue.
|
||||
Returns:
|
||||
- A positive int if we can estimate (1 = first in line).
|
||||
- 0 if we cannot reliably determine position.
|
||||
"""
|
||||
try:
|
||||
inspect = celery_app.control.inspect()
|
||||
reserved = inspect.reserved() or {} # queued but not yet running
|
||||
active = inspect.active() or {} # currently running
|
||||
|
||||
# Count tasks ahead of this one in the reserved (waiting) queue
|
||||
ahead = 0
|
||||
found = False
|
||||
for _, tasks in list(reserved.values()):
|
||||
for t in tasks:
|
||||
tid = t.get("id")
|
||||
if tid == task_id:
|
||||
found = True
|
||||
break
|
||||
ahead += 1
|
||||
if found:
|
||||
break
|
||||
|
||||
# If not found in reserved, it may already be active or not yet visible.
|
||||
# In that case, treat it as position 1.
|
||||
if found:
|
||||
return max(ahead + 1, 1)
|
||||
else:
|
||||
return 1
|
||||
except Exception:
|
||||
# If inspection fails, don't guess; caller should use a safe message.
|
||||
return 0
|
||||
|
||||
|
||||
def send_initial_email(to: str, queue_pos: int):
|
||||
"""
|
||||
Send initial confirmation email with queue position.
|
||||
Subject is customizable via EMAIL_SUBJECT_UPLOAD.
|
||||
"""
|
||||
subject = _get_subject(
|
||||
"EMAIL_SUBJECT_UPLOAD",
|
||||
"ScrAIbe: Your transcription request has been received",
|
||||
)
|
||||
|
||||
body = (
|
||||
"Hello,\n\n"
|
||||
"We have received your audio file for transcription.\n"
|
||||
)
|
||||
|
||||
if queue_pos > 0:
|
||||
body += f"Your request is currently number {queue_pos} in the queue.\n"
|
||||
queue_position_display = (
|
||||
f'<span style="color:{_accent_color()}; font-weight:bold;">{queue_pos}</span>'
|
||||
)
|
||||
else:
|
||||
body += "Your request has been queued for processing.\n"
|
||||
queue_position_display = "the queue"
|
||||
|
||||
body += (
|
||||
"\n"
|
||||
"You will receive an email with your transcript (and summary, if requested) "
|
||||
"once processing is complete.\n\n"
|
||||
"If you have any questions, contact us at "
|
||||
f"{os.getenv('EMAIL_CONTACT_ADDRESS', 'support@example.com')}.\n\n"
|
||||
"This is an automated message from ScrAIbe.\n"
|
||||
)
|
||||
|
||||
html = None
|
||||
try:
|
||||
html = load_template(
|
||||
"upload_notification_template.html",
|
||||
queue_position_text=queue_position_display,
|
||||
)
|
||||
except EmailError as e:
|
||||
logger.warning("Failed to render upload notification template: %s", e)
|
||||
|
||||
try:
|
||||
send_email(to=to, subject=subject, body=body, html=html, attachments=[])
|
||||
logger.info("Initial confirmation email sent to %s", to)
|
||||
except EmailError as e:
|
||||
logger.error("Failed to send initial email to %s: %s", to, e)
|
||||
|
||||
|
||||
def send_success_email(
|
||||
to: str,
|
||||
transcript_text: str,
|
||||
summary_text: str,
|
||||
attachments: list,
|
||||
task_id: str,
|
||||
):
|
||||
"""
|
||||
Send final email with transcript and attachments.
|
||||
Subject is customizable via EMAIL_SUBJECT_SUCCESS.
|
||||
Falls back to a safe default if the env var is missing or blank.
|
||||
"""
|
||||
subject = _get_subject(
|
||||
"EMAIL_SUBJECT_SUCCESS",
|
||||
"ScrAIbe: Your transcript is ready",
|
||||
)
|
||||
|
||||
body = (
|
||||
"Hello,\n\n"
|
||||
"Your transcription is ready.\n\n"
|
||||
"Please find the transcript and JSON files attached.\n"
|
||||
)
|
||||
|
||||
if summary_text:
|
||||
body += (
|
||||
"\n"
|
||||
"SUMMARY\n"
|
||||
"-------\n"
|
||||
f"{summary_text}\n"
|
||||
)
|
||||
|
||||
body += (
|
||||
"\n"
|
||||
"Job ID: " + str(task_id) + "\n\n"
|
||||
"If you have any questions, contact us at "
|
||||
f"{os.getenv('EMAIL_CONTACT_ADDRESS', 'support@example.com')}.\n\n"
|
||||
"This is an automated message from ScrAIbe.\n"
|
||||
)
|
||||
|
||||
html = None
|
||||
try:
|
||||
html = load_template("success_template.html")
|
||||
except EmailError as e:
|
||||
logger.warning("Failed to render success template: %s", e)
|
||||
|
||||
try:
|
||||
send_email(
|
||||
to=to,
|
||||
subject=subject,
|
||||
body=body,
|
||||
html=html,
|
||||
attachments=attachments,
|
||||
)
|
||||
logger.info("Success email sent to %s for job %s with subject: %s", to, task_id, subject)
|
||||
except EmailError as e:
|
||||
logger.error("Failed to send success email to %s for job %s: %s", to, task_id, e)
|
||||
|
||||
|
||||
def send_error_email(to: str, error_message: str, task_id: str):
|
||||
"""
|
||||
Send error notification email.
|
||||
Subject is customizable via EMAIL_SUBJECT_ERROR.
|
||||
"""
|
||||
subject = _get_subject(
|
||||
"EMAIL_SUBJECT_ERROR",
|
||||
"ScrAIbe: Error with your transcription request",
|
||||
)
|
||||
|
||||
body = (
|
||||
"Hello,\n\n"
|
||||
"We encountered an error while processing your transcription request.\n\n"
|
||||
f"Details: {error_message}\n\n"
|
||||
"Job ID: " + str(task_id) + "\n\n"
|
||||
"Please contact your administrator if the problem persists.\n\n"
|
||||
"If you have any questions, contact us at "
|
||||
f"{os.getenv('EMAIL_CONTACT_ADDRESS', 'support@example.com')}.\n\n"
|
||||
"This is an automated message from ScrAIbe.\n"
|
||||
)
|
||||
|
||||
html = None
|
||||
try:
|
||||
html = load_template(
|
||||
"error_notification_template.html",
|
||||
exception=str(error_message),
|
||||
)
|
||||
except EmailError as e:
|
||||
logger.warning("Failed to render error template: %s", e)
|
||||
|
||||
try:
|
||||
send_email(to=to, subject=subject, body=body, html=html, attachments=[])
|
||||
logger.info("Error email sent to %s for job %s", to, task_id)
|
||||
except EmailError as e:
|
||||
logger.error("Failed to send error email to %s for job %s: %s", to, task_id, e)
|
||||
|
||||
|
||||
@celery_app.task(
|
||||
name="scraibe.tasks.process_transcription_task",
|
||||
bind=True,
|
||||
max_retries=1,
|
||||
task_time_limit=14400, # 4 hours
|
||||
task_soft_time_limit=13500, # warn at 3h45m
|
||||
)
|
||||
def process_transcription_task(
|
||||
self,
|
||||
audio_path: str,
|
||||
task_type: str,
|
||||
language: str,
|
||||
num_speakers: int,
|
||||
email_to: str,
|
||||
email_cc: str,
|
||||
include_summary: bool,
|
||||
identify_speakers: bool = False,
|
||||
):
|
||||
"""
|
||||
Async task: transcribe audio, optionally summarize, then email results.
|
||||
Cleans up temporary files after completion.
|
||||
"""
|
||||
task_id = self.request.id
|
||||
|
||||
log_level = os.getenv("LOG_LEVEL", "INFO")
|
||||
setup_logging(level=log_level)
|
||||
|
||||
temp_files = []
|
||||
local = _local_part(email_to)
|
||||
date_tag = _date_tag()
|
||||
|
||||
try:
|
||||
# 1) Queue position and initial email
|
||||
queue_pos = get_queue_position(task_id)
|
||||
send_initial_email(to=email_to, queue_pos=queue_pos)
|
||||
|
||||
# 2) Initialize Scraibe
|
||||
try:
|
||||
scraibe = Scraibe(verbose=True)
|
||||
except Exception as e:
|
||||
send_error_email(
|
||||
to=email_to,
|
||||
error_message=f"Failed to initialize transcription service: {e}",
|
||||
task_id=task_id,
|
||||
)
|
||||
raise
|
||||
|
||||
# 3) Transcription
|
||||
if task_type == "transcript_and_summarize":
|
||||
result = scraibe.transcript_and_summarize(
|
||||
audio_file=audio_path,
|
||||
language=language or None,
|
||||
num_speakers=int(num_speakers) if num_speakers else None,
|
||||
verbose=True,
|
||||
for_export=True,
|
||||
)
|
||||
transcript_text = result.get("transcript", "")
|
||||
summary_text = result.get("summary", "")
|
||||
segments = result.get("segments", [])
|
||||
raw_result = result.get("raw_result")
|
||||
else:
|
||||
result = scraibe.transcribe(
|
||||
audio_file=audio_path,
|
||||
language=language or None,
|
||||
num_speakers=int(num_speakers) if num_speakers else None,
|
||||
verbose=True,
|
||||
for_export=True,
|
||||
)
|
||||
transcript_text = result.get("transcript", "")
|
||||
summary_text = ""
|
||||
segments = result.get("segments", [])
|
||||
raw_result = result.get("raw_result")
|
||||
|
||||
# 3b) Optional speaker identification
|
||||
speaker_map = {} # e.g. {"SPEAKER 1": "John", "SPEAKER 2": "Maria"}
|
||||
if identify_speakers:
|
||||
try:
|
||||
# Use the same summarizer client as transcript_and_summarize
|
||||
scraibe._ensure_summarizer()
|
||||
summarizer = scraibe._summarizer
|
||||
|
||||
prompt = (
|
||||
"Below is a transcript with speaker labels like 'SPEAKER 1', 'SPEAKER 2', etc. "
|
||||
"Based on the context and how each speaker talks, identify each speaker as:\n"
|
||||
"- Their real name, if it is clearly mentioned or strongly implied, OR\n"
|
||||
"- A concise role/position (e.g., Judge, Doctor, Manager, Interviewer, Client, Witness), "
|
||||
"if their identity is not clear.\n"
|
||||
"Do not invent random personal names. "
|
||||
"Do not add extra commentary. Output ONLY a mapping in this exact format, one per line:\n"
|
||||
"SPEAKER 1: Name or Role\n"
|
||||
"SPEAKER 2: Name or Role\n"
|
||||
"SPEAKER 3: Name or Role\n"
|
||||
"\n"
|
||||
"Transcript:\n"
|
||||
+ transcript_text
|
||||
)
|
||||
|
||||
response = summarizer._chat_completion(
|
||||
messages=[{"role": "user", "content": prompt}],
|
||||
temperature=0.3,
|
||||
max_tokens=300,
|
||||
)
|
||||
reply = (response or {}).get("choices", [{}])[0].get("message", {}).get("content", "")
|
||||
|
||||
# Parse mapping
|
||||
import re
|
||||
for m in re.finditer(
|
||||
r"SPEAKER\s+(\d+)\s*:\s*(.+)",
|
||||
reply,
|
||||
re.IGNORECASE,
|
||||
):
|
||||
spk = f"SPEAKER {m.group(1).strip()}"
|
||||
name = m.group(2).strip().rstrip(".").upper()
|
||||
if name:
|
||||
speaker_map[spk] = name
|
||||
|
||||
logger.info("Speaker identification mapping: %s", speaker_map)
|
||||
|
||||
# Apply mapping to transcript text
|
||||
if speaker_map:
|
||||
def replace_speaker(m):
|
||||
label = m.group(0).strip()
|
||||
# normalize to "SPEAKER N"
|
||||
normalized = re.sub(
|
||||
r"\s+",
|
||||
" ",
|
||||
re.sub(r"[^A-Z0-9\s]", "", label.upper()),
|
||||
).strip()
|
||||
return speaker_map.get(normalized, label)
|
||||
|
||||
# Replace in lines like "[00:12] SPEAKER 1:" but preserve timestamp and colon
|
||||
def replace_in_line(line: str) -> str:
|
||||
# match after timestamp bracket and space: "SPEAKER N:"
|
||||
return re.sub(
|
||||
r"(\[\d+:\d+(?::\d+)?\]\s*)([A-Z\s]+?):\s*",
|
||||
lambda m: m.group(1) + (speaker_map.get(m.group(2).strip(), m.group(2)) + ": "),
|
||||
line,
|
||||
)
|
||||
|
||||
transcript_lines = transcript_text.splitlines()
|
||||
transcript_text = "\n".join(
|
||||
replace_in_line(line) for line in transcript_lines
|
||||
)
|
||||
|
||||
# Also update segments for JSON export
|
||||
updated_segments = []
|
||||
for seg in segments:
|
||||
sp = (seg.get("speaker") or "").strip()
|
||||
sp_norm = re.sub(r"[^A-Z0-9\s]", "", sp.upper()).strip()
|
||||
sp_new = speaker_map.get(sp_norm, sp)
|
||||
seg = dict(seg)
|
||||
seg["speaker"] = sp_new
|
||||
updated_segments.append(seg)
|
||||
segments = updated_segments
|
||||
|
||||
except (SummarizerError, Exception) as e:
|
||||
logger.warning(
|
||||
"Speaker identification failed; falling back to Speaker IDs: %s", e
|
||||
)
|
||||
speaker_map = {}
|
||||
|
||||
# 4) Prepare files
|
||||
|
||||
# Transcript .md
|
||||
md_transcript_path = _safe_filename("TRANSCRIPT", local, date_tag, ".md")
|
||||
with open(md_transcript_path, "w", encoding="utf-8") as f:
|
||||
f.write("# Transcript\n\n")
|
||||
f.write(transcript_text)
|
||||
temp_files.append(md_transcript_path)
|
||||
|
||||
# Transcript .docx (standalone, no cover page)
|
||||
docx_transcript_path = _safe_filename("TRANSCRIPT", local, date_tag, ".docx")
|
||||
create_transcript_docx(
|
||||
transcript_text,
|
||||
docx_transcript_path,
|
||||
)
|
||||
temp_files.append(docx_transcript_path)
|
||||
|
||||
# JSON as SOURCE
|
||||
json_data = {
|
||||
"task": task_type,
|
||||
"transcript": transcript_text,
|
||||
"segments": segments,
|
||||
"metadata": {
|
||||
"timestamp": datetime.utcnow().isoformat(),
|
||||
"job_id": task_id,
|
||||
},
|
||||
}
|
||||
if summary_text:
|
||||
json_data["summary"] = summary_text
|
||||
if raw_result is not None:
|
||||
json_data["raw_result"] = raw_result
|
||||
|
||||
json_path = _safe_filename("SOURCE", local, date_tag, ".json")
|
||||
with open(json_path, "w", encoding="utf-8") as f:
|
||||
json.dump(json_data, f, indent=2, ensure_ascii=False)
|
||||
temp_files.append(json_path)
|
||||
|
||||
# Summary files (if present)
|
||||
md_summary_path = None
|
||||
docx_summary_path = None
|
||||
|
||||
if summary_text:
|
||||
# Summary .md
|
||||
md_summary_path = _safe_filename("SUMMARY", local, date_tag, ".md")
|
||||
with open(md_summary_path, "w", encoding="utf-8") as f:
|
||||
f.write("# Summary\n\n")
|
||||
f.write(summary_text)
|
||||
temp_files.append(md_summary_path)
|
||||
|
||||
# Summary .docx (standalone, no cover page)
|
||||
docx_summary_path = _safe_filename("SUMMARY", local, date_tag, ".docx")
|
||||
create_summary_docx(
|
||||
summary_text,
|
||||
docx_summary_path,
|
||||
)
|
||||
temp_files.append(docx_summary_path)
|
||||
|
||||
# 5) Build attachments list
|
||||
|
||||
# Always: JSON, transcript MD, transcript DOCX
|
||||
attachments = [
|
||||
md_transcript_path,
|
||||
docx_transcript_path,
|
||||
json_path,
|
||||
]
|
||||
|
||||
# If summary is present, add summary MD and DOCX
|
||||
if summary_text:
|
||||
attachments += [md_summary_path, docx_summary_path]
|
||||
|
||||
# 6) Send success email
|
||||
send_success_email(
|
||||
to=email_to,
|
||||
transcript_text=transcript_text,
|
||||
summary_text=summary_text if include_summary else "",
|
||||
attachments=attachments,
|
||||
task_id=task_id,
|
||||
)
|
||||
|
||||
logger.info("Job %s completed successfully.", task_id)
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error processing job %s: %s", task_id, e, exc_info=True)
|
||||
send_error_email(
|
||||
to=email_to,
|
||||
error_message=str(e),
|
||||
task_id=task_id,
|
||||
)
|
||||
raise e
|
||||
finally:
|
||||
# 7) Cleanup
|
||||
for path in temp_files:
|
||||
_remove_file(path)
|
||||
if audio_path:
|
||||
_remove_file(audio_path)
|
||||
logger.info("Cleanup completed for job %s.", task_id)
|
||||
@@ -0,0 +1,338 @@
|
||||
"""
|
||||
ScrAIbe Web GUI (Gradio) - Async Mode
|
||||
-------------------------------------
|
||||
|
||||
Runs the Web GUI that:
|
||||
- Accepts audio uploads
|
||||
- Enqueues transcription jobs asynchronously via Celery
|
||||
- Backend worker:
|
||||
- Transcribes (with diarization)
|
||||
- Optionally summarizes
|
||||
- Emails the user:
|
||||
- Immediately: confirmation + queue position
|
||||
- On success: transcript + JSON (+ summary if requested)
|
||||
- On error: error details
|
||||
|
||||
This is the default entrypoint when running in Docker.
|
||||
"""
|
||||
|
||||
import os
|
||||
import logging
|
||||
import shutil
|
||||
from datetime import datetime
|
||||
|
||||
import gradio as gr
|
||||
|
||||
from .misc import setup_logging
|
||||
|
||||
logger = logging.getLogger("scraibe.webui")
|
||||
|
||||
|
||||
def load_config():
|
||||
"""
|
||||
Load configuration from misc/config.yaml if present.
|
||||
Primary runtime configuration is via environment variables.
|
||||
"""
|
||||
config_path = os.getenv("SCRAIBE_CONFIG", "/app/src/misc/config.yaml")
|
||||
config = {}
|
||||
if os.path.exists(config_path):
|
||||
try:
|
||||
import yaml
|
||||
with open(config_path, "r", encoding="utf-8") as f:
|
||||
config = yaml.safe_load(f) or {}
|
||||
except Exception as e:
|
||||
logger.warning("Failed to load config from %s: %s", config_path, e)
|
||||
return config
|
||||
|
||||
|
||||
def load_html_template(path: str, **kwargs) -> str:
|
||||
"""
|
||||
Load an HTML template and fill placeholders.
|
||||
"""
|
||||
if not os.path.exists(path):
|
||||
return ""
|
||||
with open(path, "r", encoding="utf-8") as f:
|
||||
template = f.read()
|
||||
try:
|
||||
return template.format(**kwargs)
|
||||
except KeyError:
|
||||
return template
|
||||
|
||||
|
||||
def create_app():
|
||||
"""
|
||||
Create and launch the Gradio Web GUI (async mode).
|
||||
"""
|
||||
|
||||
# Logging
|
||||
log_level = os.getenv("LOG_LEVEL", "INFO")
|
||||
setup_logging(level=log_level)
|
||||
|
||||
# Load config (branding, layout, etc.)
|
||||
config = load_config()
|
||||
layout_cfg = config.get("layout", {})
|
||||
launch_cfg = config.get("launch", {})
|
||||
|
||||
logger.info("Starting ScrAIbe Web GUI (async mode).")
|
||||
|
||||
# Ensure upload directory exists
|
||||
upload_dir = os.getenv("SCRAIBE_UPLOAD_DIR", "/tmp/scraibe_uploads")
|
||||
os.makedirs(upload_dir, exist_ok=True)
|
||||
|
||||
# Paths for assets
|
||||
header_path = layout_cfg.get("header", "/app/src/misc/header.html")
|
||||
footer_path = layout_cfg.get("footer", "/app/src/misc/footer.html")
|
||||
|
||||
# Configurable title, logo URL, and accent color via environment
|
||||
webui_title = os.getenv("WEBUI_TITLE", "A.P.Strom Transcription")
|
||||
logo_url = os.getenv("WEBUI_LOGO_URL", "https://apstrom.ca")
|
||||
accent_color = os.getenv("EMAIL_ACCENT_COLOR", "#7C6DA0")
|
||||
|
||||
# Prepare header HTML with logo URL and accent color
|
||||
header_html = ""
|
||||
if os.path.exists(header_path):
|
||||
header_html = load_html_template(
|
||||
header_path,
|
||||
webui_title=webui_title,
|
||||
header_logo_url=logo_url,
|
||||
header_logo_src=logo_url,
|
||||
accent_color=accent_color,
|
||||
)
|
||||
|
||||
# Prepare footer HTML with accent color
|
||||
footer_html = ""
|
||||
if os.path.exists(footer_path):
|
||||
version = os.getenv("SCRABIE_VERSION", "0.1.1.dev")
|
||||
footer_html = load_html_template(
|
||||
footer_path,
|
||||
footer_scraibe_webui_version=version,
|
||||
accent_color=accent_color,
|
||||
)
|
||||
|
||||
# Build Gradio interface
|
||||
with gr.Blocks(
|
||||
title=webui_title,
|
||||
css="""
|
||||
/* Responsive layout: stack columns on smaller screens */
|
||||
@media (max-width: 850px) {
|
||||
.gradio-container {
|
||||
max-width: 100% !important;
|
||||
}
|
||||
#main-row .gr-row {
|
||||
flex-direction: column !important;
|
||||
}
|
||||
#main-row .gr-col {
|
||||
width: 100% !important;
|
||||
max-width: 100% !important;
|
||||
flex: none !important;
|
||||
}
|
||||
}
|
||||
""",
|
||||
) as app:
|
||||
|
||||
# Header
|
||||
if header_html:
|
||||
gr.HTML(header_html)
|
||||
|
||||
# Single-column layout: inputs followed by status/output
|
||||
with gr.Column():
|
||||
audio_input = gr.Audio(
|
||||
label="Upload or record audio",
|
||||
type="filepath",
|
||||
)
|
||||
|
||||
task_choice = gr.Radio(
|
||||
choices=[
|
||||
("Transcribe", "transcribe"),
|
||||
("Transcribe & summarize", "transcript_and_summarize"),
|
||||
],
|
||||
value="transcribe",
|
||||
label="Task",
|
||||
container=True,
|
||||
)
|
||||
|
||||
identify_speakers = gr.Checkbox(
|
||||
label="Identify speakers (best effort using AI)",
|
||||
value=True,
|
||||
info="If enabled, AI will attempt to infer real names for speakers and replace Speaker 1/2/etc. in the transcript.",
|
||||
)
|
||||
|
||||
email_to = gr.Textbox(
|
||||
label="Your email address (required)",
|
||||
placeholder="e.g. your.name@example.com",
|
||||
)
|
||||
|
||||
email_cc = gr.Textbox(
|
||||
label="CC (optional, comma-separated)",
|
||||
placeholder="e.g. manager@example.com",
|
||||
)
|
||||
|
||||
submit_btn = gr.Button("Submit for transcription", variant="primary")
|
||||
|
||||
status_text = gr.Textbox(
|
||||
label="Status",
|
||||
lines=6,
|
||||
interactive=False,
|
||||
)
|
||||
|
||||
# Footer
|
||||
if footer_html:
|
||||
gr.HTML(footer_html)
|
||||
|
||||
# Events
|
||||
|
||||
def on_task_change(value):
|
||||
# No special UI changes needed; both modes handled in backend
|
||||
return
|
||||
|
||||
task_choice.change(
|
||||
fn=on_task_change,
|
||||
inputs=[task_choice],
|
||||
outputs=[],
|
||||
)
|
||||
|
||||
def on_submit(
|
||||
audio,
|
||||
task,
|
||||
email_to_val,
|
||||
email_cc_val,
|
||||
identify_speakers_val,
|
||||
):
|
||||
if not audio:
|
||||
return "Please upload or record audio."
|
||||
|
||||
email_to_val = (email_to_val or "").strip()
|
||||
if not email_to_val:
|
||||
return "Please enter your email address."
|
||||
|
||||
# Copy uploaded file to a stable location
|
||||
try:
|
||||
ext = os.path.splitext(audio)[1] or ".wav"
|
||||
ts = datetime.utcnow().strftime("%Y%m%d%H%M%S%f")
|
||||
new_name = f"upload_{ts}{ext}"
|
||||
dest_path = os.path.join(upload_dir, new_name)
|
||||
shutil.copy2(audio, dest_path)
|
||||
except Exception as e:
|
||||
logger.error("Error copying uploaded file: %s", e)
|
||||
return f"Error saving your file: {e}"
|
||||
|
||||
# Import Celery task
|
||||
try:
|
||||
from .tasks import process_transcription_task
|
||||
except ImportError:
|
||||
return (
|
||||
"Error: async processing is not available (Celery not configured)."
|
||||
)
|
||||
|
||||
# Enqueue transcription job
|
||||
try:
|
||||
task_result = process_transcription_task.delay(
|
||||
audio_path=dest_path,
|
||||
task_type=task,
|
||||
language=None,
|
||||
num_speakers=None,
|
||||
email_to=email_to_val,
|
||||
email_cc=email_cc_val or None,
|
||||
include_summary=(task == "transcript_and_summarize"),
|
||||
identify_speakers=bool(identify_speakers_val),
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error("Error enqueuing job: %s", e)
|
||||
return f"Error submitting your file: {e}"
|
||||
|
||||
return (
|
||||
"Your audio file has been received and added to the queue.\n"
|
||||
"We have sent a confirmation email to you.\n"
|
||||
"You will receive another email with your transcript (and summary, if requested) "
|
||||
"once processing is complete.\n"
|
||||
f"Job ID: {task_result.id}"
|
||||
)
|
||||
|
||||
submit_btn.click(
|
||||
fn=on_submit,
|
||||
inputs=[
|
||||
audio_input,
|
||||
task_choice,
|
||||
email_to,
|
||||
email_cc,
|
||||
identify_speakers,
|
||||
],
|
||||
outputs=[status_text],
|
||||
)
|
||||
|
||||
# Launch options with accent color applied via CSS
|
||||
server_name = launch_cfg.get("server_name", os.getenv("GRADIO_SERVER_NAME", "0.0.0.0"))
|
||||
server_port = launch_cfg.get("server_port", 7860)
|
||||
|
||||
accent_css = f"""
|
||||
:root {{
|
||||
--primary-accent: {accent_color};
|
||||
}}
|
||||
button.primary,
|
||||
.primary,
|
||||
.gradio-button-primary,
|
||||
.gradio-container button.primary {{
|
||||
background-color: var(--primary-accent) !important;
|
||||
border-color: var(--primary-accent) !important;
|
||||
}}
|
||||
button.primary:hover,
|
||||
.primary:hover,
|
||||
.gradio-button-primary:hover {{
|
||||
background-color: var(--primary-accent) !important;
|
||||
opacity: 0.95;
|
||||
}}
|
||||
.radio-item.selected,
|
||||
.radio-item.selected label {{
|
||||
color: var(--primary-accent) !important;
|
||||
}}
|
||||
a,
|
||||
.gradio-container a {{
|
||||
color: var(--primary-accent) !important;
|
||||
}}
|
||||
body {{
|
||||
font-family: Arial, sans-serif;
|
||||
}}
|
||||
/* Increase main title font size */
|
||||
h1,
|
||||
.webui-title,
|
||||
.header-title {{
|
||||
font-size: 60px !important;
|
||||
}}
|
||||
/* Hide Gradio's "Use via API" link/button */
|
||||
#share-btn,
|
||||
a[href*="/api"],
|
||||
a[href*="#/api"],
|
||||
a[href*="#api"],
|
||||
.gradio-container a[href*="api"] {{
|
||||
display: none !important;
|
||||
}}
|
||||
/* Mobile-friendly adjustments */
|
||||
@media (max-width: 700px) {{
|
||||
.gradio-container {{
|
||||
padding: 0 4px !important;
|
||||
}}
|
||||
.gradio-container .gr-row {{
|
||||
flex-direction: column !important;
|
||||
gap: 8px !important;
|
||||
}}
|
||||
.gradio-container .gr-col {{
|
||||
width: 100% !important;
|
||||
max-width: 100% !important;
|
||||
flex: none !important;
|
||||
}}
|
||||
.gradio-container button.primary {{
|
||||
width: 100% !important;
|
||||
box-sizing: border-box;
|
||||
}}
|
||||
}}
|
||||
"""
|
||||
|
||||
app.launch(
|
||||
server_name=str(server_name),
|
||||
server_port=int(server_port),
|
||||
css=accent_css,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
create_app()
|
||||
@@ -0,0 +1,86 @@
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
import pytest
|
||||
|
||||
from scraibe.audio import (
|
||||
get_audio_duration,
|
||||
split_audio_into_chunks,
|
||||
)
|
||||
|
||||
TEST_AUDIO_1 = "tests/audio_test_1.mp4"
|
||||
TEST_AUDIO_2 = "tests/audio_test_2.mp4"
|
||||
|
||||
|
||||
@pytest.fixture(params=[TEST_AUDIO_1, TEST_AUDIO_2])
|
||||
def test_audio_path(request):
|
||||
return request.param
|
||||
|
||||
|
||||
def test_get_audio_duration(test_audio_path):
|
||||
dur = get_audio_duration(test_audio_path)
|
||||
assert isinstance(dur, float)
|
||||
assert dur > 0
|
||||
|
||||
|
||||
def test_split_audio_into_chunks_no_split_short(test_audio_path):
|
||||
# For short files, should return the same file with no extra chunks
|
||||
chunks = split_audio_into_chunks(
|
||||
input_path=test_audio_path,
|
||||
max_duration=600.0, # larger than both test files
|
||||
overlap=2.0,
|
||||
)
|
||||
assert len(chunks) == 1
|
||||
assert chunks[0]["path"] == test_audio_path
|
||||
assert chunks[0]["start"] == 0.0
|
||||
dur = get_audio_duration(test_audio_path)
|
||||
assert abs(chunks[0]["end"] - dur) < 0.05
|
||||
|
||||
|
||||
def test_split_audio_into_chunks_creates_chunks(tmp_path):
|
||||
# Use a small chunk duration to force splitting
|
||||
chunks = split_audio_into_chunks(
|
||||
input_path=TEST_AUDIO_1,
|
||||
max_duration=2.0,
|
||||
overlap=0.5,
|
||||
)
|
||||
assert len(chunks) > 1
|
||||
|
||||
# Check that each chunk file exists and is non-empty
|
||||
for c in chunks:
|
||||
assert os.path.exists(c["path"])
|
||||
assert os.path.getsize(c["path"]) > 0
|
||||
|
||||
# Check time ordering and overlap
|
||||
for i in range(1, len(chunks)):
|
||||
prev = chunks[i - 1]
|
||||
curr = chunks[i]
|
||||
assert curr["start"] >= prev["start"]
|
||||
assert curr["start"] < prev["end"] # overlap
|
||||
|
||||
# Cleanup
|
||||
for c in chunks:
|
||||
if os.path.exists(c["path"]):
|
||||
os.remove(c["path"])
|
||||
|
||||
|
||||
def test_split_audio_into_chunks_total_coverage(test_audio_path):
|
||||
dur = get_audio_duration(test_audio_path)
|
||||
|
||||
# Use small chunks to ensure coverage
|
||||
chunks = split_audio_into_chunks(
|
||||
input_path=test_audio_path,
|
||||
max_duration=2.0,
|
||||
overlap=0.5,
|
||||
)
|
||||
|
||||
# First chunk starts at 0
|
||||
assert chunks[0]["start"] == 0.0
|
||||
|
||||
# Last chunk end should cover the duration
|
||||
assert chunks[-1]["end"] >= dur - 0.05
|
||||
|
||||
# Cleanup
|
||||
for c in chunks:
|
||||
if os.path.exists(c["path"]):
|
||||
os.remove(c["path"])
|
||||
@@ -0,0 +1,96 @@
|
||||
"""
|
||||
Local test for transcript/summary/combined .docx generation.
|
||||
Checks:
|
||||
- Line numbering only on transcript pages.
|
||||
- Page numbering (X of Y) in footer.
|
||||
- Cover pages present and centered.
|
||||
- Combined document structure.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import tempfile
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from scraibe.email_sender import (
|
||||
create_transcript_docx,
|
||||
create_summary_docx,
|
||||
create_combined_docx,
|
||||
)
|
||||
|
||||
TRANSCRIPT_TEXT = """[00:00] Speaker 1: Good morning, everyone. Thank you for joining today's meeting.
|
||||
[00:12] Speaker 2: Good morning. I'm looking forward to discussing the new requirements.
|
||||
[00:25] Speaker 1: Let's start with the timeline. We need to finalize the scope by Friday.
|
||||
[00:38] Speaker 2: Agreed. I'll send a summary of the key points after this call.
|
||||
[00:45] Speaker 1: Perfect. If there are no other items, we can wrap up here."""
|
||||
|
||||
SUMMARY_TEXT = """# Meeting Overview
|
||||
## Key Discussion Points
|
||||
### Timeline and Scope
|
||||
#### Next Steps"""
|
||||
|
||||
COVER_DATE = "June 14, 2026"
|
||||
TRANSCRIPT_DESC = "Transcript of a project planning meeting discussing timelines and scope."
|
||||
SUMMARY_DESC = "Summary of a project planning meeting covering key decisions and next steps."
|
||||
|
||||
|
||||
def main():
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
print("Using temp directory:", tmpdir)
|
||||
|
||||
# 1) Transcript-only
|
||||
transcript_path = os.path.join(tmpdir, "TRANSCRIPT_TEST.docx")
|
||||
print("Creating transcript-only docx:", transcript_path)
|
||||
create_transcript_docx(
|
||||
text=TRANSCRIPT_TEXT,
|
||||
filename=transcript_path,
|
||||
include_cover=True,
|
||||
cover_date=COVER_DATE,
|
||||
cover_desc=TRANSCRIPT_DESC,
|
||||
)
|
||||
print("OK: transcript-only created.")
|
||||
|
||||
# 2) Summary-only
|
||||
summary_path = os.path.join(tmpdir, "SUMMARY_TEST.docx")
|
||||
print("Creating summary-only docx:", summary_path)
|
||||
create_summary_docx(
|
||||
text=SUMMARY_TEXT,
|
||||
filename=summary_path,
|
||||
include_cover=True,
|
||||
cover_date=COVER_DATE,
|
||||
cover_desc=SUMMARY_DESC,
|
||||
)
|
||||
print("OK: summary-only created.")
|
||||
|
||||
# 3) Combined
|
||||
combined_path = os.path.join(tmpdir, "COMBINED_TEST.docx")
|
||||
print("Creating combined docx:", combined_path)
|
||||
create_combined_docx(
|
||||
transcript_text=TRANSCRIPT_TEXT,
|
||||
summary_text=SUMMARY_TEXT,
|
||||
filename=combined_path,
|
||||
transcript_cover_date=COVER_DATE,
|
||||
transcript_cover_desc=TRANSCRIPT_DESC,
|
||||
summary_cover_date=COVER_DATE,
|
||||
summary_cover_desc=SUMMARY_DESC,
|
||||
)
|
||||
print("OK: combined created.")
|
||||
|
||||
# Basic size sanity checks
|
||||
for path in [transcript_path, summary_path, combined_path]:
|
||||
size = os.path.getsize(path)
|
||||
print(f"File: {os.path.basename(path)} - size: {size} bytes")
|
||||
if size < 10000:
|
||||
print("WARNING: File seems unusually small:", path)
|
||||
|
||||
print("\nAll .docx files generated successfully.")
|
||||
print("Please open them in Word to verify:")
|
||||
print("- Only transcript pages have line numbers.")
|
||||
print("- Footer shows 'X of Y' on all pages.")
|
||||
print("- Cover pages are centered and use the correct date format.")
|
||||
print("- Combined doc order: cover, page break, summary, page break, transcript.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,230 @@
|
||||
import os
|
||||
import json
|
||||
import tempfile
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
||||
import pytest
|
||||
|
||||
from scraibe.localai_client import LocalAIClient, LocalAIError
|
||||
from scraibe.audio import get_audio_duration, split_audio_into_chunks
|
||||
|
||||
|
||||
TEST_AUDIO_1 = "tests/audio_test_1.mp4"
|
||||
|
||||
|
||||
def make_fake_segments(start=0.0, count=3):
|
||||
segments = []
|
||||
for i in range(count):
|
||||
s = start + i * 2.0
|
||||
e = s + 2.0
|
||||
segments.append({
|
||||
"start": s,
|
||||
"end": e,
|
||||
"speaker": "SPEAKER_00",
|
||||
"text": f"Segment text {i}",
|
||||
})
|
||||
return segments
|
||||
|
||||
|
||||
def fake_localai_response(segments):
|
||||
return {
|
||||
"segments": segments,
|
||||
"text": " ".join(seg["text"] for seg in segments),
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def client():
|
||||
with patch.object(LocalAIClient, "__init__", lambda self, **kw: None):
|
||||
c = LocalAIClient()
|
||||
c.api_url = "http://localhost:8080"
|
||||
c.model = "vibevoice-diarize"
|
||||
c.api_key = None
|
||||
c._client = MagicMock()
|
||||
return c
|
||||
|
||||
|
||||
def test_parse_diarization_response(client):
|
||||
segs = make_fake_segments()
|
||||
raw = fake_localai_response(segs)
|
||||
|
||||
out = client._parse_diarization_response(raw)
|
||||
|
||||
assert "segments" in out
|
||||
assert "speakers" in out
|
||||
assert "transcripts" in out
|
||||
assert len(out["segments"]) == len(segs)
|
||||
for i, s in enumerate(segs):
|
||||
assert out["segments"][i][0] == s["start"]
|
||||
assert out["segments"][i][1] == s["end"]
|
||||
assert out["speakers"][i] == s["speaker"]
|
||||
assert out["transcripts"][i] == s["text"]
|
||||
|
||||
|
||||
def test_parse_diarization_empty(client):
|
||||
out = client._parse_diarization_response({"segments": []})
|
||||
assert out["segments"] == []
|
||||
assert out["speakers"] == []
|
||||
assert out["transcripts"] == []
|
||||
|
||||
|
||||
def test_diarize_and_transcribe_single_happy(client):
|
||||
with patch.object(client, "_client") as mock_client:
|
||||
mock_resp = MagicMock()
|
||||
mock_resp.status_code = 200
|
||||
mock_resp.json.return_value = fake_localai_response(make_fake_segments())
|
||||
mock_client.post.return_value = mock_resp
|
||||
|
||||
result = client.diarize_and_transcribe(
|
||||
audio_path=TEST_AUDIO_1,
|
||||
verbose=False,
|
||||
return_raw=True,
|
||||
)
|
||||
|
||||
assert "segments" in result
|
||||
assert "raw_result" in result
|
||||
assert len(result["segments"]) > 0
|
||||
|
||||
|
||||
def test_chunking_triggered_for_long_audio(client):
|
||||
# Simulate long audio by patching get_audio_duration
|
||||
with patch("scraibe.localai_client.get_audio_duration") as mock_dur, \
|
||||
patch.object(client, "_diarize_and_transcribe_chunked") as mock_chunked:
|
||||
|
||||
mock_dur.return_value = 600.0 # 10 minutes
|
||||
mock_chunked.return_value = {
|
||||
"segments": [],
|
||||
"speakers": [],
|
||||
"transcripts": [],
|
||||
}
|
||||
|
||||
client.diarize_and_transcribe(
|
||||
audio_path=TEST_AUDIO_1,
|
||||
verbose=False,
|
||||
use_chunking=None,
|
||||
max_single_request_duration=300.0,
|
||||
)
|
||||
|
||||
mock_chunked.assert_called_once()
|
||||
|
||||
|
||||
def test_chunking_not_triggered_for_short_audio(client):
|
||||
with patch("scraibe.localai_client.get_audio_duration") as mock_dur, \
|
||||
patch.object(client, "_diarize_and_transcribe_chunked") as mock_chunked, \
|
||||
patch.object(client, "_diarize_and_transcribe_single") as mock_single:
|
||||
|
||||
mock_dur.return_value = 120.0
|
||||
mock_single.return_value = {
|
||||
"segments": [],
|
||||
"speakers": [],
|
||||
"transcripts": [],
|
||||
}
|
||||
|
||||
client.diarize_and_transcribe(
|
||||
audio_path=TEST_AUDIO_1,
|
||||
verbose=False,
|
||||
use_chunking=None,
|
||||
max_single_request_duration=300.0,
|
||||
)
|
||||
|
||||
mock_chunked.assert_not_called()
|
||||
mock_single.assert_called_once()
|
||||
|
||||
|
||||
def test_chunked_transcription_adjusts_timestamps(client):
|
||||
# Mock split_audio_into_chunks to return two chunks
|
||||
chunk1_path = TEST_AUDIO_1
|
||||
chunk2_path = TEST_AUDIO_1 # reusing same file; in real usage different
|
||||
|
||||
chunks = [
|
||||
{"path": chunk1_path, "start": 0.0, "end": 10.0},
|
||||
{"path": chunk2_path, "start": 10.0, "end": 20.0},
|
||||
]
|
||||
|
||||
with patch("scraibe.localai_client.split_audio_into_chunks") as mock_split, \
|
||||
patch.object(client, "_diarize_and_transcribe_single") as mock_single, \
|
||||
patch("os.remove"):
|
||||
|
||||
mock_split.return_value = chunks
|
||||
|
||||
# First chunk: segments 0–4
|
||||
# Second chunk: segments 0–4 (local times)
|
||||
def side_effect(audio_path, **kw):
|
||||
if audio_path == chunk1_path:
|
||||
segs = make_fake_segments(start=0.0, count=2)
|
||||
else:
|
||||
segs = make_fake_segments(start=0.0, count=2)
|
||||
return client._parse_diarization_response(fake_localai_response(segs))
|
||||
|
||||
mock_single.side_effect = side_effect
|
||||
|
||||
result = client._diarize_and_transcribe_chunked(
|
||||
audio_path=TEST_AUDIO_1,
|
||||
verbose=False,
|
||||
return_raw=False,
|
||||
chunk_duration=10.0,
|
||||
chunk_overlap=2.0,
|
||||
)
|
||||
|
||||
# Check we got 4 segments total
|
||||
assert len(result["segments"]) == 4
|
||||
|
||||
# First two segments should be in [0, 4]
|
||||
assert result["segments"][0][0] == 0.0
|
||||
assert result["segments"][1][0] == 2.0
|
||||
|
||||
# Next two segments should be shifted by 10
|
||||
assert result["segments"][2][0] == 10.0
|
||||
assert result["segments"][3][0] == 12.0
|
||||
|
||||
|
||||
@pytest.mark.integration
|
||||
def test_integration_chunked_transcription_with_localai():
|
||||
"""
|
||||
Integration test: run chunked transcription against a live LocalAI instance.
|
||||
Only runs if LOCALAI_API_URL is set and an audio file is provided.
|
||||
This test is skipped by default unless run with:
|
||||
pytest -m integration
|
||||
"""
|
||||
api_url = os.getenv("LOCALAI_API_URL")
|
||||
if not api_url:
|
||||
pytest.skip("LOCALAI_API_URL not set; skipping integration test")
|
||||
|
||||
# Use one of the bundled test audio files
|
||||
audio_path = TEST_AUDIO_1
|
||||
if not os.path.exists(audio_path):
|
||||
pytest.skip(f"Test audio not found: {audio_path}")
|
||||
|
||||
# Force chunking with a very small max_single_request_duration
|
||||
# Use environment-configured model or a sensible default
|
||||
model = os.getenv("LOCALAI_MODEL") or "vibevoice-cpp-asr"
|
||||
|
||||
client = LocalAIClient(api_url=api_url, model=model)
|
||||
try:
|
||||
result = client.diarize_and_transcribe(
|
||||
audio_path=audio_path,
|
||||
verbose=True,
|
||||
return_raw=True,
|
||||
use_chunking=True,
|
||||
chunk_duration=3.0,
|
||||
chunk_overlap=0.5,
|
||||
max_single_request_duration=1.0,
|
||||
)
|
||||
|
||||
assert "segments" in result
|
||||
assert len(result["segments"]) > 0
|
||||
|
||||
# Basic sanity: segments are time-ordered
|
||||
for i in range(1, len(result["segments"])):
|
||||
prev_end = result["segments"][i - 1][1]
|
||||
curr_start = result["segments"][i][0]
|
||||
assert curr_start >= result["segments"][i - 1][0]
|
||||
|
||||
# If raw_result indicates chunked, ensure structure is sensible
|
||||
raw = result.get("raw_result")
|
||||
if raw and raw.get("chunked"):
|
||||
assert "chunks" in raw
|
||||
assert len(raw["chunks"]) > 1
|
||||
|
||||
finally:
|
||||
client.close()
|
||||
Reference in New Issue
Block a user