Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
|
@@ -335,6 +335,24 @@ server = Server(title="PASITA API", version="1.0.0", description=API_DESCRIPTION
|
|
| 335 |
|
| 336 |
_V2_CONVERT_PATHS = frozenset({"/gradio_api/call/v2/convert", "/gradio_api/call/v2/convert/"})
|
| 337 |
_V1_CONVERT_PATHS = frozenset({"/gradio_api/call/convert", "/gradio_api/call/convert/"})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
_CONVERT_PARAMS = (("text", ""), ("num_candidates", 3), ("structured_fallback", True), ("max_output_tokens", None))
|
| 339 |
|
| 340 |
|
|
@@ -389,7 +407,8 @@ class _LegacyConvertBodyMiddleware:
|
|
| 389 |
path = scope.get("path", "")
|
| 390 |
is_v2 = path in _V2_CONVERT_PATHS
|
| 391 |
is_v1 = path in _V1_CONVERT_PATHS
|
| 392 |
-
|
|
|
|
| 393 |
await self.app(scope, receive, send)
|
| 394 |
return
|
| 395 |
body = b""
|
|
@@ -403,36 +422,39 @@ class _LegacyConvertBodyMiddleware:
|
|
| 403 |
except Exception:
|
| 404 |
parsed = None
|
| 405 |
if isinstance(parsed, dict):
|
| 406 |
-
if
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
named[
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
|
|
|
|
|
|
|
|
|
| 436 |
|
| 437 |
async def receive_once():
|
| 438 |
return {"type": "http.request", "body": body, "more_body": False}
|
|
|
|
| 335 |
|
| 336 |
_V2_CONVERT_PATHS = frozenset({"/gradio_api/call/v2/convert", "/gradio_api/call/v2/convert/"})
|
| 337 |
_V1_CONVERT_PATHS = frozenset({"/gradio_api/call/convert", "/gradio_api/call/convert/"})
|
| 338 |
+
_QUEUE_JOIN_PATHS = frozenset({"/gradio_api/queue/join", "/gradio_api/queue/join/"})
|
| 339 |
+
_CONVERT_API_NAMES = frozenset({"convert", "convert_1"})
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
def _is_convert_fn(scope, fn_index) -> bool:
|
| 343 |
+
try:
|
| 344 |
+
app = scope.get("app")
|
| 345 |
+
if app is None or not hasattr(app, "get_blocks"):
|
| 346 |
+
return False
|
| 347 |
+
blocks = app.get_blocks()
|
| 348 |
+
fns = getattr(blocks, "fns", None)
|
| 349 |
+
if not fns or not isinstance(fn_index, int) or isinstance(fn_index, bool):
|
| 350 |
+
return False
|
| 351 |
+
if not 0 <= fn_index < len(fns):
|
| 352 |
+
return False
|
| 353 |
+
return getattr(fns[fn_index], "api_name", None) in _CONVERT_API_NAMES
|
| 354 |
+
except Exception:
|
| 355 |
+
return False
|
| 356 |
_CONVERT_PARAMS = (("text", ""), ("num_candidates", 3), ("structured_fallback", True), ("max_output_tokens", None))
|
| 357 |
|
| 358 |
|
|
|
|
| 407 |
path = scope.get("path", "")
|
| 408 |
is_v2 = path in _V2_CONVERT_PATHS
|
| 409 |
is_v1 = path in _V1_CONVERT_PATHS
|
| 410 |
+
is_queue = path in _QUEUE_JOIN_PATHS
|
| 411 |
+
if not (is_v2 or is_v1 or is_queue):
|
| 412 |
await self.app(scope, receive, send)
|
| 413 |
return
|
| 414 |
body = b""
|
|
|
|
| 422 |
except Exception:
|
| 423 |
parsed = None
|
| 424 |
if isinstance(parsed, dict):
|
| 425 |
+
if is_queue and not _is_convert_fn(scope, parsed.get("fn_index")):
|
| 426 |
+
pass
|
| 427 |
+
else:
|
| 428 |
+
if is_v2 and "data" in parsed and "text" not in parsed:
|
| 429 |
+
data = parsed.get("data")
|
| 430 |
+
if isinstance(data, list):
|
| 431 |
+
named: dict = {}
|
| 432 |
+
for (name, _default), value in zip(_CONVERT_PARAMS, data):
|
| 433 |
+
named[name] = value
|
| 434 |
+
for key in ("session_hash", "event_id", "fn_index", "trigger_id", "batched"):
|
| 435 |
+
if key in parsed:
|
| 436 |
+
named[key] = parsed[key]
|
| 437 |
+
parsed = named
|
| 438 |
+
if is_v2:
|
| 439 |
+
for name, default in _CONVERT_PARAMS:
|
| 440 |
+
if name in parsed and (parsed[name] is None or parsed[name] == ""):
|
| 441 |
+
parsed[name] = default
|
| 442 |
+
for name in ("num_candidates", "structured_fallback", "max_output_tokens"):
|
| 443 |
+
if name in parsed:
|
| 444 |
+
parsed[name] = _clean_convert_value(name, parsed[name])
|
| 445 |
+
elif isinstance(parsed.get("data"), list):
|
| 446 |
+
data = list(parsed["data"])
|
| 447 |
+
while len(data) < len(_CONVERT_PARAMS):
|
| 448 |
+
data.append(None)
|
| 449 |
+
cleaned = []
|
| 450 |
+
for (name, default), value in zip(_CONVERT_PARAMS, data):
|
| 451 |
+
if value is None or value == "":
|
| 452 |
+
cleaned.append(default if name != "text" else "")
|
| 453 |
+
else:
|
| 454 |
+
cleaned.append(_clean_convert_value(name, value))
|
| 455 |
+
parsed = dict(parsed)
|
| 456 |
+
parsed["data"] = cleaned
|
| 457 |
+
body = json.dumps(parsed, default=str).encode("utf-8")
|
| 458 |
|
| 459 |
async def receive_once():
|
| 460 |
return {"type": "http.request", "body": body, "more_body": False}
|