openscribe/server/app/main.py
Laurence 51321aa7c5
Some checks failed
ci / firmware (pull_request) Failing after 1s
ci / emulator (pull_request) Failing after 27s
ci / openapi (pull_request) Failing after 33s
ci / server (pull_request) Failing after 37s
feat(server): pluggable AI providers - any open-standard or commercial AI
Lets the owner point transcription and summarisation at any AI: an open-standard
endpoint (OpenAI-compatible / local faster-whisper / Ollama) or a commercial API
(OpenAI, Anthropic, Gemini). Config-driven, self-hostable, no lock-in.

What changed:
- server/app/providers/: provider layer.
  - base.py: Transcriber/Summariser protocols + shared summary prompt + tolerant JSON
    parser (uniform Summary shape across providers).
  - summary.py: OpenAICompatibleSummariser (any /chat/completions - OpenAI, Groq,
    OpenRouter, LocalAI, LM Studio, vLLM, Ollama /v1) and AnthropicSummariser (Claude
    via the official anthropic SDK; Messages API has no OpenAI-compatible endpoint).
  - transcription.py: OpenAICompatibleTranscriber (/audio/transcriptions - OpenAI,
    Groq, self-hosted whisper server) and LocalWhisperTranscriber (faster-whisper,
    execution wired in M5).
  - factory.py: builds the configured providers with per-provider defaults
    (anthropic -> claude-opus-4-8, openai_compatible -> gpt-4o-mini, ollama -> llama3.1).
- config.py + .env.example: transcription_provider / llm_provider selectors + base_url,
  key, model settings; local faster-whisper and Ollama kept as the self-hosted defaults.
- main.py: /health now reports the resolved provider names (no secrets).
- requirements.txt: httpx drives all HTTP providers; anthropic + faster-whisper are
  optional, only for their respective providers.
- docs/ai-providers.md: config recipes for OpenAI, Groq, Anthropic, Gemini, LocalAI,
  LM Studio, Ollama, self-hosted whisper.
- state/: DECISIONS, ARCHITECTURE, TODO updated.

Why:
- The user asked to connect the device to any open standard AI or commercial one; this
  is also the core differentiator vs Plaud's locked cloud.

Notes:
- Anthropic provider uses the official SDK and defaults to claude-opus-4-8 (per the
  claude-api guidance). AI deps are optional per chosen provider. Modules byte-compile
  cleanly; end-to-end wiring into the ingest pipeline lands with M5.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-03 18:56:58 +01:00

77 lines
No EOL
2.9 KiB
Python

# SPDX-License-Identifier: GPL-3.0-only
"""OpenScribe server - FastAPI app.
M0 scaffold: wires the routes from api/openapi.yaml with in-memory stubs so the API shape
is real and browsable at /docs. The AI pipeline (faster-whisper transcription in M5,
Ollama summaries in M6) and real storage/DB replace the stubs in later milestones.
"""
from __future__ import annotations
from fastapi import FastAPI, HTTPException
from .config import settings
from .models import Recording, RecordingPage, Summary, Transcript
app = FastAPI(
title="OpenScribe API",
version="0.1.0",
description="Self-hosted AI voice recorder server. See api/openapi.yaml.",
)
# In-memory store stands in for the DB + object storage until M5.
_recordings: dict[str, Recording] = {}
@app.get("/health")
def health() -> dict:
# Report the resolved provider names (never secrets) so operators can confirm config.
from .providers import build_summariser, build_transcriber
try:
summariser = build_summariser().name
except Exception as exc: # e.g. anthropic dep not installed
summariser = f"unavailable ({type(exc).__name__})"
return {
"status": "ok",
"storage_backend": settings.storage_backend,
"transcription_provider": settings.transcription_provider,
"transcriber": build_transcriber().name,
"llm_provider": settings.llm_provider,
"summariser": summariser,
}
@app.get("/api/v1/recordings", response_model=RecordingPage, tags=["recordings"])
def list_recordings(limit: int = 50) -> RecordingPage:
return RecordingPage(items=list(_recordings.values())[:limit], next_cursor=None)
@app.get("/api/v1/recordings/{rec_id}", response_model=Recording, tags=["recordings"])
def get_recording(rec_id: str) -> Recording:
rec = _recordings.get(rec_id)
if rec is None:
raise HTTPException(status_code=404, detail="No such recording")
return rec
@app.get("/api/v1/recordings/{rec_id}/transcript", response_model=Transcript, tags=["server"])
def get_transcript(rec_id: str) -> Transcript:
# Implemented in M5 (faster-whisper). Until then, signal "not transcribed yet".
if rec_id not in _recordings:
raise HTTPException(status_code=404, detail="No such recording")
raise HTTPException(status_code=409, detail="Not transcribed yet (M5)")
@app.get("/api/v1/recordings/{rec_id}/summary", response_model=Summary, tags=["server"])
def get_summary(rec_id: str) -> Summary:
# Implemented in M6 (Ollama). Until then, signal "not summarised yet".
if rec_id not in _recordings:
raise HTTPException(status_code=404, detail="No such recording")
raise HTTPException(status_code=409, detail="Not summarised yet (M6)")
@app.post("/api/v1/ingest", response_model=Recording, status_code=202, tags=["server"])
def ingest(recording: Recording) -> Recording:
# M5 will store audio to the object store and queue transcription + summary.
_recordings[recording.id] = recording
return recording