File size: 1,218 Bytes
f6f725c
 
 
 
a0d3657
f6f725c
a0d3657
 
 
 
e714e4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f6f725c
e714e4a
 
f6f725c
 
e714e4a
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
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
import gradio as grad
import ast

# 1. The RoBERTa base model is used, fine-tuned using the SQuAD 2.0 dataset. 
# It’s been trained on question-answer pairs, including unanswerable questions, for the task of question and answering.
# mdl_name = "deepset/roberta-base-squad2"
# my_pipeline = pipeline('question-answering', model=mdl_name, tokenizer=mdl_name)

# 2. Different model.
# mdl_name = "distilbert-base-cased-distilled-squad"
# my_pipeline = pipeline('question-answering', model=mdl_name, tokenizer=mdl_name)

# def answer_question(question,context):
#     text= "{"+"'question': '"+question+"','context': '"+context+"'}"
#     di=ast.literal_eval(text)
#     response = my_pipeline(di)
#     return response

# grad.Interface(answer_question, inputs=["text","text"], outputs="text").launch()

# 3. Different task: language translation.
# First model translates English to German.
mdl_name = "Helsinki-NLP/opus-mt-en-de"
opus_translator = pipeline("translation", model=mdl_name)

def translate(text):
    response = opus_translator(text)
    return response

grad.Interface(translate, inputs=["text",], outputs="text").launch()