Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,6 @@ if not HF_TOKEN:
|
|
| 14 |
print(f"Loading model: {MODEL_ID} ...")
|
| 15 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 16 |
|
| 17 |
-
# Critical: Ensure pad token is set
|
| 18 |
if tokenizer.pad_token is None:
|
| 19 |
tokenizer.pad_token = tokenizer.eos_token
|
| 20 |
|
|
@@ -23,7 +22,7 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 23 |
token=HF_TOKEN,
|
| 24 |
torch_dtype="auto",
|
| 25 |
device_map="auto",
|
| 26 |
-
trust_remote_code=True,
|
| 27 |
)
|
| 28 |
|
| 29 |
def build_messages(history: List[Tuple[str, str]], user_message: str):
|
|
@@ -39,9 +38,13 @@ def build_messages(history: List[Tuple[str, str]], user_message: str):
|
|
| 39 |
def chat_fn(
|
| 40 |
message: str,
|
| 41 |
history: List[Tuple[str, str]],
|
| 42 |
-
temperature: float = 0.7,
|
| 43 |
-
max_new_tokens: int = 512,
|
| 44 |
) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
messages = build_messages(history, message)
|
| 46 |
|
| 47 |
prompt = tokenizer.apply_chat_template(
|
|
@@ -53,7 +56,6 @@ def chat_fn(
|
|
| 53 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 54 |
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 55 |
|
| 56 |
-
# Stronger generation config to avoid NaN/inf in probabilities
|
| 57 |
with torch.no_grad():
|
| 58 |
output_ids = model.generate(
|
| 59 |
**inputs,
|
|
@@ -65,10 +67,9 @@ def chat_fn(
|
|
| 65 |
repetition_penalty=1.1,
|
| 66 |
pad_token_id=tokenizer.pad_token_id,
|
| 67 |
eos_token_id=tokenizer.eos_token_id,
|
| 68 |
-
renormalize_logits=True,
|
| 69 |
)
|
| 70 |
|
| 71 |
-
# Extract only the new tokens
|
| 72 |
generated_ids = output_ids[0][inputs["input_ids"].shape[-1] :]
|
| 73 |
response = tokenizer.decode(generated_ids, skip_special_tokens=True).strip()
|
| 74 |
|
|
@@ -81,12 +82,14 @@ demo = gr.ChatInterface(
|
|
| 81 |
gr.Slider(0.0, 1.5, value=0.7, step=0.05, label="Temperature"),
|
| 82 |
gr.Slider(32, 2048, value=512, step=32, label="Max New Tokens"),
|
| 83 |
],
|
|
|
|
| 84 |
title="Qwen Workflow Planner Chat",
|
| 85 |
description=f"Model: {MODEL_ID}",
|
| 86 |
examples=[
|
| 87 |
["Plan a simple content creation workflow"],
|
| 88 |
["How to automate a daily report generation process?"],
|
| 89 |
],
|
|
|
|
| 90 |
)
|
| 91 |
|
| 92 |
if __name__ == "__main__":
|
|
|
|
| 14 |
print(f"Loading model: {MODEL_ID} ...")
|
| 15 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=HF_TOKEN)
|
| 16 |
|
|
|
|
| 17 |
if tokenizer.pad_token is None:
|
| 18 |
tokenizer.pad_token = tokenizer.eos_token
|
| 19 |
|
|
|
|
| 22 |
token=HF_TOKEN,
|
| 23 |
torch_dtype="auto",
|
| 24 |
device_map="auto",
|
| 25 |
+
trust_remote_code=True,
|
| 26 |
)
|
| 27 |
|
| 28 |
def build_messages(history: List[Tuple[str, str]], user_message: str):
|
|
|
|
| 38 |
def chat_fn(
|
| 39 |
message: str,
|
| 40 |
history: List[Tuple[str, str]],
|
| 41 |
+
temperature: float | None = 0.7, # <-- default here
|
| 42 |
+
max_new_tokens: int | None = 512, # <-- default here
|
| 43 |
) -> str:
|
| 44 |
+
# Handle None values (from example caching)
|
| 45 |
+
temperature = temperature if temperature is not None else 0.7
|
| 46 |
+
max_new_tokens = max_new_tokens if max_new_tokens is not None else 512
|
| 47 |
+
|
| 48 |
messages = build_messages(history, message)
|
| 49 |
|
| 50 |
prompt = tokenizer.apply_chat_template(
|
|
|
|
| 56 |
inputs = tokenizer(prompt, return_tensors="pt")
|
| 57 |
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 58 |
|
|
|
|
| 59 |
with torch.no_grad():
|
| 60 |
output_ids = model.generate(
|
| 61 |
**inputs,
|
|
|
|
| 67 |
repetition_penalty=1.1,
|
| 68 |
pad_token_id=tokenizer.pad_token_id,
|
| 69 |
eos_token_id=tokenizer.eos_token_id,
|
| 70 |
+
renormalize_logits=True,
|
| 71 |
)
|
| 72 |
|
|
|
|
| 73 |
generated_ids = output_ids[0][inputs["input_ids"].shape[-1] :]
|
| 74 |
response = tokenizer.decode(generated_ids, skip_special_tokens=True).strip()
|
| 75 |
|
|
|
|
| 82 |
gr.Slider(0.0, 1.5, value=0.7, step=0.05, label="Temperature"),
|
| 83 |
gr.Slider(32, 2048, value=512, step=32, label="Max New Tokens"),
|
| 84 |
],
|
| 85 |
+
additional_inputs_accordion=gr.Accordion("Generation Settings", open=False),
|
| 86 |
title="Qwen Workflow Planner Chat",
|
| 87 |
description=f"Model: {MODEL_ID}",
|
| 88 |
examples=[
|
| 89 |
["Plan a simple content creation workflow"],
|
| 90 |
["How to automate a daily report generation process?"],
|
| 91 |
],
|
| 92 |
+
cache_examples=False, # Recommended on HF Spaces with additional inputs
|
| 93 |
)
|
| 94 |
|
| 95 |
if __name__ == "__main__":
|