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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -159
app.py CHANGED
@@ -18,114 +18,102 @@ 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: fixed navigation, header, and adjusted height
22
- st.markdown("""
 
23
  <style>
24
- html, body {
25
- height: 100%;
26
- margin: 0;
27
- overflow: hidden;
28
- }
29
- .main-container {
30
- height: calc(100vh - 55px);
31
- overflow-y: auto;
32
- padding: 20px;
33
- }
34
- .big-title {
35
- font-size: 36px;
36
- font-weight: bold;
37
- color: #4A90E2;
38
- text-align: center;
39
- margin-top: 20px;
40
- position: sticky;
41
- top: 0;
42
- background-color: white;
43
- z-index: 100;
44
- padding: 10px 0;
45
- }
46
- .sub-title {
47
- font-size: 20px;
48
- color: #666;
49
- text-align: center;
50
- margin-bottom: 20px;
51
- position: sticky;
52
- top: 56px;
53
- background-color: white;
54
- z-index: 100;
55
- padding-bottom: 10px;
56
- }
57
- .stButton>button {
58
- background-color: #4CAF50;
59
- color: white;
60
- font-size: 16px;
61
- width: 100%;
62
- border-radius: 8px;
63
- padding: 10px;
64
- }
65
- .stTextArea textarea {
66
- font-size: 16px;
67
- min-height: 120px;
68
- }
69
- .result-box {
70
- background-color: #f9f9f9;
71
- padding: 20px;
72
- border-radius: 10px;
73
- box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
74
- margin-top: 20px;
75
- }
76
- .verdict {
77
- font-size: 24px;
78
- font-weight: bold;
79
- margin: 0;
80
- display: flex;
81
- align-items: center;
82
- }
83
- .verdict-icon {
84
- margin-right: 10px;
85
- }
86
- /* Fixed sidebar */
87
- .css-1d391kg, .css-1cypcdb {
88
- position: sticky;
89
- top: 0;
90
- height: calc(100vh - 55px);
91
- overflow-y: auto;
92
- }
93
- /* Tab content area */
94
- .stTabs [data-baseweb="tab-panel"] {
95
- height: calc(100vh - 150px);
96
- overflow-y: auto;
97
- }
98
- /* Loading animation */
99
- .loading-animation {
100
- text-align: center;
101
- padding: 20px;
102
- }
103
- .loading-animation .dot {
104
- display: inline-block;
105
- width: 12px;
106
- height: 12px;
107
- border-radius: 50%;
108
- background-color: #4A90E2;
109
- margin: 0 5px;
110
- animation: pulse 1.4s infinite ease-in-out;
111
- }
112
- .loading-animation .dot:nth-child(2) {
113
- animation-delay: 0.2s;
114
- }
115
- .loading-animation .dot:nth-child(3) {
116
- animation-delay: 0.4s;
117
- }
118
- @keyframes pulse {
119
- 0%, 100% { transform: scale(0.8); opacity: 0.5; }
120
- 50% { transform: scale(1.2); opacity: 1; }
121
- }
122
  </style>
123
- """, unsafe_allow_html=True)
 
 
124
 
125
- # Main container with fixed height
126
  with st.container():
127
- st.markdown("<p class='big-title'>SemViQA: Semantic QA Information Verification System for Vietnamese</p>", unsafe_allow_html=True)
128
- st.markdown("<p class='sub-title'>Enter information to verify and context to check its accuracy</p>", unsafe_allow_html=True)
129
 
130
  # Sidebar: Global Settings
131
  with st.sidebar.expander("⚙️ Settings", expanded=True):
@@ -153,17 +141,15 @@ with st.container():
153
  "SemViQA/tc-erniem-viwikifc",
154
  "SemViQA/tc-erniem-isedsc01"
155
  ])
156
- show_details = st.checkbox("Show probability details", value=False)
157
 
158
  # Store verification history
159
  if 'history' not in st.session_state:
160
  st.session_state.history = []
161
  if 'latest_result' not in st.session_state:
162
  st.session_state.latest_result = None
163
- if 'is_verifying' not in st.session_state:
164
- st.session_state.is_verifying = False
165
 
166
- # Load selected models
167
  tokenizer_qatc, model_qatc = load_model(qatc_model_name, QATCForQuestionAnswering)
168
  tokenizer_bc, model_bc = load_model(bc_model_name, ClaimModelForClassification, is_bc=True)
169
  tokenizer_tc, model_tc = load_model(tc_model_name, ClaimModelForClassification)
@@ -175,50 +161,29 @@ with st.container():
175
  "NEI": "⚠️"
176
  }
177
 
178
- # Create tabs: Verify, History, About
179
  tabs = st.tabs(["Verify", "History", "About"])
180
 
181
- # --- Verify Tab ---
182
  with tabs[0]:
