Theflame47 commited on
Commit
e62d617
·
verified ·
1 Parent(s): 63250d9

Update Deployment_UI_BE.py

Browse files
Files changed (1) hide show
  1. Deployment_UI_BE.py +38 -1
Deployment_UI_BE.py CHANGED
@@ -789,6 +789,17 @@ async def api_middleware_infer(req: Request):
789
  if not internal:
790
  return JSONResponse({"error": "cannot resolve internal port from blob"}, status_code=400)
791
 
 
 
 
 
 
 
 
 
 
 
 
792
  base = f"https://{pid}-{internal}.proxy.runpod.net"
793
  url = f"{base}{route}"
794
  _log_status(f"PROMPT_ENDPOINT {url}")
@@ -799,12 +810,38 @@ async def api_middleware_infer(req: Request):
799
  if not isinstance(prompt, str) or not prompt.strip():
800
  return JSONResponse({"error": "Missing 'prompt' in request body."}, 400)
801
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
802
  # HF text-classification shim
803
  img = (_get_container_spec().get("imageUri","")).lower()
804
  if "huggingface-pytorch-inference" in img and isinstance(payload.get("prompt"), str):
805
  payload = {"instances": [payload["prompt"]]}
806
 
807
- # Send immediately; no readiness checks or polling gates
808
  bodies = [payload, {"prompt": prompt}, {"text": prompt}, {"inputs": prompt}, {"input": prompt}]
809
  for body in bodies:
810
  try:
 
789
  if not internal:
790
  return JSONResponse({"error": "cannot resolve internal port from blob"}, status_code=400)
791
 
792
+ # ---------------- NEW: routing override for HF_TASK=text-to-image ----------------
793
+ env = (cspec.get("env") or [])
794
+ kv = {e.get("name"): e.get("value") for e in env if isinstance(e, dict) and e.get("name")}
795
+ hf_task = (kv.get("HF_TASK") or "").strip().lower()
796
+ model_id = (kv.get("MODEL_ID") or kv.get("HF_MODEL_ID") or "").strip()
797
+
798
+ if hf_task == "text-to-image" and model_id:
799
+ route = f"/predictions/{model_id}"
800
+ _INST["predictRoute"] = route
801
+ # -------------------------------------------------------------------------------
802
+
803
  base = f"https://{pid}-{internal}.proxy.runpod.net"
804
  url = f"{base}{route}"
805
  _log_status(f"PROMPT_ENDPOINT {url}")
 
810
  if not isinstance(prompt, str) or not prompt.strip():
811
  return JSONResponse({"error": "Missing 'prompt' in request body."}, 400)
812
 
813
+ # ---------------- NEW: canonical text-to-image payload ----------------
814
+ if hf_task == "text-to-image":
815
+ body = {
816
+ "inputs": prompt,
817
+ "parameters": {
818
+ "num_inference_steps": 30,
819
+ "guidance_scale": 7.5,
820
+ "width": 1024,
821
+ "height": 1024
822
+ }
823
+ }
824
+ try:
825
+ rp = requests.post(url, json=body, timeout=120)
826
+ _log_status(f"PREDICT_RESP code={rp.status_code} len={len(rp.text)}")
827
+ if rp.ok:
828
+ ct = (rp.headers.get("content-type") or "").lower()
829
+ data = _as_json(rp) if "application/json" in ct else {"_raw": rp.text}
830
+ if isinstance(data, dict) and "image_b64" in data:
831
+ return JSONResponse({"image_b64": data["image_b64"], "timings": data.get("timings")}, rp.status_code)
832
+ return JSONResponse(data, rp.status_code)
833
+ return JSONResponse({"error": rp.text[:400]}, status_code=rp.status_code)
834
+ except Exception as e:
835
+ _log_status(f"PREDICT_ERR {e}")
836
+ return JSONResponse({"error": f"inference request failed: {e}"}, status_code=502)
837
+ # ---------------------------------------------------------------------
838
+
839
  # HF text-classification shim
840
  img = (_get_container_spec().get("imageUri","")).lower()
841
  if "huggingface-pytorch-inference" in img and isinstance(payload.get("prompt"), str):
842
  payload = {"instances": [payload["prompt"]]}
843
 
844
+ # Non-image fallback (unchanged)
845
  bodies = [payload, {"prompt": prompt}, {"text": prompt}, {"inputs": prompt}, {"input": prompt}]
846
  for body in bodies:
847
  try: