Spaces:
Running on Zero
Running on Zero
electblake commited on
Commit ·
d6e2866
1
Parent(s): af668f0
fix: let model complete native reasoning
Browse files
app.py
CHANGED
|
@@ -57,7 +57,7 @@ def file_to_text(file_path: str | None) -> str:
|
|
| 57 |
return path.read_text(encoding="utf-8")
|
| 58 |
|
| 59 |
|
| 60 |
-
@spaces.GPU(duration=
|
| 61 |
def generate(
|
| 62 |
system_prompt: str,
|
| 63 |
user_prompt: str,
|
|
@@ -110,34 +110,22 @@ def generate(
|
|
| 110 |
return_tensors="pt",
|
| 111 |
).to(model.device)
|
| 112 |
|
| 113 |
-
think_end_token = tokenizer.convert_tokens_to_ids("</think>")
|
| 114 |
with torch.inference_mode():
|
| 115 |
-
|
| 116 |
**inputs,
|
| 117 |
-
max_new_tokens=
|
| 118 |
do_sample=True,
|
| 119 |
temperature=0.6,
|
| 120 |
top_p=0.95,
|
| 121 |
top_k=20,
|
| 122 |
-
eos_token_id=think_end_token,
|
| 123 |
)
|
| 124 |
-
if reasoning_ids[0, -1].item() != think_end_token:
|
| 125 |
-
reasoning_ids = torch.cat(
|
| 126 |
-
[
|
| 127 |
-
reasoning_ids,
|
| 128 |
-
torch.tensor([[think_end_token]], device=model.device),
|
| 129 |
-
],
|
| 130 |
-
dim=-1,
|
| 131 |
-
)
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
max_new_tokens=512,
|
| 137 |
-
)
|
| 138 |
|
| 139 |
return tokenizer.decode(
|
| 140 |
-
|
| 141 |
skip_special_tokens=True,
|
| 142 |
).strip()
|
| 143 |
|
|
|
|
| 57 |
return path.read_text(encoding="utf-8")
|
| 58 |
|
| 59 |
|
| 60 |
+
@spaces.GPU(duration=300)
|
| 61 |
def generate(
|
| 62 |
system_prompt: str,
|
| 63 |
user_prompt: str,
|
|
|
|
| 110 |
return_tensors="pt",
|
| 111 |
).to(model.device)
|
| 112 |
|
|
|
|
| 113 |
with torch.inference_mode():
|
| 114 |
+
generated = model.generate(
|
| 115 |
**inputs,
|
| 116 |
+
max_new_tokens=8192,
|
| 117 |
do_sample=True,
|
| 118 |
temperature=0.6,
|
| 119 |
top_p=0.95,
|
| 120 |
top_k=20,
|
|
|
|
| 121 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
|
| 123 |
+
output_ids = generated[0, inputs["input_ids"].shape[-1] :].tolist()
|
| 124 |
+
think_end_token = tokenizer.convert_tokens_to_ids("</think>")
|
| 125 |
+
answer_start = len(output_ids) - output_ids[::-1].index(think_end_token)
|
|
|
|
|
|
|
| 126 |
|
| 127 |
return tokenizer.decode(
|
| 128 |
+
output_ids[answer_start:],
|
| 129 |
skip_special_tokens=True,
|
| 130 |
).strip()
|
| 131 |
|