Truong-Phuc Nguyen commited on
Commit
00b5c9c
·
verified ·
1 Parent(s): 3188aec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -16
app.py CHANGED
@@ -1,24 +1,36 @@
1
  import streamlit as st
2
  import pandas as pd
3
  import re
 
4
 
5
  st.set_page_config(layout='wide')
6
 
7
- def load_data():
8
- return pd.read_csv(filepath_or_buffer='./data.csv')
 
 
 
9
 
10
- df = load_data()
 
 
 
 
 
 
 
 
11
 
12
  if 'idx' not in st.session_state:
13
  st.session_state.idx = 0
14
 
15
  st.markdown("<h1 style='text-align: center;'>Investigation Legal Documents Dataset Checker</h1>", unsafe_allow_html=True)
16
 
17
-
18
  col_1, col_2, col_3, col_4, col_5, col_6, col_7, col_8, col_9, col_10 = st.columns([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
19
  btn_prev = col_1.button(label='Previous sample', use_container_width=True)
20
  btn_next = col_2.button(label='Next sample', use_container_width=True)
21
  btn_save = col_3.button(label='Save changes', use_container_width=True)
 
22
 
23
  if btn_prev:
24
  if st.session_state.idx > 0:
@@ -30,17 +42,23 @@ if btn_next:
30
 
31
  st.markdown(f"<h3 style='text-align: center;'>Sample: {st.session_state.idx+1}/{len(df)}</h3>", unsafe_allow_html=True)
32
 
33
- context = st.text_area(label='Your context: ', value=df['contexts'][st.session_state.idx], height=300)
34
- question = st.text_area(label='Your question: ', value=df['questions'][st.session_state.idx], height=100)
35
- answer = st.text_area(label='Your answer: ', value=df['answers'][st.session_state.idx], height=100)
 
 
 
 
 
 
 
 
 
 
36
 
37
- if answer.strip() and context.strip():
38
- highlighted_context = re.sub(re.escape(answer), "<mark>" + answer + "</mark>", context, flags=re.IGNORECASE)
39
- st.markdown(highlighted_context, unsafe_allow_html=True)
40
 
41
- if btn_save:
42
- df.loc[st.session_state.idx, 'contexts'] = context
43
- df.loc[st.session_state.idx, 'questions'] = question
44
- df.loc[st.session_state.idx, 'answers'] = answer
45
-
46
- df.to_csv('./data.csv', index=False)
 
1
  import streamlit as st
2
  import pandas as pd
3
  import re
4
+ import base64
5
 
6
  st.set_page_config(layout='wide')
7
 
8
+ def load_data(file):
9
+ if file is not None:
10
+ return pd.read_csv(file)
11
+ else:
12
+ return pd.DataFrame(columns=['context', 'question', 'answer'])
13
 
14
+ def download_csv(dataframe):
15
+ csv = dataframe.to_csv(index=False)
16
+ b64 = base64.b64encode(csv.encode()).decode()
17
+ href = f'<a href="data:file/csv;base64,{b64}" download="checked_data.csv">Download CSV File</a>'
18
+ st.markdown(href, unsafe_allow_html=True)
19
+
20
+ uploaded_file = st.file_uploader("Upload CSV file", type=['csv'])
21
+
22
+ df = load_data(uploaded_file)
23
 
24
  if 'idx' not in st.session_state:
25
  st.session_state.idx = 0
26
 
27
  st.markdown("<h1 style='text-align: center;'>Investigation Legal Documents Dataset Checker</h1>", unsafe_allow_html=True)
28
 
 
29
  col_1, col_2, col_3, col_4, col_5, col_6, col_7, col_8, col_9, col_10 = st.columns([1, 1, 1, 1, 1, 1, 1, 1, 1, 1])
30
  btn_prev = col_1.button(label='Previous sample', use_container_width=True)
31
  btn_next = col_2.button(label='Next sample', use_container_width=True)
32
  btn_save = col_3.button(label='Save changes', use_container_width=True)
33
+ btn_download = col_4.button(label='Download CSV', use_container_width=True)
34
 
35
  if btn_prev:
36
  if st.session_state.idx > 0:
 
42
 
43
  st.markdown(f"<h3 style='text-align: center;'>Sample: {st.session_state.idx+1}/{len(df)}</h3>", unsafe_allow_html=True)
44
 
45
+ if not df.empty:
46
+ context = st.text_area(label='Your context: ', value=df['context'][st.session_state.idx], height=300)
47
+ question = st.text_area(label='Your question: ', value=df['question'][st.session_state.idx], height=100)
48
+ answer = st.text_area(label='Your answer: ', value=df['answer'][st.session_state.idx], height=100)
49
+
50
+ if answer.strip() and context.strip():
51
+ highlighted_context = re.sub(re.escape(answer), "<mark>" + answer + "</mark>", context, flags=re.IGNORECASE)
52
+ st.markdown(highlighted_context, unsafe_allow_html=True)
53
+
54
+ if btn_save:
55
+ df.loc[st.session_state.idx, 'context'] = context
56
+ df.loc[st.session_state.idx, 'question'] = question
57
+ df.loc[st.session_state.idx, 'answer'] = answer
58
 
59
+ if uploaded_file is not None:
60
+ uploaded_file.seek(0)
61
+ df.to_csv(uploaded_file, index=False)
62
 
63
+ if btn_download:
64
+ download_csv(df)