Spaces:
Paused
Paused
File size: 1,810 Bytes
e15f1e0 9a229a7 993e75e 75eaa7d 1c8cf8d ca6ca1c 993e75e ca6ca1c e15f1e0 32a4ae2 14a130a 01c2292 14a130a 01c2292 b574413 5f6fcc3 1da755f 5f6fcc3 b574413 efdb5f5 1da755f 499634c 14a130a b574413 efdb5f5 14a130a 5f6fcc3 571da3b 14a130a e15f1e0 b574413 |
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 |
import gradio as gr
from transformers import BertForQuestionAnswering
from transformers import BertTokenizerFast
import torch
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
tokenizer = BertTokenizerFast.from_pretrained('bert-base-uncased')
model = BertForQuestionAnswering.from_pretrained("CountingMstar/ai-tutor-bert-model").to(device)
def get_prediction(context, question):
inputs = tokenizer.encode_plus(question, context, return_tensors='pt').to(device)
outputs = model(**inputs)
answer_start = torch.argmax(outputs.start_logits)
answer_end = torch.argmax(outputs.end_logits) + 1
answer = tokenizer.convert_tokens_to_string(tokenizer.convert_ids_to_tokens(inputs['input_ids'][0][answer_start:answer_end]))
return answer
def question_answer(context, question):
prediction = get_prediction(context, question)
return prediction
def submit(context, question):
answer = question_answer(context, question)
return answer
examples = [
["A large language model is...", "What is a large language model?"],
["Feature engineering is the process of...", "What is Feature engineering?"],
["Attention mechanism calculates soft weights...", "What is Attention mechanism?"]
]
input_textbox = gr.Textbox("Context", placeholder="Enter context here")
question_textbox = gr.Textbox("Question", placeholder="Enter question here")
input_section = gr.Row([input_textbox, question_textbox])
markdown_text = """
## Example Questions
Use the examples below or enter your own context and question.
"""
iface = gr.Interface(
fn=submit,
inputs=input_section,
outputs=gr.Textbox("Answer"),
examples=examples,
live=True, # Set live to True to use the submit button
title="BERT Question Answering"
)
iface.launch() |