183
- st.subheader("Verify Information")
184
- # Use 2-column layout: inputs on left, results on right
185
  col_input, col_result = st.columns([2, 1])
186
-
187
  with col_input:
188
  claim = st.text_area("Enter Claim", "Vietnam is a country in Southeast Asia.")
189
  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.")
190
-
191
- def start_verification():
192
- st.session_state.is_verifying = True
193
- st.experimental_rerun()
194
-
195
- if st.button("Verify", key="verify_button", on_click=start_verification):
196
- pass
197
-
198
- # Display results in right column
199
  with col_result:
200
- st.markdown("<h3>Verification Results</h3>", unsafe_allow_html=True)
201
-
202
- if st.session_state.is_verifying:
203
- # Show loading animation
204
- st.markdown("""
205
- <div class="result-box">
206
- <p><strong>Processing verification...</strong></p>
207
- <div class="loading-animation">
208
- <span class="dot"></span>
209
- <span class="dot"></span>
210
- <span class="dot"></span>
211
- </div>
212
- <p>1. Extracting evidence...</p>
213
- <p>2. Running binary classification...</p>
214
- <p>3. Running 3-class classification...</p>
215
- <p>4. Determining final verdict...</p>
216
- </div>
217
- """, unsafe_allow_html=True)
218
-
219
- # Perform actual verification
220
  with torch.no_grad():
221
- # Extract evidence and classify information
222
  evidence = extract_evidence_tfidf_qatc(
223
  claim, context, model_qatc, tokenizer_qatc,
224
  "cuda" if torch.cuda.is_available() else "cpu",
@@ -244,8 +209,8 @@ with st.container():
244
  verdict = ["NEI", "SUPPORTED", "REFUTED"][pred_tc]
245
  if show_details:
246
  details = f"<p><strong>3-Class Probability:</strong> {prob3class.item():.2f} - <strong>2-Class Probability:</strong> {prob2class.item():.2f}</p>"
247
-
248
- # Save verification history and latest result
249
  st.session_state.history.append({
250
  "claim": claim,
251
  "evidence": evidence,
@@ -257,17 +222,13 @@ with st.container():
257
  "verdict": verdict,
258
  "details": details
259
  }
260
-
261
  if torch.cuda.is_available():
262
  torch.cuda.empty_cache()
263
-
264
- # Turn off verification flag
265
- st.session_state.is_verifying = False
266
- st.experimental_rerun()
267
-
268
- elif st.session_state.latest_result is not None:
269
  res = st.session_state.latest_result
270
- st.markdown(f"""
271
  <div class='result-box'>
272
  <p><strong>Claim:</strong> {res['claim']}</p>
273
  <p><strong>Evidence:</strong> {res['evidence']}</p>
@@ -275,13 +236,13 @@ with st.container():
275
  {res['details']}
276
  </div>
277
  """, unsafe_allow_html=True)
278
- # Download verification result
279
  result_text = f"Claim: {res['claim']}\nEvidence: {res['evidence']}\nVerdict: {res['verdict']}\nDetails: {res['details']}"
280
  st.download_button("Download Result", data=result_text, file_name="verification_result.txt", mime="text/plain")
281
  else:
282
- st.info("No verification results yet.")
283
 
284
- # --- History Tab ---
285
  with tabs[1]:
286
  st.subheader("Verification History")
287
  if st.session_state.history:
@@ -290,7 +251,7 @@ with st.container():
290
  else:
291
  st.write("No verification history yet.")
292
 
293
- # --- About Tab ---
294
  with tabs[2]:
295
  st.subheader("About")
296
  st.markdown("""
@@ -311,6 +272,6 @@ with st.container():
311
  """, unsafe_allow_html=True)
312
  st.markdown("""
313
  **Description:**
314
- SemViQA is a Semantic QA system designed to verify information in Vietnamese.
315
- The system extracts evidence from the provided context and classifies information as **SUPPORTED**, **REFUTED**, or **NEI** (Not Enough Information) based on advanced models.
316
  """)
 
18
  # Set up page configuration
19
  st.set_page_config(page_title="SemViQA Demo", layout="wide")
20
 
21
+ # Custom CSS: fixed header and tabs, dynamic height, result box formatting
22
+ st.markdown(
23
+ """
24
  <style>
25
+ html, body {
26
+ height: 100%;
27
+ margin: 0;
28
+ overflow: hidden;
29
+ }
30
+ .main-container {
31
+ height: calc(100vh - 55px); /* Browser height - 55px */
32
+ overflow-y: auto;
33
+ padding: 20px;
34
+ }
35
+ .big-title {
36
+ font-size: 36px;
37
+ font-weight: bold;
38
+ color: #4A90E2;
39
+ text-align: center;
40
+ margin-top: 20px;
41
+ position: sticky; /* Pin the header */
42
+ top: 0;
43
+ background-color: white; /* Ensure the header covers content when scrolling */
44
+ z-index: 100; /* Ensure it's above other content */
45
+ }
46
+ .sub-title {
47
+ font-size: 20px;
48
+ color: #666;
49
+ text-align: center;
50
+ margin-bottom: 20px;
51
+ }
52
+ .stButton>button {
53
+ background-color: #4CAF50;
54
+ color: white;
55
+ font-size: 16px;
56
+ width: 100%;
57
+ border-radius: 8px;
58
+ padding: 10px;
59
+ }
60
+ .stTextArea textarea {
61
+ font-size: 16px;
62
+ min-height: 120px;
63
+ }
64
+ .result-box {
65
+ background-color: #f9f9f9;
66
+ padding: 20px;
67
+ border-radius: 10px;
68
+ box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1);
69
+ margin-top: 20px;
70
+ }
71
+ .verdict {
72
+ font-size: 24px;
73
+ font-weight: bold;
74
+ margin: 0;
75
+ display: flex;
76
+ align-items: center;
77
+ }
78
+ .verdict-icon {
79
+ margin-right: 10px;
80
+ }
81
+ /* Fix the tabs at the top */
82
+ div[data-baseweb="tab-list"] {
83
+ position: sticky;
84
+ top: 55px; /* Height of the header */
85
+ background-color: white;
86
+ z-index: 99;
87
+ }
88
+ .stSidebar .sidebar-content {
89
+ background-color: #f0f2f6;
90
+ padding: 20px;
91
+ border-radius: 10px;
92
+ }
93
+ .stSidebar .st-expander {
94
+ background-color: #ffffff;
95
+ border-radius: 10px;
96
+ padding: 10px;
97
+ margin-bottom: 10px;
98
+ }
99
+ .stSidebar .stSlider {
100
+ margin-bottom: 20px;
101
+ }
102
+ .stSidebar .stSelectbox {
103
+ margin-bottom: 20px;
104
+ }
105
+ .stSidebar .stCheckbox {
106
+ margin-bottom: 20px;
107
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  </style>
109
+ """,
110
+ unsafe_allow_html=True,
111
+ )
112
 
113
+ # Container for the whole content with dynamic height
114
  with st.container():
115
+ st.markdown("<p class='big-title'>SemViQA: Vietnamese Semantic QA for Fact Verification</p>", unsafe_allow_html=True)
116
+ st.markdown("<p class='sub-title'>Enter the claim and context to verify its accuracy</p>", unsafe_allow_html=True)
117
 
118
  # Sidebar: Global Settings
119
  with st.sidebar.expander("⚙️ Settings", expanded=True):
 
141
  "SemViQA/tc-erniem-viwikifc",
142
  "SemViQA/tc-erniem-isedsc01"
143
  ])
