Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
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 |
-
|
183 |
-
|
184 |
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
"cuda" if torch.cuda.is_available() else "cpu"
|
203 |
)
|
204 |
-
if
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
212 |
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
|
|
|
|
|
|
|
|
|
|
225 |
|
226 |
-
|
227 |
-
|
228 |
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
|
|
|
|
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 |
|