Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,7 +20,7 @@ except ImportError as e:
|
|
| 20 |
RAW_ACCESS_TOKEN = "HadCmCBmnZtI4LjC1Ad2rBwGZX7x1jZu4A0DXCX+LIjt3SpQq0vVz1kh8U74GB2XPbRdgwfqvFcVRR+i0NodOZ2P6hPC5ZV8EuxPOt+WnAvTJ29P5UBoENQCVAxu/AuRP2xgYKNYOg5voggl73mfkgdB04t89/1O/w1cDnyilFU="
|
| 21 |
RAW_SECRET = "ae66c98db5eca3e75aff2ff28ef62f25"
|
| 22 |
|
| 23 |
-
#
|
| 24 |
WEB_ORDER_URL = "https://schillercfk-ssys.hf.space/?openExternalBrowser=1"
|
| 25 |
|
| 26 |
LINE_CHANNEL_ACCESS_TOKEN = RAW_ACCESS_TOKEN.replace(" ", "").strip()
|
|
@@ -65,7 +65,6 @@ def handle_message(event):
|
|
| 65 |
user_msg = event.message.text.strip()
|
| 66 |
user_id = event.source.user_id
|
| 67 |
|
| 68 |
-
# 如果客人輸入特定的文字點餐格式
|
| 69 |
if "我要訂" in user_msg:
|
| 70 |
try:
|
| 71 |
parts = user_msg.split()
|
|
@@ -93,7 +92,6 @@ def handle_message(event):
|
|
| 93 |
reply_text = "❌ 點餐格式錯誤。\n請確認格式為:『我要訂 招牌咖啡 1』"
|
| 94 |
|
| 95 |
else:
|
| 96 |
-
# 預設回覆:提供網頁點餐連結與文字點餐說明
|
| 97 |
reply_text = f"歡迎光臨!☕\n\n📱 【網頁快速點餐】\n請點擊下方連結,系統將為您開啟點餐網頁:\n{WEB_ORDER_URL}\n\n💬 【文字點餐】\n您也可以直接輸入例如:『我要訂 拿鐵咖啡 2』 (注意中間要有空格喔!)"
|
| 98 |
|
| 99 |
try:
|
|
@@ -102,7 +100,7 @@ def handle_message(event):
|
|
| 102 |
print(f"回覆失敗: {e}")
|
| 103 |
|
| 104 |
# ----------------------------
|
| 105 |
-
# 💻 網頁後台邏輯
|
| 106 |
# ----------------------------
|
| 107 |
def get_orders_df():
|
| 108 |
if not orders_db:
|
|
@@ -116,13 +114,10 @@ def get_revenue():
|
|
| 116 |
return f"### 💰 目前總營收:NT$ {total}"
|
| 117 |
|
| 118 |
def refresh_data():
|
| 119 |
-
"""獨立的重新整理函式,避免使用 lambda 導致解析失敗"""
|
| 120 |
return get_orders_df(), get_revenue()
|
| 121 |
|
| 122 |
-
# 取消 *args,改用明確的 7 個參數,徹底解決 Gradio 參數解析失敗導致的當機 (Silent Error)
|
| 123 |
def add_order(customer_name, notes, q0, q1, q2, q3, q4):
|
| 124 |
try:
|
| 125 |
-
# 安全地轉換姓名與備註 (避免 NoneType 錯誤)
|
| 126 |
customer_name = str(customer_name) if customer_name else ""
|
| 127 |
notes = str(notes) if notes else ""
|
| 128 |
quantities = [q0, q1, q2, q3, q4]
|
|
@@ -137,7 +132,6 @@ def add_order(customer_name, notes, q0, q1, q2, q3, q4):
|
|
| 137 |
menu_items = list(MENU.items())
|
| 138 |
|
| 139 |
for i, qty in enumerate(quantities):
|
| 140 |
-
# 安全地轉換數量,任何奇怪的輸入都當作 0
|
| 141 |
try:
|
| 142 |
qty = int(qty) if qty is not None else 0
|
| 143 |
except Exception:
|
|
@@ -161,8 +155,7 @@ def add_order(customer_name, notes, q0, q1, q2, q3, q4):
|
|
| 161 |
return msg, get_orders_df(), get_revenue()
|
| 162 |
|
| 163 |
except Exception as e:
|
| 164 |
-
|
| 165 |
-
return f"❌ 系統發生異常,請截圖給開發人員:{str(e)}", get_orders_df(), get_revenue()
|
| 166 |
|
| 167 |
def clear_orders():
|
| 168 |
orders_db.clear()
|
|
@@ -228,7 +221,8 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統", css=cus
|
|
| 228 |
for item, price in MENU.items():
|
| 229 |
with gr.Row(elem_classes="menu-row"):
|
| 230 |
gr.Markdown(f"**{item}** <br><span style='color: gray;'>NT$ {price}</span>")
|
| 231 |
-
|
|
|
|
| 232 |
quantity_components.append(qty)
|
| 233 |
|
| 234 |
notes = gr.Textbox(label="備註", placeholder="例如:少冰微糖...")
|
|
@@ -253,7 +247,8 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統", css=cus
|
|
| 253 |
gr.Markdown("### ☁️ 雲端備份")
|
| 254 |
github_btn = gr.Button("🔄 備份至 GitHub", variant="primary")
|
| 255 |
|
| 256 |
-
|
|
|
|
| 257 |
github_log_output = gr.Textbox(label="執行日誌", interactive=False, lines=4, elem_classes="github-log")
|
| 258 |
clear_btn = gr.Button("🗑️ 清空所有訂單", variant="stop")
|
| 259 |
|
|
@@ -269,7 +264,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="簡易訂單管理系統", css=cus
|
|
| 269 |
# ----------------------------
|
| 270 |
# 🚀 啟動伺服器
|
| 271 |
# ----------------------------
|
| 272 |
-
# 加上 .queue() 確保 Hugging Face Spaces 的 WebSocket 長連線不會被阻擋
|
| 273 |
app = gr.mount_gradio_app(custom_api, demo_ui.queue(), path="/")
|
| 274 |
|
| 275 |
if __name__ == "__main__":
|
|
|
|
| 20 |
RAW_ACCESS_TOKEN = "HadCmCBmnZtI4LjC1Ad2rBwGZX7x1jZu4A0DXCX+LIjt3SpQq0vVz1kh8U74GB2XPbRdgwfqvFcVRR+i0NodOZ2P6hPC5ZV8EuxPOt+WnAvTJ29P5UBoENQCVAxu/AuRP2xgYKNYOg5voggl73mfkgdB04t89/1O/w1cDnyilFU="
|
| 21 |
RAW_SECRET = "ae66c98db5eca3e75aff2ff28ef62f25"
|
| 22 |
|
| 23 |
+
# 強制 LINE 使用 Safari/Chrome 外部瀏覽器開啟
|
| 24 |
WEB_ORDER_URL = "https://schillercfk-ssys.hf.space/?openExternalBrowser=1"
|
| 25 |
|
| 26 |
LINE_CHANNEL_ACCESS_TOKEN = RAW_ACCESS_TOKEN.replace(" ", "").strip()
|
|
|
|
| 65 |
user_msg = event.message.text.strip()
|
| 66 |
user_id = event.source.user_id
|
| 67 |
|
|
|
|
| 68 |
if "我要訂" in user_msg:
|
| 69 |
try:
|
| 70 |
parts = user_msg.split()
|
|
|
|
| 92 |
reply_text = "❌ 點餐格式錯誤。\n請確認格式為:『我要訂 招牌咖啡 1』"
|
| 93 |
|
| 94 |
else:
|
|
|
|
| 95 |
reply_text = f"歡迎光臨!☕\n\n📱 【網頁快速點餐】\n請點擊下方連結,系統將為您開啟點餐網頁:\n{WEB_ORDER_URL}\n\n💬 【文字點餐】\n您也可以直接輸入例如:『我要訂 拿鐵咖啡 2』 (注意中間要有空格喔!)"
|
| 96 |
|
| 97 |
try:
|
|
|
|
| 100 |
print(f"回覆失敗: {e}")
|
| 101 |
|
| 102 |
# ----------------------------
|
| 103 |
+
# 💻 網頁後台邏輯
|
| 104 |
# ----------------------------
|
| 105 |
def get_orders_df():
|
| 106 |
if not orders_db:
|
|
|
|
| 114 |
return f"### 💰 目前總營收:NT$ {total}"
|
| 115 |
|
| 116 |
def refresh_data():
|
|
|
|
| 117 |
return get_orders_df(), get_revenue()
|
| 118 |
|
|
|
|
| 119 |
def add_order(customer_name, notes, q0, q1, q2, q3, q4):
|
| 120 |
try:
|
|
|
|
| 121 |
customer_name = str(customer_name) if customer_name else ""
|
| 122 |
notes = str(notes) if notes else ""
|
| 123 |
quantities = [q0, q1, q2, q3, q4]
|
|
|
|
| 132 |
menu_items = list(MENU.items())
|
| 133 |
|
| 134 |
for i, qty in enumerate(quantities):
|
|
|
|
| 135 |
try:
|
| 136 |
qty = int(qty) if qty is not None else 0
|
| 137 |
except Exception:
|
|
|
|
| 155 |
return msg, get_orders_df(), get_revenue()
|
| 156 |
|
| 157 |
except Exception as e:
|
| 158 |
+
return f"❌ 系統發生異常:{str(e)}", get_orders_df(), get_revenue()
|
|
|
|
| 159 |
|
| 160 |
def clear_orders():
|
| 161 |
orders_db.clear()
|
|
|
|
| 221 |
for item, price in MENU.items():
|
| 222 |
with gr.Row(elem_classes="menu-row"):
|
| 223 |
gr.Markdown(f"**{item}** <br><span style='color: gray;'>NT$ {price}</span>")
|
| 224 |
+
# 移除舊版會當機的 precision=0,改用 step=1
|
| 225 |
+
qty = gr.Number(label="數量", value=0, step=1, minimum=0)
|
| 226 |
quantity_components.append(qty)
|
| 227 |
|
| 228 |
notes = gr.Textbox(label="備註", placeholder="例如:少冰微糖...")
|
|
|
|
| 247 |
gr.Markdown("### ☁️ 雲端備份")
|
| 248 |
github_btn = gr.Button("🔄 備份至 GitHub", variant="primary")
|
| 249 |
|
| 250 |
+
# 移除了舊版會衝突的 wrap=True 參數
|
| 251 |
+
orders_table = gr.Dataframe(value=get_orders_df(), headers=["時間", "顧客姓名", "商品", "單價", "數量", "總價", "備註"], interactive=False)
|
| 252 |
github_log_output = gr.Textbox(label="執行日誌", interactive=False, lines=4, elem_classes="github-log")
|
| 253 |
clear_btn = gr.Button("🗑️ 清空所有訂單", variant="stop")
|
| 254 |
|
|
|
|
| 264 |
# ----------------------------
|
| 265 |
# 🚀 啟動伺服器
|
| 266 |
# ----------------------------
|
|
|
|
| 267 |
app = gr.mount_gradio_app(custom_api, demo_ui.queue(), path="/")
|
| 268 |
|
| 269 |
if __name__ == "__main__":
|