Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,80 +1,71 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import AutoTokenizer
|
| 4 |
-
from semviqa.
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
#
|
| 17 |
-
st.
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
}
|
| 25 |
-
.
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
tokens = inputs["input_ids"][0][start_idx : end_idx + 1]
|
| 73 |
-
evidence_result = tokenizer.decode(tokens, skip_special_tokens=True)
|
| 74 |
-
|
| 75 |
-
st.markdown("""
|
| 76 |
-
<div class='result-box'>
|
| 77 |
-
<h3>π Result</h3>
|
| 78 |
-
<p><strong>π Evidence:</strong> {}</p>
|
| 79 |
-
</div>
|
| 80 |
-
""".format(evidence_result), unsafe_allow_html=True)
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
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 |
+
|
| 9 |
+
# Load models with caching
|
| 10 |
+
@st.cache_resource()
|
| 11 |
+
def load_model(model_name, model_class):
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 13 |
+
model = model_class.from_pretrained(model_name)
|
| 14 |
+
return tokenizer, model
|
| 15 |
+
|
| 16 |
+
# UI Configuration
|
| 17 |
+
st.set_page_config(page_title="SemViQA Demo", layout="wide")
|
| 18 |
+
|
| 19 |
+
st.markdown("""
|
| 20 |
+
<style>
|
| 21 |
+
.big-title { font-size: 36px; font-weight: bold; color: #4A90E2; text-align: center; }
|
| 22 |
+
.sub-title { font-size: 20px; color: #666; text-align: center; }
|
| 23 |
+
.stButton>button { background-color: #4CAF50; color: white; font-size: 16px; width: 100%; border-radius: 8px; padding: 10px; }
|
| 24 |
+
.stTextArea textarea { font-size: 16px; }
|
| 25 |
+
.result-box { background-color: #f9f9f9; padding: 20px; border-radius: 10px; box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); }
|
| 26 |
+
</style>
|
| 27 |
+
""", unsafe_allow_html=True)
|
| 28 |
+
|
| 29 |
+
st.markdown("<p class='big-title'>π SemViQA: Vietnamese Fact-Checking System</p>", unsafe_allow_html=True)
|
| 30 |
+
st.markdown("<p class='sub-title'>Enter a claim and context to verify its accuracy</p>", unsafe_allow_html=True)
|
| 31 |
+
|
| 32 |
+
# Sidebar - Configuration Settings
|
| 33 |
+
with st.sidebar.expander("βοΈ Settings", expanded=False):
|
| 34 |
+
tfidf_threshold = st.slider("π§ TF-IDF Threshold", 0.0, 1.0, 0.5, 0.01)
|
| 35 |
+
length_ratio_threshold = st.slider("π Length Ratio Threshold", 0.1, 1.0, 0.5, 0.01)
|
| 36 |
+
qatc_model_name = st.selectbox("π€ QATC Model", ["xuandin/semviqa-qatc-vimrc-viwikifc"])
|
| 37 |
+
bc_model_name = st.selectbox("π·οΈ Binary Classification Model", ["xuandin/semviqa-bc"])
|
| 38 |
+
tc_model_name = st.selectbox("π Three-Class Model", ["xuandin/semviqa-tc"])
|
| 39 |
+
|
| 40 |
+
# Load selected models
|
| 41 |
+
tokenizer_qatc, model_qatc = load_model(qatc_model_name, QATCForQuestionAnswering)
|
| 42 |
+
tokenizer_bc, model_bc = load_model(bc_model_name, ClaimModelForClassification)
|
| 43 |
+
tokenizer_tc, model_tc = load_model(tc_model_name, ClaimModelForClassification)
|
| 44 |
+
|
| 45 |
+
# User Input Fields
|
| 46 |
+
claim = st.text_area("βοΈ Enter Claim", "Vietnam is a country in Southeast Asia.")
|
| 47 |
+
context = st.text_area("π Enter Context", "Vietnam is a country located in Southeast Asia, covering an area of over 331,000 kmΒ² with a population of more than 98 million people.")
|
| 48 |
+
|
| 49 |
+
if st.button("π Verify"):
|
| 50 |
+
# Extract evidence
|
| 51 |
+
evidence = extract_evidence_tfidf_qatc(
|
| 52 |
+
claim, context, model_qatc, tokenizer_qatc, "cuda" if torch.cuda.is_available() else "cpu",
|
| 53 |
+
confidence_threshold=tfidf_threshold, length_ratio_threshold=length_ratio_threshold
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Claim Classification
|
| 57 |
+
verdict = "NEI"
|
| 58 |
+
prob3class, pred_tc = classify_claim(claim, evidence, model_tc, tokenizer_tc, "cuda" if torch.cuda.is_available() else "cpu")
|
| 59 |
+
|
| 60 |
+
if pred_tc != 0:
|
| 61 |
+
prob2class, pred_bc = classify_claim(claim, evidence, model_bc, tokenizer_bc, "cuda" if torch.cuda.is_available() else "cpu")
|
| 62 |
+
verdict = "SUPPORTED" if pred_bc == 0 else "REFUTED" if prob2class > prob3class else ["NEI", "SUPPORTED", "REFUTED"][pred_tc]
|
| 63 |
+
|
| 64 |
+
# Display Results
|
| 65 |
+
st.markdown(f"""
|
| 66 |
+
<div class='result-box'>
|
| 67 |
+
<h3>π Result</h3>
|
| 68 |
+
<p><strong>π Evidence:</strong> {evidence}</p>
|
| 69 |
+
<p><strong>β
Verdict:</strong> {verdict}</p>
|
| 70 |
+
</div>
|
| 71 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|