lambda-erp 0.1.18__tar.gz → 0.1.19__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/PKG-INFO +1 -1
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/chat.py +134 -1
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/providers.py +18 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/pyproject.toml +1 -1
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/.gitignore +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/LICENSE +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/README.md +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/__init__.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/attachments.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/auth.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/bootstrap.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/demo_limits.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/deps.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/errors.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/main.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/pdf.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/routers/__init__.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/routers/accounting.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/routers/admin.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/routers/analytics.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/routers/bank_reconciliation.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/routers/documents.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/routers/masters.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/routers/reports.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/routers/setup.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/services.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/api/templates/document.html +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/docs/agents/README.md +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/frontend/README.md +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/frontend/src/api/client.ts +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/__init__.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/accounting/__init__.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/accounting/bank_transaction.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/accounting/budget.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/accounting/chart_of_accounts.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/accounting/general_ledger.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/accounting/journal_entry.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/accounting/payment_entry.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/accounting/pos_invoice.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/accounting/purchase_invoice.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/accounting/revaluation.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/accounting/sales_invoice.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/accounting/subscription.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/buying/__init__.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/buying/purchase_order.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/controllers/__init__.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/controllers/currency.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/controllers/defaults.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/controllers/pricing_rule.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/controllers/taxes_and_totals.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/database.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/exceptions.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/hooks.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/model.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/selling/__init__.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/selling/quotation.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/selling/sales_order.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/simulation.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/stock/__init__.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/stock/delivery_note.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/stock/purchase_receipt.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/stock/stock_entry.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/stock/stock_ledger.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/lambda_erp/utils.py +0 -0
- {lambda_erp-0.1.18 → lambda_erp-0.1.19}/terraform/README.md +0 -0
|
@@ -9,9 +9,12 @@ Supports multiple chat sessions, each with their own history and auto-generated
|
|
|
9
9
|
"""
|
|
10
10
|
|
|
11
11
|
import asyncio
|
|
12
|
+
import base64
|
|
13
|
+
import io
|
|
12
14
|
import json
|
|
13
15
|
import logging
|
|
14
16
|
import os
|
|
17
|
+
import re
|
|
15
18
|
import traceback
|
|
16
19
|
import uuid
|
|
17
20
|
from datetime import date
|
|
@@ -29,7 +32,7 @@ from api.demo_limits import (
|
|
|
29
32
|
is_demo_role,
|
|
30
33
|
limiter as demo_limiter,
|
|
31
34
|
)
|
|
32
|
-
from api.providers import cost_of_anthropic_call, cost_of_openai_call
|
|
35
|
+
from api.providers import cost_of_anthropic_call, cost_of_openai_call, cost_of_transcription
|
|
33
36
|
from api.routers.masters import create_master_record, update_master_record, MASTER_IDENTITY_ALIAS
|
|
34
37
|
from lambda_erp.database import get_db
|
|
35
38
|
from lambda_erp.utils import flt, now, nowdate
|
|
@@ -2326,6 +2329,65 @@ async def run_thinking_loop(
|
|
|
2326
2329
|
await on_event({"type": "complete", "content": content})
|
|
2327
2330
|
|
|
2328
2331
|
|
|
2332
|
+
# ---------------------------------------------------------------------------
|
|
2333
|
+
# Speech-to-text (voice → text)
|
|
2334
|
+
# ---------------------------------------------------------------------------
|
|
2335
|
+
|
|
2336
|
+
# Domain-vocabulary biasing for the transcription `prompt`. A bare term list,
|
|
2337
|
+
# NO language constraint — the spoken language is auto-detected, and a
|
|
2338
|
+
# locale-derived hint would mis-transcribe users who speak another language
|
|
2339
|
+
# than their UI. Swiss German transcribes to Standard German on its own, which
|
|
2340
|
+
# is what the thinking loop wants. These are the ERP terms short utterances
|
|
2341
|
+
# otherwise mangle.
|
|
2342
|
+
VOICE_TRANSCRIBE_PROMPT = (
|
|
2343
|
+
"Lambda, ERP, Offerte, Angebot, Auftrag, Rechnung, Gutschrift, Beleg, "
|
|
2344
|
+
"Lieferschein, Bestellung, Zahlung, Buchung, Kunde, Lieferant, Artikel, "
|
|
2345
|
+
"Lager, Konto, Kostenstelle, quotation, sales order, invoice, credit note, "
|
|
2346
|
+
"purchase order, payment, journal entry, customer, supplier, item, warehouse"
|
|
2347
|
+
)
|
|
2348
|
+
|
|
2349
|
+
# Below this the API rejects the clip as "too short"; we treat it as a no-op
|
|
2350
|
+
# (the user tapped the mic by accident) rather than an error.
|
|
2351
|
+
_MIN_TRANSCRIBE_BYTES = 1500 # ~1s of webm/opus at the ~1.5 KB/s we assume below
|
|
2352
|
+
|
|
2353
|
+
|
|
2354
|
+
def transcribe_audio(audio_bytes: bytes, audio_format: str, api_key: str) -> str:
|
|
2355
|
+
"""Transcribe audio to text via OpenAI gpt-4o-transcribe.
|
|
2356
|
+
|
|
2357
|
+
Returns the transcript, or "" when the model detects silence/near-silence
|
|
2358
|
+
(empty result or a prompt-echo hallucination on a near-silent clip).
|
|
2359
|
+
Raises on an actual API error so the caller can surface it.
|
|
2360
|
+
"""
|
|
2361
|
+
client = OpenAI(api_key=api_key, timeout=httpx.Timeout(60.0, connect=10.0))
|
|
2362
|
+
|
|
2363
|
+
audio_file = io.BytesIO(audio_bytes)
|
|
2364
|
+
audio_file.name = f"voice.{audio_format or 'webm'}"
|
|
2365
|
+
|
|
2366
|
+
transcription = client.audio.transcriptions.create(
|
|
2367
|
+
model="gpt-4o-transcribe",
|
|
2368
|
+
file=audio_file,
|
|
2369
|
+
response_format="json",
|
|
2370
|
+
prompt=VOICE_TRANSCRIBE_PROMPT,
|
|
2371
|
+
)
|
|
2372
|
+
|
|
2373
|
+
text = (getattr(transcription, "text", "") or "").strip()
|
|
2374
|
+
if len(text) < 2:
|
|
2375
|
+
return ""
|
|
2376
|
+
|
|
2377
|
+
# Prompt-echo guard: on silent/noise audio, prompt-biased transcription
|
|
2378
|
+
# tends to hallucinate the biasing vocabulary back. If (nearly) every word
|
|
2379
|
+
# of the transcript is drawn from the prompt itself, it's an echo, not
|
|
2380
|
+
# speech — real utterances carry verbs/objects outside the glossary.
|
|
2381
|
+
prompt_tokens = set(re.findall(r"[\w'-]+", VOICE_TRANSCRIBE_PROMPT.lower()))
|
|
2382
|
+
text_tokens = re.findall(r"[\w'-]+", text.lower())
|
|
2383
|
+
if text_tokens:
|
|
2384
|
+
coverage = sum(1 for tok in text_tokens if tok in prompt_tokens) / len(text_tokens)
|
|
2385
|
+
if coverage >= 0.8:
|
|
2386
|
+
return ""
|
|
2387
|
+
|
|
2388
|
+
return text
|
|
2389
|
+
|
|
2390
|
+
|
|
2329
2391
|
# ---------------------------------------------------------------------------
|
|
2330
2392
|
# WebSocket endpoint
|
|
2331
2393
|
# ---------------------------------------------------------------------------
|
|
@@ -2675,6 +2737,77 @@ async def chat_websocket(
|
|
|
2675
2737
|
session_tasks[session_id] = asyncio.create_task(replay_demo_session(session_id))
|
|
2676
2738
|
continue
|
|
2677
2739
|
|
|
2740
|
+
if msg_type == "transcribe":
|
|
2741
|
+
# Voice → text: transcribe a recorded clip and return the text
|
|
2742
|
+
# for the composer. No LLM turn here — the user reviews/edits
|
|
2743
|
+
# the text and sends it like any typed message.
|
|
2744
|
+
openai_api_key = os.environ.get("OPENAI_API_KEY", "")
|
|
2745
|
+
if not openai_api_key or openai_api_key == "sk-your-key-here":
|
|
2746
|
+
await send_event(
|
|
2747
|
+
"transcription_result", request_id=request_id,
|
|
2748
|
+
success=False, error="Speech-to-text is not available (OPENAI_API_KEY not configured).",
|
|
2749
|
+
)
|
|
2750
|
+
continue
|
|
2751
|
+
|
|
2752
|
+
# Demo visitors are rate-limited the same way as LLM turns — a
|
|
2753
|
+
# paid OpenAI call still costs money. Block before transcribing.
|
|
2754
|
+
if is_demo_role(ws_user_role) and client_ip:
|
|
2755
|
+
blocked = demo_limiter.check(client_ip)
|
|
2756
|
+
if blocked:
|
|
2757
|
+
await send_event(
|
|
2758
|
+
"transcription_result", request_id=request_id,
|
|
2759
|
+
success=False, error=blocked,
|
|
2760
|
+
)
|
|
2761
|
+
continue
|
|
2762
|
+
|
|
2763
|
+
audio_b64 = data.get("audio_data") or ""
|
|
2764
|
+
audio_format = (data.get("audio_format") or "webm").lower()
|
|
2765
|
+
try:
|
|
2766
|
+
audio_bytes = base64.b64decode(audio_b64) if audio_b64 else b""
|
|
2767
|
+
except Exception:
|
|
2768
|
+
audio_bytes = b""
|
|
2769
|
+
|
|
2770
|
+
if len(audio_bytes) < _MIN_TRANSCRIBE_BYTES:
|
|
2771
|
+
# Too short to be speech (accidental tap) — treat as empty,
|
|
2772
|
+
# no API call, no cost.
|
|
2773
|
+
await send_event(
|
|
2774
|
+
"transcription_result", request_id=request_id,
|
|
2775
|
+
success=True, text="",
|
|
2776
|
+
)
|
|
2777
|
+
continue
|
|
2778
|
+
|
|
2779
|
+
try:
|
|
2780
|
+
text = await asyncio.to_thread(
|
|
2781
|
+
transcribe_audio, audio_bytes, audio_format, openai_api_key,
|
|
2782
|
+
)
|
|
2783
|
+
except Exception as e:
|
|
2784
|
+
logger.warning("Transcription failed: %s", e)
|
|
2785
|
+
await send_event(
|
|
2786
|
+
"transcription_result", request_id=request_id,
|
|
2787
|
+
success=False, error="Transcription failed. Please try again.",
|
|
2788
|
+
)
|
|
2789
|
+
continue
|
|
2790
|
+
|
|
2791
|
+
# Track the spend. Audio duration is estimated from byte size
|
|
2792
|
+
# (~1.5 KB/s for webm/opus); the Demo Spend Log records every
|
|
2793
|
+
# role for the admin dashboard, and counts public_manager rows
|
|
2794
|
+
# against the hourly cap.
|
|
2795
|
+
estimated_seconds = len(audio_bytes) / 1500
|
|
2796
|
+
demo_limiter.record(
|
|
2797
|
+
client_ip or "unknown",
|
|
2798
|
+
cost_of_transcription("gpt-4o-transcribe", estimated_seconds),
|
|
2799
|
+
role=ws_user_role,
|
|
2800
|
+
provider="openai",
|
|
2801
|
+
model="gpt-4o-transcribe",
|
|
2802
|
+
session_id=data.get("session_id"),
|
|
2803
|
+
)
|
|
2804
|
+
|
|
2805
|
+
await send_event(
|
|
2806
|
+
"transcription_result", request_id=request_id,
|
|
2807
|
+
success=True, text=text,
|
|
2808
|
+
)
|
|
2809
|
+
continue
|
|
2810
|
+
|
|
2678
2811
|
if msg_type != "send_message":
|
|
2679
2812
|
await send_error(f"Unknown message type: {msg_type}", request_id=request_id)
|
|
2680
2813
|
continue
|
|
@@ -43,6 +43,13 @@ ANTHROPIC_PRICING: dict[str, dict[str, Any]] = {
|
|
|
43
43
|
"claude-haiku-4-5": {"input": 1.00, "output": 5.00, "cache_write": 1.25, "cache_read": 0.10},
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
# Speech-to-text, priced per minute of audio (not per token).
|
|
47
|
+
TRANSCRIBE_PRICING: dict[str, dict[str, float]] = {
|
|
48
|
+
"gpt-4o-transcribe": {"per_minute": 0.006},
|
|
49
|
+
"gpt-4o-mini-transcribe": {"per_minute": 0.003},
|
|
50
|
+
"whisper-1": {"per_minute": 0.006},
|
|
51
|
+
}
|
|
52
|
+
|
|
46
53
|
|
|
47
54
|
# ---------------------------------------------------------------------------
|
|
48
55
|
# Rate lookup — unknown models fall back to the most expensive model in each
|
|
@@ -114,3 +121,14 @@ def cost_of_anthropic_call(model: str, usage: Optional[Any]) -> float:
|
|
|
114
121
|
+ output_tokens * rates["output"]
|
|
115
122
|
)
|
|
116
123
|
return total / 1_000_000
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def cost_of_transcription(model: str, audio_seconds: float) -> float:
|
|
127
|
+
"""USD cost of one speech-to-text call, billed per minute of audio.
|
|
128
|
+
|
|
129
|
+
Unknown models fall back to the most expensive STT rate so we never
|
|
130
|
+
under-count. `audio_seconds` is the (estimated) audio duration."""
|
|
131
|
+
rates = TRANSCRIBE_PRICING.get(model)
|
|
132
|
+
if rates is None:
|
|
133
|
+
rates = max(TRANSCRIBE_PRICING.values(), key=lambda r: r["per_minute"])
|
|
134
|
+
return max(float(audio_seconds or 0.0), 0.0) / 60.0 * rates["per_minute"]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|