TrungLC commited on
Commit
981f465
·
verified ·
1 Parent(s): 15d076f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -8
app.py CHANGED
@@ -1,6 +1,8 @@
1
  import gradio as gr
2
  import pandas as pd
3
  import numpy as np
 
 
4
 
5
  def process(thuoc_file, xetnghiem_file):
6
  try:
@@ -21,7 +23,7 @@ def process(thuoc_file, xetnghiem_file):
21
  plt_low = plt_low[['Mã KCB', 'Tên XN', 'Kết quả XN', 'Ngày KQ']].rename(columns={'Mã KCB': 'makcb'})
22
 
23
  if plt_low.empty:
24
- return None, "Không có PLT < 100 trong file xetnghiem"
25
 
26
  # 2. Ghép theo mã KCB: chuẩn hóa sang str ở cả hai bên
27
  plt_low['makcb_str'] = plt_low['makcb'].astype(str).str.strip()
@@ -30,7 +32,7 @@ def process(thuoc_file, xetnghiem_file):
30
  thuoc_final = thuoc_df[thuoc_df['makcb_str'].isin(valid_patients)].copy()
31
 
32
  if thuoc_final.empty:
33
- return None, f"Không có bệnh nhân khớp giữa hai file.\nPLT < 100 có: {list(valid_patients)[:10]}...\nThuốc khớp: {thuoc_df['makcb_str'].isin(valid_patients).sum()}"
34
 
35
  # 3. Ghép kết quả cuối
36
  result = thuoc_final.merge(plt_low, left_on='makcb_str', right_on='makcb_str', suffixes=('', '_XN'), how='inner')
@@ -44,13 +46,17 @@ def process(thuoc_file, xetnghiem_file):
44
  columns_out = [col for col in columns_out if col in result.columns]
45
  result = result[columns_out]
46
 
47
- output_path = "/tmp/KETQUA_GHEP.xlsx"
48
- result.to_excel(output_path, index=False)
 
 
49
 
50
- return output_path, f"Thành công! Tìm thấy {result['makcb'].nunique()} bệnh nhân khớp, tổng {len(result)} dòng."
 
 
51
 
52
  except Exception as e:
53
- return None, f"Lỗi: {str(e)}"
54
 
55
  # GIAO DIỆN GRADIO
56
  with gr.Blocks(title="Ghép File Thuoc và Xetnghiem") as demo:
@@ -59,8 +65,8 @@ with gr.Blocks(title="Ghép File Thuoc và Xetnghiem") as demo:
59
  f1 = gr.File(label="thuoc.xlsx", file_types=[".xlsx"])
60
  f4 = gr.File(label="xetnghiem.xlsx", file_types=[".xlsx"])
61
  btn = gr.Button("Ghép File", variant="primary")
62
- out_file = gr.File(label="Tải File Kết Quả")
63
  out_text = gr.Textbox(label="Thông Báo", lines=5)
64
- btn.click(process, inputs=[f1, f4], outputs=[out_file, out_text])
65
 
66
  demo.launch()
 
1
  import gradio as gr
2
  import pandas as pd
3
  import numpy as np
4
+ import tempfile
5
+ import os
6
 
7
  def process(thuoc_file, xetnghiem_file):
8
  try:
 
23
  plt_low = plt_low[['Mã KCB', 'Tên XN', 'Kết quả XN', 'Ngày KQ']].rename(columns={'Mã KCB': 'makcb'})
24
 
25
  if plt_low.empty:
26
+ return None, "Không có PLT < 100 trong file xetnghiem", None
27
 
28
  # 2. Ghép theo mã KCB: chuẩn hóa sang str ở cả hai bên
29
  plt_low['makcb_str'] = plt_low['makcb'].astype(str).str.strip()
 
32
  thuoc_final = thuoc_df[thuoc_df['makcb_str'].isin(valid_patients)].copy()
33
 
34
  if thuoc_final.empty:
35
+ return None, f"Không có bệnh nhân khớp giữa hai file.\nPLT < 100 có: {list(valid_patients)[:10]}...\nThuốc khớp: {thuoc_df['makcb_str'].isin(valid_patients).sum()}", None
36
 
37
  # 3. Ghép kết quả cuối
38
  result = thuoc_final.merge(plt_low, left_on='makcb_str', right_on='makcb_str', suffixes=('', '_XN'), how='inner')
 
46
  columns_out = [col for col in columns_out if col in result.columns]
47
  result = result[columns_out]
48
 
49
+ # Lưu file tạm thời với tempfile để đảm bảo quyền viết/đọc trên HF
50
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".xlsx") as tmp_file:
51
+ result.to_excel(tmp_file.name, index=False)
52
+ output_path = tmp_file.name
53
 
54
+ message = f"Thành công! Tìm thấy {result['makcb'].nunique()} bệnh nhân khớp, tổng {len(result)} dòng.\nFile path: {output_path}"
55
+
56
+ return output_path, message, gr.DownloadButton(visible=True, label="Tải File Kết Quả", value=output_path)
57
 
58
  except Exception as e:
59
+ return None, f"Lỗi: {str(e)} (Kiểm tra file input có cột đúng không: makcb ở thuoc, Mã KCB/Tên XN/Kết quả XN ở xetnghiem)", None
60
 
61
  # GIAO DIỆN GRADIO
62
  with gr.Blocks(title="Ghép File Thuoc và Xetnghiem") as demo:
 
65
  f1 = gr.File(label="thuoc.xlsx", file_types=[".xlsx"])
66
  f4 = gr.File(label="xetnghiem.xlsx", file_types=[".xlsx"])
67
  btn = gr.Button("Ghép File", variant="primary")
68
+ out_download = gr.DownloadButton(visible=False) # Ban đầu ẩn, cập nhật sau
69
  out_text = gr.Textbox(label="Thông Báo", lines=5)
70
+ btn.click(process, inputs=[f1, f4], outputs=[out_download.value, out_text, out_download]) # Cập nhật button
71
 
72
  demo.launch()