Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,5 @@
|
|
1 |
import gradio as gr
|
2 |
import joblib
|
3 |
-
import numpy as np
|
4 |
import json
|
5 |
import math
|
6 |
import re
|
@@ -12,14 +11,12 @@ import xgboost as xgb
|
|
12 |
|
13 |
warnings.filterwarnings("ignore")
|
14 |
|
15 |
-
# Load
|
16 |
xgb_model = xgb.Booster()
|
17 |
xgb_model.load_model("xgb_model.json")
|
18 |
-
|
19 |
-
# Load Random Forest model
|
20 |
rf = joblib.load("rf_model.pkl")
|
21 |
|
22 |
-
# Load
|
23 |
with open("tile_catalog.json", "r", encoding="utf-8") as f:
|
24 |
tile_catalog = json.load(f)
|
25 |
|
@@ -52,23 +49,24 @@ def create_pdf(text):
|
|
52 |
|
53 |
def extract_tile_area(msg, unit):
|
54 |
msg = msg.lower().replace("feet", "").replace("ft", "").replace("mm", "").strip()
|
55 |
-
msg = re.sub(r"\s*(x|Γ|\*|into)\s*", "x", msg) #
|
|
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
val1
|
|
|
60 |
if unit == "mm":
|
61 |
sqft = (val1 * val2) / 92903.04
|
62 |
else:
|
63 |
sqft = val1 * val2
|
64 |
return round(sqft, 2)
|
65 |
-
|
66 |
-
return
|
67 |
-
|
68 |
-
try:
|
69 |
-
return float(re.sub(r"[^\d.]", "", msg)) # single number case
|
70 |
-
except:
|
71 |
return None
|
|
|
|
|
72 |
|
73 |
def chat_fn(message, history, user_state={}):
|
74 |
if message.strip().lower() in ["floor", "wall"]:
|
@@ -114,15 +112,15 @@ def chat_fn(message, history, user_state={}):
|
|
114 |
|
115 |
if user_state["step"] == "get_tile_size":
|
116 |
area = extract_tile_area(message, user_state["unit"])
|
117 |
-
if area
|
118 |
-
return reply("
|
119 |
|
120 |
user_state["tile_area"] = area
|
121 |
user_state["step"] = "done"
|
122 |
|
123 |
area_needed = user_state["area"]
|
124 |
tile_type = user_state["tile_type"]
|
125 |
-
tile_needed = math.ceil((area_needed / area) * 1.1)
|
126 |
|
127 |
best = []
|
128 |
for tile in tile_catalog:
|
@@ -171,7 +169,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", radius_size="lg")) as de
|
|
171 |
|
172 |
π Start by typing **"Floor"** or **"Wall"**
|
173 |
π Then give me the **area** and **tile size** in `mm` or `ft`
|
174 |
-
βοΈ Example: `600 x 600`, `2 x 2`,
|
175 |
π Iβll recommend tiles and generate a **PDF Report**!
|
176 |
""",
|
177 |
elem_id="header"
|
|
|
1 |
import gradio as gr
|
2 |
import joblib
|
|
|
3 |
import json
|
4 |
import math
|
5 |
import re
|
|
|
11 |
|
12 |
warnings.filterwarnings("ignore")
|
13 |
|
14 |
+
# Load models
|
15 |
xgb_model = xgb.Booster()
|
16 |
xgb_model.load_model("xgb_model.json")
|
|
|
|
|
17 |
rf = joblib.load("rf_model.pkl")
|
18 |
|
19 |
+
# Load product catalog and tile sizes
|
20 |
with open("tile_catalog.json", "r", encoding="utf-8") as f:
|
21 |
tile_catalog = json.load(f)
|
22 |
|
|
|
49 |
|
50 |
def extract_tile_area(msg, unit):
|
51 |
msg = msg.lower().replace("feet", "").replace("ft", "").replace("mm", "").strip()
|
52 |
+
msg = re.sub(r"\s*(x|Γ|\*|into)\s*", "x", msg) # Normalize separators to 'x'
|
53 |
+
parts = msg.split("x")
|
54 |
|
55 |
+
try:
|
56 |
+
if len(parts) == 2:
|
57 |
+
val1 = float(re.sub(r"[^\d.]", "", parts[0]))
|
58 |
+
val2 = float(re.sub(r"[^\d.]", "", parts[1]))
|
59 |
if unit == "mm":
|
60 |
sqft = (val1 * val2) / 92903.04
|
61 |
else:
|
62 |
sqft = val1 * val2
|
63 |
return round(sqft, 2)
|
64 |
+
elif len(parts) == 1 and parts[0]:
|
65 |
+
return float(re.sub(r"[^\d.]", "", parts[0]))
|
66 |
+
else:
|
|
|
|
|
|
|
67 |
return None
|
68 |
+
except:
|
69 |
+
return None
|
70 |
|
71 |
def chat_fn(message, history, user_state={}):
|
72 |
if message.strip().lower() in ["floor", "wall"]:
|
|
|
112 |
|
113 |
if user_state["step"] == "get_tile_size":
|
114 |
area = extract_tile_area(message, user_state["unit"])
|
115 |
+
if not area or area <= 0:
|
116 |
+
return reply("β That tile size didnβt work. Try something like:\n- `600 x 600`\n- `2 x 2`\n- `4` (if in ft)"), None, user_state
|
117 |
|
118 |
user_state["tile_area"] = area
|
119 |
user_state["step"] = "done"
|
120 |
|
121 |
area_needed = user_state["area"]
|
122 |
tile_type = user_state["tile_type"]
|
123 |
+
tile_needed = math.ceil((area_needed / area) * 1.1)
|
124 |
|
125 |
best = []
|
126 |
for tile in tile_catalog:
|
|
|
169 |
|
170 |
π Start by typing **"Floor"** or **"Wall"**
|
171 |
π Then give me the **area** and **tile size** in `mm` or `ft`
|
172 |
+
βοΈ Example: `600 x 600`, `2 x 2`, `4`
|
173 |
π Iβll recommend tiles and generate a **PDF Report**!
|
174 |
""",
|
175 |
elem_id="header"
|