modified: app.py
Browse files
app.py
CHANGED
|
@@ -47,28 +47,28 @@ def ensure_gap_model() -> str:
|
|
| 47 |
if not url_exists(GAP_MODEL_URL):
|
| 48 |
continue
|
| 49 |
if download:
|
|
|
|
| 50 |
with requests.get(GAP_MODEL_URL, stream=True, timeout=60) as response:
|
| 51 |
-
with open(
|
| 52 |
for chunk in response.iter_content(chunk_size=8192):
|
| 53 |
if chunk:
|
| 54 |
model_file.write(chunk)
|
| 55 |
-
GAP_DETECTION_MODEL_PATH = str(folder_path / version / "gap_detection_model.pt")
|
| 56 |
break
|
| 57 |
else:
|
| 58 |
compare_versions = sorted([latest_version, version], key=lambda s: list(map(int, s.lstrip("v").split("."))), reverse=True)
|
| 59 |
newer_version = compare_versions[0]
|
| 60 |
if newer_version != latest_version:
|
|
|
|
| 61 |
with requests.get(GAP_MODEL_URL, stream=True, timeout=60) as response:
|
| 62 |
-
with open(
|
| 63 |
for chunk in response.iter_content(chunk_size=8192):
|
| 64 |
if chunk:
|
| 65 |
model_file.write(chunk)
|
| 66 |
-
GAP_DETECTION_MODEL_PATH = str(folder_path / version / "gap_detection_model.pt")
|
| 67 |
break
|
| 68 |
else:
|
| 69 |
-
|
| 70 |
|
| 71 |
-
return
|
| 72 |
|
| 73 |
|
| 74 |
def url_exists(url: str, timeout: float = 5.0) -> bool:
|
|
@@ -90,10 +90,13 @@ def solve_worksheet(image_path: str):
|
|
| 90 |
if not _is_allowed_image(image_path):
|
| 91 |
raise gr.Error("Please upload a valid image file (PNG, JPG, JPEG, WEBP, BMP).")
|
| 92 |
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
with tempfile.TemporaryDirectory() as tmp_dir:
|
| 99 |
unique_id = uuid.uuid4().hex
|
|
@@ -105,7 +108,7 @@ def solve_worksheet(image_path: str):
|
|
| 105 |
|
| 106 |
solver = WorksheetSolver(
|
| 107 |
input_path,
|
| 108 |
-
gap_detection_model_path=
|
| 109 |
llm_model_name="gemini-3-flash-preview",
|
| 110 |
think=True,
|
| 111 |
local=False,
|
|
|
|
| 47 |
if not url_exists(GAP_MODEL_URL):
|
| 48 |
continue
|
| 49 |
if download:
|
| 50 |
+
gd_model_path = str(folder_path / version / "gap_detection_model.pt")
|
| 51 |
with requests.get(GAP_MODEL_URL, stream=True, timeout=60) as response:
|
| 52 |
+
with open(gd_model_path, "wb") as model_file:
|
| 53 |
for chunk in response.iter_content(chunk_size=8192):
|
| 54 |
if chunk:
|
| 55 |
model_file.write(chunk)
|
|
|
|
| 56 |
break
|
| 57 |
else:
|
| 58 |
compare_versions = sorted([latest_version, version], key=lambda s: list(map(int, s.lstrip("v").split("."))), reverse=True)
|
| 59 |
newer_version = compare_versions[0]
|
| 60 |
if newer_version != latest_version:
|
| 61 |
+
gd_model_path = str(folder_path / newer_version / "gap_detection_model.pt")
|
| 62 |
with requests.get(GAP_MODEL_URL, stream=True, timeout=60) as response:
|
| 63 |
+
with open(gd_model_path, "wb") as model_file:
|
| 64 |
for chunk in response.iter_content(chunk_size=8192):
|
| 65 |
if chunk:
|
| 66 |
model_file.write(chunk)
|
|
|
|
| 67 |
break
|
| 68 |
else:
|
| 69 |
+
gd_model_path = str(model_path)
|
| 70 |
|
| 71 |
+
return gd_model_path
|
| 72 |
|
| 73 |
|
| 74 |
def url_exists(url: str, timeout: float = 5.0) -> bool:
|
|
|
|
| 90 |
if not _is_allowed_image(image_path):
|
| 91 |
raise gr.Error("Please upload a valid image file (PNG, JPG, JPEG, WEBP, BMP).")
|
| 92 |
|
| 93 |
+
if not os.path.exists(GAP_DETECTION_MODEL_PATH):
|
| 94 |
+
try:
|
| 95 |
+
model_path = ensure_gap_model()
|
| 96 |
+
except Exception as error:
|
| 97 |
+
raise gr.Error(f"Could not load the gap detection model: {error}") from error
|
| 98 |
+
else:
|
| 99 |
+
model_path = GAP_DETECTION_MODEL_PATH
|
| 100 |
|
| 101 |
with tempfile.TemporaryDirectory() as tmp_dir:
|
| 102 |
unique_id = uuid.uuid4().hex
|
|
|
|
| 108 |
|
| 109 |
solver = WorksheetSolver(
|
| 110 |
input_path,
|
| 111 |
+
gap_detection_model_path=model_path,
|
| 112 |
llm_model_name="gemini-3-flash-preview",
|
| 113 |
think=True,
|
| 114 |
local=False,
|