nicolay-r commited on
Commit
c505fbb
·
verified ·
1 Parent(s): 775e09a

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +59 -1
README.md CHANGED
@@ -29,7 +29,65 @@ pipeline_tag: text2text-generation
29
 
30
  ### Direct Use
31
 
32
- <!-- This section is for the model use without fine-tuning or plugging into a larger ecosystem/app. -->
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
  ### Downstream Use
35
 
 
29
 
30
  ### Direct Use
31
 
32
+ This sequence of scripts represent a purely `torch` and `transformers` based model usage for inference.
33
+
34
+ This example is also available on [GoogleColab](https://github.com/nicolay-r/Reasoning-for-Sentiment-Analysis-Framework/blob/main/FlanT5_Finetuned_Model_Usage.ipynb)
35
+
36
+ Here are the **following three steps for a quick start with model application**:
37
+
38
+
39
+ 1. Loading model and tokenizer
40
+
41
+ ```python
42
+ import torch
43
+ from transformers import AutoTokenizer, T5ForConditionalGeneration
44
+
45
+ # Setup model path.
46
+ model_path = "nicolay-r/flan-t5-tsa-thor-xl"
47
+ # Setup device.
48
+ device = "cuda:0"
49
+
50
+ model = T5ForConditionalGeneration.from_pretrained(model_path, torch_dtype=torch.bfloat16)
51
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
52
+ model.to(device)
53
+ ```
54
+
55
+ 2. Setup ask method for generating LLM responses
56
+ ```python
57
+ def ask(prompt):
58
+ inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
59
+ inputs.to(device)
60
+ output = model.generate(**inputs, temperature=1)
61
+ return tokenizer.batch_decode(output, skip_special_tokens=True)[0]
62
+ ```
63
+
64
+ 2. Setup Chain-of-Thought
65
+ ```python
66
+ def target_sentiment_extraction(sentence, target):
67
+ # Setup labels.
68
+ labels_list = ['neutral', 'positive', 'negative']
69
+ # Setup Chain-of-Thought
70
+ step1 = f"Given the sentence {sentence}, which specific aspect of {target} is possibly mentioned?"
71
+ aspect = ask(step1)
72
+ step2 = f"{step1}. The mentioned aspect is about {aspect}. Based on the common sense, what is the implicit opinion towards the mentioned aspect of {target}, and why?"
73
+ opinion = ask(step2)
74
+ step3 = f"{step2}. The opinion towards the mentioned aspect of {target} is {opinion}. Based on such opinion, what is the sentiment polarity towards {target}?"
75
+ emotion_state = ask(step3)
76
+ step4 = f"{step3}. The sentiment polarity is {emotion_state}. Based on these contexts, summarize and return the sentiment polarity only, " + "such as: {}.".format(", ".join(labels_list))
77
+ # Return the final response.
78
+ return ask(step4)
79
+ ```
80
+
81
+ Finally, you can infer model results as follows:
82
+ ```python
83
+ # Input sentence.
84
+ sentence = "Over the past 28 years, the leader has been working hard to achieve the release of Peltier and is a member of the Leonard Peltier Defense Committee."
85
+ # Input target.
86
+ target = "Peltier"
87
+ # output response
88
+ flant5_response = target_sentiment_extraction(sentence, target)
89
+ print(f"Author opinion towards `{target}` in `{sentence}` is:\n{flant5_response}")
90
+ ```
91
 
92
  ### Downstream Use
93