TrungLC commited on
Commit
38a2a29
·
verified ·
1 Parent(s): 2b6193e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +218 -25
app.py CHANGED
@@ -103,7 +103,6 @@ def process_files(thuoc_file, xetnghiem_file, thuoc_plt_file, icd_plt_file):
103
  if row['Percent_Drop'] >= 30: return "Trầm trọng thêm do thuốc"
104
  else:
105
  if row['PLT_nadir'] < 100:
106
- # Thêm điều kiện mới: Nếu nadir sau start_date <=24 giờ, vẫn flag nghi ngờ
107
  if pd.notna(row['Ngày_PLT_nadir']) and pd.notna(row['start_date']) and (row['Ngày_PLT_nadir'] - row['start_date']) <= timedelta(hours=24):
108
  return "Nghi ngờ (thiếu baseline nhưng gần)"
109
  return "Nghi ngờ (thiếu baseline)"
@@ -154,7 +153,6 @@ def process_files(thuoc_file, xetnghiem_file, thuoc_plt_file, icd_plt_file):
154
  file5 = "5_Check_Baseline_Nadir_Full.xlsx"
155
  final_analysis.to_excel(file5, sheet_name='Sheet1', index=False)
156
 
157
- # Function export_realtime_reports
158
  def export_realtime_reports(final_report, df_xetnghiem):
159
  today = datetime.now()
160
  filerealtime = f"Danh_Sach_Can_Thiep_DLS_{today.year}.xlsx"
@@ -195,7 +193,7 @@ def process_files(thuoc_file, xetnghiem_file, thuoc_plt_file, icd_plt_file):
195
  nadir = row.get('PLT_nadir', 'N/A')
196
  ctcae_nadir = row['CTCAE_Nadir']
197
  nguy_hiem_nadir = row['Nguy_Hiem_Nadir']
198
- if "Nghi ngờ (thiếu baseline" in row['Signal']: # Bao gồm cả "thiếu baseline nhưng gần"
199
  return (f"CẢNH BÁO ADR: Tiểu cầu bệnh nhân thấp ({nadir} G/L - {ctcae_nadir} - {nguy_hiem_nadir}) khi dùng {thuoc} (thuốc đã được báo cáo làm giảm tiểu cầu). "
200
  f"Đề xuất theo dõi và làm thêm XN Công thức máu (PLT) trong những ngày tới để cân nhắc điều trị.")
201
  elif "Trầm trọng" in row['Signal'] or "Nghi ngờ" in row['Signal']:
@@ -240,33 +238,228 @@ def process_files(thuoc_file, xetnghiem_file, thuoc_plt_file, icd_plt_file):
240
 
241
  return output_text, file1, file2, file3, file4, file5, filerealtime
242
 
243
- with gr.Blocks(title="trigertool PLT!") as demo:
244
- gr.Markdown("# 💊 trigertool PLT")
245
- gr.HTML("<span style='color:#0077b6;font-weight:bold'>Được tạo bởi DSCKI. Nguyễn Thành Trung - Trung tâm y tế khu vực Bảo Thắng.</span>")
246
- with gr.Row():
247
- thuoc_upload = gr.File(label="thuoc.xlsx")
248
- icd_plt_upload = gr.File(label="ICD_PLT.xlsx")
249
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
250
  with gr.Row():
251
- xetnghiem_upload = gr.File(label="xetnghiem.xlsx")
252
- thuoc_plt_upload = gr.File(label="Thuoc_PLT.xlsx")
253
-
254
- run_button = gr.Button("✅ Kiểm tra",variant="primary")
255
-
256
- output_text = gr.Textbox(label="Kết quả xử lý", lines=15, max_lines=15, autoscroll=False) # Tùy chỉnh kích thước, no scroll
257
-
258
- with gr.Accordion("Tải xuống từng file"):
259
- file1_download = gr.File(label="1_Check_Thuoc_Nghi_Ngo.xlsx")
260
- file2_download = gr.File(label="2_Check_Loai_ICD.xlsx")
261
- file3_download = gr.File(label="3_Check_PLT_Duoi_100.xlsx")
262
- file4_download = gr.File(label="4_Check_Giao_Thoa_Thuoc_PLT.xlsx")
263
- file5_download = gr.File(label="5_Check_Baseline_Nadir_Full.xlsx")
264
- realtime_download = gr.File(label="Danh_Sach_Can_Thiep_DLS_2026.xlsx")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
265
 
