patil-suraj / question_generation

Neural question generation using transformers
MIT License
1.11k stars 348 forks source link

How to start? #50

Open sampreethachitagubbi15 opened 3 years ago

sampreethachitagubbi15 commented 3 years ago

hey it would be of great help if you would guide us ...in understanding your project ......as the details mentioned in the readme file is bit hard to undertsand.

matt-mkidd-ko commented 3 years ago

I would recommend going straight to the colab link:

https://colab.research.google.com/github/patil-suraj/question_generation/blob/master/question_generation.ipynb

I adapted it slightly to get the results I wanted in a clear way:

First cell (get dependencies)

!pip install -U transformers==3.0.0
!python -m nltk.downloader punkt
!git clone https://github.com/patil-suraj/question_generation.git
%cd question_generation

Second cell (initiate the multi task (question answering AND question generation)):

from pipelines import pipeline

# load in the multi task qa qg
# may take a few seconds...
MODEL = pipeline("multitask-qa-qg")

Third cell (down to business ;) ):

def print_result(text, questions=None):
  """Extract and pretty print the q and a from text

  Does two things:

  - QA: asks questions if you supply them
  - QGQA: automatically generates questions and answers

  Args:
      text: the text you want to extract things from
      questions: what do you want to ask, can be str or list
  """

  # optionally answer any questions that you feed in
  if questions is not None:

    # convert single q to a list for consistency
    if type(questions)==str:
      questions = [questions]

    print("Answer the questions:")
    print()
    for question in questions:
      print('\t', 'Q:\t', question)
      print('\t', 'A:\t', MODEL({'question': question, 'context': text}))
      print()

  # auto generate questions and answers
  print()
  print("Auto generated questions and answers:")
  print()
  for d in MODEL(text):
    print('\t', 'Q:\t', d['question'])
    print('\t', 'A:\t', d['answer'])
    print()
  print()

# take example text from this repo
text = """
Question generation is the task of automatically generating questions from a text paragraph. The most straight-forward way for this is answer aware question generation. In answer aware question generation the model is presented with the answer and the passage and asked to generate a question for that answer by considering the passage context. While there are many papers available for QG task, it's still not as mainstream as QA. One of the reasons is most of the earlier papers use complicated models/processing pipelines and have no pre-trained models available. Few recent papers, specifically UniLM and ProphetNet have SOTA pre-trained weights availble for QG but the usage seems quite complicated.
"""

# questions you want to answer
questions = [
    'what is question generation?',
    'what are models from recent papers?'
]

# answer questions AND auto generate other QAs
print_result(text, questions=questions)

The output from that is the following:

Answer the custom quetions:

     Q:  what is question generation?
     A:  the task of automatically generating questions from a text paragraph

     Q:  what are models from recent papers?
     A:  UniLM and ProphetNet

Auto generated questions and answers:

     Q:  What is the task of automatically generating questions from a text paragraph?
     A:  Question generation

     Q:  What is the most straight-forward way for generating questions from a text paragraph?
     A:  answer aware question generation

     Q:  What is the model presented with in answer aware question generation?
     A:  the answer and the passage

     Q:  What is the name of the mainstream paper available for QG?
     A:  QA

     Q:  What do most of the earlier papers use?
     A:  complicated models/processing pipelines

     Q:  What pre-trained weights are available for QG?
     A:  SOTA

Hope this helps!