File size: 2,129 Bytes
9ac7967
d5372a7
 
c7a680a
 
 
 
d5372a7
 
c7a680a
 
d5372a7
 
 
 
c7a680a
d5372a7
60abd67
d5372a7
 
 
 
 
 
 
 
 
bb0478c
45432e5
 
b0b6edb
c7a680a
06ccb0a
3eb8c25
06ccb0a
 
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
import jax
import jax.numpy as jnp
from transformers import FlaxBigBirdForQuestionAnswering, BigBirdTokenizerFast
import gradio as gr

FLAX_MODEL_ID = "vasudevgupta/flax-bigbird-natural-questions"

if __name__ == "__main__":  
  model = FlaxBigBirdForQuestionAnswering.from_pretrained(FLAX_MODEL_ID, block_size=64, num_random_blocks=3)
  tokenizer = BigBirdTokenizerFast.from_pretrained(FLAX_MODEL_ID)
  
  @jax.jit
  def forward(*args, **kwargs):
    return model(*args, **kwargs)
  
  def get_answer(question, context):
  
      encoding = tokenizer(question, context, return_tensors="jax", max_length=512, padding="max_length", truncation=True)
      start_scores, end_scores = forward(**encoding).to_tuple()
  
      # Let's take the most likely token using `argmax` and retrieve the answer
      all_tokens = tokenizer.convert_ids_to_tokens(encoding["input_ids"][0].tolist())
  
      answer_tokens = all_tokens[jnp.argmax(start_scores): jnp.argmax(end_scores)+1]
      answer = tokenizer.decode(tokenizer.convert_tokens_to_ids(answer_tokens))
  
      return answer
  
  default_context = "Models like BERT, RoBERTa have a token limit of 512. But BigBird supports up to 4096 tokens! How does it do that? How can transformers be applied to longer sequences? In Abhishek Thakur's next Talks, I will discuss BigBird!! Attend this Friday, 9:30 PM IST Live link: https://www.youtube.com/watch?v=G22vNvHmHQ0.\nBigBird is a transformer based model which can process long sequences (upto 4096) very efficiently. RoBERTa variant of BigBird has shown outstanding results on long document question answering."
  question = gr.inputs.Textbox(lines=2, default="When is talk happening?", label="Question")
  context = gr.inputs.Textbox(lines=10, default=default_context, label="Context")

  title = "BigBird-RoBERTa"
  desc = "BigBird is a transformer based model which can process long sequences (upto 4096) very efficiently. RoBERTa variant of BigBird has shown outstanding results on long document question answering."

  gr.Interface(fn=get_answer, inputs=[question, context], outputs="text", title=title, description=desc).launch()