Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +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
|
@@ -18,80 +19,104 @@ def load_model(model_name, model_class, is_bc=False):
|
|
18 |
# Set up page configuration
|
19 |
st.set_page_config(page_title="SemViQA Demo", layout="wide")
|
20 |
|
21 |
-
# Custom CSS
|
22 |
st.markdown("""
|
23 |
<style>
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
}
|
29 |
.main-container {
|
30 |
-
|
|
|
31 |
overflow-y: auto;
|
32 |
padding: 20px;
|
33 |
}
|
34 |
-
.
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
.sub-title {
|
42 |
-
font-size: 20px;
|
43 |
-
color: #666;
|
44 |
-
text-align: center;
|
45 |
-
margin-bottom: 20px;
|
46 |
-
}
|
47 |
-
.stButton>button {
|
48 |
-
background-color: #4CAF50;
|
49 |
-
color: white;
|
50 |
-
font-size: 16px;
|
51 |
-
width: 100%;
|
52 |
-
border-radius: 8px;
|
53 |
-
padding: 10px;
|
54 |
}
|
55 |
-
.stTextArea textarea {
|
56 |
font-size: 16px;
|
57 |
min-height: 120px;
|
58 |
}
|
59 |
-
.result-box {
|
60 |
-
background-color: #f9f9f9;
|
61 |
-
padding: 20px;
|
62 |
-
border-radius: 10px;
|
63 |
-
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
|
64 |
margin-top: 20px;
|
65 |
}
|
66 |
-
.verdict {
|
67 |
-
font-size: 24px;
|
68 |
-
font-weight: bold;
|
69 |
margin: 0;
|
70 |
display: flex;
|
71 |
align-items: center;
|
72 |
}
|
73 |
-
.verdict-icon {
|
74 |
margin-right: 10px;
|
75 |
}
|
76 |
</style>
|
77 |
""", unsafe_allow_html=True)
|
78 |
|
79 |
-
#
|
80 |
-
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
89 |
"SemViQA/qatc-infoxlm-viwikifc",
|
90 |
"SemViQA/qatc-infoxlm-isedsc01",
|
91 |
"SemViQA/qatc-vimrc-viwikifc",
|
92 |
"SemViQA/qatc-vimrc-isedsc01"
|
93 |
])
|
94 |
-
bc_model_name = st.selectbox("
|
95 |
"SemViQA/bc-xlmr-viwikifc",
|
96 |
"SemViQA/bc-xlmr-isedsc01",
|
97 |
"SemViQA/bc-infoxlm-viwikifc",
|
@@ -99,7 +124,7 @@ with st.container():
|
|
99 |
"SemViQA/bc-erniem-viwikifc",
|
100 |
"SemViQA/bc-erniem-isedsc01"
|
101 |
])
|
102 |
-
tc_model_name = st.selectbox("
|
103 |
"SemViQA/tc-xlmr-viwikifc",
|
104 |
"SemViQA/tc-xlmr-isedsc01",
|
105 |
"SemViQA/tc-infoxlm-viwikifc",
|
@@ -107,9 +132,9 @@ with st.container():
|
|
107 |
"SemViQA/tc-erniem-viwikifc",
|
108 |
"SemViQA/tc-erniem-isedsc01"
|
109 |
])
|
110 |
-
show_details = st.checkbox("
|
111 |
|
112 |
-
#
|
113 |
if 'history' not in st.session_state:
|
114 |
st.session_state.history = []
|
115 |
if 'latest_result' not in st.session_state:
|
@@ -126,21 +151,27 @@ with st.container():
|
|
126 |
"REFUTED": "❌",
|
127 |
"NEI": "⚠️"
|
128 |
}
|
129 |
-
|
130 |
-
#
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
with tabs[0]:
|
135 |
-
st.subheader("Kiểm chứng một thông tin")
|
136 |
-
# Sử dụng layout 2 cột: bên trái là input, bên phải là ô hiển thị kết quả
|
137 |
col_input, col_result = st.columns([2, 1])
|
138 |
|
139 |
with col_input:
|
140 |
-
claim = st.text_area("
|
141 |
-
context = st.text_area("
|
142 |
-
|
143 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
144 |
with torch.no_grad():
|
145 |
# Trích xuất bằng chứng và phân loại thông tin
|
146 |
evidence = extract_evidence_tfidf_qatc(
|
@@ -169,7 +200,7 @@ with st.container():
|
|
169 |
if show_details:
|
170 |
details = f"<p><strong>3-Class Probability:</strong> {prob3class.item():.2f} - <strong>2-Class Probability:</strong> {prob2class.item():.2f}</p>"
|
171 |
|
172 |
-
# Lưu lịch sử
|
173 |
st.session_state.history.append({
|
174 |
"claim": claim,
|
175 |
"evidence": evidence,
|
@@ -184,38 +215,32 @@ with st.container():
|
|
184 |
|
185 |
if torch.cuda.is_available():
|
186 |
torch.cuda.empty_cache()
|
187 |
-
|
188 |
-
# Hiển thị kết quả ở cột bên phải
|
189 |
-
with col_result:
|
190 |
-
st.markdown("<h3>Kết quả kiểm chứng</h3>", unsafe_allow_html=True)
|
191 |
-
if st.session_state.latest_result is not None:
|
192 |
res = st.session_state.latest_result
|
|
|
193 |
st.markdown(f"""
|
194 |
<div class='result-box'>
|
195 |
<p><strong>Claim:</strong> {res['claim']}</p>
|
196 |
-
<p><strong>
|
197 |
<p class='verdict'><span class='verdict-icon'>{verdict_icons.get(res['verdict'], '')}</span>{res['verdict']}</p>
|
198 |
{res['details']}
|
199 |
</div>
|
200 |
""", unsafe_allow_html=True)
|
201 |
-
# Tính năng tải xuống kết quả kiểm chứng
|
202 |
result_text = f"Claim: {res['claim']}\nEvidence: {res['evidence']}\nVerdict: {res['verdict']}\nDetails: {res['details']}"
|
203 |
-
st.download_button("
|
204 |
else:
|
205 |
-
st.info("
|
206 |
|
207 |
-
|
208 |
-
|
209 |
-
st.subheader("Lịch sử kiểm chứng")
|
210 |
if st.session_state.history:
|
211 |
for idx, record in enumerate(reversed(st.session_state.history), 1):
|
212 |
-
st.markdown(f"**{idx}. Claim:** {record['claim']} \n**
|
213 |
else:
|
214 |
-
st.write("
|
215 |
|
216 |
-
|
217 |
-
|
218 |
-
st.subheader("Giới thiệu")
|
219 |
st.markdown("""
|
220 |
<p align="center">
|
221 |
<a href="https://arxiv.org/abs/2503.00955">
|
@@ -233,7 +258,9 @@ with st.container():
|
|
233 |
</p>
|
234 |
""", unsafe_allow_html=True)
|
235 |
st.markdown("""
|
236 |
-
**
|
237 |
-
SemViQA
|
238 |
-
|
239 |
""")
|
|
|
|
|
|
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
|
9 |
import io
|
10 |
|
11 |
# Load models with caching
|
|
|
19 |
# Set up page configuration
|
20 |
st.set_page_config(page_title="SemViQA Demo", layout="wide")
|
21 |
|
22 |
+
# Custom CSS cho header cố định và main container (chiều cao = viewport - 55px)
|
23 |
st.markdown("""
|
24 |
<style>
|
25 |
+
.header-container {
|
26 |
+
position: fixed;
|
27 |
+
top: 0;
|
28 |
+
left: 0;
|
29 |
+
width: 100%;
|
30 |
+
height: 55px;
|
31 |
+
background-color: #fff;
|
32 |
+
z-index: 1000;
|
33 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
34 |
+
display: flex;
|
35 |
+
align-items: center;
|
36 |
+
justify-content: space-between;
|
37 |
+
padding: 0 20px;
|
38 |
+
}
|
39 |
+
.header-title {
|
40 |
+
font-size: 14px;
|
41 |
+
font-weight: bold;
|
42 |
+
color: #4A90E2;
|
43 |
+
}
|
44 |
+
.header-nav {
|
45 |
+
margin: 0 20px;
|
46 |
+
}
|
47 |
+
.header-subtitle {
|
48 |
+
font-size: 12px;
|
49 |
+
color: #666;
|
50 |
+
text-align: right;
|
51 |
}
|
52 |
.main-container {
|
53 |
+
margin-top: 55px;
|
54 |
+
height: calc(100vh - 55px);
|
55 |
overflow-y: auto;
|
56 |
padding: 20px;
|
57 |
}
|
58 |
+
.stButton>button {
|
59 |
+
background-color: #4CAF50;
|
60 |
+
color: white;
|
61 |
+
font-size: 16px;
|
62 |
+
width: 100%;
|
63 |
+
border-radius: 8px;
|
64 |
+
padding: 10px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
}
|
66 |
+
.stTextArea textarea {
|
67 |
font-size: 16px;
|
68 |
min-height: 120px;
|
69 |
}
|
70 |
+
.result-box {
|
71 |
+
background-color: #f9f9f9;
|
72 |
+
padding: 20px;
|
73 |
+
border-radius: 10px;
|
74 |
+
box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
|
75 |
margin-top: 20px;
|
76 |
}
|
77 |
+
.verdict {
|
78 |
+
font-size: 24px;
|
79 |
+
font-weight: bold;
|
80 |
margin: 0;
|
81 |
display: flex;
|
82 |
align-items: center;
|
83 |
}
|
84 |
+
.verdict-icon {
|
85 |
margin-right: 10px;
|
86 |
}
|
87 |
</style>
|
88 |
""", unsafe_allow_html=True)
|
89 |
|
90 |
+
# --- Fixed Header ---
|
91 |
+
# Sử dụng st.markdown để in ra phần header cố định bao gồm title, nav (radio) và subtitle
|
92 |
+
st.markdown("""
|
93 |
+
<div class='header-container'>
|
94 |
+
<div class='header-title'>SemViQA: Semantic Fact-Checking System for Vietnamese</div>
|
95 |
+
<div class='header-nav'>
|
96 |
+
""", unsafe_allow_html=True)
|
97 |
+
# Navigation: sử dụng st.radio để chuyển đổi các trang (hiển thị theo dạng ngang)
|
98 |
+
nav_option = st.radio("", ["Verify", "History", "About"], horizontal=True, key="nav")
|
99 |
+
st.markdown("""
|
100 |
+
</div>
|
101 |
+
<div class='header-subtitle'>Enter a claim and context to verify its accuracy</div>
|
102 |
+
</div>
|
103 |
+
""", unsafe_allow_html=True)
|
104 |
|
105 |
+
# --- Main Container ---
|
106 |
+
with st.container():
|
107 |
+
st.markdown("<div class='main-container'>", unsafe_allow_html=True)
|
108 |
+
|
109 |
+
# Sidebar: Global Settings (không thay đổi)
|
110 |
+
with st.sidebar.expander("⚙️ Settings", expanded=True):
|
111 |
+
tfidf_threshold = st.slider("TF-IDF Threshold", 0.0, 1.0, 0.5, 0.01)
|
112 |
+
length_ratio_threshold = st.slider("Length Ratio Threshold", 0.1, 1.0, 0.5, 0.01)
|
113 |
+
qatc_model_name = st.selectbox("QATC Model", [
|
114 |
"SemViQA/qatc-infoxlm-viwikifc",
|
115 |
"SemViQA/qatc-infoxlm-isedsc01",
|
116 |
"SemViQA/qatc-vimrc-viwikifc",
|
117 |
"SemViQA/qatc-vimrc-isedsc01"
|
118 |
])
|
119 |
+
bc_model_name = st.selectbox("Binary Classification Model", [
|
120 |
"SemViQA/bc-xlmr-viwikifc",
|
121 |
"SemViQA/bc-xlmr-isedsc01",
|
122 |
"SemViQA/bc-infoxlm-viwikifc",
|
|
|
124 |
"SemViQA/bc-erniem-viwikifc",
|
125 |
"SemViQA/bc-erniem-isedsc01"
|
126 |
])
|
127 |
+
tc_model_name = st.selectbox("Three-Class Classification Model", [
|
128 |
"SemViQA/tc-xlmr-viwikifc",
|
129 |
"SemViQA/tc-xlmr-isedsc01",
|
130 |
"SemViQA/tc-infoxlm-viwikifc",
|
|
|
132 |
"SemViQA/tc-erniem-viwikifc",
|
133 |
"SemViQA/tc-erniem-isedsc01"
|
134 |
])
|
135 |
+
show_details = st.checkbox("Show probability details", value=False)
|
136 |
|
137 |
+
# Khởi tạo lịch sử kiểm chứng và kết quả mới nhất
|
138 |
if 'history' not in st.session_state:
|
139 |
st.session_state.history = []
|
140 |
if 'latest_result' not in st.session_state:
|
|
|
151 |
"REFUTED": "❌",
|
152 |
"NEI": "⚠️"
|
153 |
}
|
154 |
+
|
155 |
+
# Hiển thị nội dung theo lựa chọn của navigation
|
156 |
+
if nav_option == "Verify":
|
157 |
+
st.subheader("Verify a Claim")
|
158 |
+
# Layout 2 cột: bên trái cho input, bên phải hiển thị kết quả
|
|
|
|
|
|
|
159 |
col_input, col_result = st.columns([2, 1])
|
160 |
|
161 |
with col_input:
|
162 |
+
claim = st.text_area("Enter Claim", "Vietnam is a country in Southeast Asia.")
|
163 |
+
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.")
|
164 |
+
verify_clicked = st.button("Verify", key="verify_button")
|
165 |
+
|
166 |
+
with col_result:
|
167 |
+
if verify_clicked:
|
168 |
+
with st.spinner("Loading and running verification..."):
|
169 |
+
# Hiển thị progress bar mô phỏng quá trình xử lý
|
170 |
+
progress_bar = st.progress(0)
|
171 |
+
for i in range(1, 101, 20):
|
172 |
+
time.sleep(0.1)
|
173 |
+
progress_bar.progress(i)
|
174 |
+
|
175 |
with torch.no_grad():
|
176 |
# Trích xuất bằng chứng và phân loại thông tin
|
177 |
evidence = extract_evidence_tfidf_qatc(
|
|
|
200 |
if show_details:
|
201 |
details = f"<p><strong>3-Class Probability:</strong> {prob3class.item():.2f} - <strong>2-Class Probability:</strong> {prob2class.item():.2f}</p>"
|
202 |
|
203 |
+
# Lưu lịch sử và kết quả kiểm chứng mới nhất
|
204 |
st.session_state.history.append({
|
205 |
"claim": claim,
|
206 |
"evidence": evidence,
|
|
|
215 |
|
216 |
if torch.cuda.is_available():
|
217 |
torch.cuda.empty_cache()
|
218 |
+
|
|
|
|
|
|
|
|
|
219 |
res = st.session_state.latest_result
|
220 |
+
st.markdown("<h3>Verification Result</h3>", unsafe_allow_html=True)
|
221 |
st.markdown(f"""
|
222 |
<div class='result-box'>
|
223 |
<p><strong>Claim:</strong> {res['claim']}</p>
|
224 |
+
<p><strong>Evidence:</strong> {res['evidence']}</p>
|
225 |
<p class='verdict'><span class='verdict-icon'>{verdict_icons.get(res['verdict'], '')}</span>{res['verdict']}</p>
|
226 |
{res['details']}
|
227 |
</div>
|
228 |
""", unsafe_allow_html=True)
|
|
|
229 |
result_text = f"Claim: {res['claim']}\nEvidence: {res['evidence']}\nVerdict: {res['verdict']}\nDetails: {res['details']}"
|
230 |
+
st.download_button("Download Result", data=result_text, file_name="verification_result.txt", mime="text/plain")
|
231 |
else:
|
232 |
+
st.info("No verification result yet.")
|
233 |
|
234 |
+
elif nav_option == "History":
|
235 |
+
st.subheader("Verification History")
|
|
|
236 |
if st.session_state.history:
|
237 |
for idx, record in enumerate(reversed(st.session_state.history), 1):
|
238 |
+
st.markdown(f"**{idx}. Claim:** {record['claim']} \n**Result:** {verdict_icons.get(record['verdict'], '')} {record['verdict']}")
|
239 |
else:
|
240 |
+
st.write("No verification history yet.")
|
241 |
|
242 |
+
elif nav_option == "About":
|
243 |
+
st.subheader("About")
|
|
|
244 |
st.markdown("""
|
245 |
<p align="center">
|
246 |
<a href="https://arxiv.org/abs/2503.00955">
|
|
|
258 |
</p>
|
259 |
""", unsafe_allow_html=True)
|
260 |
st.markdown("""
|
261 |
+
**Description:**
|
262 |
+
SemViQA is a semantic QA system designed for fact-checking in Vietnamese.
|
263 |
+
It extracts evidence from the provided context and classifies the claim as **SUPPORTED**, **REFUTED**, or **NEI** (Not Enough Information) using state-of-the-art models.
|
264 |
""")
|
265 |
+
|
266 |
+
st.markdown("</div>", unsafe_allow_html=True)
|