Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,24 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import spaces
|
| 4 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
| 5 |
|
| 6 |
model = None
|
| 7 |
tokenizer = None
|
| 8 |
|
| 9 |
alpaca_prompt = """පහත දැක්වෙන්නේ යම් කාර්යයක් පිළිබඳ විස්තර කරන උපදෙසක් සහ එයට අදාළ තොරතුරු ඇතුළත් ආදානයකි. ඉල්ලූ කාර්යය නිවැරදිව සම්පූර්ණ කළ හැකි ප්රතිචාරයක් සපයන්න.
|
| 10 |
-
|
| 11 |
### උපදෙස:
|
| 12 |
{}
|
| 13 |
-
|
| 14 |
### ආදානය:
|
| 15 |
{}
|
| 16 |
-
|
| 17 |
### ප්රතිචාරය:
|
| 18 |
{}"""
|
| 19 |
|
| 20 |
@spaces.GPU
|
| 21 |
def infer(message, history, enable_history=False, max_new_tokens=512):
|
| 22 |
global model, tokenizer
|
|
|
|
| 23 |
if model is None:
|
| 24 |
tokenizer = AutoTokenizer.from_pretrained("manthilaffs/Gamunu-4B-Instruct-Alpha")
|
| 25 |
model = AutoModelForCausalLM.from_pretrained(
|
|
@@ -28,7 +27,7 @@ def infer(message, history, enable_history=False, max_new_tokens=512):
|
|
| 28 |
device_map="auto",
|
| 29 |
)
|
| 30 |
model.eval()
|
| 31 |
-
|
| 32 |
# Add history only if enabled
|
| 33 |
if enable_history and history:
|
| 34 |
prev = "\n".join(
|
|
@@ -37,29 +36,149 @@ def infer(message, history, enable_history=False, max_new_tokens=512):
|
|
| 37 |
context = f"{prev}\n\n{message}"
|
| 38 |
else:
|
| 39 |
context = message
|
| 40 |
-
|
| 41 |
prompt = alpaca_prompt.format(
|
| 42 |
"ඔබ ගැමුණු (Gamunu) නම් AI සහායකයායි. ඔබව නිර්මාණය කර ඇත්තේ මන්තිල විසිනි. "
|
| 43 |
"ඔබේ කාර්යය වන්නේ පරිශීලකයන්ගේ උපදෙස් නිවැරදිව පිලිපැදීම හා අසා ඇති ප්රශ්නවලට නිවැරදිව පිළිතුරු සපයමින් ඔවුන්ට සහය වීමයි.",
|
| 44 |
context.strip(),
|
| 45 |
"",
|
| 46 |
)
|
| 47 |
-
|
| 48 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
chat = gr.ChatInterface(
|
| 64 |
fn=lambda message, history: infer(message, history, enable_history.value, max_new_tokens.value),
|
| 65 |
title="🧠 Gamunu 4B Instruct — සිංහල LLM",
|
|
@@ -71,15 +190,16 @@ with gr.Blocks() as demo:
|
|
| 71 |
["ඔබ කවියෙකු ලෙස 'ගඟක්' ගැන කෙටි කවියක් ලියන්න."],
|
| 72 |
]
|
| 73 |
)
|
|
|
|
| 74 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 75 |
enable_history = gr.Checkbox(label="Enable chat history", value=False)
|
| 76 |
max_new_tokens = gr.Slider(64, 1024, value=512, step=32, label="🔢 Max New Tokens")
|
| 77 |
-
|
| 78 |
gr.Markdown("""
|
| 79 |
---
|
| 80 |
-
🪶 **Model:** [
|
| 81 |
© 2025 Gamunu Project | Experimental Release
|
| 82 |
""")
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
| 85 |
-
demo.launch()
|
|
|
|
| 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
|
| 9 |
|
| 10 |
alpaca_prompt = """පහත දැක්වෙන්නේ යම් කාර්යයක් පිළිබඳ විස්තර කරන උපදෙසක් සහ එයට අදාළ තොරතුරු ඇතුළත් ආදානයකි. ඉල්ලූ කාර්යය නිවැරදිව සම්පූර්ණ කළ හැකි ප්රතිචාරයක් සපයන්න.
|
|
|
|
| 11 |
### උපදෙස:
|
| 12 |
{}
|
|
|
|
| 13 |
### ආදානය:
|
| 14 |
{}
|
|
|
|
| 15 |
### ප්රතිචාරය:
|
| 16 |
{}"""
|
| 17 |
|
| 18 |
@spaces.GPU
|
| 19 |
def infer(message, history, enable_history=False, max_new_tokens=512):
|
| 20 |
global model, tokenizer
|
| 21 |
+
|
| 22 |
if model is None:
|
| 23 |
tokenizer = AutoTokenizer.from_pretrained("manthilaffs/Gamunu-4B-Instruct-Alpha")
|
| 24 |
model = AutoModelForCausalLM.from_pretrained(
|
|
|
|
| 27 |
device_map="auto",
|
| 28 |
)
|
| 29 |
model.eval()
|
| 30 |
+
|
| 31 |
# Add history only if enabled
|
| 32 |
if enable_history and history:
|
| 33 |
prev = "\n".join(
|
|
|
|
| 36 |
context = f"{prev}\n\n{message}"
|
| 37 |
else:
|
| 38 |
context = message
|
| 39 |
+
|
| 40 |
prompt = alpaca_prompt.format(
|
| 41 |
"ඔබ ගැමුණු (Gamunu) නම් AI සහායකයායි. ඔබව නිර්මාණය කර ඇත්තේ මන්තිල විසිනි. "
|
| 42 |
"ඔබේ කාර්යය වන්නේ පරිශීලකයන්ගේ උපදෙස් නිවැරදිව පිලිපැදීම හා අසා ඇති ප්රශ්නවලට නිවැරදිව පිළිතුරු සපයමින් ඔවුන්ට සහය වීමයි.",
|
| 43 |
context.strip(),
|
| 44 |
"",
|
| 45 |
)
|
| 46 |
+
|
| 47 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 48 |
+
|
| 49 |
+
# Setup streaming
|
| 50 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 51 |
+
|
| 52 |
+
generation_kwargs = dict(
|
| 53 |
+
**inputs,
|
| 54 |
+
max_new_tokens=max_new_tokens,
|
| 55 |
+
streamer=streamer,
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# Start generation in a separate thread
|
| 59 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 60 |
+
thread.start()
|
| 61 |
+
|
| 62 |
+
# Stream the output
|
| 63 |
+
partial_text = ""
|
| 64 |
+
response_started = False
|
| 65 |
+
|
| 66 |
+
for new_text in streamer:
|
| 67 |
+
partial_text += new_text
|
| 68 |
+
|
| 69 |
+
# Check if we've reached the response section
|
| 70 |
+
if not response_started and "### ප්රතිචාරය:" in partial_text:
|
| 71 |
+
partial_text = partial_text.split("### ප්රතිචාරය:")[-1].strip()
|
| 72 |
+
response_started = True
|
| 73 |
+
|
| 74 |
+
if response_started:
|
| 75 |
+
yield partial_text
|
| 76 |
+
elif "### ප්රතිචාරය:" not in prompt:
|
| 77 |
+
# If prompt doesn't contain the marker, stream everything
|
| 78 |
+
yield partial_text
|
| 79 |
+
|
| 80 |
+
thread.join()
|
| 81 |
|
| 82 |
+
# Custom CSS for styling
|
| 83 |
+
custom_css = """
|
| 84 |
+
#splash-screen {
|
| 85 |
+
position: fixed;
|
| 86 |
+
top: 0;
|
| 87 |
+
left: 0;
|
| 88 |
+
width: 100vw;
|
| 89 |
+
height: 100vh;
|
| 90 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 91 |
+
display: flex;
|
| 92 |
+
justify-content: center;
|
| 93 |
+
align-items: center;
|
| 94 |
+
z-index: 9999;
|
| 95 |
+
animation: fadeOut 1s ease-in-out 3s forwards;
|
| 96 |
+
}
|
| 97 |
|
| 98 |
+
@keyframes fadeOut {
|
| 99 |
+
to {
|
| 100 |
+
opacity: 0;
|
| 101 |
+
visibility: hidden;
|
| 102 |
+
}
|
| 103 |
+
}
|
| 104 |
|
| 105 |
+
.sinhala-animation {
|
| 106 |
+
font-size: 120px;
|
| 107 |
+
font-weight: bold;
|
| 108 |
+
color: white;
|
| 109 |
+
animation: letterRotate 3s ease-in-out infinite;
|
| 110 |
+
text-shadow: 0 0 20px rgba(255,255,255,0.5);
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
@keyframes letterRotate {
|
| 114 |
+
0%, 100% { transform: rotateY(0deg) scale(1); opacity: 1; }
|
| 115 |
+
25% { transform: rotateY(180deg) scale(1.2); opacity: 0.8; }
|
| 116 |
+
50% { transform: rotateY(360deg) scale(1); opacity: 1; }
|
| 117 |
+
75% { transform: rotateY(540deg) scale(1.2); opacity: 0.8; }
|
| 118 |
+
}
|
| 119 |
|
| 120 |
+
/* Smaller font sizes for chat */
|
| 121 |
+
.message-wrap .message {
|
| 122 |
+
font-size: 0.9rem !important;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
.message-wrap p {
|
| 126 |
+
font-size: 0.9rem !important;
|
| 127 |
+
}
|
| 128 |
+
|
| 129 |
+
/* Avatar styling */
|
| 130 |
+
.message-wrap.user .avatar-container::before {
|
| 131 |
+
content: "👤";
|
| 132 |
+
font-size: 24px;
|
| 133 |
+
display: flex;
|
| 134 |
+
align-items: center;
|
| 135 |
+
justify-content: center;
|
| 136 |
+
width: 40px;
|
| 137 |
+
height: 40px;
|
| 138 |
+
border-radius: 50%;
|
| 139 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
.message-wrap.bot .avatar-container::before {
|
| 143 |
+
content: "🧠";
|
| 144 |
+
font-size: 24px;
|
| 145 |
+
display: flex;
|
| 146 |
+
align-items: center;
|
| 147 |
+
justify-content: center;
|
| 148 |
+
width: 40px;
|
| 149 |
+
height: 40px;
|
| 150 |
+
border-radius: 50%;
|
| 151 |
+
background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
/* Hide default avatars if they exist */
|
| 155 |
+
.avatar-container img {
|
| 156 |
+
display: none;
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
.avatar-container {
|
| 160 |
+
width: 40px !important;
|
| 161 |
+
height: 40px !important;
|
| 162 |
+
min-width: 40px !important;
|
| 163 |
+
}
|
| 164 |
+
"""
|
| 165 |
+
|
| 166 |
+
# Splash screen HTML
|
| 167 |
+
splash_html = """
|
| 168 |
+
<div id="splash-screen">
|
| 169 |
+
<div class="sinhala-animation">ගැමුණු</div>
|
| 170 |
+
</div>
|
| 171 |
+
<script>
|
| 172 |
+
setTimeout(() => {
|
| 173 |
+
document.getElementById('splash-screen').style.display = 'none';
|
| 174 |
+
}, 4000);
|
| 175 |
+
</script>
|
| 176 |
+
"""
|
| 177 |
+
|
| 178 |
+
# ---------------- UI ----------------
|
| 179 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 180 |
+
gr.HTML(splash_html)
|
| 181 |
+
|
| 182 |
chat = gr.ChatInterface(
|
| 183 |
fn=lambda message, history: infer(message, history, enable_history.value, max_new_tokens.value),
|
| 184 |
title="🧠 Gamunu 4B Instruct — සිංහල LLM",
|
|
|
|
| 190 |
["ඔබ කවියෙකු ලෙස 'ගඟක්' ගැන කෙටි කවියක් ලියන්න."],
|
| 191 |
]
|
| 192 |
)
|
| 193 |
+
|
| 194 |
with gr.Accordion("⚙️ Advanced Settings", open=False):
|
| 195 |
enable_history = gr.Checkbox(label="Enable chat history", value=False)
|
| 196 |
max_new_tokens = gr.Slider(64, 1024, value=512, step=32, label="🔢 Max New Tokens")
|
| 197 |
+
|
| 198 |
gr.Markdown("""
|
| 199 |
---
|
| 200 |
+
🪶 **Model:** [manthilaffs/Gamunu-4B-Instruct-Alpha](https://huggingface.co/manthilaffs/Gamunu-4B-Instruct-Alpha)
|
| 201 |
© 2025 Gamunu Project | Experimental Release
|
| 202 |
""")
|
| 203 |
|
| 204 |
if __name__ == "__main__":
|
| 205 |
+
demo.launch()
|