xuandin commited on
Commit
270b4c0
·
verified ·
1 Parent(s): 7c117db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +169 -14
app.py CHANGED
@@ -10,6 +10,9 @@ import pandas as pd
10
  import os
11
  import psutil
12
  import gc
 
 
 
13
 
14
  # Set environment variables to optimize CPU performance
15
  os.environ["OMP_NUM_THREADS"] = str(psutil.cpu_count(logical=False))
@@ -80,6 +83,69 @@ def perform_verification(claim, context, model_qatc, tokenizer_qatc, model_tc, t
80
  "pred_bc": pred_bc
81
  }
82
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
 
84
  # Set page configuration
85
  st.set_page_config(
@@ -234,6 +300,34 @@ st.markdown("""
234
  background-color: var(--primary-color);
235
  color: white;
236
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
237
  </style>
238
  """, unsafe_allow_html=True)
239
 
@@ -319,7 +413,7 @@ with st.sidebar:
319
  os.environ["MKL_NUM_THREADS"] = str(num_threads)
320
 
321
  # Main content
322
- tabs = st.tabs(["🔍 Kiểm chứng", "📊 Lịch sử", "ℹ️ Thông tin"])
323
 
324
  tokenizer_qatc, model_qatc = load_model(qatc_model_name, QATCForQuestionAnswering, device=DEVICE)
325
  tokenizer_bc, model_bc = load_model(bc_model_name, ClaimModelForClassification, is_bc=True, device=DEVICE)
@@ -440,9 +534,22 @@ with tabs[0]:
440
  # --- Tab History ---
441
  with tabs[1]:
442
  st.markdown("### 📊 Lịch sử Kiểm chứng")
 
 
 
 
443
  if 'history' in st.session_state and st.session_state.history:
 
 
 
 
 
 
 
 
 
444
  # Download full history
445
- history_df = pd.DataFrame(st.session_state.history)
446
  st.download_button(
447
  "📥 Tải toàn bộ lịch sử",
448
  data=history_df.to_csv(index=False).encode('utf-8'),
@@ -450,21 +557,61 @@ with tabs[1]:
450
  mime="text/csv"
451
  )
452
 
453
- # Display history
454
- for idx, record in enumerate(reversed(st.session_state.history), 1):
455
- st.markdown(f"""
456
- <div class="result-box">
457
- <h4>Kiểm chứng #{idx}</h4>
458
- <p><strong>Câu khẳng định:</strong> {record['claim']}</p>
459
- <p><strong>Kết luận:</strong> {verdict_icons.get(record['verdict'], '')} {record['verdict']}</p>
460
- <p><strong>Thời gian:</strong> {record['total_time']:.2f} giây</p>
461
- </div>
462
- """, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
463
  else:
464
- st.info("Chưa có lịch sử kiểm chứng.")
465
 
466
  # --- Tab Info ---
467
- with tabs[2]:
468
  st.markdown("""
469
  <div class="result-box">
470
  <h3>ℹ️ Thông tin về SemViQA</h3>
@@ -491,5 +638,13 @@ with tabs[2]:
491
  <li><strong>REFUTED:</strong> Câu khẳng định bị bác bỏ bởi bằng chứng</li>
492
  <li><strong>NEI:</strong> Không đủ bằng chứng để kết luận</li>
493
  </ul>
 
 
 
 
 
 
 
 
494
  </div>
495
  """, unsafe_allow_html=True)
 
10
  import os
11
  import psutil
12
  import gc
13
+ import plotly.express as px
14
+ import plotly.graph_objects as go
15
+ from datetime import datetime
16
 
17
  # Set environment variables to optimize CPU performance
18
  os.environ["OMP_NUM_THREADS"] = str(psutil.cpu_count(logical=False))
 
83
  "pred_bc": pred_bc
84
  }
85
 
86
+ # Add new functions for analysis
87
+ def analyze_verdict_distribution(history):
88
+ if not history:
89
+ return None
90
+
91
+ df = pd.DataFrame(history)
92
+ verdict_counts = df['verdict'].value_counts()
93
+
94
+ fig = px.pie(
95
+ values=verdict_counts.values,
96
+ names=verdict_counts.index,
97
+ title='Phân bố Kết quả Kiểm chứng',
98
+ color_discrete_sequence=['#2ecc71', '#e74c3c', '#f1c40f']
99
+ )
100
+ return fig
101
+
102
+ def analyze_processing_time(history):
103
+ if not history:
104
+ return None
105
+
106
+ df = pd.DataFrame(history)
107
+ df['timestamp'] = pd.to_datetime(df['timestamp'])
108
+
109
+ fig = px.line(
110
+ df,
111
+ x='timestamp',
112
+ y=['evidence_time', 'verdict_time', 'total_time'],
113
+ title='Thời gian Xử lý theo Thời gian',
114
+ labels={'value': 'Thời gian (giây)', 'timestamp': 'Thời điểm'}
115
+ )
116
+ return fig
117
+
118
+ def generate_report(result):
119
+ report = f"""
120
+ BÁO CÁO KIỂM CHỨNG THÔNG TIN
121
+ Thời gian: {datetime.now().strftime('%d/%m/%Y %H:%M:%S')}
122
+
123
+ 1. THÔNG TIN CƠ BẢN
124
+ -------------------
125
+ Câu khẳng định: {result['claim']}
126
+ Kết luận: {result['verdict']}
127
+
128
+ 2. BẰNG CHỨNG
129
+ -------------
130
+ {result['evidence']}
131
+
132
+ 3. THỐNG KÊ THỜI GIAN
133
+ ---------------------
134
+ - Thời gian trích xuất bằng chứng: {result['evidence_time']:.2f} giây
135
+ - Thời gian phân loại: {result['verdict_time']:.2f} giây
136
+ - Tổng thời gian xử lý: {result['total_time']:.2f} giây
137
+
138
+ 4. CHI TIẾT KỸ THUẬT
139
+ -------------------
140
+ {result['details']}
141
+
142
+ 5. MÔ HÌNH SỬ DỤNG
143
+ ------------------
144
+ - QATC Model: {result['qatc_model']}
145
+ - Binary Classification Model: {result['bc_model']}
146
+ - 3-Class Classification Model: {result['tc_model']}
147
+ """
148
+ return report
149
 
150
  # Set page configuration
151
  st.set_page_config(
 
300
  background-color: var(--primary-color);
301
  color: white;
302
  }
303
+
304
+ /* Analysis box styling */
305
+ .analysis-box {
306
+ background-color: white;
307
+ border-radius: 12px;
308
+ padding: 1.5rem;
309
+ margin: 1rem 0;
310
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
311
+ }
312
+
313
+ /* Search box styling */
314
+ .search-box {
315
+ background-color: white;
316
+ border-radius: 8px;
317
+ padding: 1rem;
318
+ margin-bottom: 1rem;
319
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
320
+ }
321
+
322
+ /* Comparison box styling */
323
+ .comparison-box {
324
+ background-color: white;
325
+ border-radius: 12px;
326
+ padding: 1.5rem;
327
+ margin: 1rem 0;
328
+ box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
329
+ border-left: 4px solid var(--primary-color);
330
+ }
331
  </style>
332
  """, unsafe_allow_html=True)
333
 
 
413
  os.environ["MKL_NUM_THREADS"] = str(num_threads)
414
 
415
  # Main content
416
+ tabs = st.tabs(["🔍 Kiểm chứng", "📊 Lịch sử", "📈 Phân tích", "ℹ️ Thông tin"])
417
 
418
  tokenizer_qatc, model_qatc = load_model(qatc_model_name, QATCForQuestionAnswering, device=DEVICE)
419
  tokenizer_bc, model_bc = load_model(bc_model_name, ClaimModelForClassification, is_bc=True, device=DEVICE)
 
534
  # --- Tab History ---
535
  with tabs[1]:
536
  st.markdown("### 📊 Lịch sử Kiểm chứng")
537
+
538
+ # Add search functionality
539
+ search_query = st.text_input("🔍 Tìm kiếm trong lịch sử", "")
540
+
541
  if 'history' in st.session_state and st.session_state.history:
542
+ # Filter history based on search query
543
+ filtered_history = st.session_state.history
544
+ if search_query:
545
+ filtered_history = [
546
+ record for record in st.session_state.history
547
+ if search_query.lower() in record['claim'].lower() or
548
+ search_query.lower() in record['evidence'].lower()
549
+ ]
550
+
551
  # Download full history
552
+ history_df = pd.DataFrame(filtered_history)
553
  st.download_button(
554
  "📥 Tải toàn bộ lịch sử",
555
  data=history_df.to_csv(index=False).encode('utf-8'),
 
557
  mime="text/csv"
558
  )
559
 
560
+ # Display history with comparison option
561
+ for idx, record in enumerate(reversed(filtered_history), 1):
562
+ col1, col2 = st.columns([3, 1])
563
+
564
+ with col1:
565
+ st.markdown(f"""
566
+ <div class="result-box">
567
+ <h4>Kiểm chứng #{idx}</h4>
568
+ <p><strong>Câu khẳng định:</strong> {record['claim']}</p>
569
+ <p><strong>Kết luận:</strong> {verdict_icons.get(record['verdict'], '')} {record['verdict']}</p>
570
+ <p><strong>Thời gian:</strong> {record['total_time']:.2f} giây</p>
571
+ </div>
572
+ """, unsafe_allow_html=True)
573
+
574
+ with col2:
575
+ if st.button("🔄 So sánh", key=f"compare_{idx}"):
576
+ st.session_state.selected_for_comparison = record
577
+
578
+ # --- Tab Analysis ---
579
+ with tabs[2]:
580
+ st.markdown("### 📈 Phân tích Chi tiết")
581
+
582
+ if 'history' in st.session_state and st.session_state.history:
583
+ # Add timestamp to history records
584
+ for record in st.session_state.history:
585
+ if 'timestamp' not in record:
586
+ record['timestamp'] = datetime.now()
587
+
588
+ # Distribution analysis
589
+ st.markdown("#### 📊 Phân bố Kết quả")
590
+ verdict_fig = analyze_verdict_distribution(st.session_state.history)
591
+ if verdict_fig:
592
+ st.plotly_chart(verdict_fig, use_container_width=True)
593
+
594
+ # Processing time analysis
595
+ st.markdown("#### ⏱️ Phân tích Thời gian Xử lý")
596
+ time_fig = analyze_processing_time(st.session_state.history)
597
+ if time_fig:
598
+ st.plotly_chart(time_fig, use_container_width=True)
599
+
600
+ # Model performance analysis
601
+ st.markdown("#### 🧠 Phân tích Hiệu suất Mô hình")
602
+ model_stats = pd.DataFrame(st.session_state.history)
603
+ if not model_stats.empty:
604
+ st.markdown("##### Thống kê theo Mô hình")
605
+ model_performance = model_stats.groupby(['qatc_model', 'bc_model', 'tc_model']).agg({
606
+ 'total_time': ['mean', 'count'],
607
+ 'verdict': lambda x: (x == 'SUPPORTED').mean()
608
+ }).round(2)
609
+ st.dataframe(model_performance)
610
  else:
611
+ st.info("Chưa có dữ liệu để phân tích.")
612
 
613
  # --- Tab Info ---
614
+ with tabs[3]:
615
  st.markdown("""
616
  <div class="result-box">
617
  <h3>ℹ️ Thông tin về SemViQA</h3>
 
638
  <li><strong>REFUTED:</strong> Câu khẳng định bị bác bỏ bởi bằng chứng</li>
639
  <li><strong>NEI:</strong> Không đủ bằng chứng để kết luận</li>
640
  </ul>
641
+
642
+ <h4>🆕 Tính năng Mới</h4>
643
+ <ul>
644
+ <li><strong>Phân tích Chi tiết:</strong> Xem thống kê và biểu đồ về kết quả kiểm chứng</li>
645
+ <li><strong>Tìm kiếm Lịch sử:</strong> Dễ dàng tìm kiếm trong lịch sử kiểm chứng</li>
646
+ <li><strong>So sánh Kết quả:</strong> So sánh các kết quả kiểm chứng với nhau</li>
647
+ <li><strong>Báo cáo Chi tiết:</strong> Xuất báo cáo chi tiết về kết quả kiểm chứng</li>
648
+ </ul>
649
  </div>
650
  """, unsafe_allow_html=True)