-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfloorplan_analyser.py
More file actions
91 lines (74 loc) · 3.39 KB
/
Copy pathfloorplan_analyser.py
File metadata and controls
91 lines (74 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import requests
from transformers import pipeline
import json
from bs4 import BeautifulSoup
import re
class FloorplanAnalyser:
def __init__(self, model: str = "impira/layoutlm-document-qa", task: str = "document-question-answering") -> None:
"""
Initialize the FloorplanAnalyser with a specified model and task for document-question answering.
Args:
model (str): The model to use for the document-question answering pipeline.
task (str): The task type for the pipeline.
"""
self.model = pipeline(model=model, task=task)
def download_image(self, image_url: str, filename: str) -> None:
"""
Downloads an image from a given URL and saves it to a specified file.
Args:
image_url (str): URL of the image to download.
filename (str): Filename to save the downloaded image.
"""
try:
response = requests.get(image_url)
response.raise_for_status()
with open(filename, "wb") as file:
file.write(response.content)
except requests.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except Exception as err:
print(f"An error occurred: {err}")
def get_floorplan_url(self, property_url: str) -> str:
"""
Constructs the floorplan URL from a given property URL.
Args:
property_url (str): The URL of the property.
Returns:
str: The modified URL pointing to the property's floorplan.
"""
parts = property_url.split("#")
floorplan_url = parts[0] + "#/floorplan" + ("?" + parts[1] if len(parts) > 1 else "")
return floorplan_url
def download_property_floorplan(self, property_url: str, image_path: str) -> None:
"""
Downloads the floorplan image of a property from its URL.
Args:
property_url (str): The URL of the property.
image_path (str): Path where the floorplan image will be saved.
"""
floorplan_url = self.get_floorplan_url(property_url)
headers = requests.utils.default_headers()
headers.update({"User-Agent": "My User Agent 1.0"})
response = requests.get(floorplan_url, headers=headers)
soup = BeautifulSoup(response.text, "html.parser")
script_tag = soup.find("script", string=lambda t: t and "window.PAGE_MODEL" in t)
json_string = script_tag.string.split("=", 1)[1].strip()
json_string = json_string.rstrip(";")
data = json.loads(json_string)
floorplan_image_url = data["propertyData"]["floorplans"][0]["url"]
self.download_image(floorplan_image_url, image_path)
def get_answer(self, question: str, image_path: str) -> float:
"""
Retrieves an answer to a specified question based on the analysis of a floorplan image.
Args:
question (str): The question to be answered based on the floorplan image.
image_path (str): The path to the floorplan image.
Returns:
float: The numerical answer extracted from the analysis, or 0.0 if no answer is found.
"""
answer = self.model(image=image_path, question=question)
if not answer:
return 0.0
answer = max(answer, key=lambda x: x["score"])["answer"].strip()
matches = re.findall(r"\d+\.\d+|\d+", answer)
return float(matches[0]) if matches else 0.0