266
  run_button.click(
267
  process_files,
268
  inputs=[thuoc_upload, xetnghiem_upload, thuoc_plt_upload, icd_plt_upload],
269
- outputs=[output_text, file1_download, file2_download, file3_download, file4_download, file5_download, realtime_download]
 
270
  )
271
 
272
  demo.launch()
 
103
  if row['Percent_Drop'] >= 30: return "Trầm trọng thêm do thuốc"
104
  else:
105
  if row['PLT_nadir'] < 100:
 
106
  if pd.notna(row['Ngày_PLT_nadir']) and pd.notna(row['start_date']) and (row['Ngày_PLT_nadir'] - row['start_date']) <= timedelta(hours=24):
107
  return "Nghi ngờ (thiếu baseline nhưng gần)"
108
  return "Nghi ngờ (thiếu baseline)"
 
153
  file5 = "5_Check_Baseline_Nadir_Full.xlsx"
154
  final_analysis.to_excel(file5, sheet_name='Sheet1', index=False)
155
 
 
156
  def export_realtime_reports(final_report, df_xetnghiem):
157
  today = datetime.now()
158
  filerealtime = f"Danh_Sach_Can_Thiep_DLS_{today.year}.xlsx"
 
193
  nadir = row.get('PLT_nadir', 'N/A')
194
  ctcae_nadir = row['CTCAE_Nadir']
195
  nguy_hiem_nadir = row['Nguy_Hiem_Nadir']
196
+ if "Nghi ngờ (thiếu baseline" in row['Signal']:
197
  return (f"CẢNH BÁO ADR: Tiểu cầu bệnh nhân thấp ({nadir} G/L - {ctcae_nadir} - {nguy_hiem_nadir}) khi dùng {thuoc} (thuốc đã được báo cáo làm giảm tiểu cầu). "
198
  f"Đề xuất theo dõi và làm thêm XN Công thức máu (PLT) trong những ngày tới để cân nhắc điều trị.")
199
  elif "Trầm trọng" in row['Signal'] or "Nghi ngờ" in row['Signal']:
 
238
 
239
  return output_text, file1, file2, file3, file4, file5, filerealtime
240
 
