manthilaffs commited on
Commit
f250f88
·
verified ·
1 Parent(s): 4b2ede4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +152 -11
app.py CHANGED
@@ -1,7 +1,8 @@
1
  import gradio as gr
2
  import torch
3
  import spaces
4
- from transformers import AutoModelForCausalLM, AutoTokenizer
 
5
 
6
  model = None
7
  tokenizer = None
@@ -15,7 +16,7 @@ alpaca_prompt = """පහත දැක්වෙන්නේ යම් කාර
15
  {}"""
16
 
17
  @spaces.GPU
18
- def infer(message, history, enable_history=False, max_new_tokens=512):
19
  global model, tokenizer
20
 
21
  if model is None:
@@ -45,18 +46,52 @@ def infer(message, history, enable_history=False, max_new_tokens=512):
45
 
46
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
47
 
48
- with torch.inference_mode():
49
- outputs = model.generate(**inputs, max_new_tokens=max_new_tokens)
50
 
51
- text = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
- if "### ප්‍රතිචාරය:" in text:
54
- text = text.split("### ප්‍රතිචාරය:")[-1].strip()
 
55
 
56
- return text
 
 
 
 
 
 
 
 
 
57
 
58
- # Custom CSS for styling
59
  custom_css = """
 
 
 
 
 
 
 
 
 
 
 
 
60
  #splash-screen {
61
  position: fixed;
62
  top: 0;
@@ -93,6 +128,42 @@ custom_css = """
93
  75% { transform: rotateY(540deg) scale(1.2); opacity: 0.8; }
94
  }
