Coots commited on
Commit
4ca754d
·
verified ·
1 Parent(s): ceaf0c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -11
app.py CHANGED
@@ -5,6 +5,7 @@ import math
5
  import re
6
  from deep_translator import GoogleTranslator
7
  import warnings
 
8
  warnings.filterwarnings("ignore")
9
 
10
  # Load models
@@ -21,7 +22,8 @@ with open("tile_sizes.json", "r", encoding="utf-8") as f:
21
  def translate(text, lang="en"):
22
  try:
23
  return GoogleTranslator(source="auto", target=lang).translate(text)
24
- except:
 
25
  return text
26
 
27
  def extract_tile_area(msg):
@@ -34,21 +36,21 @@ def extract_tile_area(msg):
34
  try:
35
  val1 = float(re.sub(r"[^\d.]", "", parts[0]))
36
  val2 = float(re.sub(r"[^\d.]", "", parts[1]))
37
- if val1 > 20:
38
- sqft = (val1 * val2) / 92903.04 # mm
39
  else:
40
- sqft = val1 * val2 # ft
41
  return round(sqft, 2)
42
- except:
43
  return None
44
  elif re.match(r'^\d+(\.\d+)?$', msg):
45
  try:
46
  val = float(msg)
47
- if val > 20:
48
  return round((val * val) / 92903.04, 2)
49
  else:
50
  return round(val * val, 2)
51
- except:
52
  return None
53
  return None
54
 
@@ -62,7 +64,8 @@ def chat_fn(message, history, user_state={}):
62
  if "lang" not in user_state:
63
  try:
64
  user_state["lang"] = GoogleTranslator(source="auto", target="en").detect(message)
65
- except:
 
66
  user_state["lang"] = "en"
67
  lang = user_state["lang"]
68
  def reply(text): return translate(text, lang)
@@ -82,7 +85,7 @@ def chat_fn(message, history, user_state={}):
82
  user_state["area"] = float(message)
83
  user_state["step"] = "get_tile_size"
84
  return reply("📏 Now enter the tile size (e.g. 2x2, 600x600 mm, 200 * 200):"), None, user_state
85
- except:
86
  return reply("❗ Please enter a valid number for the area (e.g. 120)."), None, user_state
87
 
88
  # Step 3: Get tile size
@@ -136,10 +139,12 @@ def chat_fn(message, history, user_state={}):
136
 
137
  # Gradio interface
138
  with gr.Blocks() as demo:
 
 
139
  gr.ChatInterface(
140
  fn=chat_fn,
141
- title="🧱 Tilo – Tile Estimator",
142
- description="Type 'Floor' or 'Wall' to start. Then give your room size and tile size (e.g. 2x2 ft, 600x600 mm).",
143
  type="messages"
144
  )
145
 
 
5
  import re
6
  from deep_translator import GoogleTranslator
7
  import warnings
8
+
9
  warnings.filterwarnings("ignore")
10
 
11
  # Load models
 
22
  def translate(text, lang="en"):
23
  try:
24
  return GoogleTranslator(source="auto", target=lang).translate(text)
25
+ except Exception as e:
26
+ print(f"Translation error: {e}")
27
  return text
28
 
29
  def extract_tile_area(msg):
 
36
  try:
37
  val1 = float(re.sub(r"[^\d.]", "", parts[0]))
38
  val2 = float(re.sub(r"[^\d.]", "", parts[1]))
39
+ if val1 > 20: # Assuming mm
40
+ sqft = (val1 * val2) / 92903.04 # Convert mm² to ft²
41
  else:
42
+ sqft = val1 * val2 # ft²
43
  return round(sqft, 2)
44
+ except ValueError:
45
  return None
46
  elif re.match(r'^\d+(\.\d+)?$', msg):
47
  try:
48
  val = float(msg)
49
+ if val > 20: # Assuming mm
50
  return round((val * val) / 92903.04, 2)
51
  else:
52
  return round(val * val, 2)
53
+ except ValueError:
54
  return None
55
  return None
56
 
 
64
  if "lang" not in user_state:
65
  try:
66
  user_state["lang"] = GoogleTranslator(source="auto", target="en").detect(message)
67
+ except Exception as e:
68
+ print(f"Language detection error: {e}")
69
  user_state["lang"] = "en"
70
  lang = user_state["lang"]
71
  def reply(text): return translate(text, lang)
 
85
  user_state["area"] = float(message)
86
  user_state["step"] = "get_tile_size"
87
  return reply("📏 Now enter the tile size (e.g. 2x2, 600x600 mm, 200 * 200):"), None, user_state
88
+ except ValueError:
89
  return reply("❗ Please enter a valid number for the area (e.g. 120)."), None, user_state
90
 
91
  # Step 3: Get tile size
 
139
 
140
  # Gradio interface
141
  with gr.Blocks() as demo:
142
+ gr.Markdown("# 🧱 Tilo – Tile Estimator")
143
+ gr.Markdown("Type 'Floor' or 'Wall' to start. Then give your room size and tile size (e.g. 2x2 ft, 600x600 mm).")
144
  gr.ChatInterface(
145
  fn=chat_fn,
146
+ title="Tile Estimator Chat",
147
+ description="Get estimates for your tiling needs.",
148
  type="messages"
149
  )
150