Spaces:
Running on Zero
Running on Zero
I am totally not 'PauliePocket' trust
Browse files
app.py
CHANGED
|
@@ -86,15 +86,38 @@ def load_model(model_id: str, device: str):
|
|
| 86 |
return tokenizer, model
|
| 87 |
|
| 88 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
def build_messages(history, user_message: str):
|
| 90 |
-
"""history is
|
| 91 |
-
(Gradio Chatbot 'messages' format).
|
|
|
|
| 92 |
messages = []
|
| 93 |
for msg in history or []:
|
| 94 |
role = msg.get("role")
|
| 95 |
-
|
| 96 |
-
if role in ("user", "assistant") and
|
| 97 |
-
messages.append({"role": role, "content":
|
| 98 |
|
| 99 |
messages.append({"role": "user", "content": user_message})
|
| 100 |
return messages
|
|
@@ -104,13 +127,13 @@ def build_fallback_prompt(history, user_message: str) -> str:
|
|
| 104 |
parts = []
|
| 105 |
for msg in history or []:
|
| 106 |
role = msg.get("role")
|
| 107 |
-
|
| 108 |
-
if not
|
| 109 |
continue
|
| 110 |
if role == "user":
|
| 111 |
-
parts.append(f"User: {
|
| 112 |
elif role == "assistant":
|
| 113 |
-
parts.append(f"Assistant: {
|
| 114 |
|
| 115 |
parts.append(f"User: {user_message}")
|
| 116 |
parts.append("Assistant:")
|
|
@@ -137,8 +160,10 @@ def _generate(message, history, model_label, max_new_tokens, device_name):
|
|
| 137 |
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
| 138 |
|
| 139 |
input_ids = input_ids.to(_model_device(model))
|
|
|
|
| 140 |
|
| 141 |
generation_kwargs = {
|
|
|
|
| 142 |
"max_new_tokens": max_new_tokens,
|
| 143 |
"do_sample": True,
|
| 144 |
"temperature": 0.7,
|
|
@@ -221,7 +246,7 @@ with gr.Blocks(title="SmolLM2 Chat") as demo:
|
|
| 221 |
label="Conversation",
|
| 222 |
)
|
| 223 |
message = gr.Textbox(
|
| 224 |
-
placeholder="
|
| 225 |
label="Message",
|
| 226 |
lines=3,
|
| 227 |
)
|
|
@@ -233,7 +258,7 @@ with gr.Blocks(title="SmolLM2 Chat") as demo:
|
|
| 233 |
["Generate a song about a girl falling in love."],
|
| 234 |
],
|
| 235 |
inputs=message,
|
| 236 |
-
label="Try these prompts",
|
| 237 |
)
|
| 238 |
|
| 239 |
with gr.Row():
|
|
|
|
| 86 |
return tokenizer, model
|
| 87 |
|
| 88 |
|
| 89 |
+
def _extract_text(content) -> str:
|
| 90 |
+
"""Gradio 6's messages format stores `content` as either a plain string
|
| 91 |
+
or a list of content blocks (e.g. [{"type": "text", "text": "..."}]).
|
| 92 |
+
Normalize either shape down to plain text."""
|
| 93 |
+
if content is None:
|
| 94 |
+
return ""
|
| 95 |
+
if isinstance(content, str):
|
| 96 |
+
return content
|
| 97 |
+
if isinstance(content, list):
|
| 98 |
+
parts = []
|
| 99 |
+
for block in content:
|
| 100 |
+
if isinstance(block, str):
|
| 101 |
+
parts.append(block)
|
| 102 |
+
elif isinstance(block, dict):
|
| 103 |
+
if "text" in block:
|
| 104 |
+
parts.append(block.get("text") or "")
|
| 105 |
+
return "".join(parts)
|
| 106 |
+
if isinstance(content, dict):
|
| 107 |
+
return content.get("text", "") or ""
|
| 108 |
+
return str(content)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
def build_messages(history, user_message: str):
|
| 112 |
+
"""history is a list of {'role': ..., 'content': ...} dicts
|
| 113 |
+
(Gradio Chatbot 'messages' format). content may be a string or a
|
| 114 |
+
list of content blocks."""
|
| 115 |
messages = []
|
| 116 |
for msg in history or []:
|
| 117 |
role = msg.get("role")
|
| 118 |
+
text = _extract_text(msg.get("content"))
|
| 119 |
+
if role in ("user", "assistant") and text:
|
| 120 |
+
messages.append({"role": role, "content": text})
|
| 121 |
|
| 122 |
messages.append({"role": "user", "content": user_message})
|
| 123 |
return messages
|
|
|
|
| 127 |
parts = []
|
| 128 |
for msg in history or []:
|
| 129 |
role = msg.get("role")
|
| 130 |
+
text = _extract_text(msg.get("content"))
|
| 131 |
+
if not text:
|
| 132 |
continue
|
| 133 |
if role == "user":
|
| 134 |
+
parts.append(f"User: {text}")
|
| 135 |
elif role == "assistant":
|
| 136 |
+
parts.append(f"Assistant: {text}")
|
| 137 |
|
| 138 |
parts.append(f"User: {user_message}")
|
| 139 |
parts.append("Assistant:")
|
|
|
|
| 160 |
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
| 161 |
|
| 162 |
input_ids = input_ids.to(_model_device(model))
|
| 163 |
+
attention_mask = torch.ones_like(input_ids)
|
| 164 |
|
| 165 |
generation_kwargs = {
|
| 166 |
+
"attention_mask": attention_mask,
|
| 167 |
"max_new_tokens": max_new_tokens,
|
| 168 |
"do_sample": True,
|
| 169 |
"temperature": 0.7,
|
|
|
|
| 246 |
label="Conversation",
|
| 247 |
)
|
| 248 |
message = gr.Textbox(
|
| 249 |
+
placeholder="PauliePocket is just better than you...",
|
| 250 |
label="Message",
|
| 251 |
lines=3,
|
| 252 |
)
|
|
|
|
| 258 |
["Generate a song about a girl falling in love."],
|
| 259 |
],
|
| 260 |
inputs=message,
|
| 261 |
+
label="Try these stupid prompts",
|
| 262 |
)
|
| 263 |
|
| 264 |
with gr.Row():
|