Spaces:
Runtime error
Runtime error
Commit
·
23e04e2
1
Parent(s):
8fbe78b
added option to choose postprocessing
Browse files
app.py
CHANGED
@@ -29,7 +29,17 @@ def choose_model():
|
|
29 |
return model_cp, model_cp
|
30 |
|
31 |
|
32 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
"""Function filters postprocessed model output to remove answers that
|
34 |
are substrings of another answer."""
|
35 |
answers = [answer['answer'] for answer in resp]
|
@@ -44,6 +54,32 @@ def postprocess(resp):
|
|
44 |
return filtered_resp
|
45 |
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
# Page config
|
48 |
title = "Recipe Improver"
|
49 |
icon = "🍣"
|
@@ -51,8 +87,12 @@ st.set_page_config(page_title=title, page_icon=icon)
|
|
51 |
st.title(title)
|
52 |
|
53 |
|
54 |
-
#
|
55 |
model_cp, tokenizer_cp = choose_model()
|
|
|
|
|
|
|
|
|
56 |
question_answer = load_pipeline(model_cp, tokenizer_cp)
|
57 |
st.write("Model and tokenizer successfully loaded.")
|
58 |
|
@@ -70,10 +110,20 @@ if uploaded_file is not None:
|
|
70 |
user_idx = st.text_input('Enter index')
|
71 |
first_example = df['review'][int(user_idx)]
|
72 |
question = "How was the recipe improved?"
|
73 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
context=first_example,
|
75 |
top_k=5)
|
76 |
-
|
|
|
|
|
|
|
|
|
77 |
|
78 |
# Present results
|
79 |
st.markdown(f"""
|
@@ -83,16 +133,11 @@ if uploaded_file is not None:
|
|
83 |
|
84 |
{first_example}
|
85 |
|
86 |
-
The question was:
|
87 |
|
88 |
{question}
|
89 |
|
90 |
-
The
|
91 |
|
92 |
{resp}
|
93 |
-
|
94 |
-
The filtered answers were:
|
95 |
-
|
96 |
-
{filtered_resp}
|
97 |
-
|
98 |
""")
|
|
|
29 |
return model_cp, model_cp
|
30 |
|
31 |
|
32 |
+
def choose_postprocessing():
|
33 |
+
with st.sidebar:
|
34 |
+
st.write('# Postprocessing')
|
35 |
+
postprocessing = st.selectbox('Select postprocessing method',
|
36 |
+
('no postprocessing',
|
37 |
+
'remove substrings',
|
38 |
+
'iteration'))
|
39 |
+
return postprocessing
|
40 |
+
|
41 |
+
|
42 |
+
def remove_substring(resp):
|
43 |
"""Function filters postprocessed model output to remove answers that
|
44 |
are substrings of another answer."""
|
45 |
answers = [answer['answer'] for answer in resp]
|
|
|
54 |
return filtered_resp
|
55 |
|
56 |
|
57 |
+
def iterate(resp, context):
|
58 |
+
# Remove substring answers
|
59 |
+
filtered_resp = remove_substring(resp)
|
60 |
+
# Remove best answer from original context
|
61 |
+
best_answer = filtered_resp[0]['answer']
|
62 |
+
new_context = context.replace(best_answer, "")
|
63 |
+
return new_context, filtered_resp
|
64 |
+
|
65 |
+
|
66 |
+
def remove_substring_iter(pipeline, question, context):
|
67 |
+
# Create answers placeholder
|
68 |
+
ret_resp = []
|
69 |
+
|
70 |
+
# Loop through five times, removing best and re-running
|
71 |
+
for _ in range(5):
|
72 |
+
resp = pipeline(question, context, top_k=5,
|
73 |
+
handle_impossible_answer=True)
|
74 |
+
# Update context
|
75 |
+
context, filtered_resp = iterate(resp, context)
|
76 |
+
# If best score not above threshold, quit
|
77 |
+
if filtered_resp[0]['score'] < 1e-3:
|
78 |
+
break
|
79 |
+
ret_resp.append(filtered_resp[0])
|
80 |
+
return ret_resp
|
81 |
+
|
82 |
+
|
83 |
# Page config
|
84 |
title = "Recipe Improver"
|
85 |
icon = "🍣"
|
|
|
87 |
st.title(title)
|
88 |
|
89 |
|
90 |
+
# Choose model and postprocessing procedure
|
91 |
model_cp, tokenizer_cp = choose_model()
|
92 |
+
postprocessing = choose_postprocessing()
|
93 |
+
|
94 |
+
|
95 |
+
# Load model and tokenizer
|
96 |
question_answer = load_pipeline(model_cp, tokenizer_cp)
|
97 |
st.write("Model and tokenizer successfully loaded.")
|
98 |
|
|
|
110 |
user_idx = st.text_input('Enter index')
|
111 |
first_example = df['review'][int(user_idx)]
|
112 |
question = "How was the recipe improved?"
|
113 |
+
|
114 |
+
if postprocessing == "no postprocessing":
|
115 |
+
resp = question_answer(question=question,
|
116 |
+
context=first_example,
|
117 |
+
top_k=5)
|
118 |
+
elif postprocessing == "remove substrings":
|
119 |
+
resp = question_answer(question=question,
|
120 |
context=first_example,
|
121 |
top_k=5)
|
122 |
+
resp = remove_substring(resp)
|
123 |
+
elif postprocessing == "iteration":
|
124 |
+
resp = remove_substring_iter(question_answer,
|
125 |
+
question,
|
126 |
+
first_example)
|
127 |
|
128 |
# Present results
|
129 |
st.markdown(f"""
|
|
|
133 |
|
134 |
{first_example}
|
135 |
|
136 |
+
The question asked was:
|
137 |
|
138 |
{question}
|
139 |
|
140 |
+
The answers were:
|
141 |
|
142 |
{resp}
|
|
|
|
|
|
|
|
|
|
|
143 |
""")
|