File size: 834 Bytes
01a0479
8e2d616
 
01a0479
 
8e2d616
 
 
 
 
47c3535
8e2d616
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
from langchain.prompts import PromptTemplate

tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large-cnn")
model = AutoModelForSeq2SeqLM.from_pretrained("facebook/bart-large-cnn")

def generate_answer(context):
    prompt_template = PromptTemplate(template="Summarise the following context: {context}",
                                     input_variables=["context"], output_variables=["answer"])
    # Model loading
    format_prompt = prompt_template.format(context=context)
    encoded_input = tokenizer(format_prompt, return_tensors='pt')
    # Run the model
    output = model.generate(**encoded_input)  # Use generate method for text generation
    # Decode the model output to text
    decoded_output = tokenizer.decode(output[0])
    return decoded_output