nicolay-r commited on
Commit
7139804
·
verified ·
1 Parent(s): 21b2050

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +63 -1
README.md CHANGED
@@ -29,8 +29,70 @@ 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
 
36
  Please refer to the [related section](https://github.com/nicolay-r/Reasoning-for-Sentiment-Analysis-Framework?tab=readme-ov-file#three-hop-chain-of-thought-thor) of the **Reasoning-for-Sentiment-Analysis** Framework
 
29
 
30
  ### Direct Use
31
 
 
32
 
33
+ This sequence of scripts represent a purely `torch` and `transformers` based model usage for inference.
34
+
35
+ This example is also available on [GoogleColab](https://github.com/nicolay-r/Reasoning-for-Sentiment-Analysis-Framework/blob/main/FlanT5_Finetuned_Model_Usage.ipynb)
36
+
37
+ Here are the **following three steps for a quick start with model application**:
38
+
39
+
40
+ 1. Loading model and tokenizer
41
+
42
+ ```python
43
+ import torch
44
+ from transformers import AutoTokenizer, T5ForConditionalGeneration
45
+
46
+ # Setup model path.
47
+ model_path = "nicolay-r/flan-t5-tsa-thor-xl"
48
+ # Setup device.
49
+ device = "cuda:0"
50
+
51
+ model = T5ForConditionalGeneration.from_pretrained(model_path, torch_dtype=torch.bfloat16)
52
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
53
+ model.to(device)
54
+ ```
55
+
56
+ 2. Setup ask method for generating LLM responses
57
+ ```python
58
+ def ask(prompt):
59
+ inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
60
+ inputs.to(device)
61
+ output = model.generate(**inputs, temperature=1)
62
+ return tokenizer.batch_decode(output, skip_special_tokens=True)[0]
63
+ ```
64
+
65
+ 2. Setup Chain-of-Thought
66
+ ```python
67
+ def target_sentiment_extraction(sentence, target):
68
+ # Setup labels.
69
+ labels_list = ['neutral', 'positive', 'negative']
70
+ # Setup Chain-of-Thought
71
+ step1 = f"Given the sentence {sentence}, which specific aspect of {target} is possibly mentioned?"
72
+ aspect = ask(step1)
73
+ 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?"
74
+ opinion = ask(step2)
75
+ step3 = f"{step2}. The opinion towards the mentioned aspect of {target} is {opinion}. Based on such opinion, what is the sentiment polarity towards {target}?"
76
+ emotion_state = ask(step3)
77
+ 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))
78
+ # Return the final response.
79
+ return ask(step4)
80
+ ```
81
+
82
+ Finally, you can infer model results as follows:
83
+ ```python
84
+ # Input sentence.
85
+ sentence = "I would support him despite his bad behavior."
86
+ # Input target.
87
+ target = "him"
88
+ # output response
89
+ flant5_response = target_sentiment_extraction(sentence, target)
90
+ print(f"Author opinion towards `{target}` in `{sentence}` is:\n{flant5_response}")
91
+ ```
92
+
93
+ The response of the model is as follows:
94
+ > Author opinion towards `him` in `I would support him despite his bad behavior.` is: **positive**
95
+
96
  ### Downstream Use
97
 
98
  Please refer to the [related section](https://github.com/nicolay-r/Reasoning-for-Sentiment-Analysis-Framework?tab=readme-ov-file#three-hop-chain-of-thought-thor) of the **Reasoning-for-Sentiment-Analysis** Framework