Skip to content

Commit

Permalink
Add a question answering page
Browse files Browse the repository at this point in the history
  • Loading branch information
Hironsan committed Mar 27, 2023
1 parent 387ee34 commit 0b7b583
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
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

0 comments on commit 0b7b583

Please sign in to comment.