1from typing import List, Optional
2from pydantic import BaseModel, Field
3from config import CONFIG
4
5import requests
6
7
8class RetinaResult(BaseModel):
9 hash: Optional[str]
10 binary: Optional[str]
11 quality_too_low: Optional[bool] = Field(alias="qualityTooLow")
12
13
14class RetinaClient:
15 def __init__(self) -> None:
16 self._retina_host = CONFIG.retina_host
17
18 def get_image_hash(self, did: str, cid: str):
19 resp = requests.post(
20 f"{self._retina_host}/api/hash",
21 json={
22 "did": did,
23 "cid": cid,
24 },
25 )
26 resp.raise_for_status()
27
28 return RetinaResult.model_validate_json(resp.text)
29
30
31RETINA_CLIENT = RetinaClient()
32
33
34def hex_to_binary(hex: str):
35 return bytes.fromhex(hex)
36
37
38def binary_to_float_vector(bin: bytes):
39 vector_data: List[float] = []
40 for byte in bin:
41 for j in range(8):
42 if (byte >> (7 - j)) & 1 == 1:
43 vector_data.append(1.0)
44 else:
45 vector_data.append(0.0)
46 return vector_data