File size: 1,012 Bytes
fd71869
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from transformers import pipeline

model = pipeline("question-answering", model="Eitanli/distilbert-qa-checkpoint-v3")


questions = ['which ingredients are mentioned?',
             'what is the amount of ingredient mentioned?',
             'what are the special instructions mentioned?']


def predict(context, topk, answer_threshold):
    output = {}
    for question in questions:
        output['Question'] = question
        answers = model(question=question, context=context, topk=topk)
        output['Answer'] = '\n'.join([ans['answer'] for ans in answers if ans['score'] > answer_threshold])
    return output


iface = gr.Interface(
    fn=predict,
    inputs=[
        gr.Textbox(label="Recipe line"),
        gr.Slider(0, 0.99, step=0.01, value=0.8, label="answer_threshold", info="Select a threshold in [0, 0.99]"),
        gr.Slider(1, 5, step=1.0, value=2, label="top k", info="Choose between 1 and 5")],
    outputs=gr.Textbox(label='Questions and answers')
)
iface.launch()