mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-13 18:15:02 +00:00
The backend import package is now docsgpt, the name it will carry on PyPI; application was far too generic to install into anyone's site-packages. git mv plus a mechanical rewrite of every import, dotted string and path reference: 734 Python files, the compose files, Dockerfile, workflows, docs, setup scripts, devcontainer, k8s manifests, vscode config, pytest and coverage config, .gitignore. Behaviour is unchanged. Kept for one release: - A top-level application package whose meta-path finder resolves application.x.y to the already-imported docsgpt.x.y object, so old imports and entry points (celery -A application.app.celery, uvicorn application.asgi:asgi_app) keep working with a FutureWarning. - Celery registers every application.* task name as an alias of its docsgpt.* task on start-up, so messages queued by the previous release still run. The redbeat key prefix moves to redbeat:docsgpt:v2: so schedule entries the previous release wrote are left unread instead of firing twice. The backend image builds from the repository root (docker build -f docsgpt/Dockerfile .) so it can ship the alias package; a root .dockerignore allow-lists docsgpt/ and application/ and keeps caches, local data, .env files, the sample index files and the Dockerfile out. Compose and the image workflows point at the new context.
67 lines
2.1 KiB
Python
67 lines
2.1 KiB
Python
from pathlib import Path
|
|
from typing import Any, Dict, Optional
|
|
|
|
from openai import OpenAI
|
|
|
|
from docsgpt.core.settings import settings
|
|
from docsgpt.stt.base import BaseSTT
|
|
|
|
# Placeholder sent to OpenAI-compatible backends that require no credentials.
|
|
NO_API_KEY = "sk-no-key"
|
|
|
|
|
|
class OpenAISTT(BaseSTT):
|
|
def __init__(
|
|
self,
|
|
api_key: Optional[str] = None,
|
|
base_url: Optional[str] = None,
|
|
model: Optional[str] = None,
|
|
):
|
|
# See docsgpt/llm/openai.py: openai>=2.53 rejects a falsy api_key.
|
|
self.api_key = (
|
|
api_key or settings.OPENAI_API_KEY or settings.API_KEY or NO_API_KEY
|
|
)
|
|
self.base_url = base_url or settings.OPENAI_BASE_URL or "https://api.openai.com/v1"
|
|
self.model = model or settings.OPENAI_STT_MODEL
|
|
self.client = OpenAI(api_key=self.api_key, base_url=self.base_url)
|
|
|
|
def transcribe(
|
|
self,
|
|
file_path: Path,
|
|
language: Optional[str] = None,
|
|
timestamps: bool = False,
|
|
diarize: bool = False,
|
|
) -> Dict[str, Any]:
|
|
_ = diarize
|
|
request: Dict[str, Any] = {
|
|
"file": file_path,
|
|
"model": self.model,
|
|
"response_format": "verbose_json",
|
|
}
|
|
if language:
|
|
request["language"] = language
|
|
if timestamps:
|
|
request["timestamp_granularities"] = ["segment"]
|
|
|
|
with open(file_path, "rb") as audio_file:
|
|
request["file"] = audio_file
|
|
response = self.client.audio.transcriptions.create(**request)
|
|
response_dict = self._to_dict(response)
|
|
segments = response_dict.get("segments") or []
|
|
|
|
return {
|
|
"text": response_dict.get("text", ""),
|
|
"language": response_dict.get("language") or language,
|
|
"duration_s": response_dict.get("duration"),
|
|
"segments": [self._to_dict(segment) for segment in segments],
|
|
"provider": "openai",
|
|
}
|
|
|
|
@staticmethod
|
|
def _to_dict(value: Any) -> Dict[str, Any]:
|
|
if hasattr(value, "model_dump"):
|
|
return value.model_dump()
|
|
if isinstance(value, dict):
|
|
return value
|
|
return {}
|