95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  /* Smaller font sizes for chat */
97
  .message-wrap .message {
98
  font-size: 0.9rem !important;
@@ -102,6 +173,15 @@ custom_css = """
102
  font-size: 0.9rem !important;
103
  }
104
 
 
 
 
 
 
 
 
 
 
105
  /* Avatar styling */
106
  .message-wrap.user .avatar-container::before {
107
  content: "👤";
@@ -137,6 +217,67 @@ custom_css = """
137
  height: 40px !important;
138
  min-width: 40px !important;
139
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
140
  """
141
 
142
  # Splash screen HTML
@@ -152,12 +293,12 @@ splash_html = """
152
  """
153
 
154
  # ---------------- UI ----------------
155
- with gr.Blocks(css=custom_css) as demo:
156
  gr.HTML(splash_html)
157
 
158
  chat = gr.ChatInterface(
159
  fn=lambda message, history: infer(message, history, enable_history.value, max_new_tokens.value),
160
- title="🧠 Gamunu 4B Instruct",
161
  theme=gr.themes.Default(text_size="sm"),
162
  examples=[
163
  ["හෙලෝ ගැමුණු! මම සමන්, ඔයාට කොහොමද?"],
 
1
  import gradio as gr
2
  import torch
3
  import spaces
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
5
+ from threading import Thread
6
 
7
  model = None
8
  tokenizer = None
 
16
  {}"""
17
 
18
  @spaces.GPU
19
+ def infer(message, history, enable_history=False, max_new_tokens=1024):
20
  global model, tokenizer
21
 
22
  if model is None:
 
46
 
47
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
48
 
49
+ # Initialize the streamer
50
+ streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
51
 
52
+ # Generation parameters
53
+ generation_kwargs = dict(
54
+ **inputs,
55
+ max_new_tokens=max_new_tokens,
56
+ streamer=streamer,
57
+ do_sample=True,
58
+ temperature=0.7,
59
+ top_p=0.95,
60
+ )
61
+
62
+ # Start generation in a separate thread
63
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
64
+ thread.start()
65
 
66
+ # Stream the response
67
+ partial_text = ""
68
+ response_started = False
69
 
70
+ for new_text in streamer:
71
+ partial_text += new_text
72
+
73
+ # Check if we've reached the response section
74
+ if "### ප්‍රතිචාරය:" in partial_text and not response_started:
75
+ partial_text = partial_text.split("### ප්‍රතිචාරය:")[-1].strip()
76
+ response_started = True
77
+
78
+ if response_started:
79
+ yield partial_text
80
 
81
+ # Custom CSS for styling with copy button
82
  custom_css = """
83
+ /* Container width constraints for PC screens */
84
+ .gradio-container {
85
+ max-width: 1200px !important;
86
+ margin: 0 auto !important;
87
+ }
88
+
89
+ /* Chat interface max width */
90
+ .contain {
91
+ max-width: 900px !important;
92
+ margin: 0 auto !important;
93
+ }
94
+
95
  #splash-screen {
96
  position: fixed;
97
  top: 0;
 
128
  75% { transform: rotateY(540deg) scale(1.2); opacity: 0.8; }
129
  }
130
 
131
+ /* Copy button styling */
132
+ .message-wrap.bot {
133
+ position: relative;
134
+ }
135
+
136
+ .message-wrap.bot:hover .copy-button {
137
+ opacity: 1;
138
+ }
139
+
140
+ .copy-button {
141
+ position: absolute;
142
+ top: 8px;
143
+ right: 8px;
144
+ opacity: 0;
145
+ transition: opacity 0.2s;
146
+ background: rgba(102, 126, 234, 0.1);
147
+ border: 1px solid rgba(102, 126, 234, 0.3);
148
+ border-radius: 6px;
149
+ padding: 6px 10px;
150
+ cursor: pointer;
151
+ font-size: 12px;
152
+ color: #667eea;
153
+ z-index: 10;
154
+ }
155
+
156
+ .copy-button:hover {
157
+ background: rgba(102, 126, 234, 0.2);
158
+ border-color: rgba(102, 126, 234, 0.5);
159
+ }
160
+
161
+ .copy-button.copied {
162
+ background: rgba(34, 197, 94, 0.2);
163
+ border-color: rgba(34, 197, 94, 0.5);
164
+ color: #22c55e;
165
+ }
166
+
167
  /* Smaller font sizes for chat */
168
  .message-wrap .message {
169
  font-size: 0.9rem !important;
 
173
  font-size: 0.9rem !important;
174
  }
175
 
176
+ /* Compact examples grid */
177
+ .examples {
178
+ max-width: 100% !important;
179
+ }
180
+
181
+ .examples .wrap {
182
+ gap: 0.5rem !important;
183
+ }
184
+
185
  /* Avatar styling */
186
  .message-wrap.user .avatar-container::before {
187
  content: "👤";
 
217
  height: 40px !important;
218
  min-width: 40px !important;
219
  }
220
+
221
+ /* Compact padding */
222
+ .main {
223
+ padding: 1rem !important;
224
+ }
225
+
226
+ /* Title styling - more compact */
227
+ h1 {
228
+ font-size: 1.5rem !important;
229
+ margin-bottom: 0.5rem !important;
230
+ }
231
+
232
+ /* Streaming cursor effect */
233
+ .message-wrap.bot.streaming .message::after {
234
+ content: '▊';
235
+ animation: blink 1s infinite;
236
+ margin-left: 2px;
237
+ }
238
+
239
+ @keyframes blink {
240
+ 0%, 50% { opacity: 1; }
241
+ 51%, 100% { opacity: 0; }
242
+ }
243
+ """
244
+
245
+ # JavaScript for copy functionality
246
+ copy_js = """
247
+ <script>
248
+ function addCopyButtons() {
249
+ // Remove existing copy buttons first
250
+ document.querySelectorAll('.copy-button').forEach(btn => btn.remove());
251
+
252
+ // Add copy buttons to bot messages
253
+ const botMessages = document.querySelectorAll('.message-wrap.bot .message');
254
+ botMessages.forEach((message, index) => {
255
+ if (!message.querySelector('.copy-button')) {
256
+ const copyBtn = document.createElement('button');
257
+ copyBtn.className = 'copy-button';
258
+ copyBtn.innerHTML = '📋 Copy';
259
+ copyBtn.onclick = function(e) {
260
+ e.stopPropagation();
261
+ const text = message.innerText;
262
+ navigator.clipboard.writeText(text).then(() => {
263
+ copyBtn.innerHTML = '✅ Copied!';
264
+ copyBtn.classList.add('copied');
265
+ setTimeout(() => {
266
+ copyBtn.innerHTML = '📋 Copy';
267
+ copyBtn.classList.remove('copied');
268
+ }, 2000);
269
+ });
270
+ };
271
+ message.parentElement.style.position = 'relative';
272
+ message.parentElement.appendChild(copyBtn);
273
+ }
274
+ });
275
+ }
276
+
277
+ // Run on load and periodically to catch new messages
278
+ setInterval(addCopyButtons, 1000);
279
+ window.addEventListener('load', addCopyButtons);
280
+ </script>
281
  """
282
 
283
  # Splash screen HTML
 
293
  """
294
 
295
  # ---------------- UI ----------------
296
+ with gr.Blocks(css=custom_css, head=copy_js) as demo:
297
  gr.HTML(splash_html)
298
 
299
  chat = gr.ChatInterface(
300
  fn=lambda message, history: infer(message, history, enable_history.value, max_new_tokens.value),
301
+ title="🧠 Gamunu 4B Instruct - Demo",
302
  theme=gr.themes.Default(text_size="sm"),
303
  examples=[
304
  ["හෙලෝ ගැමුණු! මම සමන්, ඔයාට කොහොමද?"],