mohamedmostafa259 commited on
Commit
de49fb6
Β·
1 Parent(s): 2743a8c

add app.py

Browse files
Files changed (1) hide show
  1. app.py +154 -0
app.py ADDED
@@ -0,0 +1,154 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import BartTokenizer, BartForConditionalGeneration
4
+ from peft import PeftModel
5
+
6
+ # Model configuration
7
+ MODEL_NAME = "facebook/bart-large"
8
+ PEFT_MODEL_ID = "mohamedmostafa259/bart-emoji-translator"
9
+ MAX_LENGTH = 32
10
+
11
+ CREATIVITY_SETTINGS = {
12
+ 0: {"top_p": 0.2, "temperature": 0.5}, # Strict
13
+ 1: {"top_p": 0.4, "temperature": 1.0}, # Balanced
14
+ 2: {"top_p": 0.6, "temperature": 1.5}, # Creative
15
+ }
16
+
17
+ # Load model and tokenizer
18
+ @gr.cache
19
+ def load_model():
20
+ print("Loading model...")
21
+ base_model = BartForConditionalGeneration.from_pretrained(MODEL_NAME)
22
+ model = PeftModel.from_pretrained(base_model, PEFT_MODEL_ID)
23
+ tokenizer = BartTokenizer.from_pretrained(PEFT_MODEL_ID)
24
+
25
+ # Set to eval mode
26
+ model.eval()
27
+
28
+ # Move to GPU if available
29
+ device = "cuda" if torch.cuda.is_available() else "cpu"
30
+ model.to(device)
31
+
32
+ print(f"Model loaded on {device}")
33
+ return model, tokenizer, device
34
+
35
+ # Initialize model
36
+ model, tokenizer, device = load_model()
37
+
38
+ # Translation function
39
+ def translate_to_emoji(text, creativity_level=1):
40
+ if not text.strip():
41
+ return "⚠️ Please enter some text!"
42
+
43
+ try:
44
+ tokens = tokenizer(text, return_tensors="pt", max_length=MAX_LENGTH, truncation=True).to(device)
45
+ with torch.no_grad():
46
+ outputs = model.generate(
47
+ **tokens,
48
+ max_length=MAX_LENGTH,
49
+ num_beams=1,
50
+ do_sample=True,
51
+ temperature=CREATIVITY_SETTINGS[creativity_level]["temperature"],
52
+ top_p=CREATIVITY_SETTINGS[creativity_level]["top_p"],
53
+ )
54
+ result = tokenizer.decode(outputs[0], skip_special_tokens=True).replace(" ", "")
55
+ return result if result else "🀷 (No translation generated)"
56
+
57
+ except Exception as e:
58
+ return f"❌ Error: {str(e)}"
59
+
60
+ # Create Gradio interface
61
+ with gr.Blocks(title="🎭 Emoji Translator", theme=gr.themes.Soft()) as demo:
62
+ gr.Markdown(
63
+ """
64
+ # 🎭 BART Emoji Translator
65
+
66
+ Transform your text into emojis! This model uses curriculum learning and LoRA fine-tuning
67
+ to translate English sentences into appropriate emoji sequences.
68
+
69
+ **Examples:** Try "I am happy", "I eat dinner with my family", or "I feel misunderstood"
70
+ """
71
+ )
72
+
73
+ with gr.Row():
74
+ with gr.Column(scale=2):
75
+ text_input = gr.Textbox(
76
+ label="πŸ“ Enter your text",
77
+ placeholder="Type something like: I am happy today...",
78
+ lines=3
79
+ )
80
+
81
+ # --- Creativity Slider (0, 1, 2) ---
82
+ creativity = gr.Slider(
83
+ minimum=0,
84
+ maximum=2,
85
+ value=1,
86
+ step=1,
87
+ label="🎨 Creativity",
88
+ info="0 = strict, 1 = balanced, 2 = creative"
89
+ )
90
+
91
+ translate_btn = gr.Button("✨ Translate to Emojis", variant="primary")
92
+
93
+ with gr.Column(scale=1):
94
+ emoji_output = gr.Textbox(
95
+ label="πŸŽ‰ Emoji Translation",
96
+ lines=3,
97
+ scale=2
98
+ )
99
+
100
+ # Example inputs
101
+ gr.Examples(
102
+ examples=[
103
+ ["I am happy", 0],
104
+ ["I feel sad", 2],
105
+ ["I eat dinner with my family", 1],
106
+ ["I tweeted the news to my followers", 1],
107
+ ["Parents sleep together to have more children", 1],
108
+ ],
109
+ inputs=[text_input, creativity],
110
+ outputs=emoji_output,
111
+ fn=translate_to_emoji,
112
+ cache_examples=True,
113
+ )
114
+
115
+ # Button click event
116
+ translate_btn.click(
117
+ fn=translate_to_emoji,
118
+ inputs=[text_input, creativity],
119
+ outputs=emoji_output
120
+ )
121
+
122
+ # Also trigger on Enter key
123
+ text_input.submit(
124
+ fn=translate_to_emoji,
125
+ inputs=[text_input, creativity],
126
+ outputs=emoji_output
127
+ )
128
+
129
+ gr.Markdown(
130
+ """
131
+ ---
132
+ ### πŸ“š Model Information
133
+
134
+ - **Model:** BART-Large with LoRA fine-tuning
135
+ - **Training:** 6-phase curriculum learning with strategic data retention
136
+ - **Parameters:** 128 LoRA rank, 256 alpha
137
+ - **Dataset:** Progressive difficulty from single emojis to complex sequences
138
+
139
+ ### πŸ”— Links
140
+
141
+ - [Model on HuggingFace](https://huggingface.co/mohamedmostafa259/bart-emoji-translator)
142
+ - [Training Code](https://www.kaggle.com/code/mohamedmostafa259/emoji-translator-curriculum)
143
+
144
+ ### ⚠️ Limitations
145
+
146
+ - Works best with English text under 32 tokens
147
+ - May not recognize very rare or newly created emojis
148
+ - Performance varies with text complexity
149
+ """
150
+ )
151
+
152
+ # Launch the app
153
+ if __name__ == "__main__":
154
+ demo.launch()