144
+ show_details = st.checkbox("Show Probability Details", value=False)
145
 
146
  # Store verification history
147
  if 'history' not in st.session_state:
148
  st.session_state.history = []
149
  if 'latest_result' not in st.session_state:
150
  st.session_state.latest_result = None
 
 
151
 
152
+ # Load the selected models
153
  tokenizer_qatc, model_qatc = load_model(qatc_model_name, QATCForQuestionAnswering)
154
  tokenizer_bc, model_bc = load_model(bc_model_name, ClaimModelForClassification, is_bc=True)
155
  tokenizer_tc, model_tc = load_model(tc_model_name, ClaimModelForClassification)
 
161
  "NEI": "⚠️"
162
  }
163
 
164
+ # Tabs: Verify, History, About
165
  tabs = st.tabs(["Verify", "History", "About"])
166
 
167
+ # --- Tab Verify ---
168
  with tabs[0]:
169
+ st.subheader("Verify a Claim")
170
+ # 2-column layout: input on the left, results on the right
171
  col_input, col_result = st.columns([2, 1])
172
+
173
  with col_input:
174
  claim = st.text_area("Enter Claim", "Vietnam is a country in Southeast Asia.")
175
  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.")
176
+ verify_button = st.button("Verify", key="verify_button")
177
+
 
 
 
 
 
 
 
178
  with col_result:
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",
 
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,
 
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>
 
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
 
245
+ # --- Tab History ---
246
  with tabs[1]:
247
  st.subheader("Verification History")
248
  if st.session_state.history:
 
251
  else:
252
  st.write("No verification history yet.")
253
 
254
+ # --- Tab About ---
255
  with tabs[2]:
256
  st.subheader("About")
257
  st.markdown("""
 
272
  """, unsafe_allow_html=True)
273
  st.markdown("""
274
  **Description:**
275
+ SemViQA is a Semantic QA system designed for fact verification in Vietnamese.
276
+ The system extracts evidence from the provided context and classifies claims as **SUPPORTED**, **REFUTED**, or **NEI** (Not Enough Information) using advanced models.
277
  """)