Upload app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,7 @@ import torchvision.transforms as transforms
|
|
| 12 |
from PIL import Image
|
| 13 |
import gradio as gr
|
| 14 |
import timm
|
|
|
|
| 15 |
try:
|
| 16 |
import joblib
|
| 17 |
except ImportError:
|
|
@@ -479,12 +480,7 @@ class PlantClassifier:
|
|
| 479 |
url = config['drive_weights_url']
|
| 480 |
local_path = os.path.join(self.models_dir, 'dinov3_backbone_from_drive.pth')
|
| 481 |
print(f"⬇️ Downloading backbone weights from {url} -> {local_path}")
|
| 482 |
-
|
| 483 |
-
r.raise_for_status()
|
| 484 |
-
with open(local_path, 'wb') as f:
|
| 485 |
-
for chunk in r.iter_content(chunk_size=8192):
|
| 486 |
-
if chunk:
|
| 487 |
-
f.write(chunk)
|
| 488 |
base_obj = torch.load(local_path, map_location=self.device, weights_only=False)
|
| 489 |
if isinstance(base_obj, nn.Module):
|
| 490 |
backbone = base_obj
|
|
@@ -635,6 +631,68 @@ class PlantClassifier:
|
|
| 635 |
'actual_num_classes': num_classes
|
| 636 |
}
|
| 637 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 638 |
def get_transform(self, input_size, preproc=None):
|
| 639 |
"""Get image preprocessing transforms"""
|
| 640 |
if preproc == 'dann224':
|
|
|
|
| 12 |
from PIL import Image
|
| 13 |
import gradio as gr
|
| 14 |
import timm
|
| 15 |
+
import re
|
| 16 |
try:
|
| 17 |
import joblib
|
| 18 |
except ImportError:
|
|
|
|
| 480 |
url = config['drive_weights_url']
|
| 481 |
local_path = os.path.join(self.models_dir, 'dinov3_backbone_from_drive.pth')
|
| 482 |
print(f"⬇️ Downloading backbone weights from {url} -> {local_path}")
|
| 483 |
+
self._download_google_drive_file(url, local_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 484 |
base_obj = torch.load(local_path, map_location=self.device, weights_only=False)
|
| 485 |
if isinstance(base_obj, nn.Module):
|
| 486 |
backbone = base_obj
|
|
|
|
| 631 |
'actual_num_classes': num_classes
|
| 632 |
}
|
| 633 |
|
| 634 |
+
def _extract_drive_file_id(self, url: str):
|
| 635 |
+
"""Extract Google Drive file id from various URL forms."""
|
| 636 |
+
# Try query param id=...
|
| 637 |
+
m = re.search(r"[?&]id=([^&]+)", url)
|
| 638 |
+
if m:
|
| 639 |
+
return m.group(1)
|
| 640 |
+
# Try /d/<id>/ pattern
|
| 641 |
+
m = re.search(r"/d/([A-Za-z0-9_-]+)", url)
|
| 642 |
+
if m:
|
| 643 |
+
return m.group(1)
|
| 644 |
+
return None
|
| 645 |
+
|
| 646 |
+
def _download_google_drive_file(self, url: str, destination: str):
|
| 647 |
+
"""Download a file from Google Drive handling confirmation for large files.
|
| 648 |
+
|
| 649 |
+
Falls back to raising if the final response doesn't look like a binary download.
|
| 650 |
+
"""
|
| 651 |
+
if requests is None:
|
| 652 |
+
raise RuntimeError("requests not installed")
|
| 653 |
+
session = requests.Session()
|
| 654 |
+
file_id = self._extract_drive_file_id(url)
|
| 655 |
+
if not file_id:
|
| 656 |
+
# Assume direct binary link
|
| 657 |
+
with session.get(url, stream=True, timeout=60) as r:
|
| 658 |
+
r.raise_for_status()
|
| 659 |
+
self._stream_to_file(r, destination)
|
| 660 |
+
return
|
| 661 |
+
# Step 1: initial request
|
| 662 |
+
base = "https://drive.google.com/uc?export=download"
|
| 663 |
+
params = {"id": file_id, "acknowledgeAbuse": "true"}
|
| 664 |
+
with session.get(base, params=params, stream=True, timeout=60) as r1:
|
| 665 |
+
if "content-disposition" in r1.headers:
|
| 666 |
+
self._stream_to_file(r1, destination)
|
| 667 |
+
return
|
| 668 |
+
# look for confirm token
|
| 669 |
+
text = r1.text or ""
|
| 670 |
+
m = re.search(r"confirm=([0-9A-Za-z_]+)", text)
|
| 671 |
+
token = m.group(1) if m else None
|
| 672 |
+
if not token:
|
| 673 |
+
# Sometimes token lives in cookies
|
| 674 |
+
for k, v in r1.cookies.items():
|
| 675 |
+
if k.startswith("download_warning"):
|
| 676 |
+
token = v
|
| 677 |
+
break
|
| 678 |
+
if not token:
|
| 679 |
+
# No token and no disposition -> not a file
|
| 680 |
+
raise RuntimeError("Google Drive download requires confirmation but token was not found.")
|
| 681 |
+
# Step 2: confirmed request
|
| 682 |
+
params["confirm"] = token
|
| 683 |
+
with session.get(base, params=params, stream=True, timeout=60) as r2:
|
| 684 |
+
r2.raise_for_status()
|
| 685 |
+
if "content-disposition" not in r2.headers and "octet-stream" not in r2.headers.get("content-type", ""):
|
| 686 |
+
raise RuntimeError("Google Drive returned non-binary response after confirmation.")
|
| 687 |
+
self._stream_to_file(r2, destination)
|
| 688 |
+
|
| 689 |
+
def _stream_to_file(self, response, destination: str):
|
| 690 |
+
os.makedirs(os.path.dirname(destination), exist_ok=True)
|
| 691 |
+
with open(destination, 'wb') as f:
|
| 692 |
+
for chunk in response.iter_content(chunk_size=1048576):
|
| 693 |
+
if chunk:
|
| 694 |
+
f.write(chunk)
|
| 695 |
+
|
| 696 |
def get_transform(self, input_size, preproc=None):
|
| 697 |
"""Get image preprocessing transforms"""
|
| 698 |
if preproc == 'dann224':
|