Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,17 @@
|
|
1 |
import gradio as gr
|
2 |
import joblib
|
3 |
-
import numpy as np
|
4 |
import json
|
5 |
import math
|
6 |
import re
|
7 |
from fpdf import FPDF
|
8 |
import tempfile
|
9 |
from deep_translator import GoogleTranslator
|
10 |
-
|
|
|
|
|
11 |
|
12 |
# Load models
|
13 |
-
xgb =
|
14 |
-
xgb.load_model("xgb_model.json")
|
15 |
rf = joblib.load("rf_model.pkl")
|
16 |
|
17 |
# Load tile catalog and size mappings
|
@@ -48,23 +48,35 @@ def create_pdf(text):
|
|
48 |
def extract_tile_area(msg):
|
49 |
msg = msg.lower().replace("ร", "x").replace("into", "x").replace("*", "x")
|
50 |
msg = msg.replace("ft", "").replace("feet", "").replace("mm", "").strip()
|
51 |
-
|
52 |
if "x" in msg:
|
53 |
parts = msg.split("x")
|
54 |
if len(parts) == 2:
|
55 |
try:
|
56 |
val1 = float(re.sub(r"[^\d.]", "", parts[0]))
|
57 |
val2 = float(re.sub(r"[^\d.]", "", parts[1]))
|
58 |
-
if val1 > 20: #
|
59 |
-
sqft = (val1 * val2) / 92903.04
|
60 |
-
else:
|
61 |
sqft = val1 * val2
|
62 |
return round(sqft, 2)
|
63 |
except:
|
64 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
return None
|
66 |
|
67 |
def chat_fn(message, history, user_state={}):
|
|
|
|
|
|
|
68 |
if "lang" not in user_state:
|
69 |
try:
|
70 |
user_state["lang"] = GoogleTranslator(source="auto", target="en").detect(message)
|
@@ -80,72 +92,4 @@ def chat_fn(message, history, user_state={}):
|
|
80 |
return reply(f"โ
Great! You selected {user_state['tile_type']} tiles.\nPlease enter the total area in sq.ft:"), None, user_state
|
81 |
return reply("๐ Hello! Are you looking for Floor or Wall tiles?"), None, user_state
|
82 |
|
83 |
-
if user_state
|
84 |
-
try:
|
85 |
-
user_state["area"] = float(message)
|
86 |
-
user_state["step"] = "get_tile_size"
|
87 |
-
return reply("๐ Now enter the tile size like 2 x 2 ft or 600x600 MM or 200 * 200:"), None, user_state
|
88 |
-
except:
|
89 |
-
return reply("โ Please enter a number for area (e.g. 120)."), None, user_state
|
90 |
-
|
91 |
-
if user_state["step"] == "get_tile_size":
|
92 |
-
area = extract_tile_area(message)
|
93 |
-
if area is None:
|
94 |
-
examples = "2 x 2 ft, 600x600 MM, 200*200, 1.5x1.5"
|
95 |
-
return reply(f"โ Oops, I didn't catch that size. Try formats like: {examples}"), None, user_state
|
96 |
-
|
97 |
-
user_state["tile_area"] = area
|
98 |
-
user_state["step"] = "done"
|
99 |
-
|
100 |
-
area_needed = user_state["area"]
|
101 |
-
tile_type = user_state["tile_type"]
|
102 |
-
tile_needed = math.ceil((area_needed / area) * 1.1)
|
103 |
-
|
104 |
-
best = []
|
105 |
-
for tile in tile_catalog:
|
106 |
-
if tile["type"].lower() == tile_type.lower():
|
107 |
-
per_box = tile["coverage"] / area
|
108 |
-
if per_box > 0:
|
109 |
-
boxes = math.ceil(tile_needed / per_box)
|
110 |
-
total = boxes * tile["price"]
|
111 |
-
best.append({
|
112 |
-
"name": tile["name"],
|
113 |
-
"size": tile["size"],
|
114 |
-
"price": tile["price"],
|
115 |
-
"boxes": boxes,
|
116 |
-
"total": total,
|
117 |
-
"url": tile["url"]
|
118 |
-
})
|
119 |
-
|
120 |
-
best.sort(key=lambda x: x["total"])
|
121 |
-
top3 = best[:3]
|
122 |
-
|
123 |
-
summary = f"""
|
124 |
-
๐งฑ Tile Type: {tile_type}
|
125 |
-
๐ Area: {area_needed} sq.ft
|
126 |
-
๐งฎ Tile Size: {round(area, 2)} sq.ft
|
127 |
-
๐ข Total Tiles Needed (10% buffer): {tile_needed}
|
128 |
-
|
129 |
-
๐ฏ Top Recommendations:
|
130 |
-
"""
|
131 |
-
for i, t in enumerate(top3, 1):
|
132 |
-
summary += f"\n{i}. {t['name']} ({t['size']})\n โน{t['price']}/box โ ~{t['boxes']} boxes\n {t['url']}\n"
|
133 |
-
|
134 |
-
summary += "\nSay 'Floor' or 'Wall' to start a new estimate."
|
135 |
-
|
136 |
-
pdf_path = create_pdf(summary)
|
137 |
-
return reply(summary), [pdf_path], {}
|
138 |
-
|
139 |
-
return reply("Say 'Floor' or 'Wall' to begin again."), None, {}
|
140 |
-
|
141 |
-
# Launch the bot
|
142 |
-
with gr.Blocks() as demo:
|
143 |
-
chatbot = gr.ChatInterface(
|
144 |
-
fn=chat_fn,
|
145 |
-
title="๐งฑ TileBot โ ChatGPT-Style Tile Estimator",
|
146 |
-
description="Chat like a human. Enter sizes naturally like 200x200, 2 x 2 ft, or 600x600 MM.\nSay 'Floor' or 'Wall' to begin.",
|
147 |
-
type="messages",
|
148 |
-
additional_outputs=[gr.File(label="๐ Download Report")]
|
149 |
-
)
|
150 |
-
|
151 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import joblib
|
|
|
3 |
import json
|
4 |
import math
|
5 |
import re
|
6 |
from fpdf import FPDF
|
7 |
import tempfile
|
8 |
from deep_translator import GoogleTranslator
|
9 |
+
import warnings
|
10 |
+
|
11 |
+
warnings.filterwarnings("ignore")
|
12 |
|
13 |
# Load models
|
14 |
+
xgb = joblib.load("xgb_model.pkl")
|
|
|
15 |
rf = joblib.load("rf_model.pkl")
|
16 |
|
17 |
# Load tile catalog and size mappings
|
|
|
48 |
def extract_tile_area(msg):
|
49 |
msg = msg.lower().replace("ร", "x").replace("into", "x").replace("*", "x")
|
50 |
msg = msg.replace("ft", "").replace("feet", "").replace("mm", "").strip()
|
51 |
+
|
52 |
if "x" in msg:
|
53 |
parts = msg.split("x")
|
54 |
if len(parts) == 2:
|
55 |
try:
|
56 |
val1 = float(re.sub(r"[^\d.]", "", parts[0]))
|
57 |
val2 = float(re.sub(r"[^\d.]", "", parts[1]))
|
58 |
+
if val1 > 20: # mm
|
59 |
+
sqft = (val1 * val2) / 92903.04
|
60 |
+
else:
|
61 |
sqft = val1 * val2
|
62 |
return round(sqft, 2)
|
63 |
except:
|
64 |
return None
|
65 |
+
elif re.match(r'^\d+(\.\d+)?$', msg):
|
66 |
+
try:
|
67 |
+
val = float(msg)
|
68 |
+
if val > 20: # in mmยฒ
|
69 |
+
return round((val * val) / 92903.04, 2)
|
70 |
+
else:
|
71 |
+
return round(val * val, 2)
|
72 |
+
except:
|
73 |
+
return None
|
74 |
return None
|
75 |
|
76 |
def chat_fn(message, history, user_state={}):
|
77 |
+
if message.strip().lower() in ["restart", "start", "reset", "floor", "wall"]:
|
78 |
+
user_state.clear()
|
79 |
+
|
80 |
if "lang" not in user_state:
|
81 |
try:
|
82 |
user_state["lang"] = GoogleTranslator(source="auto", target="en").detect(message)
|
|
|
92 |
return reply(f"โ
Great! You selected {user_state['tile_type']} tiles.\nPlease enter the total area in sq.ft:"), None, user_state
|
93 |
return reply("๐ Hello! Are you looking for Floor or Wall tiles?"), None, user_state
|
94 |
|
95 |
+
if user_state
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|