File size: 5,023 Bytes
de49fb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e02181b
 
 
 
 
 
 
 
 
 
 
 
de49fb6
e02181b
de49fb6
 
01e5900
de49fb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269a1cc
de49fb6
 
269a1cc
 
 
de49fb6
 
c00f050
 
269a1cc
 
de49fb6
 
 
 
 
269a1cc
de49fb6
269a1cc
de49fb6
 
269a1cc
de49fb6
 
 
 
 
269a1cc
524cc17
de49fb6
 
269a1cc
de49fb6
 
 
269a1cc
 
 
de49fb6
 
 
 
 
 
 
 
 
53b616d
de49fb6
 
 
 
ea5a12b
ebfd86a
de49fb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
269a1cc
 
 
 
 
 
 
de49fb6
269a1cc
 
 
 
 
 
 
de49fb6
269a1cc
 
 
de49fb6
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import gradio as gr
import torch
from transformers import BartTokenizer, BartForConditionalGeneration
from peft import PeftModel

# Model configuration
MODEL_NAME = "facebook/bart-large"
PEFT_MODEL_ID = "mohamedmostafa259/bart-emoji-translator"
MAX_LENGTH = 32

CREATIVITY_SETTINGS = {
    0: {"top_p": 0.2, "temperature": 0.5},  # Strict
    1: {"top_p": 0.4, "temperature": 1.0},  # Balanced
    2: {"top_p": 0.6, "temperature": 1.5},  # Creative
}

# Load model and tokenizer at startup (automatically cached)
print("Loading model...")
base_model = BartForConditionalGeneration.from_pretrained(MODEL_NAME)
model = PeftModel.from_pretrained(base_model, PEFT_MODEL_ID)
tokenizer = BartTokenizer.from_pretrained(PEFT_MODEL_ID)

# Set to eval mode
model.eval()

# Move to GPU if available
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device)

print(f"Model loaded on {device}")

# Translation function
def translate_to_emoji(text, creativity_level=0):
    if not text.strip():
        return "⚠️ Please enter some text!"
    
    try:
        tokens = tokenizer(text, return_tensors="pt", max_length=MAX_LENGTH, truncation=True).to(device)
        with torch.no_grad():
            outputs = model.generate(
                **tokens,
                max_length=MAX_LENGTH,
                num_beams=1,
                do_sample=True,
                temperature=CREATIVITY_SETTINGS[creativity_level]["temperature"],
                top_p=CREATIVITY_SETTINGS[creativity_level]["top_p"],
            )
        result = tokenizer.decode(outputs[0], skip_special_tokens=True).replace(" ", "") 
        return result if result else "🀷 (No translation generated)"
    
    except Exception as e:
        return f"❌ Error: {str(e)}"

# Create Gradio interface
with gr.Blocks(title="🎭 Emoji Translator", theme=gr.themes.Soft()) as demo:
    gr.Markdown(
        """
        # 🎭 BART Emoji Translator
        
        Transform your text into emojis! This model uses curriculum learning and LoRA fine-tuning 
        to translate English sentences into appropriate emoji sequences.
        
        **Dataset:** Synthetic dataset generated using **Gemini 3 Pro** to ensure high quality and diversity.
        
        **Examples:** Try "I am happy", "I eat dinner with my family", or "I feel misunderstood"
        """
    )
    
    with gr.Row():
        with gr.Column(scale=2):
            text_input = gr.Textbox(
                label="πŸ“ Enter your text",
                placeholder="Type something like: I am happy today...",
                lines=3
            )
            
            # --- Creativity Slider (0, 1, 2) ---
            creativity = gr.Slider(
                minimum=0,
                maximum=2,
                value=1,
                step=1,
                label="🎨 Creativity",
                info="0 = strict, 1 = balanced, 2 = creative (more randomness; maybe not accurate)"
            )

            translate_btn = gr.Button("✨ Translate to Emojis", variant="primary")
        
        with gr.Column(scale=1):
            emoji_output = gr.Textbox(
                label="πŸŽ‰ Emoji Translation",
                lines=3,
                scale=2
            )
    
    # Example inputs
    gr.Examples(
        examples=[
            ["I am happy", 0],
            ["I feel sad", 2],
            ["I eat dinner with my family", 1],
            ["I tweeted the news to my followers", 1],
            ["My parents want to have a new baby", 2],
        ],
        inputs=[text_input, creativity],
        outputs=emoji_output,
        fn=translate_to_emoji,
        cache_examples=False,
        run_on_click=True
    )
    
    # Button click event
    translate_btn.click(
        fn=translate_to_emoji,
        inputs=[text_input, creativity],
        outputs=emoji_output
    )
    
    # Also trigger on Enter key
    text_input.submit(
        fn=translate_to_emoji,
        inputs=[text_input, creativity],
        outputs=emoji_output
    )
    
    gr.Markdown(
        """
        ---
        ### πŸ“š Model Information
        
        - **Model:** BART-Large with LoRA fine-tuning
        - **Training:** 6-phase curriculum learning with strategic data retention
        - **Parameters:** 128 LoRA rank, 256 alpha
        - **Dataset:** Progressive difficulty from single emojis to complex sequences
        
        ### πŸ”— Links
        
        - [πŸ€— Model on HuggingFace](https://huggingface.co/mohamedmostafa259/bart-emoji-translator)
        - [πŸ’» Training Code](https://www.kaggle.com/code/mohamedmostafa259/emoji-translator-curriculum-learning)
        - [πŸ“Š Dataset](https://www.kaggle.com/datasets/mohamedmostafa259/english-to-emoji)

        ### ⚠️ Limitations
        
        - Works best with English text under 32 tokens
        - May not recognize very rare or newly created emojis
        - Performance varies with text complexity
        """
    )

# Launch the app
if __name__ == "__main__":
    demo.launch()