Views
No views yet
1# prerequisites
2!pip install transformers
3from transformers import pipeline # Pipeline is needed to import the model
4
5
6# Loading model & setup
7model_name_30 = 'FlorianSteigleder/NLP4W_30'
8model_30 = AutoModelForQuestionAnswering.from_pretrained(model_name_30) # First we load the model from our repo
9tokenizer_30 = AutoTokenizer.from_pretrained(model_name_30) # Then we load the tokenizer from our repo
10pipeline_30 = pipeline('question-answering', model=model_30, tokenizer=tokenizer_30) # Then we create an inference pipeline with the model and the tokenizer
11
12
13# Inference Example 1
14print(pipeline_30(
15 "Where do I live?", # Question
16 "My name is Caimbeul and I live in Scotland" # Context
17 )) # Full Inference result containing score, start, end and answer -> https://huggingface.co/docs/transformers/en/main_classes/pipelinesa
18# {'score': 0.9879983067512512, 'start': 34, 'end': 42, 'answer': 'Scotland'}
19
20# Inference Example 2
21print(pipeline_30(
22 "Which name is also used to describe the Amazon rainforest in English?", # Question
23 """The Amazon rainforest (Portuguese: Floresta Amazônica or Amazônia; Spanish: Selva Amazónica, Amazonía or usually Amazonia; French: Forêt amazonienne; Dutch: Amazoneregenwoud), also known in English as Amazonia or the Amazon Jungle, is a moist broadleaf forest that covers most of the Amazon basin of South America. This basin encompasses 7,000,000 square kilometres (2,700,000 sq mi), of which 5,500,000 square kilometres (2,100,000 sq mi) are covered by the rainforest. This region includes territory belonging to nine nations. The majority of the forest is contained within Brazil, with 60% of the rainforest, followed by Peru with 13%, Colombia with 10%, and with minor amounts in Venezuela, Ecuador, Bolivia, Guyana, Suriname and French Guiana. States or departments in four nations contain "Amazonas" in their names. The Amazon represents over half of the planet's remaining rainforests, and comprises the largest and most biodiverse tract of tropical rainforest in the world, with an estimated 390 billion individual trees divided into 16,000 species.""" # Context
24 )["answer"]) # Answer only output
25# Amazonia or the Amazon Jungle
26
27# Inference Example 3
28print(pipeline_30(
29 "Where is New York City?", # Question
30 "New York City is in the United States." # Context
31 )["answer"])
32# Germany