Elixpo commited on
Commit
67ad21d
Β·
verified Β·
1 Parent(s): 5ae8dbb

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +209 -0
README.md ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ datasets:
4
+ - Elixpo/Emoji-Text-Sentiment-Translation
5
+ language:
6
+ - en
7
+ metrics:
8
+ - rouge
9
+ base_model:
10
+ - google/mt5-small
11
+ ---
12
+ # Emoji Contextual Translator
13
+
14
+ This project leverages a fine-tuned T5 model to translate emoji sentences into plain English. The model was trained to understand contextual emoji usage and provide natural language translations.
15
+
16
+ ## Model Overview
17
+
18
+ The model is based on the T5 (Text-to-Text Transfer Transformer) architecture, fine-tuned on a custom dataset containing emoji-sentiment sentences. It accepts emoji-rich sentences and translates them into a meaningful plain-text description.
19
+
20
+ ## Files
21
+
22
+ - `t5-emoji-model-final-retrain2/`: Contains the fine-tuned T5 model and tokenizer.
23
+ - `test_inference.py`: A script for performing inference using the trained model.
24
+ - `test_rouge.py`: A script for evaluating the model’s output using ROUGE metrics and plotting results.
25
+
26
+ ## Requirements
27
+
28
+ - Python 3.7+
29
+ - PyTorch
30
+ - HuggingFace Transformers
31
+ - matplotlib
32
+ - rouge_score
33
+
34
+ You can install the necessary dependencies with the following:
35
+
36
+ ```bash
37
+ pip install torch transformers matplotlib rouge-score
38
+ ```
39
+
40
+ # Usage
41
+ ## Running Inference
42
+ - To run the inference, get the code followup!
43
+ ```python
44
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
45
+ import torch
46
+
47
+ # Load the hosted model and tokenizer
48
+ model_name = "Elixpo/Emoji-Contextual-Translator"
49
+ tokenizer = T5Tokenizer.from_pretrained(model_name)
50
+ model = T5ForConditionalGeneration.from_pretrained(model_name)
51
+
52
+ # Set model to evaluation mode
53
+ model.eval()
54
+
55
+ # Check for GPU availability
56
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
57
+ model.to(device)
58
+
59
+ # Example emoji sentences for inference
60
+ emoji_inputs = [
61
+ "Translate the emoji sentence to plain English: I just got my first job πŸ˜ŽπŸ’Ό",
62
+ "Translate the emoji sentence to plain English: Feeling so relaxed after yoga πŸ§˜β€β™€οΈβœ¨",
63
+ "Translate the emoji sentence to plain English: Celebrating my friend's birthday πŸŽ‚πŸŽ‰",
64
+ "Translate the emoji sentence to plain English: Watching a beautiful sunset πŸŒ…β€οΈ",
65
+ "Translate the emoji sentence to plain English: So proud of my accomplishments πŸ†πŸŽ‰",
66
+ "Translate the emoji sentence to plain English: Eating my favorite pizza πŸ•πŸ˜",
67
+ "Translate the emoji sentence to plain English: Going on a fun road trip πŸš—πŸŽΆ",
68
+ "Translate the emoji sentence to plain English: Finally home after a long day 🏑😌",
69
+ "Translate the emoji sentence to plain English: Feeling adventurous today 🌍✈️",
70
+ "Translate the emoji sentence to plain English: Spending time with family πŸ’•πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦",
71
+ "Translate the emoji sentence to plain English: Getting ready for a workout πŸ‹οΈβ€β™€οΈπŸ’ͺ",
72
+ "Translate the emoji sentence to plain English: Taking a break with some coffee β˜•πŸ“–",
73
+ "Translate the emoji sentence to plain English: Happy to be outdoors 🌳🌞",
74
+ "Translate the emoji sentence to plain English: Enjoying a rainy day indoors πŸŒ§οΈπŸ“š",
75
+ "Translate the emoji sentence to plain English: Excited for the new season of my favorite show πŸ“ΊπŸΏ",
76
+ "Translate the emoji sentence to plain English: I am feeling confident today πŸ’β€β™‚οΈπŸ’«",
77
+ "Translate the emoji sentence to plain English: Can't wait to see my friends this weekend πŸ‘―β€β™‚οΈπŸ»",
78
+ "Translate the emoji sentence to plain English: I love spending time at the beach πŸ–οΈπŸŒŠ",
79
+ "Translate the emoji sentence to plain English: I'm so proud of my hard work πŸ’ΌπŸ’ͺ",
80
+ "Translate the emoji sentence to plain English: Taking a stroll in the park πŸŒ³πŸšΆβ€β™‚οΈ"
81
+ ]
82
+
83
+ # Run inference on the emoji inputs
84
+ for input_text in emoji_inputs:
85
+ # Tokenize the input text
86
+ input_ids = tokenizer.encode(input_text, return_tensors="pt", truncation=True).to(device)
87
+
88
+ # Generate output using the model
89
+ output_ids = model.generate(
90
+ input_ids,
91
+ max_length=20,
92
+ num_beams=5, # Beam search for better quality
93
+ no_repeat_ngram_size=1, # Prevent repetition of n-grams
94
+ temperature=0.7, # Higher temperature for more varied outputs
95
+ top_p=0.9, # Top-p sampling to avoid too repetitive or deterministic outputs
96
+ top_k=70, # Restrict to the top k most likely tokens
97
+ early_stopping=True
98
+ )
99
+
100
+ # Decode and print the output text
101
+ output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
102
+ print(f"\nInput: {input_text}\nOutput: {output_text}")
103
+ ```
104
+
105
+ ## Running ROUGE Evaluation
106
+ - To evaluate the model’s performance on a set of example sentences using the ROUGE metric, use the following script:
107
+ ```python
108
+ from rouge_score import rouge_scorer
109
+ from transformers import T5Tokenizer, T5ForConditionalGeneration
110
+ import torch
111
+
112
+ # Load the hosted model and tokenizer
113
+ model_name = "Elixpo/Emoji-Contextual-Translator"
114
+ tokenizer = T5Tokenizer.from_pretrained(model_name)
115
+ model = T5ForConditionalGeneration.from_pretrained(model_name)
116
+
117
+ # Set model to evaluation mode
118
+ model.eval()
119
+
120
+ # Check for GPU availability
121
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
122
+ model.to(device)
123
+
124
+ # Example emoji sentences for inference
125
+ emoji_inputs = [
126
+ "Translate the emoji sentence to plain English: I just got my first job πŸ˜ŽπŸ’Ό",
127
+ "Translate the emoji sentence to plain English: Feeling so relaxed after yoga πŸ§˜β€β™€οΈβœ¨",
128
+ "Translate the emoji sentence to plain English: Celebrating my friend's birthday πŸŽ‚πŸŽ‰",
129
+ "Translate the emoji sentence to plain English: Watching a beautiful sunset πŸŒ…β€οΈ",
130
+ "Translate the emoji sentence to plain English: So proud of my accomplishments πŸ†πŸŽ‰",
131
+ "Translate the emoji sentence to plain English: Eating my favorite pizza πŸ•πŸ˜",
132
+ "Translate the emoji sentence to plain English: Going on a fun road trip πŸš—πŸŽΆ",
133
+ "Translate the emoji sentence to plain English: Finally home after a long day 🏑😌",
134
+ "Translate the emoji sentence to plain English: Feeling adventurous today 🌍✈️",
135
+ "Translate the emoji sentence to plain English: Spending time with family πŸ’•πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦",
136
+ "Translate the emoji sentence to plain English: Getting ready for a workout πŸ‹οΈβ€β™€οΈπŸ’ͺ",
137
+ "Translate the emoji sentence to plain English: Taking a break with some coffee β˜•πŸ“–",
138
+ "Translate the emoji sentence to plain English: Happy to be outdoors 🌳🌞",
139
+ "Translate the emoji sentence to plain English: Enjoying a rainy day indoors πŸŒ§οΈπŸ“š",
140
+ "Translate the emoji sentence to plain English: Excited for the new season of my favorite show πŸ“ΊπŸΏ",
141
+ "Translate the emoji sentence to plain English: I am feeling confident today πŸ’β€β™‚οΈπŸ’«",
142
+ "Translate the emoji sentence to plain English: Can't wait to see my friends this weekend πŸ‘―β€β™‚οΈπŸ»",
143
+ "Translate the emoji sentence to plain English: I love spending time at the beach πŸ–οΈπŸŒŠ",
144
+ "Translate the emoji sentence to plain English: I'm so proud of my hard work πŸ’ΌπŸ’ͺ",
145
+ "Translate the emoji sentence to plain English: Taking a stroll in the park πŸŒ³πŸšΆβ€β™‚οΈ"
146
+ ]
147
+
148
+ # Initialize the ROUGE scorer
149
+ scorer = rouge_scorer.RougeScorer(["rouge1", "rouge2", "rougeL"], use_stemmer=True)
150
+
151
+ # Store the ROUGE scores
152
+ rouge1_scores = []
153
+ rouge2_scores = []
154
+ rougeL_scores = []
155
+ for input_text in emoji_inputs:
156
+ # Tokenize the input text
157
+ input_ids = tokenizer.encode(input_text, return_tensors="pt", truncation=True).to(device)
158
+
159
+ # Generate output using the model
160
+ output_ids = model.generate(
161
+ input_ids,
162
+ max_length=20,
163
+ num_beams=5, # Beam search for better quality
164
+ no_repeat_ngram_size=1, # Prevent repetition of n-grams
165
+ temperature=0.7, # Higher temperature for more varied outputs
166
+ top_p=0.9, # Top-p sampling to avoid too repetitive or deterministic outputs
167
+ top_k=70, # Restrict to the top k most likely tokens
168
+ early_stopping=True
169
+ )
170
+
171
+ # Decode the generated output
172
+ output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
173
+
174
+ # For metric evaluation: decode the input text (to compare against) and compute ROUGE scores
175
+ decoded_input_text = input_text.split(": ")[1] # Remove the instruction part
176
+ scores = scorer.score(decoded_input_text, output_text)
177
+
178
+ rouge1_scores.append(scores["rouge1"].fmeasure)
179
+ rouge2_scores.append(scores["rouge2"].fmeasure)
180
+ rougeL_scores.append(scores["rougeL"].fmeasure)
181
+
182
+ print(f"\nInput: {input_text}\nOutput: {output_text}")
183
+ print(f"ROUGE-1: {scores['rouge1'].fmeasure:.4f}, ROUGE-2: {scores['rouge2'].fmeasure:.4f}, ROUGE-L: {scores['rougeL'].fmeasure:.4f}")
184
+
185
+ # Step 2: Plotting the ROUGE Scores
186
+ import matplotlib.pyplot as plt
187
+
188
+ epochs = list(range(1, len(rouge1_scores) + 1))
189
+ plt.figure(figsize=(10, 6))
190
+ plt.plot(epochs, rouge1_scores, label="ROUGE-1", color="blue", marker="o")
191
+ plt.plot(epochs, rouge2_scores, label="ROUGE-2", color="green", marker="x")
192
+ plt.plot(epochs, rougeL_scores, label="ROUGE-L", color="red", marker="s")
193
+ plt.xlabel("Sample Index")
194
+ plt.ylabel("ROUGE Score")
195
+ plt.title("ROUGE Scores for Emoji to Text Translation")
196
+ plt.legend()
197
+ plt.grid(True)
198
+ plt.show()
199
+ ```
200
+
201
+
202
+
203
+ # Training Instructions
204
+
205
+ To retrain or fine-tune the model, use the scripts provided in the repository, which require a dataset of emoji-sentiment sentences. Make sure your dataset is formatted properly before starting the fine-tuning process.
206
+
207
+ ---
208
+
209
+ Feel free to adapt this README to fit your exact project details, and let me know if there are any adjustments you need!