Views
No views yet
1from transformers import T5Tokenizer, T5ForConditionalGeneration
2
3# Initialize the tokenizer and model
4tokenizer = T5Tokenizer.from_pretrained("mrutyunjay-patil/keywordGen-v2")
5model = T5ForConditionalGeneration.from_pretrained("mrutyunjay-patil/keywordGen-v2")
6
7# Define your input sequence, prefixing with "Keyword: "
8input_sequence = "Keyword: I purchased the new Android smartphone last week and I've been thoroughly impressed. The display is incredibly vibrant and sharp, and the battery life is surprisingly good, easily lasting a full day with heavy usage."
9
10# Encode the input sequence
11input_ids = tokenizer.encode(input_sequence, return_tensors="pt")
12
13# Generate output
14outputs = model.generate(input_ids)
15output_sequence = tokenizer.decode(outputs[0], skip_special_tokens=True)
16
17print(output_sequence)1from transformers import T5Tokenizer, T5ForConditionalGeneration
2
3# Initialize the tokenizer and model
4tokenizer = T5Tokenizer.from_pretrained("mrutyunjay-patil/keywordGen-v2")
5model = T5ForConditionalGeneration.from_pretrained("mrutyunjay-patil/keywordGen-v2")
6
7# Define the prefix
8task_prefix = "Keyword: "
9
10# Define your list of input sequences
11inputs = [
12 "Absolutely love this tablet. It has a clear, sharp screen and runs apps smoothly without any hiccups.",
13 "The headphones are fantastic with great sound quality, but the build quality could be better.",
14 "Bought this smartwatch last week, and I'm thrilled with its performance. Battery life is impressive.",
15 "This laptop exceeded my expectations. Excellent speed, plenty of storage, and light weight. Perfect for my needs.",
16 "The camera quality on this phone is exceptional. It captures detailed and vibrant photos. However, battery life is not the best."
17]
18
19
20# Loop through each input and generate keywords
21for sample in inputs:
22 input_sequence = task_prefix + sample
23 input_ids = tokenizer.encode(input_sequence, return_tensors="pt")
24 outputs = model.generate(input_ids)
25 output_sequence = tokenizer.decode(outputs[0], skip_special_tokens=True)
26 print(sample, "\n --->", output_sequence)