Spaces:
Runtime error
Runtime error
Update backend/server.py
Browse files368 unmodified lines3693703713723733743753763773783793803813823833843853863873883893903914 unmodified lines396397398399400401402403404405406407569 unmodified lines9779789799809819822 unmodified lines98598698798898999099184 unmodified lines1076107710781079108010811082108323 unmodified lines1107110811091110111111121113368 unmodified linesdef _build_ai_prompt_packet_custom(target_lang: str, original_text_full: str, prompt_editable: str, is_retry: bool = False) -> tuple[str, List[str]]:lang = _normalize_lang(target_lang)style_prompt = (prompt_editable or "").strip()if not style_prompt:style_prompt = (getattr(core, "ai_prompt_user_default",lambda _l, _m=None: "")(lang)or "").strip()base = (getattr(core, "AI_PROMPT_SYSTEM_BASE", "") or "").strip()style = ((getattr(core, "AI_LANG_STYLE", {}) or {}).get(lang)or (getattr(core, "AI_LANG_STYLE", {}) or {}).get("default")or "").strip()contract_parts: List[str] = ["Follow the user's StylePrompt as hard constraints (unless it would break marker rules).","Output ONLY the translated text (no JSON, no markdown, no extra commentary).","Markers: Keep every paragraph marker like <<TP_P0>> unchanged and in order. Do not remove, rename, or add markers.","For each marker, output the marker followed by that paragraph's translated text.",4 unmodified lines)system_text = "\n\n".join([p for p in [base, style, "\n".join(contract_parts)] if p])user_parts: List[str] = []if style_prompt:user_parts.append("StylePrompt:\n" + style_prompt)user_parts.append("Input:\n" + str(original_text_full or ""))return system_text, user_partsdef ai_translate_text(original_text_full: str, target_lang: str, ai: AiConfig, is_retry: bool = False) -> dict:569 unmodified linesapi_key = str(payload.get('api_key') or '').strip() or (os.getenv('AI_API_KEY') or '').strip()lang = _normalize_lang(str(payload.get('lang') or 'en'))if not api_key:return {'ok': False,2 unmodified lines'default_model': '','models': [],'lang': lang,'prompt_editable_default': (getattr(core, 'ai_prompt_user_default', lambda _l: '')(lang) or '').strip(),}provider = core._canonical_provider(str(payload.get('provider') or 'auto'))84 unmodified linesif models and resolved_model not in models:resolved_model = models[0]prompt_default = (getattr(core, 'ai_prompt_user_default',lambda _l: '')(lang) or '').strip()return {'ok': True,23 unmodified linesreturn {'ok': True,'lang': l,'prompt_editable_default': (getattr(core, 'ai_prompt_user_default', lambda _l: '')(l) or '').strip(),'lang_style': style,'system_base': base,'contract': contract,368 unmodified lines3693703713723733743753763773783793803813823833843853864 unmodified lines391392393394395396397398399400569 unmodified lines9709719729739749759762 unmodified lines97998098198298398498584 unmodified lines107010711072107310741075107623 unmodified lines1100110111021103110411051106368 unmodified linesdef _build_ai_prompt_packet_custom(target_lang: str, original_text_full: str, prompt_editable: str, is_retry: bool = False) -> tuple[str, List[str]]:lang = _normalize_lang(target_lang)base = (getattr(core, "AI_PROMPT_SYSTEM_BASE", "") or "").strip()style = (prompt_editable or "").strip()if not style:style = ((getattr(core, "AI_LANG_STYLE", {}) or {}).get(lang)or (getattr(core, "AI_LANG_STYLE", {}) or {}).get("default")or "").strip()contract_parts: List[str] = ["Output ONLY the translated text (no JSON, no markdown, no extra commentary).","Markers: Keep every paragraph marker like <<TP_P0>> unchanged and in order. Do not remove, rename, or add markers.","For each marker, output the marker followed by that paragraph's translated text.",4 unmodified lines)system_text = "\n\n".join([p for p in [base, style, "\n".join(contract_parts)] if p])user_parts: List[str] = ["Input:\n" + str(original_text_full or "")]return system_text, user_partsdef ai_translate_text(original_text_full: str, target_lang: str, ai: AiConfig, is_retry: bool = False) -> dict:569 unmodified linesapi_key = str(payload.get('api_key') or '').strip() or (os.getenv('AI_API_KEY') or '').strip()lang = _normalize_lang(str(payload.get('lang') or 'en'))style_default = ((getattr(core, 'AI_LANG_STYLE', {}) or {}).get(lang) or (getattr(core, 'AI_LANG_STYLE', {}) or {}).get('default') or '').strip()if not api_key:return {'ok': False,2 unmodified lines'default_model': '','models': [],'lang': lang,'prompt_editable_default': style_default,}provider = core._canonical_provider(str(payload.get('provider') or 'auto'))84 unmodified linesif models and resolved_model not in models:resolved_model = models[0]prompt_default = style_defaultreturn {'ok': True,23 unmodified linesreturn {'ok': True,'lang': l,'prompt_editable_default': style,'lang_style': style,'system_base': base,'contract': contract,
backend/server.py+15-22