Deploy free text provider fallback and zero-budget source research
Browse files
phase-1-core-infra/backend/src/services/ai_router.py
CHANGED
|
@@ -115,7 +115,17 @@ class TextProvider:
|
|
| 115 |
# Cloudflare connections from hosted Spaces can take longer than the
|
| 116 |
# old five-second preflight window. Still respect the request ceiling.
|
| 117 |
health_timeout = 20 if self.name == 'cloudflare' else 5
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
result['http_status'] = response.status_code
|
| 120 |
if not response.is_success:
|
| 121 |
result["reason"] = self.classifyError(response).kind
|
|
|
|
| 115 |
# Cloudflare connections from hosted Spaces can take longer than the
|
| 116 |
# old five-second preflight window. Still respect the request ceiling.
|
| 117 |
health_timeout = 20 if self.name == 'cloudflare' else 5
|
| 118 |
+
request_timeout = min(health_timeout, settings.ai_request_timeout_ms / 1000)
|
| 119 |
+
try:
|
| 120 |
+
response = httpx.get(url, headers=headers, params=params, timeout=request_timeout)
|
| 121 |
+
except (httpx.ConnectError, httpx.ConnectTimeout):
|
| 122 |
+
if self.name != 'cloudflare':
|
| 123 |
+
raise
|
| 124 |
+
# Some hosted networks cannot route the preferred IPv6 address.
|
| 125 |
+
# A read-only IPv4 check is safe to repeat; TLS verification stays on.
|
| 126 |
+
with httpx.Client(transport=httpx.HTTPTransport(local_address='0.0.0.0')) as client:
|
| 127 |
+
response = client.get(url,headers=headers,params=params,timeout=request_timeout)
|
| 128 |
+
result['ipv4_required'] = True
|
| 129 |
result['http_status'] = response.status_code
|
| 130 |
if not response.is_success:
|
| 131 |
result["reason"] = self.classifyError(response).kind
|
phase-1-core-infra/backend/src/services/image_generation.py
CHANGED
|
@@ -91,7 +91,8 @@ def generate_cloudflare_image(prompt, usage=None):
|
|
| 91 |
model = '@cf/black-forest-labs/flux-1-schnell'
|
| 92 |
provider = TextProvider('cloudflare', model)
|
| 93 |
request_id = (usage or {}).get('request_id') or str(uuid.uuid4())
|
| 94 |
-
|
|
|
|
| 95 |
raise HTTPException(503, 'Cloudflare free image model is unavailable or the token lacks Workers AI access. No paid fallback was used.')
|
| 96 |
with controls.slot():
|
| 97 |
try:
|
|
@@ -100,10 +101,13 @@ def generate_cloudflare_image(prompt, usage=None):
|
|
| 100 |
raise HTTPException(503, 'Image usage controls are unavailable. No image request was sent.') from None
|
| 101 |
started = time.monotonic()
|
| 102 |
try:
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
| 107 |
if not response.is_success:
|
| 108 |
raise HTTPException(503, {
|
| 109 |
401:'Cloudflare rejected the image API token.',
|
|
|
|
| 91 |
model = '@cf/black-forest-labs/flux-1-schnell'
|
| 92 |
provider = TextProvider('cloudflare', model)
|
| 93 |
request_id = (usage or {}).get('request_id') or str(uuid.uuid4())
|
| 94 |
+
health = provider.healthCheck()
|
| 95 |
+
if not health['available']:
|
| 96 |
raise HTTPException(503, 'Cloudflare free image model is unavailable or the token lacks Workers AI access. No paid fallback was used.')
|
| 97 |
with controls.slot():
|
| 98 |
try:
|
|
|
|
| 101 |
raise HTTPException(503, 'Image usage controls are unavailable. No image request was sent.') from None
|
| 102 |
started = time.monotonic()
|
| 103 |
try:
|
| 104 |
+
url=f'https://api.cloudflare.com/client/v4/accounts/{settings.cloudflare_account_id}/ai/run/{model}'
|
| 105 |
+
args={'headers':{'Authorization':'Bearer '+settings.cloudflare_api_token.strip()},'json':{'prompt':prompt[:2048],'steps':4},'timeout':120}
|
| 106 |
+
if health.get('ipv4_required'):
|
| 107 |
+
with httpx.Client(transport=httpx.HTTPTransport(local_address='0.0.0.0')) as client:
|
| 108 |
+
response=client.post(url,**args)
|
| 109 |
+
else:
|
| 110 |
+
response=httpx.post(url,**args)
|
| 111 |
if not response.is_success:
|
| 112 |
raise HTTPException(503, {
|
| 113 |
401:'Cloudflare rejected the image API token.',
|
scripts/start-space-backend.py
CHANGED
|
@@ -22,7 +22,7 @@ try:
|
|
| 22 |
import json
|
| 23 |
if cloudflare_image_ready():
|
| 24 |
image_health = TextProvider('cloudflare', '@cf/black-forest-labs/flux-1-schnell').healthCheck()
|
| 25 |
-
print(json.dumps({'event':'image.provider_health','provider':'cloudflare','available':image_health['available'],'reason':image_health['reason'],'http_status':image_health.get('http_status'),'error_codes':image_health.get('error_codes',[]),'error_kind':image_health.get('error_kind')}), flush=True)
|
| 26 |
if image_health['reason'] == 'authentication':
|
| 27 |
import httpx
|
| 28 |
from src.config import settings
|
|
|
|
| 22 |
import json
|
| 23 |
if cloudflare_image_ready():
|
| 24 |
image_health = TextProvider('cloudflare', '@cf/black-forest-labs/flux-1-schnell').healthCheck()
|
| 25 |
+
print(json.dumps({'event':'image.provider_health','provider':'cloudflare','available':image_health['available'],'reason':image_health['reason'],'http_status':image_health.get('http_status'),'error_codes':image_health.get('error_codes',[]),'error_kind':image_health.get('error_kind'),'ipv4_required':image_health.get('ipv4_required',False)}), flush=True)
|
| 26 |
if image_health['reason'] == 'authentication':
|
| 27 |
import httpx
|
| 28 |
from src.config import settings
|