xuandin commited on
Commit
4fdfda4
·
verified ·
1 Parent(s): 4d37b8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -55
app.py CHANGED
@@ -5,7 +5,7 @@ from semviqa.ser.qatc_model import QATCForQuestionAnswering
5
  from semviqa.tvc.model import ClaimModelForClassification
6
  from semviqa.ser.ser_eval import extract_evidence_tfidf_qatc
7
  from semviqa.tvc.tvc_eval import classify_claim
8
- import io
9
 
10
  # Load models with caching
11
  @st.cache_resource()
@@ -179,66 +179,92 @@ with st.container():
179
  st.markdown("<h3>Verification Result</h3>", unsafe_allow_html=True)
180
  if verify_button:
181
  # Placeholder for displaying result/loading
182
- result_placeholder = st.empty()
183
- result_placeholder.markdown("<em>Verifying...</em>")
184
 
185
- with torch.no_grad():
186
- # Extract evidence and classify the claim
187
- evidence = extract_evidence_tfidf_qatc(
188
- claim, context, model_qatc, tokenizer_qatc,
189
- "cuda" if torch.cuda.is_available() else "cpu",
190
- confidence_threshold=tfidf_threshold,
191
- length_ratio_threshold=length_ratio_threshold
192
- )
193
- verdict = "NEI"
194
- details = ""
195
- prob3class, pred_tc = classify_claim(
196
- claim, evidence, model_tc, tokenizer_tc,
197
- "cuda" if torch.cuda.is_available() else "cpu"
198
- )
199
- if pred_tc != 0:
200
- prob2class, pred_bc = classify_claim(
201
- claim, evidence, model_bc, tokenizer_bc,
 
 
 
 
 
 
 
 
202
  "cuda" if torch.cuda.is_available() else "cpu"
203
  )
204
- if pred_bc == 0:
205
- verdict = "SUPPORTED"
206
- elif prob2class > prob3class:
207
- verdict = "REFUTED"
208
- else:
209
- verdict = ["NEI", "SUPPORTED", "REFUTED"][pred_tc]
210
- if show_details:
211
- details = f"<p><strong>3-Class Probability:</strong> {prob3class.item():.2f} - <strong>2-Class Probability:</strong> {prob2class.item():.2f}</p>"
 
 
 
 
 
 
 
 
 
 
 
212
 
213
- # Store verification history and the latest result
214
- st.session_state.history.append({
215
- "claim": claim,
216
- "evidence": evidence,
217
- "verdict": verdict
218
- })
219
- st.session_state.latest_result = {
220
- "claim": claim,
221
- "evidence": evidence,
222
- "verdict": verdict,
223
- "details": details
224
- }
 
 
 
 
 
225
 
226
- if torch.cuda.is_available():
227
- torch.cuda.empty_cache()
228
 
229
- # Display the result after verification
230
- res = st.session_state.latest_result
231
- result_placeholder.markdown(f"""
232
- <div class='result-box'>
233
- <p><strong>Claim:</strong> {res['claim']}</p>
234
- <p><strong>Evidence:</strong> {res['evidence']}</p>
235
- <p class='verdict'><span class='verdict-icon'>{verdict_icons.get(res['verdict'], '')}</span>{res['verdict']}</p>
236
- {res['details']}
237
- </div>
238
- """, unsafe_allow_html=True)
239
- # Download Verification Result Feature
240
- result_text = f"Claim: {res['claim']}\nEvidence: {res['evidence']}\nVerdict: {res['verdict']}\nDetails: {res['details']}"
241
- st.download_button("Download Result", data=result_text, file_name="verification_result.txt", mime="text/plain")
 
 
242
  else:
243
  st.info("No verification result yet.")
244
 
 
5
  from semviqa.tvc.model import ClaimModelForClassification
6
  from semviqa.ser.ser_eval import extract_evidence_tfidf_qatc
7
  from semviqa.tvc.tvc_eval import classify_claim
8
+ import time # Thêm thư viện time để đo thời gian inference
9
 
10
  # Load models with caching
11
  @st.cache_resource()
 
179
  st.markdown("<h3>Verification Result</h3>", unsafe_allow_html=True)
180
  if verify_button:
181
  # Placeholder for displaying result/loading
