Spaces:
Sleeping
Sleeping
File size: 10,320 Bytes
da784c8 efe44f0 da784c8 efe44f0 18f610c da784c8 efe44f0 46da343 efe44f0 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d c1e9709 4d7115d efe44f0 c1e9709 4d7115d c1e9709 4d7115d c1e9709 efe44f0 a1130ae 4d7115d c1e9709 4d7115d a71fb86 4d7115d a71fb86 4d7115d efe44f0 4d7115d c1e9709 |
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
import os
from threading import Thread
from typing import Iterator
import gradio as gr
import spaces
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
import subprocess
subprocess.run(
"pip install flash-attn --no-build-isolation",
env={"FLASH_ATTENTION_SKIP_CUDA_BUILD": "TRUE"},
shell=True,
)
# Constants
MAX_MAX_NEW_TOKENS = 2048
DEFAULT_MAX_NEW_TOKENS = 1024
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
# Model initialization
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model_id = "DeepMount00/Lexora-Lite-3B"
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto",
torch_dtype=torch.bfloat16,
attn_implementation="flash_attention_2",
trust_remote_code=True,
)
model.eval()
# Custom CSS
CUSTOM_CSS = """
/* Base styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Inter', sans-serif;
background-color: #f8fafc;
color: #1e293b;
}
/* Container styles */
.container {
max-width: 1000px !important;
margin: auto !important;
padding: 2rem !important;
}
/* Header styles */
.header-container {
background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 100%);
padding: 2.5rem;
border-radius: 1rem;
margin-bottom: 2rem;
color: white;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}
.header-title {
font-size: 2.5rem;
font-weight: 700;
margin-bottom: 1.5rem;
text-align: center;
letter-spacing: -0.025em;
}
/* Model info styles */
.model-info {
background: white;
padding: 1.75rem;
border-radius: 0.75rem;
margin-top: 1.5rem;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}
.model-info h2 {
font-size: 1.5rem;
font-weight: 600;
color: #1e3a8a;
margin-bottom: 1rem;
}
.model-info p {
color: #374151;
line-height: 1.6;
font-size: 1.1rem;
}
.model-info a {
color: #2563eb;
font-weight: 600;
text-decoration: none;
transition: color 0.2s;
}
.model-info a:hover {
color: #1d4ed8;
text-decoration: underline;
}
/* Chat container styles */
.chat-container {
border: 1px solid #e5e7eb;
border-radius: 1rem;
background: white;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
margin-bottom: 2rem;
}
/* Message styles */
.message {
padding: 1.25rem;
margin: 0.75rem;
border-radius: 0.5rem;
font-size: 1.05rem;
line-height: 1.6;
}
.user-message {
background: #f3f4f6;
border-left: 4px solid #3b82f6;
}
.assistant-message {
background: #dbeafe;
border-left: 4px solid #1d4ed8;
}
/* Controls container styles */
.controls-container {
background: #f8fafc;
padding: 1.75rem;
border-radius: 0.75rem;
margin-top: 1.5rem;
border: 1px solid #e5e7eb;
}
/* Slider styles */
.slider-label {
font-weight: 600;
color: #374151;
margin-bottom: 0.5rem;
font-size: 1.05rem;
}
/* Button styles */
.duplicate-button {
background: #2563eb !important;
color: white !important;
padding: 0.875rem 1.75rem !important;
border-radius: 0.5rem !important;
font-weight: 600 !important;
font-size: 1.05rem !important;
transition: all 0.2s !important;
border: none !important;
cursor: pointer !important;
display: inline-flex !important;
align-items: center !important;
justify-content: center !important;
text-align: center !important;
box-shadow: 0 2px 4px rgba(37, 99, 235, 0.2) !important;
}
.duplicate-button:hover {
background: #1d4ed8 !important;
transform: translateY(-1px) !important;
box-shadow: 0 4px 6px rgba(37, 99, 235, 0.3) !important;
}
/* Input field styles */
.input-textarea {
border: 2px solid #e5e7eb !important;
border-radius: 0.5rem !important;
padding: 1rem !important;
font-size: 1.05rem !important;
transition: border-color 0.2s !important;
}
.input-textarea:focus {
border-color: #3b82f6 !important;
outline: none !important;
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1) !important;
}
/* System message styles */
.system-message {
background: #f8fafc;
border: 1px solid #e5e7eb;
border-radius: 0.5rem;
padding: 1rem;
margin-bottom: 1rem;
}
"""
# HTML Description
DESCRIPTION = '''
<div class="header-container">
<h1 class="header-title">Lexora-Lite-3B</h1>
<div class="model-info">
<h2>About the Model</h2>
<p>
Welcome to the demonstration of <a href="https://huggingface.co/DeepMount00/Lexora-Lite-3B">Lexora-Lite-3B Chat ITA</a>,
currently the leading open-source large language model for the Italian language. This model represents the state-of-the-art
in Italian natural language processing, combining powerful language understanding with efficient performance.
</p>
<p style="margin-top: 1rem;">
View its performance metrics and compare it with other models on the
<a href="https://huggingface.co/spaces/FinancialSupport/open_ita_llm_leaderboard">official leaderboard</a>.
</p>
</div>
</div>
'''
@spaces.GPU(duration=90)
def generate(
message: str,
chat_history: list[tuple[str, str]],
system_message: str = "",
max_new_tokens: int = 2048,
temperature: float = 0.0001,
top_p: float = 1.0,
top_k: int = 50,
repetition_penalty: float = 1.0,
) -> Iterator[str]:
conversation = [{"role": "system", "content": system_message}]
for user, assistant in chat_history:
conversation.extend(
[
{"role": "user", "content": user},
{"role": "assistant", "content": assistant},
]
)
conversation.append({"role": "user", "content": message})
input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
input_ids = input_ids.to(model.device)
streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
generate_kwargs = dict(
{"input_ids": input_ids},
streamer=streamer,
max_new_tokens=max_new_tokens,
do_sample=True,
top_p=top_p,
top_k=top_k,
temperature=temperature,
num_beams=1,
repetition_penalty=repetition_penalty,
)
t = Thread(target=model.generate, kwargs=generate_kwargs)
t.start()
outputs = []
for text in streamer:
outputs.append(text)
yield "".join(outputs)
def create_chat_interface():
theme = gr.themes.Soft(
primary_hue="blue",
secondary_hue="blue",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
radius_size=gr.themes.sizes.radius_sm,
)
with gr.Blocks(css=CUSTOM_CSS, theme=theme) as demo:
with gr.Column(elem_classes="container"):
gr.Markdown(DESCRIPTION)
with gr.Column(elem_classes="chat-container"):
additional_inputs = [
gr.Textbox(
value="",
label="System Message",
elem_classes="system-message",
render=False,
),
]
# Create controls without context manager
controls = gr.Column(elem_classes="controls-container")
with controls:
additional_inputs.extend([
gr.Slider(
label="Maximum New Tokens",
minimum=1,
maximum=MAX_MAX_NEW_TOKENS,
step=1,
value=DEFAULT_MAX_NEW_TOKENS,
elem_classes="slider-label",
),
gr.Slider(
label="Temperature",
minimum=0,
maximum=4.0,
step=0.1,
value=0.001,
elem_classes="slider-label",
),
gr.Slider(
label="Top-p (Nucleus Sampling)",
minimum=0.05,
maximum=1.0,
step=0.05,
value=1.0,
elem_classes="slider-label",
),
gr.Slider(
label="Top-k",
minimum=1,
maximum=1000,
step=1,
value=50,
elem_classes="slider-label",
),
gr.Slider(
label="Repetition Penalty",
minimum=1.0,
maximum=2.0,
step=0.05,
value=1.0,
elem_classes="slider-label",
),
])
chat_interface = gr.ChatInterface(
fn=generate,
additional_inputs=additional_inputs,
examples=[
["Ciao! Come stai?"],
["Raccontami una breve storia."],
["Qual è il tuo piatto italiano preferito?"],
],
cache_examples=False,
)
gr.DuplicateButton(
value="Duplicate Space for Private Use",
elem_classes="duplicate-button",
elem_id="duplicate-button",
)
return demo
if __name__ == "__main__":
demo = create_chat_interface()
demo.queue(max_size=20).launch() |