Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a question answering page #22

Merged
merged 1 commit into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions doccano_mini/examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,20 @@ def make_classification_example() -> pd.DataFrame:
return df


def make_question_answering_example() -> pd.DataFrame:
df = pd.DataFrame(
[
{
"context": "Google was founded by computer scientists Larry Page and Sergey Brin.",
"question": "Who founded Google?",
"answer": "Larry Page and Sergey Brin",
},
],
columns=["context", "question", "answer"],
)
return df


def make_task_free_example() -> pd.DataFrame:
df = pd.DataFrame([{"Column 1": "", "Column 2": ""}], columns=["Column 1", "Column 2"])
return df
28 changes: 28 additions & 0 deletions doccano_mini/pages/02_Question_Answering.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from typing import Dict, List

import streamlit as st

from doccano_mini.examples import make_question_answering_example
from doccano_mini.layout import BasePage
from doccano_mini.prompts import make_question_answering_prompt


class QuestionAnsweringPage(BasePage):
def make_examples(self, columns: List[str]):
df = make_question_answering_example()
edited_df = st.experimental_data_editor(df, num_rows="dynamic", width=1000)
examples = edited_df.to_dict(orient="records")
return examples

def make_prompt(self, examples: List[Dict]):
return make_question_answering_prompt(examples)

def prepare_inputs(self, columns: List[str]):
return {
"context": st.text_area(label="Context.", value="", height=300),
"question": st.text_area(label="Question.", value="", height=100),
}


page = QuestionAnsweringPage(title="Question Answering")
page.render()
22 changes: 22 additions & 0 deletions doccano_mini/prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,28 @@ def make_classification_prompt(examples: List[dict]) -> FewShotPromptTemplate:
return prompt


def make_question_answering_prompt(examples: List[dict]) -> FewShotPromptTemplate:
task_instruction = (
"You are a highly intelligent question answering bot. "
"You take context and question as input and return the answer from the context. "
"Retain as much information as needed to answer the question at a later time. "
"If you don't know the answer, you should return N/A."
)

example_prompt = PromptTemplate(
input_variables=["context", "question", "answer"],
template="context: {context}\nquestion: {question}\nanswer: {answer}",
)
prompt = FewShotPromptTemplate(
examples=examples,
example_prompt=example_prompt,
prefix=task_instruction,
suffix="context: {context}\nquestion: {question}",
input_variables=["context", "question"],
)
return prompt


def make_task_free_prompt(examples: List[dict]) -> FewShotPromptTemplate:
columns = list(examples[0])

Expand Down