Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -166,6 +166,14 @@ def _api_error(message: str, error_type: str = "invalid_request_error", status:
|
|
| 166 |
)
|
| 167 |
|
| 168 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
def _with_request_context(request: fastapi.Request, text: str, candidates: int, fallback: bool) -> dict:
|
| 170 |
if GradioRequest is not None:
|
| 171 |
gr_request = GradioRequest(request=request, session_hash="api")
|
|
@@ -265,7 +273,7 @@ def v1_convert(body: ConvertRequest, request: fastapi.Request) -> dict | JSONRes
|
|
| 265 |
try:
|
| 266 |
result = _with_request_context(request, body.text.strip(), int(body.candidates), bool(body.structured_fallback))
|
| 267 |
except Exception as exc:
|
| 268 |
-
return
|
| 269 |
payload = _result_payload(result, body.text.strip(), started)
|
| 270 |
payload.update({"id": f"conv-{uuid.uuid4().hex[:24]}", "object": "pasita.conversion", "created": int(time.time()), "model": "pasita-v1"})
|
| 271 |
return payload
|
|
@@ -305,7 +313,7 @@ def chat_completions(body: ChatCompletionRequest, request: fastapi.Request) -> d
|
|
| 305 |
try:
|
| 306 |
result = _with_request_context(request, text.strip(), int(body.candidates), bool(body.structured_fallback))
|
| 307 |
except Exception as exc:
|
| 308 |
-
return
|
| 309 |
|
| 310 |
payload = _result_payload(result, text.strip(), started)
|
| 311 |
completion_id = f"chatcmpl-{uuid.uuid4().hex[:24]}"
|
|
@@ -393,7 +401,8 @@ FOOTER = f"""
|
|
| 393 |
<span>PASITA v1 converts plain text to Markdown, in Spanish and English. It works best on medium and long documents.</span>
|
| 394 |
<span>
|
| 395 |
<a href="{MODEL_CARD}" target="_blank" rel="noopener">MODEL</a> /
|
| 396 |
-
<a href="
|
|
|
|
| 397 |
RUNS ON ZEROGPU
|
| 398 |
</span>
|
| 399 |
</footer>
|
|
|
|
| 166 |
)
|
| 167 |
|
| 168 |
|
| 169 |
+
def _exception_to_error(exc: Exception) -> JSONResponse:
|
| 170 |
+
message = f"{type(exc).__name__}: {exc}"
|
| 171 |
+
text = str(exc).lower()
|
| 172 |
+
if "zerogpu" in text and ("quota" in text or "limit" in text or "quota" in message.lower()):
|
| 173 |
+
return _api_error(str(exc), "rate_limit_error", status=429, code="gpu_quota_exceeded")
|
| 174 |
+
return _api_error(message, "server_error", status=500)
|
| 175 |
+
|
| 176 |
+
|
| 177 |
def _with_request_context(request: fastapi.Request, text: str, candidates: int, fallback: bool) -> dict:
|
| 178 |
if GradioRequest is not None:
|
| 179 |
gr_request = GradioRequest(request=request, session_hash="api")
|
|
|
|
| 273 |
try:
|
| 274 |
result = _with_request_context(request, body.text.strip(), int(body.candidates), bool(body.structured_fallback))
|
| 275 |
except Exception as exc:
|
| 276 |
+
return _exception_to_error(exc)
|
| 277 |
payload = _result_payload(result, body.text.strip(), started)
|
| 278 |
payload.update({"id": f"conv-{uuid.uuid4().hex[:24]}", "object": "pasita.conversion", "created": int(time.time()), "model": "pasita-v1"})
|
| 279 |
return payload
|
|
|
|
| 313 |
try:
|
| 314 |
result = _with_request_context(request, text.strip(), int(body.candidates), bool(body.structured_fallback))
|
| 315 |
except Exception as exc:
|
| 316 |
+
return _exception_to_error(exc)
|
| 317 |
|
| 318 |
payload = _result_payload(result, text.strip(), started)
|
| 319 |
completion_id = f"chatcmpl-{uuid.uuid4().hex[:24]}"
|
|
|
|
| 401 |
<span>PASITA v1 converts plain text to Markdown, in Spanish and English. It works best on medium and long documents.</span>
|
| 402 |
<span>
|
| 403 |
<a href="{MODEL_CARD}" target="_blank" rel="noopener">MODEL</a> /
|
| 404 |
+
<a href="/docs" target="_blank" rel="noopener">API DOCS</a> /
|
| 405 |
+
<a href="?view=api" target="_blank" rel="noopener">GRADIO API</a> /
|
| 406 |
RUNS ON ZEROGPU
|
| 407 |
</span>
|
| 408 |
</footer>
|