Views
No views yet
1from transformers import T5ForConditionalGeneration, T5Tokenizer
2model = T5ForConditionalGeneration.from_pretrained("kiri-ai/t5-base-qa-summary-emotion")
3tokenizer = T5Tokenizer.from_pretrained("kiri-ai/t5-base-qa-summary-emotion")
4
5def get_answer(question, prev_qa, context):
6 input_text = [f"q: {qa[0]} a: {qa[1]}" for qa in prev_qa]
7 input_text.append(f"q: {question}")
8 input_text.append(f"c: {context}")
9 input_text = " ".join(input_text)
10 features = tokenizer([input_text], return_tensors='pt')
11 tokens = model.generate(input_ids=features['input_ids'],
12 attention_mask=features['attention_mask'], max_length=64)
13 return tokenizer.decode(tokens[0], skip_special_tokens=True)
14
15print(get_answer("Why is the moon yellow?", "I'm not entirely sure why the moon is yellow.")) # unknown
16
17context = "Elon Musk left OpenAI to avoid possible future conflicts with his role as CEO of Tesla."
18
19print(get_answer("Why not?", [("Does Elon Musk still work with OpenAI", "No")], context)) # to avoid possible future conflicts with his role as CEO of Tesla1from kiri.models import T5QASummaryEmotion
2
3context = "Elon Musk left OpenAI to avoid possible future conflicts with his role as CEO of Tesla."
4prev_qa = [("Does Elon Musk still work with OpenAI", "No")]
5model = T5QASummaryEmotion()
6
7# Leave prev_qa blank for non conversational question-answering
8model.qa("Why not?", context, prev_qa=prev_qa)
9> "to avoid possible future conflicts with his role as CEO of Tesla"1from transformers import T5ForConditionalGeneration, T5Tokenizer
2model = T5ForConditionalGeneration.from_pretrained("kiri-ai/t5-base-qa-summary-emotion")
3tokenizer = T5Tokenizer.from_pretrained("kiri-ai/t5-base-qa-summary-emotion")
4
5def summary(context):
6 input_text = f"summarize: {context}"
7 features = tokenizer([input_text], return_tensors='pt')
8 tokens = model.generate(input_ids=features['input_ids'],
9 attention_mask=features['attention_mask'], max_length=64)
10 return tokenizer.decode(tokens[0], skip_special_tokens=True)1from kiri.models import T5QASummaryEmotion
2
3model = T5QASummaryEmotion()
4
5model.summarise("Long text to summarise")
6> "Short summary of long text"1from transformers import T5ForConditionalGeneration, T5Tokenizer
2model = T5ForConditionalGeneration.from_pretrained("kiri-ai/t5-base-qa-summary-emotion")
3tokenizer = T5Tokenizer.from_pretrained("kiri-ai/t5-base-qa-summary-emotion")
4
5def emotion(context):
6 input_text = f"emotion: {context}"
7 features = tokenizer([input_text], return_tensors='pt')
8 tokens = model.generate(input_ids=features['input_ids'],
9 attention_mask=features['attention_mask'], max_length=64)
10 return tokenizer.decode(tokens[0], skip_special_tokens=True)1from kiri.models import T5QASummaryEmotion
2
3model = T5QASummaryEmotion()
4
5model.emotion("I hope this works!")
6> "optimism"