Use Gradio to implement the Question Answering interactive question-and-answer interface. First, you need to have a trained Question Answering model. Here you mentioned that you want to use the bert-base-chinese model.
Gradio supports PyTorch and TensorFlow models, so you need to convert the bert-base-chinese model to PyTorch or TensorFlow format for use in Gradio.
Here, I will demonstrate how to use the Hugging Face Transformers library (PyTorch version) to load the bert-base-chinese model and use Gradio to create an interactive question-answer interface.
Make sure you have installed the necessary libraries:
pip install gradio torch transformers
Then, an interactive question-and-answer interface can be implemented with the following code:
import gradio as gr
import torch
from transformers import BertTokenizer, BertForQuestionAnswering
# 加载bert-base-chinese模型和分词器
model_name = "bert-base-chinese"
tokenizer = BertTokenizer.from_pretrained(model_name)
model = BertForQuestionAnswering.from_pretrained(model_name)
def question_answering(context, question):
# 使用分词器对输入进行处理
inputs = tokenizer(question, context, return_tensors="pt")
# 调用模型进行问答
outputs = model(**inputs)
# 获取答案的起始和结束位置
start_scores = outputs.start_logits
end_scores = outputs.end_logits
# 获取最佳答案
answer_start = torch.argmax(start_scores)
answer_end = torch.argmax(end_scores) + 1
answer = tokenizer.decode(inputs["input_ids"][0][answer_start:answer_end])
return answer
# 创建Gradio界面
interface = gr.Interface(
fn=question_answering,
inputs=["text", "text"], # 输入分别为context和question
outputs="text", # 输出为答案
)
interface.launch()
After running the above code, Gradio will start a local interactive interface. You can enter text on the left side of the interface, fill in the context and question respectively, and then click the "Answer" button, and the answer output of the model will be displayed on the right side. Please make sure that the entered context contains relevant information about the question, and question is the question you are asking.
Terminal output:
Visit the link http://127.0.0.1:7860/, so that a Question Answering interactive question-and-answer interface based on the bert-base-chinese model can be implemented in Gradio.