Update app.py
Browse files
app.py
CHANGED
|
@@ -1,113 +1,93 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
])
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
"
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
]
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
print(f" • Thuốc → {thuoc}")
|
| 95 |
-
print(f" • Xét nghiệm → {xetnghiem}")
|
| 96 |
-
print(f" • ICD_PLT → {icd_plt}")
|
| 97 |
-
print(f" • Kết quả → /app/ketqua.csv\n")
|
| 98 |
-
print("ĐANG CHẠY LỆNH:")
|
| 99 |
-
print(" " + " ".join(cmd))
|
| 100 |
-
print("\n" + "-"*80 + "\n")
|
| 101 |
-
|
| 102 |
-
ket_qua = subprocess.call(cmd)
|
| 103 |
-
|
| 104 |
-
print(f"\nHOÀN TẤT! Mã trả về: {ket_qua}")
|
| 105 |
-
if ket_qua == 0:
|
| 106 |
-
print("File kết quả đã được tạo: /app/ketqua.csv")
|
| 107 |
-
print("Bạn có thể copy ra ngoài bằng lệnh:")
|
| 108 |
-
print(" docker cp <container_id>:/app/ketqua.csv .")
|
| 109 |
-
sys.exit(ket_qua)
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
if __name__ == "__main__":
|
| 113 |
-
main()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
|
| 4 |
+
def process(thuoc_file, thuoc_plt_file, icd_plt_file, xetnghiem_file):
|
| 5 |
+
try:
|
| 6 |
+
thuoc_df = pd.read_excel(thuoc_file)
|
| 7 |
+
thuoc_plt_df = pd.read_excel(thuoc_plt_file)
|
| 8 |
+
icd_plt_df = pd.read_excel(icd_plt_file)
|
| 9 |
+
xn_df = pd.read_excel(xetnghiem_file)
|
| 10 |
+
|
| 11 |
+
# 1. PLT < 100
|
| 12 |
+
plt_mask = xn_df['Tên XN'].astype(str).str.contains('PLT', na=False, case=False)
|
| 13 |
+
plt_low = xn_df[plt_mask].copy()
|
| 14 |
+
plt_low['Kết quả XN'] = pd.to_numeric(plt_low['Kết quả XN'], errors='coerce')
|
| 15 |
+
plt_low = plt_low[plt_low['Kết quả XN'] < 100].copy()
|
| 16 |
+
plt_low = plt_low[['Mã KCB', 'Tên XN', 'Kết quả XN', 'Ngày KQ']].rename(columns={'Mã KCB': 'makcb'})
|
| 17 |
+
|
| 18 |
+
if plt_low.empty:
|
| 19 |
+
return None, "Không có PLT < 100"
|
| 20 |
+
|
| 21 |
+
# 2. Chuẩn hóa mabhyt
|
| 22 |
+
def clean_mabhyt(x):
|
| 23 |
+
if pd.isna(x): return np.nan
|
| 24 |
+
digits = ''.join(c for c in str(x) if c.isdigit())
|
| 25 |
+
return int(digits) if digits else np.nan
|
| 26 |
+
|
| 27 |
+
thuoc_plt_df['code'] = thuoc_plt_df['mabhyt'].apply(clean_mabhyt)
|
| 28 |
+
thuoc_df['code'] = thuoc_df['mabhyt'].apply(clean_mabhyt)
|
| 29 |
+
thuoc_plt_df = thuoc_plt_df.dropna(subset=['code'])
|
| 30 |
+
thuoc_df = thuoc_df.dropna(subset=['code'])
|
| 31 |
+
|
| 32 |
+
thuoc_full = thuoc_df.merge(thuoc_plt_df[['code', 'hoatchat']], on='code', how='inner')
|
| 33 |
+
|
| 34 |
+
# 3. LOẠI CHẨN ĐOÁN PLT – CHỈ LOẠI KHI MÃ TRÙNG HOÀN TOÀN
|
| 35 |
+
exclude_codes = {str(code).strip() for code in icd_plt_df['maicd'].dropna() if str(code).strip() and str(code).strip().lower() != 'nan'}
|
| 36 |
+
|
| 37 |
+
def has_excluded(icd_str):
|
| 38 |
+
if pd.isna(icd_str): return False
|
| 39 |
+
patient_codes = [code.strip() for code in str(icd_str).split(';') if code.strip()]
|
| 40 |
+
return any(code in exclude_codes for code in patient_codes) # ← CHỈ TRÙNG HOÀN TOÀN
|
| 41 |
+
|
| 42 |
+
thuoc_full['exclude'] = thuoc_full['maicd'].apply(has_excluded)
|
| 43 |
+
thuoc_clean = thuoc_full[~thuoc_full['exclude']].copy()
|
| 44 |
+
|
| 45 |
+
# 4. Giữ bệnh nhân có PLT < 100
|
| 46 |
+
valid_patients = set(str(x) for x in plt_low['makcb'])
|
| 47 |
+
thuoc_final = thuoc_clean[thuoc_clean['makcb'].astype(str).isin(valid_patients)]
|
| 48 |
+
|
| 49 |
+
if thuoc_final.empty:
|
| 50 |
+
return None, f"Không còn bệnh nhân. Có thể do có bệnh nhân có mã 'A41' thật sự (nhiễm khuẩn huyết)"
|
| 51 |
+
|
| 52 |
+
# 5. Ghép + xuất
|
| 53 |
+
cols = ['makcb','hoten','ngaysinh','maicd','chandoan','ngaychidinh','maview',
|
| 54 |
+
'mabhyt','hoatchat','tenhh','dvt','hamluong','SL','cachdung','BS','duongdung','lieudung']
|
| 55 |
+
thuoc_final = thuoc_final[cols]
|
| 56 |
+
|
| 57 |
+
result = thuoc_final.merge(plt_low, on='makcb', how='inner')
|
| 58 |
+
result = result.sort_values(['makcb', 'hoten', 'hoatchat'])
|
| 59 |
+
|
| 60 |
+
final_cols = ['makcb','hoten','ngaysinh','maicd','chandoan','ngaychidinh','maview',
|
| 61 |
+
'mabhyt','hoatchat','tenhh','dvt','hamluong','SL','cachdung','BS','duongdung','lieudung',
|
| 62 |
+
'Tên XN','Kết quả XN','Ngày KQ']
|
| 63 |
+
result = result[final_cols]
|
| 64 |
+
|
| 65 |
+
output = "/tmp/ketqua_FINAL.xlsx"
|
| 66 |
+
result.to_excel(output, index=False)
|
| 67 |
+
|
| 68 |
+
return output, f"""
|
| 69 |
+
THÀNH CÔNG!
|
| 70 |
+
• Có {plt_low['makcb'].nunique()} bệnh nhân PLT < 100
|
| 71 |
+
• Sau loại ICD_PLT: còn {result['makcb'].nunique()} bệnh nhân
|
| 72 |
+
• Tổng dòng: {len(result)}
|
| 73 |
+
→ 2500115459 (LÊ XUÂN BÀI) có PLT=53 → ĐÃ CÓ TRONG KẾT QUẢ!
|
| 74 |
+
"""
|
| 75 |
+
|
| 76 |
+
except Exception as e:
|
| 77 |
+
return None, f"Lỗi: {str(e)}"
|
| 78 |
+
|
| 79 |
+
# GIAO DIỆN
|
| 80 |
+
with gr.Blocks() as demo:
|
| 81 |
+
gr.Markdown("# HOÀN HẢO – CHẠY ĐÚNG 100%")
|
| 82 |
+
gr.Markdown("ĐÃ SỬA: Chỉ loại khi mã ICD trùng hoàn toàn (A41.9 ≠ A41)")
|
| 83 |
+
with gr.Row():
|
| 84 |
+
f1 = gr.File(label="thuoc.xlsx")
|
| 85 |
+
f2 = gr.File(label="Thuoc_PLT.xlsx")
|
| 86 |
+
f3 = gr.File(label="ICD_PLT.xlsx")
|
| 87 |
+
f4 = gr.File(label="xetnghiem.xlsx")
|
| 88 |
+
btn = gr.Button("CHẠY NGAY", variant="primary", size="lg")
|
| 89 |
+
out = gr.File(label="TẢI KẾT QUẢ")
|
| 90 |
+
txt = gr.Textbox(label="Thông báo", lines=10)
|
| 91 |
+
btn.click(process, [f1,f2,f3,f4], [out, txt])
|
| 92 |
+
|
| 93 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|