Spaces:
Runtime error
Runtime error
Commit
Β·
7128b84
1
Parent(s):
b8a7cf9
WIP
Browse files
app.py
CHANGED
|
@@ -72,25 +72,39 @@ class Interactive:
|
|
| 72 |
|
| 73 |
interactive = Interactive()
|
| 74 |
|
| 75 |
-
def predict(statement,
|
| 76 |
result = interactive.run(statement)
|
| 77 |
-
|
| 78 |
-
row = {
|
| 79 |
-
'timestamp': datetime.datetime.now().strftime('%Y%m%d-%H%M%S'),
|
| 80 |
-
'statement': statement,
|
| 81 |
-
'logit': result['logit'],
|
| 82 |
-
'logit_calibrated': result['logit_calibrated'],
|
| 83 |
-
'score': result['score'],
|
| 84 |
-
'score_calibrated': result['score_calibrated'],
|
| 85 |
-
}
|
| 86 |
-
json.dump(row, f, ensure_ascii=False)
|
| 87 |
-
f.write('\n')
|
| 88 |
-
commit_url = repo.push_to_hub()
|
| 89 |
-
print(commit_url)
|
| 90 |
-
return {
|
| 91 |
'True': result['score_calibrated'],
|
| 92 |
'False': 1 - result['score_calibrated'],
|
| 93 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
examples = [
|
| 96 |
# openbookqa
|
|
@@ -155,17 +169,55 @@ examples = [
|
|
| 155 |
# 'If A sits next to B and B sits next to C, then A might not sit next to C.',
|
| 156 |
]
|
| 157 |
|
| 158 |
-
input_statement = gr.Dropdown(choices=examples, label='Statement:')
|
| 159 |
-
input_model = gr.Textbox(label='Commonsense statement verification model:', value=MODEL_NAME, interactive=False)
|
| 160 |
-
output = gr.outputs.Label(num_top_classes=2)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
-
|
| 163 |
-
β οΈ Data Collection: by default, we are collecting the inputs entered in this app to further improve and evaluate the model. Do not share any personal or sensitive information while using the app!
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
|
| 165 |
-
|
| 166 |
-
fn=predict,
|
| 167 |
-
inputs=[input_statement, input_model],
|
| 168 |
-
outputs=output,
|
| 169 |
-
title="Vera",
|
| 170 |
-
description=description,
|
| 171 |
-
).launch()
|
|
|
|
| 72 |
|
| 73 |
interactive = Interactive()
|
| 74 |
|
| 75 |
+
def predict(statement, do_save=True):
|
| 76 |
result = interactive.run(statement)
|
| 77 |
+
output = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
'True': result['score_calibrated'],
|
| 79 |
'False': 1 - result['score_calibrated'],
|
| 80 |
}
|
| 81 |
+
output_raw = {
|
| 82 |
+
'timestamp': datetime.datetime.now().strftime('%Y%m%d-%H%M%S'),
|
| 83 |
+
'statement': statement,
|
| 84 |
+
}
|
| 85 |
+
output_raw.update(result)
|
| 86 |
+
if do_save:
|
| 87 |
+
with open(DATA_PATH, 'a') as f:
|
| 88 |
+
json.dump(output_raw, f, ensure_ascii=False)
|
| 89 |
+
f.write('\n')
|
| 90 |
+
commit_url = repo.push_to_hub()
|
| 91 |
+
print('Logged statement to dataset:')
|
| 92 |
+
print('Commit URL:', commit_url)
|
| 93 |
+
print(output_raw)
|
| 94 |
+
print()
|
| 95 |
+
return output, output_raw
|
| 96 |
+
|
| 97 |
+
def record_feedback(output_raw, feedback, do_save=True):
|
| 98 |
+
if do_save:
|
| 99 |
+
output_raw.update({ 'feedback': feedback })
|
| 100 |
+
with open(DATA_PATH, 'a') as f:
|
| 101 |
+
json.dump(output_raw, f, ensure_ascii=False)
|
| 102 |
+
f.write('\n')
|
| 103 |
+
commit_url = repo.push_to_hub()
|
| 104 |
+
print('Logged feedback to dataset:')
|
| 105 |
+
print('Commit URL:', commit_url)
|
| 106 |
+
print(output_raw)
|
| 107 |
+
print()
|
| 108 |
|
| 109 |
examples = [
|
| 110 |
# openbookqa
|
|
|
|
| 169 |
# 'If A sits next to B and B sits next to C, then A might not sit next to C.',
|
| 170 |
]
|
| 171 |
|
| 172 |
+
# input_statement = gr.Dropdown(choices=examples, label='Statement:')
|
| 173 |
+
# input_model = gr.Textbox(label='Commonsense statement verification model:', value=MODEL_NAME, interactive=False)
|
| 174 |
+
# output = gr.outputs.Label(num_top_classes=2)
|
| 175 |
+
|
| 176 |
+
# description = '''This is a demo for Vera, a commonsense statement verification model. Under development.
|
| 177 |
+
# β οΈ Data Collection: by default, we are collecting the inputs entered in this app to further improve and evaluate the model. Do not share any personal or sensitive information while using the app!'''
|
| 178 |
+
|
| 179 |
+
# gr.Interface(
|
| 180 |
+
# fn=predict,
|
| 181 |
+
# inputs=[input_statement, input_model],
|
| 182 |
+
# outputs=output,
|
| 183 |
+
# title="Vera",
|
| 184 |
+
# description=description,
|
| 185 |
+
# ).launch()
|
| 186 |
+
|
| 187 |
+
with gr.Blocks() as demo:
|
| 188 |
+
with gr.Column():
|
| 189 |
+
gr.Markdown(
|
| 190 |
+
'''# Vera
|
| 191 |
|
| 192 |
+
This is a demo for Vera, a commonsense statement verification model. Under development.
|
| 193 |
+
β οΈ Data Collection: by default, we are collecting the inputs entered in this app to further improve and evaluate the model. Do not share any personal or sensitive information while using the app!
|
| 194 |
+
'''
|
| 195 |
+
)
|
| 196 |
+
with gr.Row():
|
| 197 |
+
with gr.Column(scale=1):
|
| 198 |
+
do_save = gr.Checkbox(
|
| 199 |
+
value=True,
|
| 200 |
+
label="Store data",
|
| 201 |
+
info="You agree to the storage of your prompt and generated text for research and development purposes:")
|
| 202 |
+
statement = gr.Textbox(placeholder="Enter a commonsense statement here", label="statement", elem_id="q-input")
|
| 203 |
+
submit = gr.Button(label='Submit', variant='primary')
|
| 204 |
+
with gr.Column(scale=1):
|
| 205 |
+
output = gr.outputs.Label(num_top_classes=2)
|
| 206 |
+
output_raw = gr.outputs.JSON(visible=True)
|
| 207 |
+
with gr.Row():
|
| 208 |
+
feedback_agree = gr.Button(label='π Agree', variant='primary')
|
| 209 |
+
feedback_disagree = gr.Button(label='π Disagree', variant='primary')
|
| 210 |
+
with gr.Row():
|
| 211 |
+
gr.Examples(
|
| 212 |
+
examples=examples,
|
| 213 |
+
inputs=[statement],
|
| 214 |
+
cache_examples=False,
|
| 215 |
+
fn=predict,
|
| 216 |
+
outputs=[output, output_raw],
|
| 217 |
+
)
|
| 218 |
+
submit.click(predict, inputs=[statement, do_save], outputs=[output])
|
| 219 |
+
statement.submit(predict, inputs=[statement], outputs=[output])
|
| 220 |
+
feedback_agree.click(record_feedback, inputs=[output_raw, 'agree', do_save])
|
| 221 |
+
feedback_disagree.click(record_feedback, inputs=[output_raw, 'disagree', do_save])
|
| 222 |
|
| 223 |
+
demo.queue(concurrency_count=16).launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|