Spaces:
Sleeping
Sleeping
DeepMount00
commited on
Commit
•
4d7115d
1
Parent(s):
c1e9709
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import os
|
2 |
from threading import Thread
|
3 |
from typing import Iterator
|
@@ -14,168 +15,341 @@ subprocess.run(
|
|
14 |
shell=True,
|
15 |
)
|
16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
CUSTOM_CSS = """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
.container {
|
19 |
max-width: 1000px !important;
|
20 |
margin: auto !important;
|
21 |
-
padding
|
22 |
}
|
23 |
|
|
|
24 |
.header-container {
|
25 |
background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 100%);
|
26 |
-
padding:
|
27 |
border-radius: 1rem;
|
28 |
margin-bottom: 2rem;
|
29 |
color: white;
|
30 |
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
|
31 |
}
|
32 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
.model-info {
|
34 |
background: white;
|
35 |
-
padding: 1.
|
36 |
-
border-radius: 0.
|
37 |
-
margin-top:
|
38 |
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
39 |
}
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
.chat-container {
|
42 |
border: 1px solid #e5e7eb;
|
43 |
border-radius: 1rem;
|
44 |
background: white;
|
45 |
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
|
|
46 |
}
|
47 |
|
|
|
48 |
.message {
|
49 |
-
padding:
|
50 |
-
margin: 0.
|
51 |
border-radius: 0.5rem;
|
|
|
|
|
52 |
}
|
53 |
|
54 |
.user-message {
|
55 |
background: #f3f4f6;
|
|
|
56 |
}
|
57 |
|
58 |
.assistant-message {
|
59 |
background: #dbeafe;
|
|
|
60 |
}
|
61 |
|
|
|
62 |
.controls-container {
|
63 |
background: #f8fafc;
|
64 |
-
padding: 1.
|
65 |
-
border-radius: 0.
|
66 |
-
margin-top:
|
|
|
67 |
}
|
68 |
|
|
|
69 |
.slider-label {
|
70 |
font-weight: 600;
|
71 |
color: #374151;
|
|
|
|
|
72 |
}
|
73 |
|
|
|
74 |
.duplicate-button {
|
75 |
background: #2563eb !important;
|
76 |
color: white !important;
|
77 |
-
padding: 0.
|
78 |
border-radius: 0.5rem !important;
|
79 |
font-weight: 600 !important;
|
|
|
80 |
transition: all 0.2s !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
}
|
82 |
|
83 |
.duplicate-button:hover {
|
84 |
background: #1d4ed8 !important;
|
85 |
transform: translateY(-1px) !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
}
|
87 |
"""
|
88 |
|
|
|
89 |
DESCRIPTION = '''
|
90 |
<div class="header-container">
|
91 |
-
<h1
|
92 |
<div class="model-info">
|
93 |
-
<h2
|
94 |
-
<p
|
95 |
-
|
96 |
-
currently the
|
97 |
-
|
|
|
|
|
|
|
|
|
98 |
</p>
|
99 |
</div>
|
100 |
</div>
|
101 |
'''
|
102 |
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
label="System Message",
|
122 |
-
elem_classes="system-message",
|
123 |
-
render=False,
|
124 |
-
),
|
125 |
-
gr.Column(elem_classes="controls-container") as controls:
|
126 |
-
with controls:
|
127 |
-
gr.Slider(
|
128 |
-
label="Maximum New Tokens",
|
129 |
-
minimum=1,
|
130 |
-
maximum=MAX_MAX_NEW_TOKENS,
|
131 |
-
step=1,
|
132 |
-
value=DEFAULT_MAX_NEW_TOKENS,
|
133 |
-
elem_classes="slider-label",
|
134 |
-
),
|
135 |
-
gr.Slider(
|
136 |
-
label="Temperature",
|
137 |
-
minimum=0,
|
138 |
-
maximum=4.0,
|
139 |
-
step=0.1,
|
140 |
-
value=0.001,
|
141 |
-
elem_classes="slider-label",
|
142 |
-
),
|
143 |
-
gr.Slider(
|
144 |
-
label="Top-p (Nucleus Sampling)",
|
145 |
-
minimum=0.05,
|
146 |
-
maximum=1.0,
|
147 |
-
step=0.05,
|
148 |
-
value=1.0,
|
149 |
-
elem_classes="slider-label",
|
150 |
-
),
|
151 |
-
gr.Slider(
|
152 |
-
label="Top-k",
|
153 |
-
minimum=1,
|
154 |
-
maximum=1000,
|
155 |
-
step=1,
|
156 |
-
value=50,
|
157 |
-
elem_classes="slider-label",
|
158 |
-
),
|
159 |
-
gr.Slider(
|
160 |
-
label="Repetition Penalty",
|
161 |
-
minimum=1.0,
|
162 |
-
maximum=2.0,
|
163 |
-
step=0.05,
|
164 |
-
value=1.0,
|
165 |
-
elem_classes="slider-label",
|
166 |
-
),
|
167 |
-
],
|
168 |
-
examples=[
|
169 |
-
["Ciao! Come stai?"],
|
170 |
-
],
|
171 |
-
cache_examples=False,
|
172 |
-
)
|
173 |
-
|
174 |
-
gr.DuplicateButton(
|
175 |
-
value="Duplicate Space for Private Use",
|
176 |
-
elem_classes="duplicate-button",
|
177 |
-
elem_id="duplicate-button",
|
178 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
179 |
|
180 |
if __name__ == "__main__":
|
|
|
181 |
demo.queue(max_size=20).launch()
|
|
|
1 |
+
# app.py
|
2 |
import os
|
3 |
from threading import Thread
|
4 |
from typing import Iterator
|
|
|
15 |
shell=True,
|
16 |
)
|
17 |
|
18 |
+
# Constants
|
19 |
+
MAX_MAX_NEW_TOKENS = 2048
|
20 |
+
DEFAULT_MAX_NEW_TOKENS = 1024
|
21 |
+
MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "4096"))
|
22 |
+
|
23 |
+
# Model initialization
|
24 |
+
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
|
25 |
+
model_id = "DeepMount00/Lexora-Lite-3B"
|
26 |
+
|
27 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
28 |
+
model = AutoModelForCausalLM.from_pretrained(
|
29 |
+
model_id,
|
30 |
+
device_map="auto",
|
31 |
+
torch_dtype=torch.bfloat16,
|
32 |
+
attn_implementation="flash_attention_2",
|
33 |
+
trust_remote_code=True,
|
34 |
+
)
|
35 |
+
model.eval()
|
36 |
+
|
37 |
+
# Custom CSS
|
38 |
CUSTOM_CSS = """
|
39 |
+
/* Base styles */
|
40 |
+
* {
|
41 |
+
margin: 0;
|
42 |
+
padding: 0;
|
43 |
+
box-sizing: border-box;
|
44 |
+
}
|
45 |
+
|
46 |
+
body {
|
47 |
+
font-family: 'Inter', sans-serif;
|
48 |
+
background-color: #f8fafc;
|
49 |
+
color: #1e293b;
|
50 |
+
}
|
51 |
+
|
52 |
+
/* Container styles */
|
53 |
.container {
|
54 |
max-width: 1000px !important;
|
55 |
margin: auto !important;
|
56 |
+
padding: 2rem !important;
|
57 |
}
|
58 |
|
59 |
+
/* Header styles */
|
60 |
.header-container {
|
61 |
background: linear-gradient(135deg, #1e3a8a 0%, #3b82f6 100%);
|
62 |
+
padding: 2.5rem;
|
63 |
border-radius: 1rem;
|
64 |
margin-bottom: 2rem;
|
65 |
color: white;
|
66 |
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
|
67 |
}
|
68 |
|
69 |
+
.header-title {
|
70 |
+
font-size: 2.5rem;
|
71 |
+
font-weight: 700;
|
72 |
+
margin-bottom: 1.5rem;
|
73 |
+
text-align: center;
|
74 |
+
letter-spacing: -0.025em;
|
75 |
+
}
|
76 |
+
|
77 |
+
/* Model info styles */
|
78 |
.model-info {
|
79 |
background: white;
|
80 |
+
padding: 1.75rem;
|
81 |
+
border-radius: 0.75rem;
|
82 |
+
margin-top: 1.5rem;
|
83 |
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
84 |
}
|
85 |
|
86 |
+
.model-info h2 {
|
87 |
+
font-size: 1.5rem;
|
88 |
+
font-weight: 600;
|
89 |
+
color: #1e3a8a;
|
90 |
+
margin-bottom: 1rem;
|
91 |
+
}
|
92 |
+
|
93 |
+
.model-info p {
|
94 |
+
color: #374151;
|
95 |
+
line-height: 1.6;
|
96 |
+
font-size: 1.1rem;
|
97 |
+
}
|
98 |
+
|
99 |
+
.model-info a {
|
100 |
+
color: #2563eb;
|
101 |
+
font-weight: 600;
|
102 |
+
text-decoration: none;
|
103 |
+
transition: color 0.2s;
|
104 |
+
}
|
105 |
+
|
106 |
+
.model-info a:hover {
|
107 |
+
color: #1d4ed8;
|
108 |
+
text-decoration: underline;
|
109 |
+
}
|
110 |
+
|
111 |
+
/* Chat container styles */
|
112 |
.chat-container {
|
113 |
border: 1px solid #e5e7eb;
|
114 |
border-radius: 1rem;
|
115 |
background: white;
|
116 |
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
|
117 |
+
margin-bottom: 2rem;
|
118 |
}
|
119 |
|
120 |
+
/* Message styles */
|
121 |
.message {
|
122 |
+
padding: 1.25rem;
|
123 |
+
margin: 0.75rem;
|
124 |
border-radius: 0.5rem;
|
125 |
+
font-size: 1.05rem;
|
126 |
+
line-height: 1.6;
|
127 |
}
|
128 |
|
129 |
.user-message {
|
130 |
background: #f3f4f6;
|
131 |
+
border-left: 4px solid #3b82f6;
|
132 |
}
|
133 |
|
134 |
.assistant-message {
|
135 |
background: #dbeafe;
|
136 |
+
border-left: 4px solid #1d4ed8;
|
137 |
}
|
138 |
|
139 |
+
/* Controls container styles */
|
140 |
.controls-container {
|
141 |
background: #f8fafc;
|
142 |
+
padding: 1.75rem;
|
143 |
+
border-radius: 0.75rem;
|
144 |
+
margin-top: 1.5rem;
|
145 |
+
border: 1px solid #e5e7eb;
|
146 |
}
|
147 |
|
148 |
+
/* Slider styles */
|
149 |
.slider-label {
|
150 |
font-weight: 600;
|
151 |
color: #374151;
|
152 |
+
margin-bottom: 0.5rem;
|
153 |
+
font-size: 1.05rem;
|
154 |
}
|
155 |
|
156 |
+
/* Button styles */
|
157 |
.duplicate-button {
|
158 |
background: #2563eb !important;
|
159 |
color: white !important;
|
160 |
+
padding: 0.875rem 1.75rem !important;
|
161 |
border-radius: 0.5rem !important;
|
162 |
font-weight: 600 !important;
|
163 |
+
font-size: 1.05rem !important;
|
164 |
transition: all 0.2s !important;
|
165 |
+
border: none !important;
|
166 |
+
cursor: pointer !important;
|
167 |
+
display: inline-flex !important;
|
168 |
+
align-items: center !important;
|
169 |
+
justify-content: center !important;
|
170 |
+
text-align: center !important;
|
171 |
+
box-shadow: 0 2px 4px rgba(37, 99, 235, 0.2) !important;
|
172 |
}
|
173 |
|
174 |
.duplicate-button:hover {
|
175 |
background: #1d4ed8 !important;
|
176 |
transform: translateY(-1px) !important;
|
177 |
+
box-shadow: 0 4px 6px rgba(37, 99, 235, 0.3) !important;
|
178 |
+
}
|
179 |
+
|
180 |
+
/* Input field styles */
|
181 |
+
.input-textarea {
|
182 |
+
border: 2px solid #e5e7eb !important;
|
183 |
+
border-radius: 0.5rem !important;
|
184 |
+
padding: 1rem !important;
|
185 |
+
font-size: 1.05rem !important;
|
186 |
+
transition: border-color 0.2s !important;
|
187 |
+
}
|
188 |
+
|
189 |
+
.input-textarea:focus {
|
190 |
+
border-color: #3b82f6 !important;
|
191 |
+
outline: none !important;
|
192 |
+
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1) !important;
|
193 |
+
}
|
194 |
+
|
195 |
+
/* System message styles */
|
196 |
+
.system-message {
|
197 |
+
background: #f8fafc;
|
198 |
+
border: 1px solid #e5e7eb;
|
199 |
+
border-radius: 0.5rem;
|
200 |
+
padding: 1rem;
|
201 |
+
margin-bottom: 1rem;
|
202 |
}
|
203 |
"""
|
204 |
|
205 |
+
# HTML Description
|
206 |
DESCRIPTION = '''
|
207 |
<div class="header-container">
|
208 |
+
<h1 class="header-title">Lexora-Lite-3B</h1>
|
209 |
<div class="model-info">
|
210 |
+
<h2>About the Model</h2>
|
211 |
+
<p>
|
212 |
+
Welcome to the demonstration of <a href="https://huggingface.co/DeepMount00/Lexora-Lite-3B">Lexora-Lite-3B Chat ITA</a>,
|
213 |
+
currently the leading open-source large language model for the Italian language. This model represents the state-of-the-art
|
214 |
+
in Italian natural language processing, combining powerful language understanding with efficient performance.
|
215 |
+
</p>
|
216 |
+
<p style="margin-top: 1rem;">
|
217 |
+
View its performance metrics and compare it with other models on the
|
218 |
+
<a href="https://huggingface.co/spaces/FinancialSupport/open_ita_llm_leaderboard">official leaderboard</a>.
|
219 |
</p>
|
220 |
</div>
|
221 |
</div>
|
222 |
'''
|
223 |
|
224 |
+
@spaces.GPU(duration=90)
|
225 |
+
def generate(
|
226 |
+
message: str,
|
227 |
+
chat_history: list[tuple[str, str]],
|
228 |
+
system_message: str = "",
|
229 |
+
max_new_tokens: int = 2048,
|
230 |
+
temperature: float = 0.0001,
|
231 |
+
top_p: float = 1.0,
|
232 |
+
top_k: int = 50,
|
233 |
+
repetition_penalty: float = 1.0,
|
234 |
+
) -> Iterator[str]:
|
235 |
+
conversation = [{"role": "system", "content": system_message}]
|
236 |
+
for user, assistant in chat_history:
|
237 |
+
conversation.extend(
|
238 |
+
[
|
239 |
+
{"role": "user", "content": user},
|
240 |
+
{"role": "assistant", "content": assistant},
|
241 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
242 |
)
|
243 |
+
conversation.append({"role": "user", "content": message})
|
244 |
+
|
245 |
+
input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")
|
246 |
+
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
247 |
+
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
248 |
+
gr.Warning(f"Trimmed input from conversation as it was longer than {MAX_INPUT_TOKEN_LENGTH} tokens.")
|
249 |
+
input_ids = input_ids.to(model.device)
|
250 |
+
|
251 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
|
252 |
+
generate_kwargs = dict(
|
253 |
+
{"input_ids": input_ids},
|
254 |
+
streamer=streamer,
|
255 |
+
max_new_tokens=max_new_tokens,
|
256 |
+
do_sample=True,
|
257 |
+
top_p=top_p,
|
258 |
+
top_k=top_k,
|
259 |
+
temperature=temperature,
|
260 |
+
num_beams=1,
|
261 |
+
repetition_penalty=repetition_penalty,
|
262 |
+
)
|
263 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
264 |
+
t.start()
|
265 |
+
|
266 |
+
outputs = []
|
267 |
+
for text in streamer:
|
268 |
+
outputs.append(text)
|
269 |
+
yield "".join(outputs)
|
270 |
+
|
271 |
+
def create_chat_interface():
|
272 |
+
theme = gr.themes.Soft(
|
273 |
+
primary_hue="blue",
|
274 |
+
secondary_hue="blue",
|
275 |
+
neutral_hue="slate",
|
276 |
+
font=gr.themes.GoogleFont("Inter"),
|
277 |
+
radius_size=gr.themes.sizes.radius_sm,
|
278 |
+
)
|
279 |
+
|
280 |
+
with gr.Blocks(css=CUSTOM_CSS, theme=theme) as demo:
|
281 |
+
with gr.Column(elem_classes="container"):
|
282 |
+
gr.Markdown(DESCRIPTION)
|
283 |
+
|
284 |
+
with gr.Column(elem_classes="chat-container"):
|
285 |
+
chat_interface = gr.ChatInterface(
|
286 |
+
fn=generate,
|
287 |
+
additional_inputs=[
|
288 |
+
gr.Textbox(
|
289 |
+
value="",
|
290 |
+
label="System Message",
|
291 |
+
elem_classes="system-message",
|
292 |
+
render=False,
|
293 |
+
),
|
294 |
+
gr.Column(elem_classes="controls-container") as controls:
|
295 |
+
with controls:
|
296 |
+
gr.Slider(
|
297 |
+
label="Maximum New Tokens",
|
298 |
+
minimum=1,
|
299 |
+
maximum=MAX_MAX_NEW_TOKENS,
|
300 |
+
step=1,
|
301 |
+
value=DEFAULT_MAX_NEW_TOKENS,
|
302 |
+
elem_classes="slider-label",
|
303 |
+
),
|
304 |
+
gr.Slider(
|
305 |
+
label="Temperature",
|
306 |
+
minimum=0,
|
307 |
+
maximum=4.0,
|
308 |
+
step=0.1,
|
309 |
+
value=0.001,
|
310 |
+
elem_classes="slider-label",
|
311 |
+
),
|
312 |
+
gr.Slider(
|
313 |
+
label="Top-p (Nucleus Sampling)",
|
314 |
+
minimum=0.05,
|
315 |
+
maximum=1.0,
|
316 |
+
step=0.05,
|
317 |
+
value=1.0,
|
318 |
+
elem_classes="slider-label",
|
319 |
+
),
|
320 |
+
gr.Slider(
|
321 |
+
label="Top-k",
|
322 |
+
minimum=1,
|
323 |
+
maximum=1000,
|
324 |
+
step=1,
|
325 |
+
value=50,
|
326 |
+
elem_classes="slider-label",
|
327 |
+
),
|
328 |
+
gr.Slider(
|
329 |
+
label="Repetition Penalty",
|
330 |
+
minimum=1.0,
|
331 |
+
maximum=2.0,
|
332 |
+
step=0.05,
|
333 |
+
value=1.0,
|
334 |
+
elem_classes="slider-label",
|
335 |
+
),
|
336 |
+
],
|
337 |
+
examples=[
|
338 |
+
["Ciao! Come stai?"],
|
339 |
+
["Raccontami una breve storia."],
|
340 |
+
["Qual è il tuo piatto italiano preferito?"],
|
341 |
+
],
|
342 |
+
cache_examples=False,
|
343 |
+
)
|
344 |
+
|
345 |
+
gr.DuplicateButton(
|
346 |
+
value="Duplicate Space for Private Use",
|
347 |
+
elem_classes="duplicate-button",
|
348 |
+
elem_id="duplicate-button",
|
349 |
+
)
|
350 |
+
|
351 |
+
return demo
|
352 |
|
353 |
if __name__ == "__main__":
|
354 |
+
demo = create_chat_interface()
|
355 |
demo.queue(max_size=20).launch()
|