KKingzor commited on
Commit
af9d426
·
verified ·
1 Parent(s): c43732d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from paddleocr import PaddleOCR
3
+ import numpy as np
4
+ import pandas as pd
5
+ from PIL import Image
6
+
7
+ # 建立 PaddleOCR 實例
8
+ ocr = PaddleOCR(use_angle_cls=True, lang='ch')
9
+
10
+ # 權限密碼
11
+ ADMIN_PASSWORD = "admin123"
12
+
13
+ # 儲存 DataFrame
14
+ global_df = pd.DataFrame(columns=["門市", "日期", "時段", "人員"])
15
+
16
+ # 工時換算(可依需求更改)
17
+ TIME_SLOTS = {
18
+ "早(07-15)": 8,
19
+ "早(06-14)": 8,
20
+ "晚(15-23)": 8,
21
+ "晚(18-23)": 5,
22
+ "夜(23-07)": 8,
23
+ "晚(17-25)": 8,
24
+ "早(08-18)": 10,
25
+ "晚(18-23)": 5
26
+ }
27
+
28
+ def extract_info_from_text(text):
29
+ """
30
+ 模擬從 OCR 文本解析結構化資料,可依實際格式進階開發
31
+ """
32
+ import re
33
+ rows = []
34
+ for line in text.splitlines():
35
+ match = re.findall(r"(安學|天期|永華東|鎮山|頂美)\s*(\d+月\d+日)\s*(早.*?|晚.*?|夜.*?)\s*([^\s]+)", line)
36
+ for m in match:
37
+ rows.append({"門市": m[0], "日期": m[1], "時段": m[2], "人員": m[3]})
38
+ return pd.DataFrame(rows)
39
+
40
+ def admin_upload(pwd, image):
41
+ if pwd != ADMIN_PASSWORD:
42
+ return "❌ 密碼錯誤", None
43
+
44
+ img_np = np.array(image)
45
+ result = ocr.ocr(img_np, cls=True)
46
+ full_text = "\n".join([line[1][0] for line in result[0]])
47
+
48
+ df = extract_info_from_text(full_text)
49
+
50
+ global global_df
51
+ global_df = pd.concat([global_df, df], ignore_index=True)
52
+
53
+ return "✅ OCR 辨識完成", df
54
+
55
+ def query_by_staff(staff_name):
56
+ df = global_df[global_df["人員"] == staff_name]
57
+ if df.empty:
58
+ return f"未找到 {staff_name} 的值班紀錄", None
59
+ return "", df
60
+
61
+ def calc_total_hours(staff_name):
62
+ df = global_df[global_df["人員"] == staff_name]
63
+ total = 0
64
+ for _, row in df.iterrows():
65
+ time_label = row["時段"]
66
+ for key, val in TIME_SLOTS.items():
67
+ if key in time_label:
68
+ total += val
69
+ return f"{staff_name} 一週總值班時數為:{total} 小時"
70
+
71
+ # 建立 Gradio 介面
72
+ with gr.Blocks() as demo:
73
+ gr.Markdown("## 📋 排班表 OCR 辨識系統")
74
+
75
+ with gr.Tab("🔐 管理者上傳 (admin123)"):
76
+ admin_pwd = gr.Textbox(label="輸入管理者密碼", type="password")
77
+ admin_img = gr.Image(type="pil", label="上傳排班表圖片")
78
+ admin_btn = gr.Button("辨識並儲存")
79
+ admin_status = gr.Textbox(label="狀態")
80
+ admin_df = gr.Dataframe()
81
+
82
+ admin_btn.click(fn=admin_upload,
83
+ inputs=[admin_pwd, admin_img],
84
+ outputs=[admin_status, admin_df])
85
+
86
+ with gr.Tab("🔍 查詢值班紀錄"):
87
+ staff_input = gr.Textbox(label="輸入人員姓名")
88
+ staff_btn = gr.Button("查詢每日紀錄")
89
+ staff_status = gr.Textbox(label="狀態")
90
+ staff_result = gr.Dataframe()
91
+
92
+ staff_btn.click(fn=query_by_staff,
93
+ inputs=staff_input,
94
+ outputs=[staff_status, staff_result])
95
+
96
+ with gr.Tab("📊 整週總時數"):
97
+ staff_total = gr.Textbox(label="輸入人員姓名")
98
+ total_btn = gr.Button("計算總時數")
99
+ total_output = gr.Textbox(label="總時數")
100
+
101
+ total_btn.click(fn=calc_total_hours,
102
+ inputs=staff_total,
103
+ outputs=total_output)
104
+
105
+ demo.launch()