electblake commited on
Commit
be9bfee
·
1 Parent(s): d6e2866

fix: bound thinking for usable spreadsheet answers

Browse files
Files changed (1) hide show
  1. app.py +19 -7
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=300)
61
  def generate(
62
  system_prompt: str,
63
  user_prompt: str,
@@ -110,22 +110,34 @@ def generate(
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
 
 
57
  return path.read_text(encoding="utf-8")
58
 
59
 
60
+ @spaces.GPU(duration=120)
61
  def generate(
62
  system_prompt: str,
63
  user_prompt: str,
 
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
+ reasoning_ids = model.generate(
116
  **inputs,
117
+ max_new_tokens=2048,
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
+ answer_ids = model.generate(
134
+ input_ids=reasoning_ids,
135
+ attention_mask=torch.ones_like(reasoning_ids),
136
+ max_new_tokens=512,
137
+ )
138
 
139
  return tokenizer.decode(
140
+ answer_ids[0, reasoning_ids.shape[-1] :],
141
  skip_special_tokens=True,
142
  ).strip()
143