UnboundLocalError: local variable 'par' referenced before assignment:

49 Views Asked by At

I want to get the context data into a par variable to be processed into the pipeline context=par. but got this error, how can i fix this ?

def index(request):
    qas_form = QasForms()
    post_form = MyForm(request.POST)
    db_context = Context.objects.filter(category=post_form)

    for con in db_context :
        par= con.context

    context = {
        'heading' : 'Question Answering',
        'data_form' :qas_form,
        'form' : post_form,
        'Cont' : db_context,
    }

    model_name = BertForQuestionAnswering.from_pretrained('./save_model', from_tf=True)
    tokenizer = BertTokenizer.from_pretrained('./save_model')
    nlp = pipeline('question-answering', model=model_name, tokenizer=tokenizer)
        
    if request.method == 'POST':
        question = request.POST['question']
        par = hasil
        result = nlp(question=question, context=par)
        context['question'] = question
        context['answer'] = result['answer']
    return render(request,'qas/index.html',context)

I am trying to get a solution to this problem

1

There are 1 best solutions below

0
On

You need to initialize par variable as None at before for loop then you can assign value like par = con.context

qas_form = QasForms()
post_form = MyForm(request.POST)
db_context = Context.objects.filter(category=post_form)
par = None # here need to initialize  
for con in db_context :
    par= con.context