241
+
242
+ with gr.Blocks(title="triggertool PLT", css="""
243
+ /* ===== HEADER ===== */
244
+ #plt-header {
245
+ background: linear-gradient(135deg, #e65c00 0%, #d44c00 45%, #b33b00 100%);
246
+ border-radius: 16px; padding: 28px 40px; margin-bottom: 18px;
247
+ position: relative; overflow: hidden;
248
+ box-shadow: 0 8px 32px rgba(180,60,0,0.55), inset 0 1px 0 rgba(255,255,255,0.12);
249
+ display: flex; flex-direction: column; align-items: center; justify-content: center;
250
+ min-height: 130px; text-align: center;
251
+ }
252
+ #plt-header::before {
253
+ content: ''; position: absolute; inset: 0;
254
+ background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80'%3E%3Ccircle cx='40' cy='40' r='20' fill='none' stroke='rgba(255,255,255,0.03)' stroke-width='2'/%3E%3Cpath d='M30 40h20M40 30v20' stroke='rgba(255,255,255,0.04)' stroke-width='2'/%3E%3C/svg%3E");
255
+ pointer-events: none;
256
+ }
257
+ #plt-ecg { position:absolute; bottom:6px; left:0; right:0; width:100%; height:20px; opacity:0.12; }
258
+ #header-logo-left {
259
+ position: absolute; left: 20px; top: 50%; transform: translateY(-50%);
260
+ width: 90px; height: 90px; border-radius: 50%; object-fit: cover;
261
+ border: 2px solid rgba(255,255,255,0.35);
262
+ box-shadow: 0 4px 16px rgba(0,0,0,0.4), 0 0 20px rgba(255,160,60,0.3);
263
+ }
264
+ #logo-pulse-ring {
265
+ position:absolute; left:20px; top:50%; transform:translateY(-50%);
266
+ width:90px; height:90px; border-radius:50%;
267
+ border:2px solid rgba(255,200,80,0.5);
268
+ animation:ringPulse 2.5s ease-in-out infinite; pointer-events:none;
269
+ }
270
+ @keyframes ringPulse {
271
+ 0%,100%{box-shadow:0 0 0 0 rgba(255,180,60,0.3);opacity:1;}
272
+ 50%{box-shadow:0 0 0 10px rgba(255,180,60,0);opacity:0.6;}
273
+ }
274
+ #header-center { display:flex; flex-direction:column; align-items:center; gap:5px; position:relative; z-index:2; max-width:70%; }
275
+ #header-main-title {
276
+ color: white; font-size: 24px; font-weight: 800; letter-spacing: 0.5px;
277
+ text-shadow: 0 2px 8px rgba(0,0,0,0.5);
278
+ font-family: 'Segoe UI','Arial',sans-serif; margin: 0;
279
+ }
280
+ #header-separator { width: 60px; height: 2px; background: linear-gradient(to right,transparent,rgba(255,255,255,0.5),transparent); border-radius: 2px; }
281
+ #animated-credit {
282
+ font-size: 13px; font-weight: 600; color: #FFD700 !important;
283
+ min-height: 18px; letter-spacing: 0.3px;
284
+ font-family: 'Segoe UI', sans-serif;
285
+ text-shadow: 0 1px 6px rgba(0,0,0,0.6);
286
+ }
287
+ #animated-credit * { color: #FFD700 !important; }
288
+ .credit-char { display: inline-block; color: #FFD700 !important; }
289
+
290
+ /* ===== SECTION LABELS ===== */
291
+ .section-label {
292
+ font-size: 13px; font-weight: 700; color: #c84a00;
293
+ border-left: 3px solid #e65c00; padding-left: 8px; margin-bottom: 4px;
294
+ }
295
+
296
+ /* ===== UPLOAD CARDS ===== */
297
+ .upload-card {
298
+ background: #fff8f5; border: 1px solid #ffd0b0;
299
+ border-radius: 10px; padding: 8px 10px;
300
+ }
301
+
302
+ /* ===== RUN BUTTON ===== */
303
+ .run-btn { background: linear-gradient(135deg,#e65c00,#c84a00) !important; color:white !important; border-radius:10px !important; font-weight:700 !important; }
304
+
305
+ /* ===== RESULT BOX ===== */
306
+ #result-summary textarea {
307
+ background: #fff8f5 !important; border: 1px solid #ffd0b0 !important;
308
+ border-radius:10px !important; font-size:13px !important;
309
+ }
310
+
311
+ /* ===== ACCORDION ===== */
312
+ .download-accordion { border: 1px solid #ffd0b0 !important; border-radius: 12px !important; }
313
+ .download-accordion .label-wrap { background: #fff3ec !important; }
314
+ """) as demo:
315
+
316
+ # ===== HEADER HTML =====
317
+ gr.HTML("""
318
+ <div id="plt-header">
319
+ <div id="logo-pulse-ring"></div>
320
+ <div id="header-center">
321
+ <div id="header-main-title">🩸 triggertool PLT</div>
322
+ <div id="header-separator"></div>
323
+ <div id="animated-credit"></div>
324
+ </div>
325
+ <svg id="plt-ecg" viewBox="0 0 800 20" preserveAspectRatio="none" xmlns="http://www.w3.org/2000/svg">
326
+ <polyline points="0,10 80,10 100,2 110,18 120,10 200,10 220,2 230,18 240,10 320,10 340,2 350,18 360,10 440,10 460,2 470,18 480,10 560,10 580,2 590,18 600,10 680,10 700,2 710,18 720,10 800,10"
327
+ stroke="white" stroke-width="1.5" fill="none"/>
328
+ </svg>
329
+ </div>
330
+ <script>
331
+ (function() {
332
+ const text = "Được tạo bởi DSCKI. Nguyễn Thành Trung - Trung tâm y tế khu vực Bảo Thắng";
333
+ function getEl() { return document.getElementById('animated-credit'); }
334
+ const effects = [runTypewriter, runSlideIn, runExplodeAssemble, runFadeInLetters, runBounceIn, runWaveIn];
335
+ let cur = 0;
336
+ function runTypewriter(cb) {
337
+ const el=getEl(); if(!el)return; el.innerHTML='';
338
+ let i=0; const sp=document.createElement('span');
339
+ sp.style.cssText='border-right:2px solid #FFD700;color:#FFD700'; el.appendChild(sp);
340
+ const iv=setInterval(()=>{ sp.textContent=text.slice(0,++i);
341
+ if(i>=text.length){clearInterval(iv);setTimeout(()=>{sp.style.borderRight='none';cb&&setTimeout(cb,800);},300);}},40);
342
+ }
343
+ function runSlideIn(cb) {
344
+ const el=getEl(); if(!el)return;
345
+ el.innerHTML='<span id="ss" style="display:inline-block;color:#FFD700;transform:translateX(-120%);opacity:0;transition:transform 0.8s cubic-bezier(.22,1,.36,1),opacity 0.6s">'+text+'</span>';
346
+ requestAnimationFrame(()=>requestAnimationFrame(()=>{
347
+ const s=document.getElementById('ss'); if(s){s.style.transform='translateX(0)';s.style.opacity='1';}
348
+ cb&&setTimeout(cb,1200);
349
+ }));
350
+ }
351
+ function runExplodeAssemble(cb) {
352
+ const el=getEl(); if(!el)return; el.innerHTML='';
353
+ const spans=text.split('').map((ch,i)=>{
354
+ const s=document.createElement('span'); s.className='credit-char';
355
+ s.textContent=ch===' '?'\u00A0':ch; s.style.color='#FFD700';
356
+ const rx=(Math.random()-0.5)*500,ry=(Math.random()-0.5)*250;
357
+ s.style.cssText='color:#FFD700;opacity:0;transform:translate('+rx+'px,'+ry+'px) rotate('+((Math.random()-0.5)*360)+'deg);transition:transform '+(0.5+Math.random()*0.4)+'s cubic-bezier(.22,1,.36,1) '+(i*16)+'ms,opacity 0.3s ease '+(i*16)+'ms';
358
+ el.appendChild(s); return s;
359
+ });
360
+ requestAnimationFrame(()=>requestAnimationFrame(()=>{
361
+ spans.forEach(s=>{s.style.transform='translate(0,0) rotate(0deg)';s.style.opacity='1';});
362
+ cb&&setTimeout(cb,text.length*16+800);
363
+ }));
364
+ }
365
+ function runFadeInLetters(cb) {
366
+ const el=getEl(); if(!el)return; el.innerHTML='';
367
+ text.split('').forEach((ch,i)=>{
368
+ const s=document.createElement('span'); s.className='credit-char';
369
+ s.textContent=ch===' '?'\u00A0':ch;
370
+ s.style.cssText='color:#FFD700;opacity:0;transition:opacity 0.4s ease '+(i*28)+'ms'; el.appendChild(s);
371
+ });
372
+ requestAnimationFrame(()=>requestAnimationFrame(()=>{
373
+ el.querySelectorAll('.credit-char').forEach(s=>s.style.opacity='1');
374
+ cb&&setTimeout(cb,text.length*28+600);
375
+ }));
376
+ }
377
+ function runBounceIn(cb) {
378
+ const el=getEl(); if(!el)return; el.innerHTML='';
379
+ text.split('').forEach((ch,i)=>{
380
+ const s=document.createElement('span'); s.className='credit-char';
381
+ s.textContent=ch===' '?'\u00A0':ch;
382
+ s.style.cssText='color:#FFD700;display:inline-block;opacity:0;transform:translateY(-40px) scale(0.5);transition:transform 0.5s cubic-bezier(.34,1.56,.64,1) '+(i*22)+'ms,opacity 0.3s ease '+(i*22)+'ms';
383
+ el.appendChild(s);
384
+ });
385
+ requestAnimationFrame(()=>requestAnimationFrame(()=>{
386
+ el.querySelectorAll('.credit-char').forEach(s=>{s.style.transform='translateY(0) scale(1)';s.style.opacity='1';});
387
+ cb&&setTimeout(cb,text.length*22+700);
388
+ }));
389
+ }
390
+ function runWaveIn(cb) {
391
+ const el=getEl(); if(!el)return; el.innerHTML='';
392
+ if(!document.getElementById('wave-style')){
393
+ const st=document.createElement('style'); st.id='wave-style';
394
+ st.textContent='@keyframes wch{0%{opacity:0;transform:translateY(16px)}60%{transform:translateY(-5px)}100%{opacity:1;transform:translateY(0)}}';
395
+ document.head.appendChild(st);
396
+ }
397
+ text.split('').forEach((ch,i)=>{
398
+ const s=document.createElement('span'); s.className='credit-char';
399
+ s.textContent=ch===' '?'\u00A0':ch;
400
+ s.style.cssText='color:#FFD700;display:inline-block;animation:wch 0.6s ease '+(i*32)+'ms both'; el.appendChild(s);
401
+ });
402
+ cb&&setTimeout(cb,text.length*32+700);
403
+ }
404
+ function nextEffect() {
405
+ const fn=effects[cur%effects.length]; cur++;
406
+ fn(()=>{
407
+ setTimeout(()=>{
408
+ const el=getEl(); if(!el)return;
409
+ el.style.opacity='0'; el.style.transition='opacity 0.5s';
410
+ setTimeout(()=>{ el.style.opacity='1'; el.style.transition=''; nextEffect(); },600);
411
+ },2200);
412
+ });
413
+ }
414
+ function tryStart(){
415
+ if(document.getElementById('animated-credit')) setTimeout(nextEffect,500);
416
+ else setTimeout(tryStart,200);
417
+ }
418
+ tryStart();
419
+ })();
420
+ </script>
421
+ """)
422
+
423
+ # ===== MAIN LAYOUT =====
424
  with gr.Row():
