Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -354,16 +354,17 @@ def generate_text():
|
|
| 354 |
prompt = data['prompt']
|
| 355 |
max_new_tokens = data.get('max_new_tokens', 256)
|
| 356 |
temperature = data.get('temperature', 0.0)
|
| 357 |
-
system_prompt = data.get('system_prompt', 'You are an expert assistant.')
|
| 358 |
messages = [
|
| 359 |
{"role": "system", "content": system_prompt},
|
| 360 |
{"role": "user", "content": prompt}
|
| 361 |
]
|
|
|
|
| 362 |
encoded = tokenizer.apply_chat_template(
|
| 363 |
messages,
|
| 364 |
add_generation_prompt=True,
|
| 365 |
tokenize=True,
|
| 366 |
-
enable_thinking=False
|
| 367 |
)
|
| 368 |
if IS_DIFFUSION:
|
| 369 |
input_ids = torch.tensor([encoded], dtype=torch.long, device=device)
|
|
@@ -403,14 +404,14 @@ def generate_text_stream():
|
|
| 403 |
prompt = data['prompt']
|
| 404 |
max_new_tokens = data.get('max_new_tokens', 256)
|
| 405 |
temperature = data.get('temperature', 0.0)
|
| 406 |
-
system_prompt = data.get('system_prompt', 'You are an expert assistant.')
|
| 407 |
messages = [
|
| 408 |
{"role": "system", "content": system_prompt},
|
| 409 |
{"role": "user", "content": prompt}
|
| 410 |
]
|
| 411 |
encoded = tokenizer.apply_chat_template(
|
| 412 |
messages, add_generation_prompt=True, tokenize=True,
|
| 413 |
-
enable_thinking=False
|
| 414 |
)
|
| 415 |
if IS_DIFFUSION:
|
| 416 |
input_ids = torch.tensor([encoded], dtype=torch.long, device=device)
|
|
@@ -463,7 +464,7 @@ def generate_text_sse():
|
|
| 463 |
]
|
| 464 |
encoded = tokenizer.apply_chat_template(
|
| 465 |
messages, add_generation_prompt=True, tokenize=True,
|
| 466 |
-
enable_thinking=False
|
| 467 |
)
|
| 468 |
input_ids = torch.tensor([encoded], dtype=torch.long, device=device)
|
| 469 |
|
|
@@ -480,7 +481,9 @@ def generate_text_sse():
|
|
| 480 |
temperature=temperature, cfg_scale=cfg_scale, remasking=remasking,
|
| 481 |
capture_interval=capture_interval
|
| 482 |
):
|
| 483 |
-
yield f"data: {json.dumps(state)}
|
|
|
|
|
|
|
| 484 |
else:
|
| 485 |
streamer = TextIteratorStreamer(
|
| 486 |
tokenizer, skip_prompt=True, skip_special_tokens=True
|
|
@@ -493,14 +496,25 @@ def generate_text_sse():
|
|
| 493 |
do_sample=True if temperature > 0 else False,
|
| 494 |
pad_token_id=tokenizer.eos_token_id,
|
| 495 |
)
|
|
|
|
| 496 |
def _generate():
|
| 497 |
with torch.inference_mode():
|
| 498 |
model.generate(**generation_kwargs)
|
|
|
|
| 499 |
thread = Thread(target=_generate)
|
| 500 |
thread.start()
|
|
|
|
|
|
|
| 501 |
for text in streamer:
|
| 502 |
-
|
| 503 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 504 |
|
| 505 |
return Response(
|
| 506 |
stream(), mimetype='text/event-stream',
|
|
|
|
| 354 |
prompt = data['prompt']
|
| 355 |
max_new_tokens = data.get('max_new_tokens', 256)
|
| 356 |
temperature = data.get('temperature', 0.0)
|
| 357 |
+
system_prompt = data.get('system_prompt', 'You are an expert real-time translation assistant.')
|
| 358 |
messages = [
|
| 359 |
{"role": "system", "content": system_prompt},
|
| 360 |
{"role": "user", "content": prompt}
|
| 361 |
]
|
| 362 |
+
# enable_thinking=False for ALL routes to prevent Qwen3 from leaking internal monologue
|
| 363 |
encoded = tokenizer.apply_chat_template(
|
| 364 |
messages,
|
| 365 |
add_generation_prompt=True,
|
| 366 |
tokenize=True,
|
| 367 |
+
enable_thinking=False
|
| 368 |
)
|
| 369 |
if IS_DIFFUSION:
|
| 370 |
input_ids = torch.tensor([encoded], dtype=torch.long, device=device)
|
|
|
|
| 404 |
prompt = data['prompt']
|
| 405 |
max_new_tokens = data.get('max_new_tokens', 256)
|
| 406 |
temperature = data.get('temperature', 0.0)
|
| 407 |
+
system_prompt = data.get('system_prompt', 'You are an expert real-time translation assistant.')
|
| 408 |
messages = [
|
| 409 |
{"role": "system", "content": system_prompt},
|
| 410 |
{"role": "user", "content": prompt}
|
| 411 |
]
|
| 412 |
encoded = tokenizer.apply_chat_template(
|
| 413 |
messages, add_generation_prompt=True, tokenize=True,
|
| 414 |
+
enable_thinking=False
|
| 415 |
)
|
| 416 |
if IS_DIFFUSION:
|
| 417 |
input_ids = torch.tensor([encoded], dtype=torch.long, device=device)
|
|
|
|
| 464 |
]
|
| 465 |
encoded = tokenizer.apply_chat_template(
|
| 466 |
messages, add_generation_prompt=True, tokenize=True,
|
| 467 |
+
enable_thinking=False
|
| 468 |
)
|
| 469 |
input_ids = torch.tensor([encoded], dtype=torch.long, device=device)
|
| 470 |
|
|
|
|
| 481 |
temperature=temperature, cfg_scale=cfg_scale, remasking=remasking,
|
| 482 |
capture_interval=capture_interval
|
| 483 |
):
|
| 484 |
+
yield f"data: {json.dumps(state)}
|
| 485 |
+
|
| 486 |
+
"
|
| 487 |
else:
|
| 488 |
streamer = TextIteratorStreamer(
|
| 489 |
tokenizer, skip_prompt=True, skip_special_tokens=True
|
|
|
|
| 496 |
do_sample=True if temperature > 0 else False,
|
| 497 |
pad_token_id=tokenizer.eos_token_id,
|
| 498 |
)
|
| 499 |
+
|
| 500 |
def _generate():
|
| 501 |
with torch.inference_mode():
|
| 502 |
model.generate(**generation_kwargs)
|
| 503 |
+
|
| 504 |
thread = Thread(target=_generate)
|
| 505 |
thread.start()
|
| 506 |
+
|
| 507 |
+
accumulated = []
|
| 508 |
for text in streamer:
|
| 509 |
+
accumulated.append(text)
|
| 510 |
+
yield f"data: {json.dumps({'type': 'token', 'text': text})}
|
| 511 |
+
|
| 512 |
+
"
|
| 513 |
+
|
| 514 |
+
full_text = "".join(accumulated)
|
| 515 |
+
yield f"data: {json.dumps({'type': 'final', 'text': full_text, 'total_steps': 1})}
|
| 516 |
+
|
| 517 |
+
"
|
| 518 |
|
| 519 |
return Response(
|
| 520 |
stream(), mimetype='text/event-stream',
|