Spaces:
Running
Running
fix: resolve all ruff lint errors
Browse files- Remove unused imports (sys, re, io, numpy, pytest, BBox, etc.)
- Fix import ordering across all files
- Replace IOError with OSError (Python 3 alias)
- Add noqa for intentional import-check imports
- Move json import to top level in app.py
- app.py +2 -4
- src/inference.py +2 -5
- src/utils.py +0 -1
- src/visualization.py +2 -3
- tests/test_app_smoke.py +7 -5
- tests/test_parsing.py +0 -1
- tests/test_prompts.py +1 -2
- tests/test_visualization.py +2 -1
app.py
CHANGED
|
@@ -6,17 +6,16 @@ and spacecraft components in space imagery.
|
|
| 6 |
|
| 7 |
from __future__ import annotations
|
| 8 |
|
|
|
|
| 9 |
import logging
|
| 10 |
import os
|
| 11 |
-
import sys
|
| 12 |
|
| 13 |
import gradio as gr
|
| 14 |
from PIL import Image
|
| 15 |
|
| 16 |
from src.config import APP_SUBTITLE, APP_TITLE
|
| 17 |
from src.inference import LocateAnythingWorker, run_localization
|
| 18 |
-
from src.
|
| 19 |
-
from src.prompts import SPACE_DEBRIS_EXAMPLES, get_example_prompts
|
| 20 |
from src.utils import ensure_rgb, format_json_output, format_metadata, validate_image
|
| 21 |
|
| 22 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
|
@@ -59,7 +58,6 @@ def run_inference(
|
|
| 59 |
|
| 60 |
metadata = format_metadata(parsed)
|
| 61 |
json_out = format_json_output(parsed)
|
| 62 |
-
import json
|
| 63 |
json_str = json.dumps(json_out, indent=2, ensure_ascii=False)
|
| 64 |
|
| 65 |
status = f"Done. Found {parsed.num_detections} object(s)."
|
|
|
|
| 6 |
|
| 7 |
from __future__ import annotations
|
| 8 |
|
| 9 |
+
import json
|
| 10 |
import logging
|
| 11 |
import os
|
|
|
|
| 12 |
|
| 13 |
import gradio as gr
|
| 14 |
from PIL import Image
|
| 15 |
|
| 16 |
from src.config import APP_SUBTITLE, APP_TITLE
|
| 17 |
from src.inference import LocateAnythingWorker, run_localization
|
| 18 |
+
from src.prompts import get_example_prompts
|
|
|
|
| 19 |
from src.utils import ensure_rgb, format_json_output, format_metadata, validate_image
|
| 20 |
|
| 21 |
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
|
|
|
|
| 58 |
|
| 59 |
metadata = format_metadata(parsed)
|
| 60 |
json_out = format_json_output(parsed)
|
|
|
|
| 61 |
json_str = json.dumps(json_out, indent=2, ensure_ascii=False)
|
| 62 |
|
| 63 |
status = f"Done. Found {parsed.num_detections} object(s)."
|
src/inference.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import re
|
| 6 |
from typing import Any
|
| 7 |
|
| 8 |
import torch
|
|
@@ -10,8 +9,6 @@ from PIL import Image
|
|
| 10 |
from transformers import AutoModel, AutoProcessor, AutoTokenizer
|
| 11 |
|
| 12 |
from src.config import (
|
| 13 |
-
COORD_MAX,
|
| 14 |
-
DEFAULT_CONFIDENCE,
|
| 15 |
DEVICE,
|
| 16 |
DTYPE,
|
| 17 |
GENERATION_MODE,
|
|
@@ -19,7 +16,7 @@ from src.config import (
|
|
| 19 |
MODEL_ID,
|
| 20 |
TEMPERATURE,
|
| 21 |
)
|
| 22 |
-
from src.parsing import
|
| 23 |
|
| 24 |
|
| 25 |
class LocateAnythingWorker:
|
|
@@ -148,7 +145,7 @@ def run_localization(
|
|
| 148 |
Returns:
|
| 149 |
Tuple of (annotated_image, raw_output, parse_result).
|
| 150 |
"""
|
| 151 |
-
from src.visualization import
|
| 152 |
|
| 153 |
if worker is None:
|
| 154 |
worker = LocateAnythingWorker()
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
from typing import Any
|
| 6 |
|
| 7 |
import torch
|
|
|
|
| 9 |
from transformers import AutoModel, AutoProcessor, AutoTokenizer
|
| 10 |
|
| 11 |
from src.config import (
|
|
|
|
|
|
|
| 12 |
DEVICE,
|
| 13 |
DTYPE,
|
| 14 |
GENERATION_MODE,
|
|
|
|
| 16 |
MODEL_ID,
|
| 17 |
TEMPERATURE,
|
| 18 |
)
|
| 19 |
+
from src.parsing import ParseResult, parse_boxes
|
| 20 |
|
| 21 |
|
| 22 |
class LocateAnythingWorker:
|
|
|
|
| 145 |
Returns:
|
| 146 |
Tuple of (annotated_image, raw_output, parse_result).
|
| 147 |
"""
|
| 148 |
+
from src.visualization import create_no_detection_overlay, draw_boxes
|
| 149 |
|
| 150 |
if worker is None:
|
| 151 |
worker = LocateAnythingWorker()
|
src/utils.py
CHANGED
|
@@ -2,7 +2,6 @@
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
| 5 |
-
import io
|
| 6 |
import logging
|
| 7 |
from typing import Any
|
| 8 |
|
|
|
|
| 2 |
|
| 3 |
from __future__ import annotations
|
| 4 |
|
|
|
|
| 5 |
import logging
|
| 6 |
from typing import Any
|
| 7 |
|
src/visualization.py
CHANGED
|
@@ -4,7 +4,6 @@ from __future__ import annotations
|
|
| 4 |
|
| 5 |
from typing import TYPE_CHECKING
|
| 6 |
|
| 7 |
-
import numpy as np
|
| 8 |
from PIL import Image, ImageDraw, ImageFont
|
| 9 |
|
| 10 |
if TYPE_CHECKING:
|
|
@@ -22,10 +21,10 @@ def _get_font(size: int = 14) -> ImageFont.FreeTypeFont | ImageFont.ImageFont:
|
|
| 22 |
"""Try to load a reasonable font, fall back to default."""
|
| 23 |
try:
|
| 24 |
return ImageFont.truetype("arial.ttf", size)
|
| 25 |
-
except
|
| 26 |
try:
|
| 27 |
return ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", size)
|
| 28 |
-
except
|
| 29 |
return ImageFont.load_default()
|
| 30 |
|
| 31 |
|
|
|
|
| 4 |
|
| 5 |
from typing import TYPE_CHECKING
|
| 6 |
|
|
|
|
| 7 |
from PIL import Image, ImageDraw, ImageFont
|
| 8 |
|
| 9 |
if TYPE_CHECKING:
|
|
|
|
| 21 |
"""Try to load a reasonable font, fall back to default."""
|
| 22 |
try:
|
| 23 |
return ImageFont.truetype("arial.ttf", size)
|
| 24 |
+
except OSError:
|
| 25 |
try:
|
| 26 |
return ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", size)
|
| 27 |
+
except OSError:
|
| 28 |
return ImageFont.load_default()
|
| 29 |
|
| 30 |
|
tests/test_app_smoke.py
CHANGED
|
@@ -5,14 +5,13 @@ import pytest
|
|
| 5 |
|
| 6 |
def test_app_module_imports():
|
| 7 |
"""Verify lightweight source modules can be imported without error."""
|
| 8 |
-
import src.config
|
| 9 |
-
import src.parsing
|
| 10 |
-
import src.prompts
|
| 11 |
-
import src.utils
|
| 12 |
|
| 13 |
|
| 14 |
def test_config_values():
|
| 15 |
-
from src.config import
|
| 16 |
|
| 17 |
assert MODEL_ID == "nvidia/LocateAnything-3B"
|
| 18 |
assert COORD_MAX == 1000
|
|
@@ -23,6 +22,7 @@ def test_config_values():
|
|
| 23 |
def test_visualization_import():
|
| 24 |
"""Verify visualization module imports."""
|
| 25 |
import src.visualization
|
|
|
|
| 26 |
assert hasattr(src.visualization, "draw_boxes")
|
| 27 |
assert hasattr(src.visualization, "create_no_detection_overlay")
|
| 28 |
|
|
@@ -31,6 +31,7 @@ def test_app_build_callable():
|
|
| 31 |
"""Verify the Gradio app builder is importable and callable."""
|
| 32 |
try:
|
| 33 |
from app import build_app
|
|
|
|
| 34 |
assert callable(build_app)
|
| 35 |
except ImportError:
|
| 36 |
pytest.skip("Gradio not available in test environment")
|
|
@@ -40,6 +41,7 @@ def test_inference_module_imports():
|
|
| 40 |
"""Verify inference module structure without heavy imports."""
|
| 41 |
try:
|
| 42 |
from src.inference import LocateAnythingWorker
|
|
|
|
| 43 |
w = LocateAnythingWorker.__new__(LocateAnythingWorker)
|
| 44 |
assert not getattr(w, "_loaded", True)
|
| 45 |
except ImportError:
|
|
|
|
| 5 |
|
| 6 |
def test_app_module_imports():
|
| 7 |
"""Verify lightweight source modules can be imported without error."""
|
| 8 |
+
import src.config # noqa: F401
|
| 9 |
+
import src.parsing # noqa: F401
|
| 10 |
+
import src.prompts # noqa: F401
|
|
|
|
| 11 |
|
| 12 |
|
| 13 |
def test_config_values():
|
| 14 |
+
from src.config import APP_SUBTITLE, APP_TITLE, COORD_MAX, MODEL_ID
|
| 15 |
|
| 16 |
assert MODEL_ID == "nvidia/LocateAnything-3B"
|
| 17 |
assert COORD_MAX == 1000
|
|
|
|
| 22 |
def test_visualization_import():
|
| 23 |
"""Verify visualization module imports."""
|
| 24 |
import src.visualization
|
| 25 |
+
|
| 26 |
assert hasattr(src.visualization, "draw_boxes")
|
| 27 |
assert hasattr(src.visualization, "create_no_detection_overlay")
|
| 28 |
|
|
|
|
| 31 |
"""Verify the Gradio app builder is importable and callable."""
|
| 32 |
try:
|
| 33 |
from app import build_app
|
| 34 |
+
|
| 35 |
assert callable(build_app)
|
| 36 |
except ImportError:
|
| 37 |
pytest.skip("Gradio not available in test environment")
|
|
|
|
| 41 |
"""Verify inference module structure without heavy imports."""
|
| 42 |
try:
|
| 43 |
from src.inference import LocateAnythingWorker
|
| 44 |
+
|
| 45 |
w = LocateAnythingWorker.__new__(LocateAnythingWorker)
|
| 46 |
assert not getattr(w, "_loaded", True)
|
| 47 |
except ImportError:
|
tests/test_parsing.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
"""Tests for the output parsing module."""
|
| 2 |
|
| 3 |
-
import pytest
|
| 4 |
from src.parsing import BBox, ParseResult, parse_boxes, parse_points
|
| 5 |
|
| 6 |
|
|
|
|
| 1 |
"""Tests for the output parsing module."""
|
| 2 |
|
|
|
|
| 3 |
from src.parsing import BBox, ParseResult, parse_boxes, parse_points
|
| 4 |
|
| 5 |
|
tests/test_prompts.py
CHANGED
|
@@ -1,9 +1,8 @@
|
|
| 1 |
"""Tests for prompt templates."""
|
| 2 |
|
| 3 |
-
import pytest
|
| 4 |
from src.prompts import (
|
| 5 |
-
SPACE_DEBRIS_EXAMPLES,
|
| 6 |
DETECTION_TEMPLATES,
|
|
|
|
| 7 |
PromptTemplate,
|
| 8 |
build_detect_prompt,
|
| 9 |
build_grounding_prompt,
|
|
|
|
| 1 |
"""Tests for prompt templates."""
|
| 2 |
|
|
|
|
| 3 |
from src.prompts import (
|
|
|
|
| 4 |
DETECTION_TEMPLATES,
|
| 5 |
+
SPACE_DEBRIS_EXAMPLES,
|
| 6 |
PromptTemplate,
|
| 7 |
build_detect_prompt,
|
| 8 |
build_grounding_prompt,
|
tests/test_visualization.py
CHANGED
|
@@ -2,8 +2,9 @@
|
|
| 2 |
|
| 3 |
import pytest
|
| 4 |
from PIL import Image
|
|
|
|
| 5 |
from src.parsing import BBox
|
| 6 |
-
from src.visualization import
|
| 7 |
|
| 8 |
|
| 9 |
@pytest.fixture
|
|
|
|
| 2 |
|
| 3 |
import pytest
|
| 4 |
from PIL import Image
|
| 5 |
+
|
| 6 |
from src.parsing import BBox
|
| 7 |
+
from src.visualization import create_no_detection_overlay, draw_boxes
|
| 8 |
|
| 9 |
|
| 10 |
@pytest.fixture
|