Update code.txt
Browse files
code.txt
CHANGED
@@ -1,24 +1,17 @@
|
|
1 |
!pip install -U adapter-transformers
|
2 |
!pip install -U transformers
|
3 |
-
!pip install torch torchvision torchaudio
|
4 |
-
!pip install opencv-python
|
5 |
import gradio as gr
|
6 |
from transformers import CLIPProcessor, CLIPModel
|
7 |
from PIL import Image
|
8 |
import torch
|
9 |
-
import cv2
|
10 |
|
11 |
-
# Load the
|
12 |
model = CLIPModel.from_pretrained("Taarhoinc/TaarhoGen1")
|
13 |
processor = CLIPProcessor.from_pretrained("Taarhoinc/TaarhoGen1")
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
# Define the function to describe a floor plan with sizes
|
20 |
-
def describe_floorplan_with_sizes(floorplan_image: Image.Image, top_k: int = 3):
|
21 |
-
"""Describes a floor plan drawing by listing components and their sizes."""
|
22 |
|
23 |
# Define a list of common floor plan components
|
24 |
components = [
|
@@ -35,41 +28,35 @@ def describe_floorplan_with_sizes(floorplan_image: Image.Image, top_k: int = 3):
|
|
35 |
"window",
|
36 |
]
|
37 |
|
38 |
-
#
|
39 |
-
|
|
|
|
|
40 |
|
41 |
-
# Get
|
42 |
-
|
|
|
|
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
-
filtered_detections = detections[
|
47 |
-
(detections['confidence'] > threshold)
|
48 |
-
& (detections['name'].isin(components))
|
49 |
-
]
|
50 |
|
51 |
-
#
|
52 |
-
|
53 |
-
component_sizes = []
|
54 |
-
for index, row in filtered_detections.iterrows():
|
55 |
-
width = (row['xmax'] - row['xmin']) * scale
|
56 |
-
height = (row['ymax'] - row['ymin']) * scale
|
57 |
-
component_sizes.append(f"{row['name']}: {width:.2f}ft x {height:.2f}ft")
|
58 |
|
59 |
-
#
|
60 |
-
|
61 |
-
final_description = clip_description + ", " + ", ".join(component_sizes)
|
62 |
|
63 |
-
return
|
64 |
|
65 |
# Create the Gradio interface
|
66 |
gr.Interface(
|
67 |
-
fn=
|
68 |
inputs=[
|
69 |
gr.Image(label="Upload a floor plan drawing", type="pil"),
|
70 |
gr.Slider(1, 10, step=1, value=3, label="Number of components to detect"),
|
71 |
],
|
72 |
-
outputs=gr.Label(label="Detected Components
|
73 |
-
title="Floor Plan Description with TaarhoGen1
|
74 |
-
description="Upload a floor plan drawing to get a list of detected components
|
75 |
).launch()
|
|
|
1 |
!pip install -U adapter-transformers
|
2 |
!pip install -U transformers
|
|
|
|
|
3 |
import gradio as gr
|
4 |
from transformers import CLIPProcessor, CLIPModel
|
5 |
from PIL import Image
|
6 |
import torch
|
|
|
7 |
|
8 |
+
# Load the model and processor
|
9 |
model = CLIPModel.from_pretrained("Taarhoinc/TaarhoGen1")
|
10 |
processor = CLIPProcessor.from_pretrained("Taarhoinc/TaarhoGen1")
|
11 |
|
12 |
+
# Define the function to describe a floor plan
|
13 |
+
def describe_floorplan(floorplan_image: Image.Image, top_k: int = 3):
|
14 |
+
"""Describes a floor plan drawing by listing components."""
|
|
|
|
|
|
|
|
|
15 |
|
16 |
# Define a list of common floor plan components
|
17 |
components = [
|
|
|
28 |
"window",
|
29 |
]
|
30 |
|
31 |
+
# Preprocess the image and text prompts
|
32 |
+
inputs = processor(
|
33 |
+
text=components, images=floorplan_image, return_tensors="pt", padding=True
|
34 |
+
)
|
35 |
|
36 |
+
# Get the logits (similarity scores)
|
37 |
+
with torch.no_grad():
|
38 |
+
outputs = model(**inputs)
|
39 |
+
logits_per_image = outputs.logits_per_image
|
40 |
|
41 |
+
# Get the predicted probabilities
|
42 |
+
probs = logits_per_image.softmax(dim=1).cpu().numpy()[0]
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
# Get the indices of the top-k components
|
45 |
+
top_k_indices = probs.argsort()[-top_k:][::-1]
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
# Get the top-k components
|
48 |
+
detected_components = [components[i] for i in top_k_indices]
|
|
|
49 |
|
50 |
+
return ", ".join(detected_components) # Return as a comma-separated string
|
51 |
|
52 |
# Create the Gradio interface
|
53 |
gr.Interface(
|
54 |
+
fn=describe_floorplan,
|
55 |
inputs=[
|
56 |
gr.Image(label="Upload a floor plan drawing", type="pil"),
|
57 |
gr.Slider(1, 10, step=1, value=3, label="Number of components to detect"),
|
58 |
],
|
59 |
+
outputs=gr.Label(label="Detected Components"),
|
60 |
+
title="Floor Plan Description with TaarhoGen1",
|
61 |
+
description="Upload a floor plan drawing to get a list of detected components.",
|
62 |
).launch()
|