Spaces:
Running
Running
try fix again
Browse files
app.py
CHANGED
|
@@ -120,12 +120,13 @@ def normalize_country_query(q: str) -> str | None:
|
|
| 120 |
return q.upper()
|
| 121 |
return None
|
| 122 |
|
|
|
|
| 123 |
def parse_freeform_query(text: str):
|
| 124 |
"""Supports 'plushies in uk' and 'uk plushies'."""
|
| 125 |
if not text:
|
| 126 |
return "", ""
|
| 127 |
text = text.strip().lower()
|
| 128 |
-
m = re.match(r"(.+?)
|
| 129 |
if m:
|
| 130 |
return m.group(1).strip(), m.group(2).strip()
|
| 131 |
parts = text.split()
|
|
@@ -138,7 +139,7 @@ def parse_freeform_query(text: str):
|
|
| 138 |
return text, ""
|
| 139 |
|
| 140 |
# ---------------- Semantic Match ----------------
|
| 141 |
-
def semantic_match(query, top_k=15
|
| 142 |
if not query:
|
| 143 |
return {"category": None, "items": []}
|
| 144 |
|
|
@@ -180,6 +181,7 @@ def fetch_yata(force_refresh=False):
|
|
| 180 |
|
| 181 |
# ---------------- Core logic ----------------
|
| 182 |
def query_inventory(query_text="", category="", country_name="", capacity=10, refresh=False):
|
|
|
|
| 183 |
data, last_update = fetch_yata(force_refresh=refresh)
|
| 184 |
stocks = data.get("stocks", {})
|
| 185 |
rows = []
|
|
@@ -197,7 +199,7 @@ def query_inventory(query_text="", category="", country_name="", capacity=10, re
|
|
| 197 |
for code_raw, cdata in stocks.items():
|
| 198 |
code = code_raw.upper()
|
| 199 |
cname = COUNTRY_NAMES.get(code, code)
|
| 200 |
-
|
| 201 |
# Filter by requested country
|
| 202 |
if country_name:
|
| 203 |
if user_code:
|
|
@@ -205,20 +207,19 @@ def query_inventory(query_text="", category="", country_name="", capacity=10, re
|
|
| 205 |
continue
|
| 206 |
elif country_name.lower() not in cname.lower():
|
| 207 |
continue
|
| 208 |
-
|
| 209 |
update_ts = cdata.get("update")
|
| 210 |
update_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(update_ts)) if update_ts else "Unknown"
|
| 211 |
-
|
| 212 |
live_lookup = {i["name"]: i for i in cdata.get("stocks", [])}
|
| 213 |
-
|
| 214 |
# Merge known items (include 0-qty)
|
| 215 |
for iname, itype in ITEM_TO_TYPE.items():
|
| 216 |
itype = itype.lower()
|
| 217 |
item_data = live_lookup.get(iname, {"quantity": 0, "cost": 0})
|
| 218 |
qty = item_data.get("quantity", 0)
|
| 219 |
cost = item_data.get("cost", 0)
|
| 220 |
-
|
| 221 |
-
# Match against query
|
| 222 |
if item_term:
|
| 223 |
item_ok = (
|
| 224 |
item_term.lower() in iname.lower()
|
|
@@ -229,7 +230,7 @@ def query_inventory(query_text="", category="", country_name="", capacity=10, re
|
|
| 229 |
item_ok = category.lower() == itype
|
| 230 |
else:
|
| 231 |
item_ok = True
|
| 232 |
-
|
| 233 |
if item_ok:
|
| 234 |
rows.append({
|
| 235 |
"Country": cname,
|
|
@@ -255,14 +256,6 @@ def query_inventory(query_text="", category="", country_name="", capacity=10, re
|
|
| 255 |
def run_query(query_text, category, country, capacity, refresh):
|
| 256 |
return query_inventory(query_text, category, country, capacity, refresh)
|
| 257 |
|
| 258 |
-
def init_capacity(saved_capacity):
|
| 259 |
-
try:
|
| 260 |
-
if saved_capacity and 5 <= float(saved_capacity) <= 88:
|
| 261 |
-
return float(saved_capacity)
|
| 262 |
-
except Exception:
|
| 263 |
-
pass
|
| 264 |
-
return 10
|
| 265 |
-
|
| 266 |
# ---------------- Gradio UI ----------------
|
| 267 |
with gr.Blocks(title="π§³ Torn Inventory Viewer") as iface:
|
| 268 |
gr.Markdown("## π§³ Torn Inventory Viewer")
|
|
@@ -273,9 +266,6 @@ with gr.Blocks(title="π§³ Torn Inventory Viewer") as iface:
|
|
| 273 |
country_box = gr.Textbox(label="Country (optional, e.g. UK, Cayman, Japan)")
|
| 274 |
capacity_slider = gr.Number(label="Travel Capacity", value=10, minimum=5, maximum=88, precision=0)
|
| 275 |
refresh_check = gr.Checkbox(label="Force refresh (ignore cache)", value=False)
|
| 276 |
-
saved_capacity_state = gr.State(value=None)
|
| 277 |
-
|
| 278 |
-
iface.load(init_capacity, inputs=[saved_capacity_state], outputs=[capacity_slider])
|
| 279 |
|
| 280 |
run_btn = gr.Button("π Search / Refresh")
|
| 281 |
result_df = gr.Dataframe(label="Results")
|
|
@@ -285,19 +275,16 @@ with gr.Blocks(title="π§³ Torn Inventory Viewer") as iface:
|
|
| 285 |
inputs=[query_box, category_drop, country_box, capacity_slider, refresh_check],
|
| 286 |
outputs=[result_df, meta_box])
|
| 287 |
|
|
|
|
| 288 |
gr.HTML("""
|
| 289 |
<script>
|
| 290 |
window.addEventListener('DOMContentLoaded', () => {
|
| 291 |
-
// Restore saved capacity
|
| 292 |
const saved = localStorage.getItem('travel_capacity');
|
| 293 |
const capField = document.querySelector('input[type=number]');
|
| 294 |
if (saved && capField) {
|
| 295 |
capField.value = saved;
|
| 296 |
-
|
| 297 |
-
capField.dispatchEvent(inputEvent);
|
| 298 |
}
|
| 299 |
-
|
| 300 |
-
// Save new changes
|
| 301 |
const observer = new MutationObserver(() => {
|
| 302 |
const field = document.querySelector('input[type=number]');
|
| 303 |
if (field && !field.dataset.synced) {
|
|
@@ -313,8 +300,9 @@ with gr.Blocks(title="π§³ Torn Inventory Viewer") as iface:
|
|
| 313 |
""")
|
| 314 |
|
| 315 |
try:
|
|
|
|
| 316 |
iface.launch()
|
| 317 |
except Exception as e:
|
| 318 |
import traceback
|
| 319 |
-
print("β
|
| 320 |
traceback.print_exc()
|
|
|
|
| 120 |
return q.upper()
|
| 121 |
return None
|
| 122 |
|
| 123 |
+
# ---------------- Helpers ----------------
|
| 124 |
def parse_freeform_query(text: str):
|
| 125 |
"""Supports 'plushies in uk' and 'uk plushies'."""
|
| 126 |
if not text:
|
| 127 |
return "", ""
|
| 128 |
text = text.strip().lower()
|
| 129 |
+
m = re.match(r"(.+?)\s+in\s+(.+)", text, flags=re.IGNORECASE)
|
| 130 |
if m:
|
| 131 |
return m.group(1).strip(), m.group(2).strip()
|
| 132 |
parts = text.split()
|
|
|
|
| 139 |
return text, ""
|
| 140 |
|
| 141 |
# ---------------- Semantic Match ----------------
|
| 142 |
+
def semantic_match(query, top_k=15):
|
| 143 |
if not query:
|
| 144 |
return {"category": None, "items": []}
|
| 145 |
|
|
|
|
| 181 |
|
| 182 |
# ---------------- Core logic ----------------
|
| 183 |
def query_inventory(query_text="", category="", country_name="", capacity=10, refresh=False):
|
| 184 |
+
print(f"π Query received: '{query_text}', category='{category}', country='{country_name}', cap={capacity}")
|
| 185 |
data, last_update = fetch_yata(force_refresh=refresh)
|
| 186 |
stocks = data.get("stocks", {})
|
| 187 |
rows = []
|
|
|
|
| 199 |
for code_raw, cdata in stocks.items():
|
| 200 |
code = code_raw.upper()
|
| 201 |
cname = COUNTRY_NAMES.get(code, code)
|
| 202 |
+
|
| 203 |
# Filter by requested country
|
| 204 |
if country_name:
|
| 205 |
if user_code:
|
|
|
|
| 207 |
continue
|
| 208 |
elif country_name.lower() not in cname.lower():
|
| 209 |
continue
|
| 210 |
+
|
| 211 |
update_ts = cdata.get("update")
|
| 212 |
update_str = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(update_ts)) if update_ts else "Unknown"
|
| 213 |
+
|
| 214 |
live_lookup = {i["name"]: i for i in cdata.get("stocks", [])}
|
| 215 |
+
|
| 216 |
# Merge known items (include 0-qty)
|
| 217 |
for iname, itype in ITEM_TO_TYPE.items():
|
| 218 |
itype = itype.lower()
|
| 219 |
item_data = live_lookup.get(iname, {"quantity": 0, "cost": 0})
|
| 220 |
qty = item_data.get("quantity", 0)
|
| 221 |
cost = item_data.get("cost", 0)
|
| 222 |
+
|
|
|
|
| 223 |
if item_term:
|
| 224 |
item_ok = (
|
| 225 |
item_term.lower() in iname.lower()
|
|
|
|
| 230 |
item_ok = category.lower() == itype
|
| 231 |
else:
|
| 232 |
item_ok = True
|
| 233 |
+
|
| 234 |
if item_ok:
|
| 235 |
rows.append({
|
| 236 |
"Country": cname,
|
|
|
|
| 256 |
def run_query(query_text, category, country, capacity, refresh):
|
| 257 |
return query_inventory(query_text, category, country, capacity, refresh)
|
| 258 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
# ---------------- Gradio UI ----------------
|
| 260 |
with gr.Blocks(title="π§³ Torn Inventory Viewer") as iface:
|
| 261 |
gr.Markdown("## π§³ Torn Inventory Viewer")
|
|
|
|
| 266 |
country_box = gr.Textbox(label="Country (optional, e.g. UK, Cayman, Japan)")
|
| 267 |
capacity_slider = gr.Number(label="Travel Capacity", value=10, minimum=5, maximum=88, precision=0)
|
| 268 |
refresh_check = gr.Checkbox(label="Force refresh (ignore cache)", value=False)
|
|
|
|
|
|
|
|
|
|
| 269 |
|
| 270 |
run_btn = gr.Button("π Search / Refresh")
|
| 271 |
result_df = gr.Dataframe(label="Results")
|
|
|
|
| 275 |
inputs=[query_box, category_drop, country_box, capacity_slider, refresh_check],
|
| 276 |
outputs=[result_df, meta_box])
|
| 277 |
|
| 278 |
+
# --- JS fix for persistent capacity ---
|
| 279 |
gr.HTML("""
|
| 280 |
<script>
|
| 281 |
window.addEventListener('DOMContentLoaded', () => {
|
|
|
|
| 282 |
const saved = localStorage.getItem('travel_capacity');
|
| 283 |
const capField = document.querySelector('input[type=number]');
|
| 284 |
if (saved && capField) {
|
| 285 |
capField.value = saved;
|
| 286 |
+
capField.dispatchEvent(new Event('input', { bubbles: true }));
|
|
|
|
| 287 |
}
|
|
|
|
|
|
|
| 288 |
const observer = new MutationObserver(() => {
|
| 289 |
const field = document.querySelector('input[type=number]');
|
| 290 |
if (field && !field.dataset.synced) {
|
|
|
|
| 300 |
""")
|
| 301 |
|
| 302 |
try:
|
| 303 |
+
print("β
App initialized successfully β launching UI...")
|
| 304 |
iface.launch()
|
| 305 |
except Exception as e:
|
| 306 |
import traceback
|
| 307 |
+
print("β Application failed to start:")
|
| 308 |
traceback.print_exc()
|