182
+ with st.spinner("Verifying..."): # Thêm spinner khi đang xử lý
183
+ start_time = time.time() # Bắt đầu đo thời gian inference
184
 
185
+ with torch.no_grad():
186
+ # Extract evidence
187
+ evidence_start_time = time.time()
188
+ evidence = extract_evidence_tfidf_qatc(
189
+ claim, context, model_qatc, tokenizer_qatc,
190
+ "cuda" if torch.cuda.is_available() else "cpu",
191
+ confidence_threshold=tfidf_threshold,
192
+ length_ratio_threshold=length_ratio_threshold
193
+ )
194
+ evidence_time = time.time() - evidence_start_time
195
+
196
+ # Hiển thị evidence trước
197
+ st.markdown(f"""
198
+ <div class='result-box'>
199
+ <p><strong>Evidence:</strong> {evidence}</p>
200
+ <p><strong>Evidence Inference Time:</strong> {evidence_time:.2f} seconds</p>
201
+ </div>
202
+ """, unsafe_allow_html=True)
203
+
204
+ # Classify the claim
205
+ verdict_start_time = time.time()
206
+ verdict = "NEI"
207
+ details = ""
208
+ prob3class, pred_tc = classify_claim(
209
+ claim, evidence, model_tc, tokenizer_tc,
210
  "cuda" if torch.cuda.is_available() else "cpu"
211
  )
212
+ if pred_tc != 0:
213
+ prob2class, pred_bc = classify_claim(
214
+ claim, evidence, model_bc, tokenizer_bc,
215
+ "cuda" if torch.cuda.is_available() else "cpu"
216
+ )
217
+ if pred_bc == 0:
218
+ verdict = "SUPPORTED"
219
+ elif prob2class > prob3class:
220
+ verdict = "REFUTED"
221
+ else:
222
+ verdict = ["NEI", "SUPPORTED", "REFUTED"][pred_tc]
223
+ if show_details:
224
+ details = f"""
225
+ <p><strong>3-Class Probability:</strong> {prob3class.item():.2f}</p>
226
+ <p><strong>3-Class Predicted Label:</strong> {['NEI', 'SUPPORTED', 'REFUTED'][pred_tc]}</p>
227
+ <p><strong>2-Class Probability:</strong> {prob2class.item():.2f}</p>
228
+ <p><strong>2-Class Predicted Label:</strong> {['SUPPORTED', 'REFUTED'][pred_bc]}</p>
229
+ """
230
+ verdict_time = time.time() - verdict_start_time
231
 
232
+ # Store verification history and the latest result
233
+ st.session_state.history.append({
234
+ "claim": claim,
235
+ "evidence": evidence,
236
+ "verdict": verdict,
237
+ "evidence_time": evidence_time,
238
+ "verdict_time": verdict_time,
239
+ "details": details
240
+ })
241
+ st.session_state.latest_result = {
242
+ "claim": claim,
243
+ "evidence": evidence,
244
+ "verdict": verdict,
245
+ "evidence_time": evidence_time,
246
+ "verdict_time": verdict_time,
247
+ "details": details
248
+ }
249
 
250
+ if torch.cuda.is_available():
251
+ torch.cuda.empty_cache()
252
 
253
+ # Display the result after verification
254
+ res = st.session_state.latest_result
255
+ st.markdown(f"""
256
+ <div class='result-box'>
257
+ <p><strong>Claim:</strong> {res['claim']}</p>
258
+ <p><strong>Evidence:</strong> {res['evidence']}</p>
259
+ <p><strong>Evidence Inference Time:</strong> {res['evidence_time']:.2f} seconds</p>
260
+ <p><strong>Verdict Inference Time:</strong> {res['verdict_time']:.2f} seconds</p>
261
+ <p class='verdict'><span class='verdict-icon'>{verdict_icons.get(res['verdict'], '')}</span>{res['verdict']}</p>
262
+ {res['details']}
263
+ </div>
264
+ """, unsafe_allow_html=True)
265
+ # Download Verification Result Feature
266
+ result_text = f"Claim: {res['claim']}\nEvidence: {res['evidence']}\nVerdict: {res['verdict']}\nDetails: {res['details']}"
267
+ st.download_button("Download Result", data=result_text, file_name="verification_result.txt", mime="text/plain")
268
  else:
269
  st.info("No verification result yet.")
270