Spaces:
Sleeping
Sleeping
try to fix got5-mini
Browse files33 unmodified lines343536373839122 unmodified lines16216316416516616731 unmodified lines1992002012022032042052062072082092102112122132142152 unmodified lines2182192202212222237 unmodified lines23123223323423523623723823924033 unmodified linesUSE_OPENAI = os.getenv("USE_OPENAI", "").lower() == "true"PORT = int(os.getenv("PORT", "7860"))gemini_client = genai.Client(api_key=API_KEY) if API_KEY else Nonegpt_client = OpenAI(api_key=OPENAI_API_KEY) if (OPENAI_API_KEY and OpenAI and USE_OPENAI) else None122 unmodified lines{"role": "user", "content": _build_openai_content(contents, for_chat=False)},]def _extract_chat_content(resp: Any) -> str:content = resp.choices[0].message.content31 unmodified linesif not gpt_client:raise RuntimeError("GPT client is not configured")messages = _build_chat_messages(system, contents)try:resp = gpt_client.chat.completions.create(model=model, messages=messages)return _extract_chat_content(resp)except Exception as err:message = str(err)if "only supported in v1/responses" not in message:raiseresp = gpt_client.responses.create(model=model,input=_build_responses_input(system, contents),)return _extract_responses_content(resp)2 unmodified linesif not gemini_client:raise RuntimeError("Gemini client is not configured")limiter.acquire()resp = gemini_client.models.generate_content(model=MODEL,config=types.GenerateContentConfig(system_instruction=system),7 unmodified linesstoryboard_limiter.acquire()if gpt_client:target_model = OPENAI_SMALL_MODELreturn _invoke_gpt_model(target_model, system, contents)if not gemini_client:raise RuntimeError("Gemini client is not configured")fallback_model = GEMINI_SMALL_MODEL or MODELresp = gemini_client.models.generate_content(model=fallback_model,config=types.GenerateContentConfig(system_instruction=system),33 unmodified lines3435363738394041424344454647484950515253545556575859606162636465122 unmodified lines18818919019119219319419519619719819920020120220320420520620720831 unmodified lines2402412422432442452462472482492502512522532542552562572582592602612 unmodified lines2642652662672682692707 unmodified lines27827928028128228328428528628728828933 unmodified linesUSE_OPENAI = os.getenv("USE_OPENAI", "").lower() == "true"PORT = int(os.getenv("PORT", "7860"))_OPENAI_RESPONSES_MODELS_ENV = os.getenv("OPENAI_RESPONSES_MODELS", "")RESPONSES_API_MODEL_NAMES = {"gpt-5-mini"}if _OPENAI_RESPONSES_MODELS_ENV:RESPONSES_API_MODEL_NAMES.update(model.strip().lower()for model in _OPENAI_RESPONSES_MODELS_ENV.split(",")if model.strip())_OPENAI_RESPONSES_PREFIXES_ENV = os.getenv("OPENAI_RESPONSES_PREFIXES", "")_RESPONSES_API_MODEL_PREFIXES = ["gpt-5"]if _OPENAI_RESPONSES_PREFIXES_ENV:_RESPONSES_API_MODEL_PREFIXES.extend(prefix.strip().lower()for prefix in _OPENAI_RESPONSES_PREFIXES_ENV.split(",")if prefix.strip())RESPONSES_API_MODEL_PREFIXES = tuple(_RESPONSES_API_MODEL_PREFIXES)RESPONSES_API_ERROR_HINTS = ("only supported in v1/responses","use the responses api","use the responses endpoint","please call the responses api","please use the responses endpoint",)gemini_client = genai.Client(api_key=API_KEY) if API_KEY else Nonegpt_client = OpenAI(api_key=OPENAI_API_KEY) if (OPENAI_API_KEY and OpenAI and USE_OPENAI) else None122 unmodified lines{"role": "user", "content": _build_openai_content(contents, for_chat=False)},]def _requires_responses_api(model: str) -> bool:lowered = (model or "").lower()if not lowered:return Falseif lowered in RESPONSES_API_MODEL_NAMES:return Truereturn any(prefix and lowered.startswith(prefix)for prefix in RESPONSES_API_MODEL_PREFIXES)def _should_use_responses_fallback(err: Exception) -> bool:message = str(err).lower()return any(hint in message for hint in RESPONSES_API_ERROR_HINTS)def _extract_chat_content(resp: Any) -> str:content = resp.choices[0].message.content31 unmodified linesif not gpt_client:raise RuntimeError("GPT client is not configured")messages = _build_chat_messages(system, contents)print(f"[Storyboard] OpenAI request model={model}", file=sys.stderr)responses_input: Optional[List[Dict[str, Any]]] = Noneif _requires_responses_api(model):responses_input = _build_responses_input(system, contents)resp = gpt_client.responses.create(model=model, input=responses_input)return _extract_responses_content(resp)try:resp = gpt_client.chat.completions.create(model=model, messages=messages)return _extract_chat_content(resp)except Exception as err:if not _should_use_responses_fallback(err):raiseif responses_input is None:responses_input = _build_responses_input(system, contents)print(f"[Storyboard] OpenAI responses fallback model={model}", file=sys.stderr)resp = gpt_client.responses.create(model=model, input=responses_input)return _extract_responses_content(resp)2 unmodified linesif not gemini_client:raise RuntimeError("Gemini client is not configured")limiter.acquire()print(f"[Storyboard] Gemini request model={MODEL}", file=sys.stderr)resp = gemini_client.models.generate_content(model=MODEL,config=types.GenerateContentConfig(system_instruction=system),7 unmodified linesstoryboard_limiter.acquire()if gpt_client:target_model = OPENAI_SMALL_MODELprint(f"[Storyboard] Using OpenAI small model={target_model}", file=sys.stderr)return _invoke_gpt_model(target_model, system, contents)if not gemini_client:raise RuntimeError("Gemini client is not configured")fallback_model = GEMINI_SMALL_MODEL or MODELprint(f"[Storyboard] OpenAI disabled; using Gemini fallback model={fallback_model}", file=sys.stderr)resp = gemini_client.models.generate_content(model=fallback_model,config=types.GenerateContentConfig(system_instruction=system),
app/main.py+55-6