Add services layer, tests, streaming UI, and cleanup legacy code

This commit is contained in:
Viktor Barzin 2026-02-06 20:55:10 +00:00
parent 5514fa6381
commit d205d15c74
62 changed files with 3729 additions and 1024 deletions

View file

@ -1,4 +1,4 @@
def get_districts():
def get_districts() -> dict[str, str]:
return {
"Barking and Dagenham": "REGION^61400",
"Barnet": "REGION^93929",

View file

@ -1,10 +1,12 @@
import re
from pathlib import Path
from typing import Any
from PIL import Image
import cv2
import numpy as np
def inference(image_path):
def inference(image_path: str | Path) -> tuple[str, Any]:
from transformers import Pix2StructProcessor, Pix2StructForConditionalGeneration
image = Image.open(image_path)
@ -19,7 +21,7 @@ def inference(image_path):
return output, predictions
def extract_total_sqm(input_str: str):
def extract_total_sqm(input_str: str) -> float | None:
sqmregex = r"(\d+\.?\d*) ?(sq ?m|sq. ?m)"
matches = re.findall(sqmregex, input_str.lower())
sqms = [float(m[0]) for m in matches]
@ -29,13 +31,13 @@ def extract_total_sqm(input_str: str):
return max(filtered)
def calculate_model(image_path):
def calculate_model(image_path: str | Path) -> tuple[float | None, str, Any]:
output, predictions_tensor = inference(image_path)
estimated_sqm = extract_total_sqm(output)
return estimated_sqm, output, predictions_tensor
def improve_img_for_ocr(img: Image):
def improve_img_for_ocr(img: Image.Image) -> Image.Image:
img2 = np.array(img.convert("L"))
cv2.resize(img2, None, fx=1.2, fy=1.2, interpolation=cv2.INTER_CUBIC)
thresh = cv2.adaptiveThreshold(
@ -44,7 +46,7 @@ def improve_img_for_ocr(img: Image):
return Image.fromarray(thresh)
def calculate_ocr(image_path) -> tuple[float | None, str]:
def calculate_ocr(image_path: str | Path) -> tuple[float | None, str]:
import pytesseract
img = Image.open(image_path)

View file

@ -0,0 +1,41 @@
import json
from typing import List
from models.listing import DestinationMode, Route, RouteLegStep
from rec import routing
class RouteSerializer:
@staticmethod
def serialize(route): ...
@staticmethod
def deserialize(route_data_json: str) -> dict[DestinationMode, List[Route]]:
json_data = json.loads(route_data_json)
destimation_routes = {}
for destination_mode_str, routes_json in json_data.items():
destination_mode = DestinationMode(
destination_address=json.loads(destination_mode_str)[
"destination_address"
],
travel_mode=routing.TravelMode(
json.loads(destination_mode_str)["travel_mode"]
),
)
parsed_route = json.loads(routes_json[0])
routes = [
Route(
legs=[
RouteLegStep(
distance_meters=step["distance_meters"],
duration_s=step["duration_s"],
travel_mode=routing.TravelMode(step["travel_mode"]),
)
for step in parsed_route["legs"]
],
distance_meters=parsed_route["distance_meters"],
duration_s=int(parsed_route["duration_s"]),
)
]
destimation_routes[destination_mode] = routes
return destimation_routes