Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import pipeline | |
| # Load the models only once at the beginning of the script. | |
| # This happens when the Gradio app is first launched, not with every button click. | |
| bert_pipeline = pipeline( | |
| "question-answering", | |
| model="bert-large-uncased-whole-word-masking-finetuned-squad" | |
| ) | |
| roberta_pipeline = pipeline( | |
| "question-answering", | |
| model="deepset/roberta-base-squad2" | |
| ) | |
| # The function now simply uses the already-loaded pipeline objects. | |
| def answer_question(c, q): | |
| bert_answer = bert_pipeline(question=q, context=c)['answer'] | |
| roberta_answer = roberta_pipeline(question=q, context=c)['answer'] | |
| return bert_answer, roberta_answer | |
| # Interface using gradio | |
| with gr.Blocks() as demo: | |
| gr.Markdown("Question Answering session with BERT and RoBERTa Model") | |
| context_input = gr.Textbox(label='Context', placeholder="Enter the context information here....") | |
| question_input = gr.Textbox(label='Question', placeholder="Enter the question here one by one....") | |
| bert_output = gr.Textbox(label='Bert Answer') | |
| robert_output = gr.Textbox(label='Roberta Answer') | |
| submit_button = gr.Button("Get Answers") | |
| # Click action | |
| submit_button.click(answer_question, inputs=[context_input, question_input], outputs=[bert_output, robert_output]) | |
| demo.launch() |