Spaces:
Sleeping
Sleeping
Commit
·
d528517
1
Parent(s):
ade8c92
forcing css by creating own function
Browse files
app.py
CHANGED
@@ -8,12 +8,20 @@ import spaces
|
|
8 |
import torch
|
9 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
10 |
|
|
|
|
|
|
|
11 |
DESCRIPTION = """\
|
12 |
<h1 style="text-align: center;">Hi, I'm Gemma 2 (2B) 👋</h1>
|
13 |
|
14 |
-
This is a demo of <strong>google/gemma-2-2b-it</strong> fine-tuned for instruction following.
|
|
|
|
|
15 |
|
16 |
-
👉 Looking for a larger
|
|
|
|
|
|
|
17 |
"""
|
18 |
|
19 |
MAX_MAX_NEW_TOKENS = 2048
|
@@ -33,6 +41,9 @@ model.config.sliding_window = 4096
|
|
33 |
model.eval()
|
34 |
|
35 |
|
|
|
|
|
|
|
36 |
@spaces.GPU
|
37 |
def generate(
|
38 |
message: str,
|
@@ -50,10 +61,9 @@ def generate(
|
|
50 |
input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")
|
51 |
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
52 |
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
53 |
-
gr.Warning(f"Trimmed input from conversation as it
|
54 |
input_ids = input_ids.to(model.device)
|
55 |
|
56 |
-
# Stream out tokens
|
57 |
streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
|
58 |
generate_kwargs = dict(
|
59 |
{"input_ids": input_ids},
|
@@ -80,201 +90,229 @@ def generate(
|
|
80 |
return
|
81 |
|
82 |
|
83 |
-
|
84 |
-
#
|
85 |
-
|
86 |
-
:
|
87 |
-
|
88 |
-
|
89 |
-
|
|
|
|
|
|
|
|
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
text-decoration: underline;
|
104 |
-
}
|
105 |
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
-
|
120 |
-
.chat-message {
|
121 |
-
border: none !important;
|
122 |
-
position: relative;
|
123 |
-
}
|
124 |
|
125 |
-
/* Icons for user and bot messages */
|
126 |
-
.chat-message.user::before {
|
127 |
-
content: '';
|
128 |
-
display: inline-block;
|
129 |
-
background: url('user.png') center center no-repeat;
|
130 |
-
background-size: cover;
|
131 |
-
width: 24px;
|
132 |
-
height: 24px;
|
133 |
-
margin-right: 8px;
|
134 |
-
vertical-align: middle;
|
135 |
-
}
|
136 |
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
margin-right: 8px;
|
145 |
-
vertical-align: middle;
|
146 |
-
}
|
147 |
|
148 |
-
/* User bubble: a deeper blue with white text */
|
149 |
-
.chat-message.user {
|
150 |
-
background-color: #0284C7 !important;
|
151 |
-
color: #FFFFFF !important;
|
152 |
-
border-radius: 8px;
|
153 |
-
padding: 8px 12px;
|
154 |
-
margin: 6px 0;
|
155 |
-
}
|
156 |
-
|
157 |
-
/* Bot bubble: very light blue with darker text */
|
158 |
-
.chat-message.bot {
|
159 |
-
background-color: #EFF8FF !important;
|
160 |
-
color: #333 !important;
|
161 |
-
border-radius: 8px;
|
162 |
-
padding: 8px 12px;
|
163 |
-
margin: 6px 0;
|
164 |
-
}
|
165 |
-
|
166 |
-
/* Chat input area */
|
167 |
-
.chat-input textarea {
|
168 |
-
background-color: #FFFFFF;
|
169 |
-
color: #333;
|
170 |
-
border: 1px solid #66AEEF;
|
171 |
-
border-radius: 6px;
|
172 |
-
padding: 8px;
|
173 |
-
}
|
174 |
-
|
175 |
-
/* Sliders & other controls */
|
176 |
-
form.sliders input[type="range"] {
|
177 |
-
accent-color: #66AEEF;
|
178 |
-
}
|
179 |
-
form.sliders label {
|
180 |
-
color: #333;
|
181 |
-
}
|
182 |
-
|
183 |
-
.gradio-button, .chat-send-btn {
|
184 |
-
background-color: #0284C7 !important;
|
185 |
-
color: #FFFFFF !important;
|
186 |
-
border-radius: 5px;
|
187 |
-
border: none;
|
188 |
-
cursor: pointer;
|
189 |
-
}
|
190 |
-
.gradio-button:hover, .chat-send-btn:hover {
|
191 |
-
background-color: #026FA6 !important;
|
192 |
-
}
|
193 |
-
|
194 |
-
/* Style the example "pill" buttons (the built-in ChatInterface examples) */
|
195 |
-
.gr-examples {
|
196 |
-
display: flex !important;
|
197 |
-
flex-wrap: wrap;
|
198 |
-
gap: 16px;
|
199 |
-
justify-content: center;
|
200 |
-
margin-bottom: 1em !important;
|
201 |
-
}
|
202 |
-
.gr-examples button.example {
|
203 |
-
background-color: #EFF8FF !important;
|
204 |
-
border: 1px solid #66AEEF !important;
|
205 |
-
border-radius: 8px !important;
|
206 |
-
color: #333 !important;
|
207 |
-
padding: 10px 16px !important;
|
208 |
-
cursor: pointer !important;
|
209 |
-
transition: background-color 0.2s !important;
|
210 |
-
}
|
211 |
-
.gr-examples button.example:hover {
|
212 |
-
background-color: #E0F2FF !important;
|
213 |
-
}
|
214 |
-
|
215 |
-
/* Additional spacing / small tweaks */
|
216 |
-
#duplicate-button {
|
217 |
-
margin: auto;
|
218 |
-
background: #1565c0;
|
219 |
-
border-radius: 100vh;
|
220 |
-
color: #fff;
|
221 |
-
}
|
222 |
-
"""
|
223 |
-
|
224 |
-
demo = gr.ChatInterface(
|
225 |
-
fn=generate,
|
226 |
-
additional_inputs=[
|
227 |
-
gr.Slider(
|
228 |
-
label="Max new tokens",
|
229 |
-
minimum=1,
|
230 |
-
maximum=MAX_MAX_NEW_TOKENS,
|
231 |
-
step=1,
|
232 |
-
value=DEFAULT_MAX_NEW_TOKENS,
|
233 |
-
),
|
234 |
-
gr.Slider(
|
235 |
-
label="Temperature",
|
236 |
-
minimum=0.1,
|
237 |
-
maximum=4.0,
|
238 |
-
step=0.1,
|
239 |
-
value=0.6,
|
240 |
-
),
|
241 |
-
gr.Slider(
|
242 |
-
label="Top-p (nucleus sampling)",
|
243 |
-
minimum=0.05,
|
244 |
-
maximum=1.0,
|
245 |
-
step=0.05,
|
246 |
-
value=0.9,
|
247 |
-
),
|
248 |
-
gr.Slider(
|
249 |
-
label="Top-k",
|
250 |
-
minimum=1,
|
251 |
-
maximum=1000,
|
252 |
-
step=1,
|
253 |
-
value=50,
|
254 |
-
),
|
255 |
-
gr.Slider(
|
256 |
-
label="Repetition penalty",
|
257 |
-
minimum=1.0,
|
258 |
-
maximum=2.0,
|
259 |
-
step=0.05,
|
260 |
-
value=1.2,
|
261 |
-
),
|
262 |
-
],
|
263 |
-
stop_btn=None,
|
264 |
-
examples=[
|
265 |
-
["Hello there! How are you doing?"],
|
266 |
-
["Can you explain briefly to me what is the Python programming language?"],
|
267 |
-
["Explain the plot of Cinderella in a sentence."],
|
268 |
-
["How many hours does it take a man to eat a Helicopter?"],
|
269 |
-
["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
|
270 |
-
],
|
271 |
-
cache_examples=False,
|
272 |
-
type="messages",
|
273 |
-
description=DESCRIPTION,
|
274 |
-
css=gemini_css,
|
275 |
-
fill_height=True,
|
276 |
-
)
|
277 |
|
278 |
if __name__ == "__main__":
|
279 |
-
|
280 |
-
demo.queue(max_size=20).launch()
|
|
|
8 |
import torch
|
9 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
10 |
|
11 |
+
############################################################
|
12 |
+
# Model setup (modify as needed)
|
13 |
+
############################################################
|
14 |
DESCRIPTION = """\
|
15 |
<h1 style="text-align: center;">Hi, I'm Gemma 2 (2B) 👋</h1>
|
16 |
|
17 |
+
This is a demo of <strong>google/gemma-2-2b-it</strong> fine-tuned for instruction following.
|
18 |
+
For more details, please check
|
19 |
+
<a href="https://huggingface.co/blog/gemma2" target="_blank">the post</a>.
|
20 |
|
21 |
+
👉 Looking for a larger version? Try the 27B in
|
22 |
+
<a href="https://huggingface.co/chat/models/google/gemma-2-27b-it" target="_blank">HuggingChat</a>
|
23 |
+
and the 9B in
|
24 |
+
<a href="https://huggingface.co/spaces/huggingface-projects/gemma-2-9b-it" target="_blank">this Space</a>.
|
25 |
"""
|
26 |
|
27 |
MAX_MAX_NEW_TOKENS = 2048
|
|
|
41 |
model.eval()
|
42 |
|
43 |
|
44 |
+
############################################################
|
45 |
+
# Generator function (streaming approach)
|
46 |
+
############################################################
|
47 |
@spaces.GPU
|
48 |
def generate(
|
49 |
message: str,
|
|
|
61 |
input_ids = tokenizer.apply_chat_template(conversation, add_generation_prompt=True, return_tensors="pt")
|
62 |
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
63 |
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
64 |
+
gr.Warning(f"Trimmed input from conversation as it exceeded {MAX_INPUT_TOKEN_LENGTH} tokens.")
|
65 |
input_ids = input_ids.to(model.device)
|
66 |
|
|
|
67 |
streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
|
68 |
generate_kwargs = dict(
|
69 |
{"input_ids": input_ids},
|
|
|
90 |
return
|
91 |
|
92 |
|
93 |
+
############################################################
|
94 |
+
# CREATE_INTERFACE function returning a gr.Blocks
|
95 |
+
############################################################
|
96 |
+
def create_interface() -> gr.Blocks:
|
97 |
+
"""
|
98 |
+
Build a custom Blocks interface containing:
|
99 |
+
- A Chatbot with user/bot icons
|
100 |
+
- A ChatInterface that uses the chatbot
|
101 |
+
- Custom example suggestions with special styling
|
102 |
+
- Gemini-like gradient background and bubble styling
|
103 |
+
"""
|
104 |
|
105 |
+
gemini_css = """
|
106 |
+
:root {
|
107 |
+
--gradient-start: #66AEEF; /* lighter top */
|
108 |
+
--gradient-end: #F0F8FF; /* very light at bottom */
|
109 |
+
}
|
110 |
+
/* Overall page & container background gradient */
|
111 |
+
html, body, .gradio-container {
|
112 |
+
margin: 0;
|
113 |
+
padding: 0;
|
114 |
+
background: linear-gradient(to bottom, var(--gradient-start), var(--gradient-end));
|
115 |
+
font-family: "Helvetica", sans-serif;
|
116 |
+
color: #333; /* dark gray for better contrast */
|
117 |
+
}
|
118 |
+
/* Make anchor (link) text a clearly visible dark blue */
|
119 |
+
a, a:visited {
|
120 |
+
color: #02497A !important;
|
121 |
+
text-decoration: underline;
|
122 |
+
}
|
123 |
+
/* Center the top headings in the description */
|
124 |
+
.gradio-container h1 {
|
125 |
+
margin-top: 0.8em;
|
126 |
+
margin-bottom: 0.5em;
|
127 |
+
text-align: center;
|
128 |
+
color: #fff; /* White text on top gradient for pop */
|
129 |
+
}
|
130 |
+
/* Chat container background: a very light blue so it's distinct from the outer gradient */
|
131 |
+
.chatbot, .chatbot .wrap, .chat-interface, .chat-interface .wrap {
|
132 |
+
background-color: #F8FDFF !important;
|
133 |
+
}
|
134 |
+
/* Remove harsh frames around chat messages */
|
135 |
+
.chatbot .message, .chat-message {
|
136 |
+
border: none !important;
|
137 |
+
position: relative;
|
138 |
+
}
|
139 |
+
/* Icons for user and bot messages (Chatbot) */
|
140 |
+
.chatbot .user .chat-avatar {
|
141 |
+
background: url('user.png') center center no-repeat;
|
142 |
+
background-size: cover;
|
143 |
+
}
|
144 |
+
.chatbot .bot .chat-avatar {
|
145 |
+
background: url('gemma.png') center center no-repeat;
|
146 |
+
background-size: cover;
|
147 |
+
}
|
148 |
+
/* Icons for user and bot messages (ChatInterface) */
|
149 |
+
.chat-message.user::before {
|
150 |
+
content: '';
|
151 |
+
display: inline-block;
|
152 |
+
background: url('user.png') center center no-repeat;
|
153 |
+
background-size: cover;
|
154 |
+
width: 24px;
|
155 |
+
height: 24px;
|
156 |
+
margin-right: 8px;
|
157 |
+
vertical-align: middle;
|
158 |
+
}
|
159 |
+
.chat-message.bot::before {
|
160 |
+
content: '';
|
161 |
+
display: inline-block;
|
162 |
+
background: url('gemma.png') center center no-repeat;
|
163 |
+
background-size: cover;
|
164 |
+
width: 24px;
|
165 |
+
height: 24px;
|
166 |
+
margin-right: 8px;
|
167 |
+
vertical-align: middle;
|
168 |
+
}
|
169 |
+
/* Chat bubbles (ChatInterface) */
|
170 |
+
.chat-message.user {
|
171 |
+
background-color: #0284C7 !important;
|
172 |
+
color: #FFFFFF !important;
|
173 |
+
border-radius: 8px;
|
174 |
+
padding: 8px 12px;
|
175 |
+
margin: 6px 0;
|
176 |
+
}
|
177 |
+
.chat-message.bot {
|
178 |
+
background-color: #EFF8FF !important;
|
179 |
+
color: #333 !important;
|
180 |
+
border-radius: 8px;
|
181 |
+
padding: 8px 12px;
|
182 |
+
margin: 6px 0;
|
183 |
+
}
|
184 |
+
/* Chat input area */
|
185 |
+
.chat-input textarea {
|
186 |
+
background-color: #FFFFFF;
|
187 |
+
color: #333;
|
188 |
+
border: 1px solid #66AEEF;
|
189 |
+
border-radius: 6px;
|
190 |
+
padding: 8px;
|
191 |
+
}
|
192 |
+
/* Sliders & other controls */
|
193 |
+
form.sliders input[type="range"] {
|
194 |
+
accent-color: #66AEEF;
|
195 |
+
}
|
196 |
+
form.sliders label {
|
197 |
+
color: #333;
|
198 |
+
}
|
199 |
+
.gradio-button, .chat-send-btn {
|
200 |
+
background-color: #0284C7 !important;
|
201 |
+
color: #FFFFFF !important;
|
202 |
+
border-radius: 5px;
|
203 |
+
border: none;
|
204 |
+
cursor: pointer;
|
205 |
+
}
|
206 |
+
.gradio-button:hover, .chat-send-btn:hover {
|
207 |
+
background-color: #026FA6 !important;
|
208 |
+
}
|
209 |
+
/* Style the example "pill" buttons (ChatInterface) */
|
210 |
+
.gr-examples {
|
211 |
+
display: flex !important;
|
212 |
+
flex-wrap: wrap;
|
213 |
+
gap: 16px;
|
214 |
+
justify-content: center;
|
215 |
+
margin-bottom: 1em !important;
|
216 |
+
}
|
217 |
+
.gr-examples button.example {
|
218 |
+
background-color: #EFF8FF !important;
|
219 |
+
border: 1px solid #66AEEF !important;
|
220 |
+
border-radius: 8px !important;
|
221 |
+
color: #333 !important;
|
222 |
+
padding: 10px 16px !important;
|
223 |
+
cursor: pointer !important;
|
224 |
+
transition: background-color 0.2s !important;
|
225 |
+
}
|
226 |
+
.gr-examples button.example:hover {
|
227 |
+
background-color: #E0F2FF !important;
|
228 |
+
}
|
229 |
+
/* Additional spacing / small tweaks */
|
230 |
+
#duplicate-button {
|
231 |
+
margin: auto;
|
232 |
+
background: #1565c0;
|
233 |
+
border-radius: 100vh;
|
234 |
+
color: #fff;
|
235 |
+
}
|
236 |
+
"""
|
237 |
|
238 |
+
with gr.Blocks(css=gemini_css) as app:
|
239 |
+
# A heading or custom markdown
|
240 |
+
gr.Markdown(DESCRIPTION)
|
|
|
|
|
241 |
|
242 |
+
# We can define a custom Gradio Chatbot (if you want both Chatbot and ChatInterface)
|
243 |
+
chatbot = gr.Chatbot(
|
244 |
+
label="Gemma Chat (Blocks-based)",
|
245 |
+
avatar_images=("user.png", "gemma.png"),
|
246 |
+
height=450,
|
247 |
+
show_copy_button=True
|
248 |
+
)
|
249 |
|
250 |
+
# Then define a ChatInterface that references your generate function
|
251 |
+
# and optionally reuses the same "chatbot" component if you want.
|
252 |
+
interface = gr.ChatInterface(
|
253 |
+
fn=generate,
|
254 |
+
chatbot=chatbot, # link the Chatbot to the ChatInterface
|
255 |
+
css=gemini_css, # keep your custom CSS
|
256 |
+
description="Gemini-themed Chatbot with user/bot icons",
|
257 |
+
additional_inputs=[
|
258 |
+
gr.Slider(
|
259 |
+
label="Max new tokens",
|
260 |
+
minimum=1,
|
261 |
+
maximum=MAX_MAX_NEW_TOKENS,
|
262 |
+
step=1,
|
263 |
+
value=DEFAULT_MAX_NEW_TOKENS,
|
264 |
+
),
|
265 |
+
gr.Slider(
|
266 |
+
label="Temperature",
|
267 |
+
minimum=0.1,
|
268 |
+
maximum=4.0,
|
269 |
+
step=0.1,
|
270 |
+
value=0.6,
|
271 |
+
),
|
272 |
+
gr.Slider(
|
273 |
+
label="Top-p (nucleus sampling)",
|
274 |
+
minimum=0.05,
|
275 |
+
maximum=1.0,
|
276 |
+
step=0.05,
|
277 |
+
value=0.9,
|
278 |
+
),
|
279 |
+
gr.Slider(
|
280 |
+
label="Top-k",
|
281 |
+
minimum=1,
|
282 |
+
maximum=1000,
|
283 |
+
step=1,
|
284 |
+
value=50,
|
285 |
+
),
|
286 |
+
gr.Slider(
|
287 |
+
label="Repetition penalty",
|
288 |
+
minimum=1.0,
|
289 |
+
maximum=2.0,
|
290 |
+
step=0.05,
|
291 |
+
value=1.2,
|
292 |
+
),
|
293 |
+
],
|
294 |
+
examples=[
|
295 |
+
["Hello there! How are you doing?"],
|
296 |
+
["Can you explain briefly to me what is the Python programming language?"],
|
297 |
+
["Explain the plot of Cinderella in a sentence."],
|
298 |
+
["How many hours does it take a man to eat a Helicopter?"],
|
299 |
+
["Write a 100-word article on 'Benefits of Open-Source in AI research'"],
|
300 |
+
],
|
301 |
+
cache_examples=False,
|
302 |
+
fill_height=True,
|
303 |
+
)
|
304 |
|
305 |
+
return app
|
|
|
|
|
|
|
|
|
306 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
307 |
|
308 |
+
############################################################
|
309 |
+
# Main script entry
|
310 |
+
############################################################
|
311 |
+
def main():
|
312 |
+
demo = create_interface()
|
313 |
+
# Launch the app with queue for concurrency/streaming
|
314 |
+
demo.queue(max_size=20).launch(server_name="0.0.0.0", server_port=7860, debug=True)
|
|
|
|
|
|
|
315 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
316 |
|
317 |
if __name__ == "__main__":
|
318 |
+
main()
|
|