Spaces:
Paused
Paused
Codex commited on
Commit ·
eb9efc0
1
Parent(s): e662caa
Keep ZeroGPU calls under proxy token lifetime
Browse files- MainProc/src/group_pipeline.py +1 -1
- MainProc/src/ocr.py +16 -7
- MainProc/src/ocr_preprocess_profiles.py +2 -2
- app.py +5 -5
MainProc/src/group_pipeline.py
CHANGED
|
@@ -113,7 +113,7 @@ def _parse_view_batch(
|
|
| 113 |
|
| 114 |
max_batch_views = max(1, _env_int(
|
| 115 |
"LENTA_GROUP_FULL_OCR_BATCH_VIEWS" if force_full_variants else "LENTA_GROUP_OCR_BATCH_VIEWS",
|
| 116 |
-
|
| 117 |
))
|
| 118 |
if len(views) > max_batch_views:
|
| 119 |
for start in range(0, len(views), max_batch_views):
|
|
|
|
| 113 |
|
| 114 |
max_batch_views = max(1, _env_int(
|
| 115 |
"LENTA_GROUP_FULL_OCR_BATCH_VIEWS" if force_full_variants else "LENTA_GROUP_OCR_BATCH_VIEWS",
|
| 116 |
+
1,
|
| 117 |
))
|
| 118 |
if len(views) > max_batch_views:
|
| 119 |
for start in range(0, len(views), max_batch_views):
|
MainProc/src/ocr.py
CHANGED
|
@@ -65,11 +65,11 @@ def _env_flag(name: str, default: bool = False) -> bool:
|
|
| 65 |
|
| 66 |
def _gpu_duration(base_name: str, default_base: float, count: int = 1, per_item: float = 2.0) -> int:
|
| 67 |
seconds = _env_float(base_name, default_base) + max(1, count) * per_item
|
| 68 |
-
return max(5, min(
|
| 69 |
|
| 70 |
|
| 71 |
def ocr_one_gpu_duration(*_args, **_kwargs) -> int:
|
| 72 |
-
return _gpu_duration("LENTA_OCR_ONE_GPU_SECONDS_BASE",
|
| 73 |
|
| 74 |
|
| 75 |
def extract_text_many_gpu_duration(*args, **kwargs) -> int:
|
|
@@ -86,11 +86,11 @@ def extract_text_many_gpu_duration(*args, **kwargs) -> int:
|
|
| 86 |
has_full = bool(force_full_variants)
|
| 87 |
|
| 88 |
if has_full:
|
| 89 |
-
per_item = _env_float("LENTA_OCR_FULL_GPU_SECONDS_PER_IMAGE",
|
| 90 |
-
return _gpu_duration("LENTA_OCR_FULL_GPU_SECONDS_BASE",
|
| 91 |
|
| 92 |
-
per_item = _env_float("LENTA_OCR_GPU_SECONDS_PER_IMAGE",
|
| 93 |
-
return _gpu_duration("LENTA_OCR_GPU_SECONDS_BASE",
|
| 94 |
|
| 95 |
|
| 96 |
def bbox_to_list(bbox) -> list | None:
|
|
@@ -518,7 +518,7 @@ def _read_profile_variants_many(
|
|
| 518 |
variant_meta: list[tuple[int, str]] = []
|
| 519 |
for image_index in sorted(unresolved):
|
| 520 |
if use_full_variants:
|
| 521 |
-
variants =
|
| 522 |
else:
|
| 523 |
variants = build_ocr_variant_stage_for_profile(images[image_index], profile, stage_index)
|
| 524 |
for variant in variants:
|
|
@@ -564,6 +564,15 @@ def _read_profile_variants_many(
|
|
| 564 |
return selected_results
|
| 565 |
|
| 566 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 567 |
def _resolve_ocr_image_path(
|
| 568 |
image_name: str,
|
| 569 |
*,
|
|
|
|
| 65 |
|
| 66 |
def _gpu_duration(base_name: str, default_base: float, count: int = 1, per_item: float = 2.0) -> int:
|
| 67 |
seconds = _env_float(base_name, default_base) + max(1, count) * per_item
|
| 68 |
+
return max(5, min(55, int(seconds + 0.999)))
|
| 69 |
|
| 70 |
|
| 71 |
def ocr_one_gpu_duration(*_args, **_kwargs) -> int:
|
| 72 |
+
return _gpu_duration("LENTA_OCR_ONE_GPU_SECONDS_BASE", 45.0, 1, 0.0)
|
| 73 |
|
| 74 |
|
| 75 |
def extract_text_many_gpu_duration(*args, **kwargs) -> int:
|
|
|
|
| 86 |
has_full = bool(force_full_variants)
|
| 87 |
|
| 88 |
if has_full:
|
| 89 |
+
per_item = _env_float("LENTA_OCR_FULL_GPU_SECONDS_PER_IMAGE", 25.0)
|
| 90 |
+
return _gpu_duration("LENTA_OCR_FULL_GPU_SECONDS_BASE", 25.0, count, per_item)
|
| 91 |
|
| 92 |
+
per_item = _env_float("LENTA_OCR_GPU_SECONDS_PER_IMAGE", 20.0)
|
| 93 |
+
return _gpu_duration("LENTA_OCR_GPU_SECONDS_BASE", 20.0, count, per_item)
|
| 94 |
|
| 95 |
|
| 96 |
def bbox_to_list(bbox) -> list | None:
|
|
|
|
| 518 |
variant_meta: list[tuple[int, str]] = []
|
| 519 |
for image_index in sorted(unresolved):
|
| 520 |
if use_full_variants:
|
| 521 |
+
variants = _build_full_fallback_variants(images[image_index], profile)
|
| 522 |
else:
|
| 523 |
variants = build_ocr_variant_stage_for_profile(images[image_index], profile, stage_index)
|
| 524 |
for variant in variants:
|
|
|
|
| 564 |
return selected_results
|
| 565 |
|
| 566 |
|
| 567 |
+
def _build_full_fallback_variants(image, profile: str) -> list[dict]:
|
| 568 |
+
"""Return a compact set of heavy OCR variants that can fit one ZeroGPU lease."""
|
| 569 |
+
max_variants = max(1, _env_int("LENTA_OCR_FULL_VARIANTS_PER_IMAGE", 2))
|
| 570 |
+
variants: list[dict] = []
|
| 571 |
+
for stage_index in (2, 1, 0):
|
| 572 |
+
variants.extend(build_ocr_variant_stage_for_profile(image, profile, stage_index))
|
| 573 |
+
return variants[:max_variants]
|
| 574 |
+
|
| 575 |
+
|
| 576 |
def _resolve_ocr_image_path(
|
| 577 |
image_name: str,
|
| 578 |
*,
|
MainProc/src/ocr_preprocess_profiles.py
CHANGED
|
@@ -80,8 +80,8 @@ def build_ocr_variant_stages_for_profile(image, profile: str = "default") -> lis
|
|
| 80 |
def ocr_variant_stage_count(profile: str = "default") -> int:
|
| 81 |
"""Return how many staged OCR variant groups a profile has."""
|
| 82 |
if profile == "red_promo":
|
| 83 |
-
return
|
| 84 |
-
return
|
| 85 |
|
| 86 |
|
| 87 |
def build_ocr_variant_stage_for_profile(image, profile: str = "default", stage_index: int = 0) -> list[dict]:
|
|
|
|
| 80 |
def ocr_variant_stage_count(profile: str = "default") -> int:
|
| 81 |
"""Return how many staged OCR variant groups a profile has."""
|
| 82 |
if profile == "red_promo":
|
| 83 |
+
return 2
|
| 84 |
+
return 2
|
| 85 |
|
| 86 |
|
| 87 |
def build_ocr_variant_stage_for_profile(image, profile: str = "default", stage_index: int = 0) -> list[dict]:
|
app.py
CHANGED
|
@@ -134,9 +134,9 @@ def ensure_detector_device(model):
|
|
| 134 |
|
| 135 |
def detector_gpu_duration(frames, conf):
|
| 136 |
count = len(frames) if frames is not None else 1
|
| 137 |
-
seconds = env_float("LENTA_DETECT_GPU_SECONDS_BASE",
|
| 138 |
-
seconds += count * env_float("LENTA_DETECT_GPU_SECONDS_PER_FRAME",
|
| 139 |
-
return max(
|
| 140 |
|
| 141 |
|
| 142 |
def serialize_detector_results(results):
|
|
@@ -172,7 +172,7 @@ def predict_detector_batch(frames, conf):
|
|
| 172 |
predict_kwargs = {
|
| 173 |
"conf": conf,
|
| 174 |
"verbose": False,
|
| 175 |
-
"batch": max(1, min(len(frames), env_int("LENTA_DETECT_BATCH_SIZE",
|
| 176 |
"device": device,
|
| 177 |
}
|
| 178 |
if env_flag("LENTA_DETECT_HALF", False) and device == "cuda":
|
|
@@ -449,7 +449,7 @@ def extract_grouped_input(
|
|
| 449 |
tracks = {}
|
| 450 |
prev_processed_gray = None
|
| 451 |
total_saved = 0
|
| 452 |
-
detector_batch_size = max(1, env_int("LENTA_DETECT_BATCH_SIZE",
|
| 453 |
|
| 454 |
def detections_from_result(frame, result):
|
| 455 |
frame_height, frame_width = frame.shape[:2]
|
|
|
|
| 134 |
|
| 135 |
def detector_gpu_duration(frames, conf):
|
| 136 |
count = len(frames) if frames is not None else 1
|
| 137 |
+
seconds = env_float("LENTA_DETECT_GPU_SECONDS_BASE", 18.0)
|
| 138 |
+
seconds += count * env_float("LENTA_DETECT_GPU_SECONDS_PER_FRAME", 8.0)
|
| 139 |
+
return max(20, min(55, int(math.ceil(seconds))))
|
| 140 |
|
| 141 |
|
| 142 |
def serialize_detector_results(results):
|
|
|
|
| 172 |
predict_kwargs = {
|
| 173 |
"conf": conf,
|
| 174 |
"verbose": False,
|
| 175 |
+
"batch": max(1, min(len(frames), env_int("LENTA_DETECT_BATCH_SIZE", 1))),
|
| 176 |
"device": device,
|
| 177 |
}
|
| 178 |
if env_flag("LENTA_DETECT_HALF", False) and device == "cuda":
|
|
|
|
| 449 |
tracks = {}
|
| 450 |
prev_processed_gray = None
|
| 451 |
total_saved = 0
|
| 452 |
+
detector_batch_size = max(1, env_int("LENTA_DETECT_BATCH_SIZE", 1))
|
| 453 |
|
| 454 |
def detections_from_result(frame, result):
|
| 455 |
frame_height, frame_width = frame.shape[:2]
|