425
+ # === CỘT TRÁI: Upload ===
426
+ with gr.Column(scale=1):
427
+ gr.HTML("<div class='section-label'>📂 Tải lên file dữ liệu</div>")
428
+ with gr.Group(elem_classes=["upload-card"]):
429
+ thuoc_upload = gr.File(label="💊 thuoc.xlsx")
430
+ icd_plt_upload = gr.File(label="🏷️ ICD_PLT.xlsx")
431
+ xetnghiem_upload= gr.File(label="🧪 xetnghiem.xlsx")
432
+ thuoc_plt_upload= gr.File(label="📋 Thuoc_PLT.xlsx")
433
+
434
+ run_button = gr.Button("🔍 Kiểm tra", variant="primary", size="lg", elem_classes=["run-btn"])
435
+
436
+ # === CỘT PHẢI: Kết quả ===
437
+ with gr.Column(scale=1):
438
+ gr.HTML("<div class='section-label'>📊 Kết quả xử lý</div>")
439
+ output_text = gr.Textbox(
440
+ label="Tổng hợp kết quả",
441
+ lines=10,
442
+ max_lines=12,
443
+ autoscroll=False,
444
+ elem_id="result-summary"
445
+ )
446
+
447
+ # ===== DOWNLOAD SECTION =====
448
+ with gr.Accordion("📥 Tải xuống file kết quả", open=False, elem_classes=["download-accordion"]):
449
+ with gr.Row():
450
+ file1_download = gr.File(label="1 · Thuốc nghi ngờ")
451
+ file2_download = gr.File(label="2 · Loại ICD đã loại trừ")
452
+ file3_download = gr.File(label="3 · PLT dưới 100")
453
+ with gr.Row():
454
+ file4_download = gr.File(label="4 · Giao thoa Thuốc × PLT")
455
+ file5_download = gr.File(label="5 · Baseline & Nadir đầy đủ")
456
+ realtime_download = gr.File(label="🏥 Danh sách can thiệp DLS")
457
 
458
  run_button.click(
459
  process_files,
460
  inputs=[thuoc_upload, xetnghiem_upload, thuoc_plt_upload, icd_plt_upload],
461
+ outputs=[output_text, file1_download, file2_download, file3_download,
462
+ file4_download, file5_download, realtime_download]
463
  )
464
 
465
  demo.launch()