Spaces:
Runtime error
Runtime error
File size: 5,764 Bytes
a715bd8 c2ee91b a715bd8 c2ee91b a715bd8 c2ee91b a715bd8 c2ee91b a715bd8 c2ee91b a715bd8 fe414ee 1410875 a715bd8 fe414ee 1410875 a715bd8 33d5720 f74f2ff 33d5720 a715bd8 1410875 a715bd8 fe414ee a715bd8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
"""Gradio app to validate examples of the FoQA dataset."""
from functools import partial
import os
from typing import Generator
import gradio as gr
from datasets import Dataset, load_dataset
import logging
import pandas as pd
import os
from dotenv import load_dotenv
load_dotenv()
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("foqa")
dataset = load_dataset(
"alexandrainst/foqa", split="train", token=os.getenv("HF_HUB_TOKEN")
)
assert isinstance(dataset, Dataset)
df = pd.DataFrame(dataset.to_pandas())
def non_validated_samples() -> Generator[tuple[str, str, str], None, None]:
"""Iterate over non-validated samples in the FoQA dataset.
Yields:
A tuple (idx, question, answer) of a non-validated sample.
"""
for idx, sample in df.iterrows():
if sample.validation is None:
yield str(idx), sample.question, sample.answers["text"][0]
itr = non_validated_samples()
def main():
idx, question, answer = next(itr)
with gr.Blocks(theme="monochrome", title="FoQA validation") as demo:
gr.Markdown("""
# FoQA Validation
This app automatically fetches examples from the Faroese Question Answering
dataset (FoQA), allowing you to annotate whether the question and answer
are correct Faroese or not.
""")
with gr.Row():
with gr.Column():
gr.Markdown("### Sample ID")
idx_box = gr.Markdown(value=idx)
gr.Markdown("### Question")
question_box = gr.Markdown(value=question)
gr.Markdown("### Answer")
answer_box = gr.Markdown(value=answer)
with gr.Column():
correct_btn = gr.Button(value="Correct")
incorrect_btn = gr.Button(value="Incorrect")
incorrect_answer_btn = gr.Button(value="Incorrect Answer")
save_results_btn = gr.Button(value="Save results")
correct_btn.click(
fn=partial(assign_correct, itr=itr),
inputs=[idx_box, question_box, answer_box],
outputs=[idx_box, question_box, answer_box],
)
incorrect_btn.click(
fn=partial(assign_incorrect, itr=itr),
inputs=[idx_box, question_box, answer_box],
outputs=[idx_box, question_box, answer_box],
)
incorrect_answer_btn.click(
fn=partial(assign_incorrect_answer, itr=itr),
inputs=[idx_box, question_box, answer_box],
outputs=[idx_box, question_box, answer_box],
)
save_results_btn.click(fn=save_results)
auth = [
("admin", os.environ["ADMIN_PASSWORD"]),
("annika", os.environ["ANNIKA_PASSWORD"]),
]
demo.launch(auth=auth)
def save_results() -> None:
"""Update the FoQA dataset with the validation status of a sample."""
logger.info("Saving results...")
gr.Info(message="Saving results...")
Dataset.from_pandas(df, preserve_index=False).push_to_hub(
repo_id="alexandrainst/foqa", token=os.getenv("HF_HUB_TOKEN")
)
gr.Info(message="Saved results!")
logger.info("Saved results.")
def assign_correct(
idx: str, question: str, answer: str, itr: Generator
) -> tuple[gr.Markdown, gr.Markdown, gr.Markdown]:
"""Assign the question and answer as correct.
Args:
idx:
The index of the sample to be assigned as correct.
question:
The question to be assigned as correct.
answer:
The answer to be assigned as correct.
itr:
The iterator over non-validated samples.
Returns:
The updated textboxes.
"""
gr.Info(message="Assigned sample as correct")
logger.info(f"Assigned sample as correct: {question} - {answer}")
df.iloc[int(idx)].validation = "correct"
idx, question, answer = next(itr)
return (
gr.Markdown(value=idx), gr.Markdown(value=question), gr.Markdown(value=answer)
)
def assign_incorrect(
idx: str, question: str, answer: str, itr: Generator
) -> tuple[gr.Markdown, gr.Markdown, gr.Markdown]:
"""Assign the question and answer as incorrect.
Args:
idx:
The index of the sample to be assigned as incorrect.
question:
The question to be assigned as incorrect.
answer:
The answer to be assigned as incorrect.
itr:
The iterator over non-validated samples.
Returns:
The updated textboxes.
"""
gr.Info(message="Assigned sample as incorrect")
logger.info(f"Assigned sample as incorrect: {question} - {answer}")
df.iloc[int(idx)].validation = "incorrect"
idx, question, answer = next(itr)
return (
gr.Markdown(value=idx), gr.Markdown(value=question), gr.Markdown(value=answer)
)
def assign_incorrect_answer(
idx: str, question: str, answer: str, itr: Generator
) -> tuple[gr.Markdown, gr.Markdown, gr.Markdown]:
"""Assign the answer as incorrect.
Args:
idx:
The index of the sample to be assigned as incorrect.
question:
The question to be assigned as incorrect.
answer:
The answer to be assigned as incorrect.
itr:
The iterator over non-validated samples.
Returns:
The updated textboxes.
"""
gr.Info(message="Assigned sample answer as incorrect")
logger.info(f"Assigned sample answer as incorrect: {answer}")
df.iloc[int(idx)].validation = "incorrect-answer"
idx, question, answer = next(itr)
return (
gr.Markdown(value=idx), gr.Markdown(value=question), gr.Markdown(value=answer)
)
if __name__ == "__main__":
main()
|