Spaces:
Paused
Paused
Sync card and scripts from the monorepo
Browse files- app.py +8 -2
- hf_client.py +23 -1
app.py
CHANGED
|
@@ -52,6 +52,7 @@ client = make_client(MODEL_ID)
|
|
| 52 |
# Utility functions
|
| 53 |
# ---------------------------------------------------------------------------
|
| 54 |
|
|
|
|
| 55 |
def detect_language(code: str) -> str:
|
| 56 |
"""Attempt to detect the programming language."""
|
| 57 |
try:
|
|
@@ -87,6 +88,7 @@ def format_code_html(code: str, language: str) -> str:
|
|
| 87 |
# Main explanation function
|
| 88 |
# ---------------------------------------------------------------------------
|
| 89 |
|
|
|
|
| 90 |
def explain_code(code: str, language: str, level: str) -> tuple[str, str]:
|
| 91 |
"""Generate an explanation for the provided code."""
|
| 92 |
if not code.strip():
|
|
@@ -98,7 +100,9 @@ def explain_code(code: str, language: str, level: str) -> tuple[str, str]:
|
|
| 98 |
detected_lang = detect_language(code)
|
| 99 |
|
| 100 |
# Build prompt
|
| 101 |
-
level_instruction = EXPLANATION_LEVELS.get(
|
|
|
|
|
|
|
| 102 |
lexer_hint = detected_lang.split()[0].lower() if detected_lang.strip() else ""
|
| 103 |
|
| 104 |
system_prompt = f"You are an expert programming tutor. {level_instruction}"
|
|
@@ -138,7 +142,9 @@ Keep your explanation clear, accurate, and educational."""
|
|
| 138 |
explanation = completion.choices[0].message.content.strip()
|
| 139 |
|
| 140 |
# Add language badge
|
| 141 |
-
explanation =
|
|
|
|
|
|
|
| 142 |
|
| 143 |
# Format the code with syntax highlighting
|
| 144 |
formatted_code = format_code_html(code, detected_lang)
|
|
|
|
| 52 |
# Utility functions
|
| 53 |
# ---------------------------------------------------------------------------
|
| 54 |
|
| 55 |
+
|
| 56 |
def detect_language(code: str) -> str:
|
| 57 |
"""Attempt to detect the programming language."""
|
| 58 |
try:
|
|
|
|
| 88 |
# Main explanation function
|
| 89 |
# ---------------------------------------------------------------------------
|
| 90 |
|
| 91 |
+
|
| 92 |
def explain_code(code: str, language: str, level: str) -> tuple[str, str]:
|
| 93 |
"""Generate an explanation for the provided code."""
|
| 94 |
if not code.strip():
|
|
|
|
| 100 |
detected_lang = detect_language(code)
|
| 101 |
|
| 102 |
# Build prompt
|
| 103 |
+
level_instruction = EXPLANATION_LEVELS.get(
|
| 104 |
+
level, EXPLANATION_LEVELS["Intermediate"]
|
| 105 |
+
)
|
| 106 |
lexer_hint = detected_lang.split()[0].lower() if detected_lang.strip() else ""
|
| 107 |
|
| 108 |
system_prompt = f"You are an expert programming tutor. {level_instruction}"
|
|
|
|
| 142 |
explanation = completion.choices[0].message.content.strip()
|
| 143 |
|
| 144 |
# Add language badge
|
| 145 |
+
explanation = (
|
| 146 |
+
f"**Detected Language:** `{detected_lang}`\n\n---\n\n{explanation}"
|
| 147 |
+
)
|
| 148 |
|
| 149 |
# Format the code with syntax highlighting
|
| 150 |
formatted_code = format_code_html(code, detected_lang)
|
hf_client.py
CHANGED
|
@@ -42,6 +42,17 @@ _TRANSIENT_MARKERS = (
|
|
| 42 |
"timed out",
|
| 43 |
)
|
| 44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
class InferenceError(RuntimeError):
|
| 47 |
"""Raised when an inference call fails after exhausting retries."""
|
|
@@ -67,12 +78,23 @@ def make_client(model: Optional[str] = None, timeout: int = DEFAULT_TIMEOUT):
|
|
| 67 |
|
| 68 |
def _is_transient(exc: Exception) -> bool:
|
| 69 |
msg = str(exc).lower()
|
|
|
|
|
|
|
| 70 |
return any(marker in msg for marker in _TRANSIENT_MARKERS)
|
| 71 |
|
| 72 |
|
| 73 |
def friendly_error(exc: Exception) -> str:
|
| 74 |
"""Map a raw inference exception to an actionable, user-facing message."""
|
| 75 |
msg = str(exc).lower()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
if any(k in msg for k in ("rate limit", "429", "too many requests", "quota")):
|
| 77 |
return (
|
| 78 |
"The model is rate-limited right now. Wait a moment and try again, or "
|
|
@@ -80,7 +102,7 @@ def friendly_error(exc: Exception) -> str:
|
|
| 80 |
)
|
| 81 |
if any(k in msg for k in ("currently loading", "loading", "503", "starting")):
|
| 82 |
return "The model is warming up (cold start). Please try again in ~20 seconds."
|
| 83 |
-
if any(k in msg for k in ("401", "unauthorized", "
|
| 84 |
return (
|
| 85 |
"Inference was rejected for authentication. Set a valid HF_TOKEN secret "
|
| 86 |
"in the Space settings."
|
|
|
|
| 42 |
"timed out",
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# Billing failures are permanent until the account changes -- retrying just
|
| 46 |
+
# delays the error. These win over _TRANSIENT_MARKERS, which would otherwise
|
| 47 |
+
# match a credits message that happens to mention "quota".
|
| 48 |
+
_PERMANENT_MARKERS = (
|
| 49 |
+
"402",
|
| 50 |
+
"payment required",
|
| 51 |
+
"included credits",
|
| 52 |
+
"subscribe to pro",
|
| 53 |
+
"exceeded your monthly",
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
|
| 57 |
class InferenceError(RuntimeError):
|
| 58 |
"""Raised when an inference call fails after exhausting retries."""
|
|
|
|
| 78 |
|
| 79 |
def _is_transient(exc: Exception) -> bool:
|
| 80 |
msg = str(exc).lower()
|
| 81 |
+
if any(marker in msg for marker in _PERMANENT_MARKERS):
|
| 82 |
+
return False
|
| 83 |
return any(marker in msg for marker in _TRANSIENT_MARKERS)
|
| 84 |
|
| 85 |
|
| 86 |
def friendly_error(exc: Exception) -> str:
|
| 87 |
"""Map a raw inference exception to an actionable, user-facing message."""
|
| 88 |
msg = str(exc).lower()
|
| 89 |
+
# Checked before rate limiting: a credits message often mentions "quota"
|
| 90 |
+
# too, and before auth: the token is valid, so telling the user to replace
|
| 91 |
+
# it sends them to fix something that is not broken.
|
| 92 |
+
if any(k in msg for k in _PERMANENT_MARKERS):
|
| 93 |
+
return (
|
| 94 |
+
"This account is out of HuggingFace Inference credits, so the request "
|
| 95 |
+
"was declined. The HF_TOKEN is valid -- the monthly free allowance is "
|
| 96 |
+
"used up. Wait for the monthly reset or subscribe to PRO for more."
|
| 97 |
+
)
|
| 98 |
if any(k in msg for k in ("rate limit", "429", "too many requests", "quota")):
|
| 99 |
return (
|
| 100 |
"The model is rate-limited right now. Wait a moment and try again, or "
|
|
|
|
| 102 |
)
|
| 103 |
if any(k in msg for k in ("currently loading", "loading", "503", "starting")):
|
| 104 |
return "The model is warming up (cold start). Please try again in ~20 seconds."
|
| 105 |
+
if any(k in msg for k in ("401", "unauthorized", "authentication")):
|
| 106 |
return (
|
| 107 |
"Inference was rejected for authentication. Set a valid HF_TOKEN secret "
|
| 108 |
"in the Space settings."
|