Maia Pelletier commited on
Commit
85532f2
·
1 Parent(s): 9ed3f59

add down message

Browse files
Files changed (1) hide show
  1. app.py +22 -0
app.py CHANGED
@@ -100,6 +100,11 @@ class RAGChatbot:
100
  "meta-llama/Llama-3.1-8B-Instruct",
101
  ]
102
 
 
 
 
 
 
103
  def __init__(
104
  self,
105
  model_name: str = None,
@@ -155,6 +160,15 @@ class RAGChatbot:
155
 
156
  self.chat_history = []
157
 
 
 
 
 
 
 
 
 
 
158
  def _generate_with_chat(self, user_content: str, max_new_tokens: int = 512) -> str:
159
  """Call the Inference API using chat_completion; try fallback models if current is not supported."""
160
  last_error = None
@@ -302,6 +316,10 @@ Answer:"""
302
  raise ValueError("Empty response from model")
303
  except Exception as api_error:
304
  print(f"[DEBUG] RAG generation failed: {type(api_error).__name__}: {api_error}")
 
 
 
 
305
  err_str = str(api_error).lower()
306
  if "model_not_supported" in err_str or "not supported by any provider" in err_str:
307
  return (
@@ -341,6 +359,10 @@ Answer:"""
341
  return "I couldn't generate a response. Please try again."
342
  except Exception as e:
343
  print(f"Error generating response: {e}")
 
 
 
 
344
  return f"I encountered an error while generating a response: {str(e)}. Please check your HF_TOKEN configuration."
345
 
346
  def chat(self, message: str, history):
 
100
  "meta-llama/Llama-3.1-8B-Instruct",
101
  ]
102
 
103
+ # Shown to users when the HF account behind HF_TOKEN has run out of Inference Providers credits (HTTP 402)
104
+ OUT_OF_CREDITS_MESSAGE = (
105
+ "The Organic Certification Assistant is temporarily unavailable. Please try again later."
106
+ )
107
+
108
  def __init__(
109
  self,
110
  model_name: str = None,
 
160
 
161
  self.chat_history = []
162
 
163
+ @staticmethod
164
+ def _is_out_of_credits(error: Exception) -> bool:
165
+ """Return True if the error is a 402 Payment Required (no Inference Providers credits left)."""
166
+ status = getattr(getattr(error, "response", None), "status_code", None)
167
+ if status == 402:
168
+ return True
169
+ err_str = str(error).lower()
170
+ return "402 payment required" in err_str or "no remaining credits" in err_str
171
+
172
  def _generate_with_chat(self, user_content: str, max_new_tokens: int = 512) -> str:
173
  """Call the Inference API using chat_completion; try fallback models if current is not supported."""
174
  last_error = None
 
316
  raise ValueError("Empty response from model")
317
  except Exception as api_error:
318
  print(f"[DEBUG] RAG generation failed: {type(api_error).__name__}: {api_error}")
319
+ if self._is_out_of_credits(api_error):
320
+ print("[DEBUG] Inference Providers credits exhausted for the account behind HF_TOKEN. "
321
+ "Add credits at https://huggingface.co/settings/billing")
322
+ return self.OUT_OF_CREDITS_MESSAGE
323
  err_str = str(api_error).lower()
324
  if "model_not_supported" in err_str or "not supported by any provider" in err_str:
325
  return (
 
359
  return "I couldn't generate a response. Please try again."
360
  except Exception as e:
361
  print(f"Error generating response: {e}")
362
+ if self._is_out_of_credits(e):
363
+ print("[DEBUG] Inference Providers credits exhausted for the account behind HF_TOKEN. "
364
+ "Add credits at https://huggingface.co/settings/billing")
365
+ return self.OUT_OF_CREDITS_MESSAGE
366
  return f"I encountered an error while generating a response: {str(e)}. Please check your HF_TOKEN configuration."
367
 
368
  def chat(self, message: str, history):