Spaces:
Sleeping
Sleeping
GitHub Actions commited on
Commit ·
2d9f3fa
1
Parent(s): 0fab727
Deploy backend from GitHub
Browse filesFiles changed (5)
- INFERENCE_CONFIG.mdAdded+28
- requirements.txt+49-49
- src/inference_service.py+174-174
- src/managers.py+94-94
- test_groq_recovery.pyAdded+92
12345678910111213141516171819202122232425262728# Online inference configuration
The production Spaces use a Groq organization confirmed by its owner to be onthe Free plan. Store the credential as the `GROQ_API_KEY` Space Secret.
Set the Space Variable `INFERENCE_FREE_ONLY=true`. The `groq` route defaults to`qwen/qwen3.8-27b`; `GROQ_MODEL_NAME` may select another explicitly permittedGroq model. These models can be billed on a paid Groq account: the routingpolicy does not inspect or change the organization's billing plan.
With this policy, `auto` tries Groq and then `openrouter-free` if configured.It never tries the legacy Alibaba, DeepSeek, Gemini, or retired Llama routes.The OpenRouter route requires `OPENROUTER_API_KEY` and uses `openrouter/free`.Changing its endpoint or model to a paid alternative is rejected.
The application sends an explicit user-agent header because the Groq gatewaycan reject Python urllib's default user agent before authenticating the key.HTTP 401 is reported as `PROVIDER_AUTH_FAILED`; HTTP 403 is reported separatelyas `PROVIDER_ACCESS_DENIED`. Neither error body nor key is returned as content.
REST, Gradio v3 and legacy Groq mission/analysis calls share the boundedinference adapter. Groq Qwen uses instruct mode to avoid spending the responsebudget on reasoning instead of the requested translation or conversation.
`GET /api/health` lists configuration only. Verify a short synthetic request to`POST /api/translate` with `ai_model: "groq"` and check the returned model androute before claiming provider readiness. Configuration metadata is not alinguistic-quality evaluation.12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849# --- CORE UTILS ---setuptools==69.5.1wheelpytz# --- AI & TRANSCRIPTION ---openai-whisper @ git+https://github.com/openai/whisper.gitrapidfuzzpandas# --- INTERFACE ---gradio>=4.36.0# --- DEPENDENCIES ---# cpu-basic Spaces must not mix CPU torch with CUDA audio/vision extensions.# Keep this matched set in sync (https://pytorch.org/get-started/previous-versions/).--extra-index-url https://download.pytorch.org/whl/cputransformerstorch==2.8.0+cputorchaudio==2.8.0+cputorchvision==0.23.0+cpulibrosapydubffmpeg-pythonjiwergoogle-genaigroqpython-dotenvrequestsyt-dlpsoundfile# --- TRUST / BLOCKCHAIN & TRAINER ---web3>=6.0.0py-solc-x>=1.1.1autotrain-advanced# --- RAG PIPELINE (LOCAL EMBEDDINGS) ---sentence-transformersfaiss-cpu# --- FASTAPI MEGA-SERVER ---fastapipydantic>=2.7,<3uvicornopenai# Private, consent-gated recording encryptioncryptography>=4312345678910111213141516171819202122232425262728293031323334353637383940414243444546474849# --- CORE UTILS ---setuptools==69.5.1wheelpytz# --- AI & TRANSCRIPTION ---openai-whisper @ git+https://github.com/openai/whisper.gitrapidfuzzpandas# --- INTERFACE ---gradio>=4.36.0# --- DEPENDENCIES ---# cpu-basic Spaces must not mix CPU torch with CUDA audio/vision extensions.# Keep this matched set in sync (https://pytorch.org/get-started/previous-versions/).--extra-index-url https://download.pytorch.org/whl/cputransformerstorch==2.8.0+cputorchaudio==2.8.0+cputorchvision==0.23.0+cpulibrosapydubffmpeg-pythonjiwergoogle-genaigroqpython-dotenvrequestsyt-dlpsoundfile# --- TRUST / BLOCKCHAIN & TRAINER ---web3>=6.0.0py-solc-x>=1.1.1autotrain-advanced# --- RAG PIPELINE (LOCAL EMBEDDINGS) ---sentence-transformersfaiss-cpu# --- FASTAPI MEGA-SERVER ---fastapipydantic>=2.7,<3uvicornopenai# Private, consent-gated recording encryptioncryptography>=43
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174"""Small, fail-closed provider adapter shared by REST and Gradio.Configured routes are not health claims. Explicit model choices never fall backto another provider; ``auto`` may choose from configured routes."""import jsonimport osimport reimport timeimport urllib.errorimport urllib.parseimport urllib.requestROUTES = {"qwen": ("QWEN", "https://dashscope-intl.aliyuncs.com/compatible-mode/v1", "qwen-plus"),"llama": ("GROQ", "https://api.groq.com/openai/v1", "llama-3.3-70b-versatile"),"deepseek": ("DEEPSEEK", "https://api.deepseek.com", "deepseek-chat"),"gemini": ("GEMINI", "https://generativelanguage.googleapis.com/v1beta/openai", "gemini-2.5-flash"),"nemotron": ("OPENROUTER", "https://openrouter.ai/api/v1", "nvidia/nemotron-3-nano-30b-a3b:free"),"gpt-oss": ("OPENROUTER", "https://openrouter.ai/api/v1", "openai/gpt-oss-20b:free"),"lfm": ("OPENROUTER", "https://openrouter.ai/api/v1", "liquid/lfm-2.5-1.2b-instruct:free"),"openrouter-free": ("OPENROUTER", "https://openrouter.ai/api/v1", "openrouter/free"),"groq": ("GROQ", "https://api.groq.com/openai/v1", "qwen/qwen3.8-27b"),}MODEL_ENV = {"nemotron": "OPENROUTER_NEMOTRON_MODEL_NAME","gpt-oss": "OPENROUTER_GPT_OSS_MODEL_NAME","lfm": "OPENROUTER_LFM_MODEL_NAME","openrouter-free": "OPENROUTER_FREE_MODEL_NAME",}ERROR_TEXT = re.compile(r"^(?:AI[_ ]GENERATION[_ ]FAILED|AI generation failed|Transcription (?:failed|unavailable)|"r"Translation (?:failed|unavailable)|Error\s*:|Service unavailable)", re.I)def failure(code, message):return {"ok": False, "error": {"code": code, "message": message}}def valid_content(text):return isinstance(text, str) and 0 < len(text.strip()) <= 4000 and not ERROR_TEXT.search(text.strip())def free_only():"""Deployment policy; Groq additionally requires the operator's Free plan."""return os.getenv("INFERENCE_FREE_ONLY", "false").strip().lower() in {"1", "true", "yes"}def _route(choice):prefix, default_url, default_model = ROUTES[choice]key = os.getenv(prefix + "_API_KEY", "").strip()if prefix == "GEMINI":key = key or os.getenv("GOOGLE_API_KEY", "").strip()if not key or key.lower() in {"your-api-key-here", "none", "null"}:return Nonemodel = os.getenv(MODEL_ENV.get(choice, prefix + "_MODEL_NAME"), default_model).strip()base_url = os.getenv(prefix + "_BASE_URL", default_url).strip().rstrip("/")parsed = urllib.parse.urlsplit(base_url)# Only operator-configured endpoints are used. Never put keys in URLs.if parsed.scheme not in {"https", "http"} or not parsed.hostname or parsed.username or parsed.password or parsed.query or parsed.fragment:return Noneif parsed.scheme == "http" and parsed.hostname not in {"localhost", "127.0.0.1", "::1"}:return Noneif not model:return Noneif free_only():groq_free = (choice == "groq" and base_url == "https://api.groq.com/openai/v1"and model in {"qwen/qwen3.8-27b", "openai/gpt-oss-20b", "openai/gpt-oss-120b"})router_free = (choice == "openrouter-free" and base_url == "https://openrouter.ai/api/v1"and model == "openrouter/free")if not (groq_free or router_free):return Nonereturn {"key": key, "url": base_url + "/chat/completions", "model": model, "route": choice}def available_routes():"""Return non-secret configuration metadata, without claiming availability."""return [{"route": name, "model": route["model"], "configured": True, "verified": False}for name in ROUTES if (route := _route(name))]class _NoRedirect(urllib.request.HTTPRedirectHandler):def redirect_request(self, req, fp, code, msg, headers, newurl):# Do not forward Authorization to a redirected host.return Noneclass _OutputFailure(Exception):def __init__(self, code, message):super().__init__(message)self.code = codeself.message = messagedef _post(route, prompt, timeout=10):payload = {"model": route["model"],"messages": [{"role": "user", "content": prompt}],"temperature": 0.3,"max_tokens": 768,}if route["url"].startswith("https://api.groq.com/") and route["model"] == "qwen/qwen3.8-27b":payload["reasoning_effort"] = "none"body = json.dumps(payload).encode("utf-8")request = urllib.request.Request(route["url"], data=body, headers={"Authorization": "Bearer " + route["key"],"Content-Type": "application/json","User-Agent": "PureDialectApps/1.0",}, method="POST")with urllib.request.build_opener(_NoRedirect).open(request, timeout=timeout) as response:raw = response.read(1024 * 1024 + 1)if len(raw) > 1024 * 1024:raise ValueError("Response exceeded limit")payload = json.loads(raw)choice = payload["choices"][0]finish_reason = choice.get("finish_reason")if finish_reason in {"length", "max_tokens"}:raise _OutputFailure("OUTPUT_TRUNCATED", "The model response was cut short. Shorten the text or retry.")if finish_reason != "stop" or choice.get("message", {}).get("tool_calls"):raise _OutputFailure("INVALID_OUTPUT", "The model did not return a complete usable response.")return choice["message"]["content"]def generate_text(prompt, model_choice="auto", *, deadline=None):if not isinstance(prompt, str) or not prompt.strip() or len(prompt) > 24000:return failure("INVALID_REQUEST", "A non-empty, bounded inference prompt is required.")choice = str(model_choice or "auto").strip().lower()if choice != "auto" and choice not in ROUTES:return failure("MODEL_UNAVAILABLE", "The selected model route is not supported.")names = list(ROUTES) if choice == "auto" else [choice]if free_only() and choice == "auto":names = ["groq", "openrouter-free"]configured = [route for name in names if (route := _route(name))]if not configured:return failure("MODEL_UNAVAILABLE", "The selected model route is not configured.")deadline = min(deadline or (time.monotonic() + 30), time.monotonic() + 30)attempts = []result = failure("PROVIDER_UNAVAILABLE", "The selected provider is unavailable. Please retry.")for route in configured[:3]:remaining = deadline - time.monotonic()if remaining <= 0:breaktry:content = _post(route, prompt, timeout=min(10, remaining))if valid_content(content):return {"ok": True, "text": content.strip(), "model": route["model"], "route": route["route"], "attempts": attempts}result = failure("INVALID_OUTPUT", "The model did not return usable content.")except _OutputFailure as exc:result = failure(exc.code, exc.message)except urllib.error.HTTPError as exc:if exc.code == 401:result = failure("PROVIDER_AUTH_FAILED", "The selected provider rejected its credentials.")elif exc.code == 403:result = failure("PROVIDER_ACCESS_DENIED", "The provider denied access. Check its model and project permissions.")elif exc.code == 429:result = failure("PROVIDER_RATE_LIMITED", "The selected provider is rate limited or has no remaining quota.")elif exc.code in {400, 404, 422}:result = failure("PROVIDER_CONFIGURATION_ERROR", "The selected provider rejected the configured model or request.")else:result = failure("PROVIDER_UNAVAILABLE", "The selected provider is unavailable. Please retry.")except (TimeoutError, urllib.error.URLError):result = failure("PROVIDER_UNAVAILABLE", "The selected provider could not be reached. Please retry.")except Exception:result = failure("INVALID_OUTPUT", "The model did not return usable content.")attempts.append({"route": route["route"], "code": result["error"]["code"]})return {**result, "attempts": attempts}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174"""Small, fail-closed provider adapter shared by REST and Gradio.Configured routes are not health claims. Explicit model choices never fall backto another provider; ``auto`` may choose from configured routes."""import jsonimport osimport reimport timeimport urllib.errorimport urllib.parseimport urllib.requestROUTES = {"qwen": ("QWEN", "https://dashscope-intl.aliyuncs.com/compatible-mode/v1", "qwen-plus"),"llama": ("GROQ", "https://api.groq.com/openai/v1", "llama-3.3-70b-versatile"),"deepseek": ("DEEPSEEK", "https://api.deepseek.com", "deepseek-chat"),"gemini": ("GEMINI", "https://generativelanguage.googleapis.com/v1beta/openai", "gemini-2.5-flash"),"nemotron": ("OPENROUTER", "https://openrouter.ai/api/v1", "nvidia/nemotron-3-nano-30b-a3b:free"),"gpt-oss": ("OPENROUTER", "https://openrouter.ai/api/v1", "openai/gpt-oss-20b:free"),"lfm": ("OPENROUTER", "https://openrouter.ai/api/v1", "liquid/lfm-2.5-1.2b-instruct:free"),"openrouter-free": ("OPENROUTER", "https://openrouter.ai/api/v1", "openrouter/free"),"groq": ("GROQ", "https://api.groq.com/openai/v1", "qwen/qwen3.8-27b"),}MODEL_ENV = {"nemotron": "OPENROUTER_NEMOTRON_MODEL_NAME","gpt-oss": "OPENROUTER_GPT_OSS_MODEL_NAME","lfm": "OPENROUTER_LFM_MODEL_NAME","openrouter-free": "OPENROUTER_FREE_MODEL_NAME",}ERROR_TEXT = re.compile(r"^(?:AI[_ ]GENERATION[_ ]FAILED|AI generation failed|Transcription (?:failed|unavailable)|"r"Translation (?:failed|unavailable)|Error\s*:|Service unavailable)", re.I)def failure(code, message):return {"ok": False, "error": {"code": code, "message": message}}def valid_content(text):return isinstance(text, str) and 0 < len(text.strip()) <= 4000 and not ERROR_TEXT.search(text.strip())def free_only():"""Deployment policy; Groq additionally requires the operator's Free plan."""return os.getenv("INFERENCE_FREE_ONLY", "false").strip().lower() in {"1", "true", "yes"}def _route(choice):prefix, default_url, default_model = ROUTES[choice]key = os.getenv(prefix + "_API_KEY", "").strip()if prefix == "GEMINI":key = key or os.getenv("GOOGLE_API_KEY", "").strip()if not key or key.lower() in {"your-api-key-here", "none", "null"}:return Nonemodel = os.getenv(MODEL_ENV.get(choice, prefix + "_MODEL_NAME"), default_model).strip()base_url = os.getenv(prefix + "_BASE_URL", default_url).strip().rstrip("/")parsed = urllib.parse.urlsplit(base_url)# Only operator-configured endpoints are used. Never put keys in URLs.if parsed.scheme not in {"https", "http"} or not parsed.hostname or parsed.username or parsed.password or parsed.query or parsed.fragment:return Noneif parsed.scheme == "http" and parsed.hostname not in {"localhost", "127.0.0.1", "::1"}:return Noneif not model:return Noneif free_only():groq_free = (choice == "groq" and base_url == "https://api.groq.com/openai/v1"and model in {"qwen/qwen3.8-27b", "openai/gpt-oss-20b", "openai/gpt-oss-120b"})router_free = (choice == "openrouter-free" and base_url == "https://openrouter.ai/api/v1"and model == "openrouter/free")if not (groq_free or router_free):return Nonereturn {"key": key, "url": base_url + "/chat/completions", "model": model, "route": choice}def available_routes():"""Return non-secret configuration metadata, without claiming availability."""return [{"route": name, "model": route["model"], "configured": True, "verified": False}for name in ROUTES if (route := _route(name))]class _NoRedirect(urllib.request.HTTPRedirectHandler):def redirect_request(self, req, fp, code, msg, headers, newurl):# Do not forward Authorization to a redirected host.return Noneclass _OutputFailure(Exception):def __init__(self, code, message):super().__init__(message)self.code = codeself.message = messagedef _post(route, prompt, timeout=10):payload = {"model": route["model"],"messages": [{"role": "user", "content": prompt}],"temperature": 0.3,"max_tokens": 768,}if route["url"].startswith("https://api.groq.com/") and route["model"] == "qwen/qwen3.8-27b":payload["reasoning_effort"] = "none"body = json.dumps(payload).encode("utf-8")request = urllib.request.Request(route["url"], data=body, headers={"Authorization": "Bearer " + route["key"],"Content-Type": "application/json","User-Agent": "PureDialectApps/1.0",}, method="POST")with urllib.request.build_opener(_NoRedirect).open(request, timeout=timeout) as response:raw = response.read(1024 * 1024 + 1)if len(raw) > 1024 * 1024:raise ValueError("Response exceeded limit")payload = json.loads(raw)choice = payload["choices"][0]finish_reason = choice.get("finish_reason")if finish_reason in {"length", "max_tokens"}:raise _OutputFailure("OUTPUT_TRUNCATED", "The model response was cut short. Shorten the text or retry.")if finish_reason != "stop" or choice.get("message", {}).get("tool_calls"):raise _OutputFailure("INVALID_OUTPUT", "The model did not return a complete usable response.")return choice["message"]["content"]def generate_text(prompt, model_choice="auto", *, deadline=None):if not isinstance(prompt, str) or not prompt.strip() or len(prompt) > 24000:return failure("INVALID_REQUEST", "A non-empty, bounded inference prompt is required.")choice = str(model_choice or "auto").strip().lower()if choice != "auto" and choice not in ROUTES:return failure("MODEL_UNAVAILABLE", "The selected model route is not supported.")names = list(ROUTES) if choice == "auto" else [choice]if free_only() and choice == "auto":names = ["groq", "openrouter-free"]configured = [route for name in names if (route := _route(name))]if not configured:return failure("MODEL_UNAVAILABLE", "The selected model route is not configured.")deadline = min(deadline or (time.monotonic() + 30), time.monotonic() + 30)attempts = []result = failure("PROVIDER_UNAVAILABLE", "The selected provider is unavailable. Please retry.")for route in configured[:3]:remaining = deadline - time.monotonic()if remaining <= 0:breaktry:content = _post(route, prompt, timeout=min(10, remaining))if valid_content(content):return {"ok": True, "text": content.strip(), "model": route["model"], "route": route["route"], "attempts": attempts}result = failure("INVALID_OUTPUT", "The model did not return usable content.")except _OutputFailure as exc:result = failure(exc.code, exc.message)except urllib.error.HTTPError as exc:if exc.code == 401:result = failure("PROVIDER_AUTH_FAILED", "The selected provider rejected its credentials.")elif exc.code == 403:result = failure("PROVIDER_ACCESS_DENIED", "The provider denied access. Check its model and project permissions.")elif exc.code == 429:result = failure("PROVIDER_RATE_LIMITED", "The selected provider is rate limited or has no remaining quota.")elif exc.code in {400, 404, 422}:result = failure("PROVIDER_CONFIGURATION_ERROR", "The selected provider rejected the configured model or request.")else:result = failure("PROVIDER_UNAVAILABLE", "The selected provider is unavailable. Please retry.")except (TimeoutError, urllib.error.URLError):result = failure("PROVIDER_UNAVAILABLE", "The selected provider could not be reached. Please retry.")except Exception:result = failure("INVALID_OUTPUT", "The model did not return usable content.")attempts.append({"route": route["route"], "code": result["error"]["code"]})return {**result, "attempts": attempts}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394import osimport asyncioimport threadingimport jsonfrom threading import Lockfrom huggingface_hub import HfApi, hf_hub_downloadfrom google import genaifrom .inference_service import generate_text# ... (Keep your existing GeminiManager and HFManager code) ...class GroqManager:def __init__(self, api_key):self.api_key = api_key# We do NOT initialize the Groq client globally here anymore!self.client_available = True if self.api_key else Falseself.model_flash = os.getenv("GROQ_MODEL_NAME", "qwen/qwen3.8-27b")self.last_used_model = "Idle"if self.client_available:print(f"⚡ Groq Manager Connected: Standardized on {self.model_flash}")# 2. Convert to an ASYNC functionasync def generate_fast(self, prompt):if not self.client_available:raise Exception("Groq API Key not found.")# Legacy mission/analysis calls share the same policy, deadline and transport.result = await asyncio.to_thread(generate_text, prompt, "groq")if not result.get("ok"):raise RuntimeError(result.get("error", {}).get("message", "Groq inference unavailable."))self.last_used_model = result["model"]class GroqResponse:def __init__(self, text):self.text = textreturn GroqResponse(result["text"])# 4. Convert this to async as well since it calls generate_fastasync def generate_smart(self, prompt):# Both entry points use the same bounded Groq inference route.return await self.generate_fast(prompt)def get_status_string(self):return f"⚡ {self.last_used_model}"class GeminiManager:def __init__(self, api_key):self.api_key = api_keyself.client = genai.Client(api_key=self.api_key) if self.api_key else Noneself.model_flash = "gemini-2.0-flash"self.last_used_model = "Idle"if self.client:print(f"🧠 Gemini Manager Connected: Standardized on {self.model_flash}")def generate_fast(self, prompt):if not self.client: raise Exception("Google API Key not found.")self.last_used_model = "Gemini 2.0 Flash"return self.client.models.generate_content(model=self.model_flash, contents=prompt)def generate_smart(self, prompt):return self.generate_fast(prompt)def get_status_string(self):return f"⚡ {self.last_used_model}"class HFManager:def __init__(self, config):self.config = configself.api = HfApi(token=self.config.HF_TOKEN)self.lock = Lock()def pull_datasets(self):"""Fail closed: governed datasets are never restored from publication."""print("🔒 Governed dataset startup pull is disabled.")return {"status": "disabled", "downloaded": 0}def seed_initial_data(self):p_path = os.path.join(self.config.PROFILES_DIR, "NSL Lab Trainer.json")if not os.path.exists(p_path):with open(p_path, 'w') as f: json.dump({"lab_name": "NSL", "jargon": {}, "pragmatic_rules": []}, f)def push_update(self, filepath, commit_msg="Update"):del filepath, commit_msgprint("🔒 Working-dataset publication is disabled; no file was uploaded.")return {"status": "disabled", "uploaded": False}def upload_audio_sample(self, audio_path, dialect):del audio_path, dialectprint("🔒 Raw-audio publication is disabled; no file was uploaded.")return None12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394import osimport asyncioimport threadingimport jsonfrom threading import Lockfrom huggingface_hub import HfApi, hf_hub_downloadfrom google import genaifrom .inference_service import generate_text# ... (Keep your existing GeminiManager and HFManager code) ...class GroqManager:def __init__(self, api_key):self.api_key = api_key# We do NOT initialize the Groq client globally here anymore!self.client_available = True if self.api_key else Falseself.model_flash = os.getenv("GROQ_MODEL_NAME", "qwen/qwen3.8-27b")self.last_used_model = "Idle"if self.client_available:print(f"⚡ Groq Manager Connected: Standardized on {self.model_flash}")# 2. Convert to an ASYNC functionasync def generate_fast(self, prompt):if not self.client_available:raise Exception("Groq API Key not found.")# Legacy mission/analysis calls share the same policy, deadline and transport.result = await asyncio.to_thread(generate_text, prompt, "groq")if not result.get("ok"):raise RuntimeError(result.get("error", {}).get("message", "Groq inference unavailable."))self.last_used_model = result["model"]class GroqResponse:def __init__(self, text):self.text = textreturn GroqResponse(result["text"])# 4. Convert this to async as well since it calls generate_fastasync def generate_smart(self, prompt):# Both entry points use the same bounded Groq inference route.return await self.generate_fast(prompt)def get_status_string(self):return f"⚡ {self.last_used_model}"class GeminiManager:def __init__(self, api_key):self.api_key = api_keyself.client = genai.Client(api_key=self.api_key) if self.api_key else Noneself.model_flash = "gemini-2.0-flash"self.last_used_model = "Idle"if self.client:print(f"🧠 Gemini Manager Connected: Standardized on {self.model_flash}")def generate_fast(self, prompt):if not self.client: raise Exception("Google API Key not found.")self.last_used_model = "Gemini 2.0 Flash"return self.client.models.generate_content(model=self.model_flash, contents=prompt)def generate_smart(self, prompt):return self.generate_fast(prompt)def get_status_string(self):return f"⚡ {self.last_used_model}"class HFManager:def __init__(self, config):self.config = configself.api = HfApi(token=self.config.HF_TOKEN)self.lock = Lock()def pull_datasets(self):"""Fail closed: governed datasets are never restored from publication."""print("🔒 Governed dataset startup pull is disabled.")return {"status": "disabled", "downloaded": 0}def seed_initial_data(self):p_path = os.path.join(self.config.PROFILES_DIR, "NSL Lab Trainer.json")if not os.path.exists(p_path):with open(p_path, 'w') as f: json.dump({"lab_name": "NSL", "jargon": {}, "pragmatic_rules": []}, f)def push_update(self, filepath, commit_msg="Update"):del filepath, commit_msgprint("🔒 Working-dataset publication is disabled; no file was uploaded.")return {"status": "disabled", "uploaded": False}def upload_audio_sample(self, audio_path, dialect):del audio_path, dialectprint("🔒 Raw-audio publication is disabled; no file was uploaded.")return None
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192"""Regression checks for Groq transport and the deployed free-only policy."""import asyncioimport astimport jsonimport osfrom pathlib import Pathimport urllib.errorfrom unittest.mock import patch
import pytestfrom src import inference_service as service
FREE_ENV = {"INFERENCE_FREE_ONLY": "true", "GROQ_API_KEY": "synthetic-groq"}
def test_free_auto_uses_groq_even_when_paid_keys_are_present(): env = {**FREE_ENV, "QWEN_API_KEY": "synthetic", "DEEPSEEK_API_KEY": "synthetic", "GOOGLE_API_KEY": "synthetic"} with patch.dict(os.environ, env, clear=True), patch.object(service, "_post", return_value="Hello") as post: result = service.generate_text("Synthetic prompt", "auto") assert result["ok"] and result["route"] == "groq" assert result["model"] == "qwen/qwen3.8-27b" assert post.call_count == 1
@pytest.mark.parametrize("choice", ["qwen", "llama", "deepseek", "gemini", "nemotron", "gpt-oss", "lfm"])def test_free_policy_rejects_paid_and_retired_choices(choice): env = {**FREE_ENV, "QWEN_API_KEY": "synthetic", "OPENROUTER_API_KEY": "synthetic", "DEEPSEEK_API_KEY": "synthetic", "GOOGLE_API_KEY": "synthetic"} with patch.dict(os.environ, env, clear=True), patch.object(service, "_post") as post: result = service.generate_text("Synthetic prompt", choice) assert result["error"]["code"] == "MODEL_UNAVAILABLE" post.assert_not_called()
def test_free_auto_fallback_only_uses_the_free_router(): env = {**FREE_ENV, "OPENROUTER_API_KEY": "synthetic", "QWEN_API_KEY": "synthetic"} with patch.dict(os.environ, env, clear=True), patch.object(service, "_post", side_effect=[TimeoutError(), "Hello"]) as post: result = service.generate_text("Synthetic prompt", "auto") assert result["route"] == "openrouter-free" assert [call.args[0]["route"] for call in post.call_args_list] == ["groq", "openrouter-free"]
@pytest.mark.parametrize("override", [ {"GROQ_MODEL_NAME": "llama-3.3-70b-versatile"}, {"GROQ_BASE_URL": "https://other-provider.example/v1"}, {"OPENROUTER_FREE_MODEL_NAME": "deepseek/deepseek-v3.2"}, {"OPENROUTER_BASE_URL": "https://other-provider.example/v1"},])def test_free_policy_rejects_provider_or_model_substitution(override): env = {**FREE_ENV, "OPENROUTER_API_KEY": "synthetic", **override} choice = "groq" if any(key.startswith("GROQ_") for key in override) else "openrouter-free" with patch.dict(os.environ, env, clear=True), patch.object(service, "_post") as post: assert not service.generate_text("Synthetic prompt", choice)["ok"] post.assert_not_called()
def test_transport_has_application_user_agent_and_uses_instruct_mode(): payload = {"choices": [{"finish_reason": "stop", "message": {"content": "Hello"}}]} with patch.dict(os.environ, FREE_ENV, clear=True), patch.object(service.urllib.request, "build_opener") as opener: opener.return_value.open.return_value.__enter__.return_value.read.return_value = json.dumps(payload).encode() assert service.generate_text("Synthetic prompt", "groq")["ok"] request = opener.return_value.open.call_args.args[0] assert request.get_header("User-agent") == "PureDialectApps/1.0" assert json.loads(request.data)["reasoning_effort"] == "none" assert json.loads(request.data)["model"] == "qwen/qwen3.8-27b"
@pytest.mark.parametrize("status, expected", [(401, "PROVIDER_AUTH_FAILED"), (403, "PROVIDER_ACCESS_DENIED")])def test_credentials_and_permissions_are_distinct_and_redacted(status, expected): error = urllib.error.HTTPError("https://api.groq.com", status, "private provider text", {}, None) with patch.dict(os.environ, FREE_ENV, clear=True), patch.object(service, "_post", side_effect=error): result = service.generate_text("Synthetic prompt", "groq") assert result["error"]["code"] == expected assert "private" not in json.dumps(result) assert "synthetic-groq" not in json.dumps(result)
def test_legacy_manager_uses_the_shared_policy_adapter(): path = Path(__file__).parent / "src" / "managers.py" tree = ast.parse(path.read_text(encoding="utf-8")) manager = next(node for node in tree.body if isinstance(node, ast.ClassDef) and node.name == "GroqManager") calls = [] def generate(prompt, choice): calls.append((prompt, choice)) return {"ok": True, "text": "Hello", "model": "qwen/qwen3.8-27b", "route": "groq"} namespace = {"os": os, "asyncio": asyncio, "generate_text": generate} exec(compile(ast.Module(body=[manager], type_ignores=[]), str(path), "exec"), namespace) owner = namespace["GroqManager"]("synthetic") result = asyncio.run(owner.generate_fast("Synthetic prompt")) assert result.text == "Hello" assert calls == [("Synthetic prompt", "groq")] assert owner.last_used_model == "qwen/qwen3.8-27b"INFERENCE_CONFIG.mdAdded+28
requirements.txt+49-49
src/inference_service.py+174-174
src/managers.py+94-94
test_groq_recovery.pyAdded+92