Spaces:
Sleeping
Sleeping
rowankwang
commited on
Commit
•
f396b8b
1
Parent(s):
5a7aa66
init
Browse files- app.py +155 -0
- requirements.txt +3 -0
- synth_toy_eval.json +0 -0
app.py
ADDED
@@ -0,0 +1,155 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import json
|
3 |
+
from streamlit_shortcuts import add_keyboard_shortcuts
|
4 |
+
|
5 |
+
st.set_page_config(layout="wide")
|
6 |
+
|
7 |
+
file_path = 'synth_toy_eval.json'
|
8 |
+
|
9 |
+
# Load your data
|
10 |
+
@st.cache_data()
|
11 |
+
def load_data():
|
12 |
+
with open(file_path, 'r') as file:
|
13 |
+
data = json.load(file)
|
14 |
+
return data
|
15 |
+
|
16 |
+
def save_data(data):
|
17 |
+
print(file_path.split(".json")[0])
|
18 |
+
with open(f"{file_path.split('.json')[0]}_graded.json", 'w') as file:
|
19 |
+
json.dump(data, file, indent=4)
|
20 |
+
|
21 |
+
data = load_data()
|
22 |
+
|
23 |
+
for query in data:
|
24 |
+
for result in query['results']:
|
25 |
+
if 'verified' not in result:
|
26 |
+
result['verified'] = False
|
27 |
+
|
28 |
+
# State management for current query index
|
29 |
+
if 'current_query_index' not in st.session_state:
|
30 |
+
st.session_state.current_query_index = 0
|
31 |
+
if 'data' not in st.session_state:
|
32 |
+
st.session_state.data = data
|
33 |
+
if 'graded_queries' not in st.session_state:
|
34 |
+
st.session_state.graded_queries = 0
|
35 |
+
|
36 |
+
def truncate_text(text, length=250):
|
37 |
+
return text if len(text) <= length else text[:length] + '...'
|
38 |
+
|
39 |
+
result_box_style = """
|
40 |
+
<style>
|
41 |
+
.rounded-box {
|
42 |
+
border: 1px solid #ddd;
|
43 |
+
border-radius: 10px;
|
44 |
+
padding: 0.01px;
|
45 |
+
margin-bottom: 10px;
|
46 |
+
}
|
47 |
+
</style>
|
48 |
+
"""
|
49 |
+
# Navigation to next query
|
50 |
+
def next_query():
|
51 |
+
if st.session_state.current_query_index < len(data) - 1:
|
52 |
+
st.session_state.current_query_index += 1
|
53 |
+
st.rerun()
|
54 |
+
|
55 |
+
# Display current query and its results
|
56 |
+
def display_query():
|
57 |
+
# Navigation bar
|
58 |
+
global current_query
|
59 |
+
|
60 |
+
st.session_state.graded_queries = sum(query.get('status', None) is not None for query in st.session_state.data)
|
61 |
+
print(f"Current Query Index: {st.session_state.current_query_index} | Graded Queries: {st.session_state.graded_queries} | Total Queries: {len(st.session_state.data)} | Current Query Status {current_query.get('status', None)}")
|
62 |
+
col1, col2 = st.columns([5, 1], gap="small")
|
63 |
+
with col1:
|
64 |
+
if st.button('Previous'):
|
65 |
+
if st.session_state.current_query_index > 0:
|
66 |
+
st.session_state.current_query_index -= 1 % len(st.session_state.data)
|
67 |
+
st.rerun()
|
68 |
+
st.progress((st.session_state.current_query_index + 1) / len(st.session_state.data))
|
69 |
+
with col2:
|
70 |
+
col1, col2, col3 = st.columns([1, 1, 1], gap = "small")
|
71 |
+
with col1:
|
72 |
+
if st.button('Next'):
|
73 |
+
if st.session_state.current_query_index < len(st.session_state.data) - 1:
|
74 |
+
st.session_state.current_query_index += 1 % len(st.session_state.data)
|
75 |
+
st.rerun()
|
76 |
+
with col2:
|
77 |
+
if st.button('Skip'):
|
78 |
+
current_query['status'] = 'skipped'
|
79 |
+
next_query()
|
80 |
+
with col3:
|
81 |
+
if st.button('Junk'):
|
82 |
+
current_query['status'] = 'nonsense'
|
83 |
+
next_query()
|
84 |
+
|
85 |
+
st.markdown(f"<p>At index {st.session_state.current_query_index + 1}. Graded Queries: {st.session_state.graded_queries}/{len(st.session_state.data)}</p>", unsafe_allow_html=True)
|
86 |
+
|
87 |
+
if st.session_state.graded_queries >= len(data):
|
88 |
+
save_data(st.session_state.data)
|
89 |
+
st.success(f"{len(data)} Queries graded and data saved!")
|
90 |
+
|
91 |
+
st.markdown(result_box_style, unsafe_allow_html=True)
|
92 |
+
|
93 |
+
st.header(f"Query: {current_query['query']}")
|
94 |
+
status_color = 'green' if current_query.get('status', None) is not None else 'red'
|
95 |
+
st.markdown(f"{current_query['grid_pos_str']} | Query Grade: <b style='color: {status_color};'>{'Graded' if status_color == 'green' else 'Ungraded'}</b>", unsafe_allow_html = True)
|
96 |
+
|
97 |
+
st.subheader("Results:")
|
98 |
+
for index, result in enumerate(current_query['results']):
|
99 |
+
st.markdown(f"<div class='rounded-box'>", unsafe_allow_html=True)
|
100 |
+
col1, col2 = st.columns([3, 2], gap="small")
|
101 |
+
with col1:
|
102 |
+
# title_style = f"color: {'green' if result.get('verified') is True else 'red' if result.get('verified') is False else 'white'};"
|
103 |
+
|
104 |
+
st.markdown(f"<h5>{result['title']}</h5>", unsafe_allow_html=True)
|
105 |
+
st.markdown(f"[<span style='font-size: 0.8em;'>{truncate_text(result['url'], length = 50)}</span>]({result['url']}) | {result['published_date']}", unsafe_allow_html=True)
|
106 |
+
st.markdown(f"{truncate_text(result['text'], length = len(result['model_trace']))}")
|
107 |
+
with col2:
|
108 |
+
grade_color = 'green' if result['grade'].lower() == 'yes' else 'red'
|
109 |
+
st.markdown(f"<b style='color: {grade_color};'>Model Grade: {result['grade']}</b>", unsafe_allow_html=True)
|
110 |
+
st.write(result['model_trace'])
|
111 |
+
|
112 |
+
if st.checkbox("Accept", value=result.get('verified'), key=f'verify-{index}'):
|
113 |
+
result['verified'] = True
|
114 |
+
|
115 |
+
# btn_cols = st.columns([1, 1])
|
116 |
+
# with btn_cols[0]:
|
117 |
+
# if st.button('Accept', key=f'accept-{index}'):
|
118 |
+
# result['verified'] = True
|
119 |
+
# if result.get('verified') is True:
|
120 |
+
# st.write('Accepted')
|
121 |
+
# with btn_cols[1]:
|
122 |
+
# if st.button('Reject', key=f'reject-{index}'):
|
123 |
+
# result['verified'] = False
|
124 |
+
# if result.get('verified') is False:
|
125 |
+
# st.write('Rejected')
|
126 |
+
|
127 |
+
st.markdown("</div>", unsafe_allow_html=True)
|
128 |
+
st.markdown(f"<div class='rounded-box'>", unsafe_allow_html=True)
|
129 |
+
|
130 |
+
# Show current query and its results
|
131 |
+
current_query = st.session_state.data[st.session_state.current_query_index]
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
display_query()
|
136 |
+
|
137 |
+
col1, col2 = st.columns([5, 1], gap="small")
|
138 |
+
with col2:
|
139 |
+
if st.button('Mark Done and Go to Next'):
|
140 |
+
current_query['status'] = 'graded'
|
141 |
+
next_query()
|
142 |
+
|
143 |
+
|
144 |
+
add_keyboard_shortcuts({
|
145 |
+
's': 'Skip',
|
146 |
+
})
|
147 |
+
add_keyboard_shortcuts({
|
148 |
+
'j': 'Junk',
|
149 |
+
})
|
150 |
+
add_keyboard_shortcuts({
|
151 |
+
'p': 'Previous',
|
152 |
+
})
|
153 |
+
add_keyboard_shortcuts({
|
154 |
+
'n': 'Next',
|
155 |
+
})
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
streamlit
|
2 |
+
streamlit-shortcuts
|
3 |
+
|
synth_toy_eval.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|