Gregniuki commited on
Commit
039b741
·
verified ·
1 Parent(s): 339dbcd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -3
app.py CHANGED
@@ -35,22 +35,34 @@ print("--- Logging in to Hugging Face Hub ---")
35
  login(token=HF_TOKEN)
36
 
37
  # --- Step 2: Initialize Model and Tokenizer ---
38
- #MODEL_NAME = "Gregniuki/Qwen3-0.6B-translate-EN-PL-EN"
39
- MODEL_NAME = "Gregniuki/Qwen3-1.7B-translatate-En-Pl-En"
40
  print(f"--- Loading model from Hugging Face Hub: {MODEL_NAME} ---")
41
 
42
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
43
  dtype = torch.bfloat16 if device.type == "cuda" else torch.float32
44
  print(f"--- Using device: {device}, dtype: {dtype} ---")
45
 
 
 
 
 
 
 
 
46
  try:
47
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
 
 
 
 
 
 
48
  model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=dtype, trust_remote_code=True).to(device)
49
  model.eval()
50
  print("--- Model and Tokenizer Loaded Successfully ---")
51
  except Exception as e:
52
  raise RuntimeError(f"FATAL: Could not load components. Error: {e}")
53
-
54
  # --- Helper Functions ---
55
  def chunk_text(text: str, max_size: int) -> list[str]:
56
  if not text: return []
 
35
  login(token=HF_TOKEN)
36
 
37
  # --- Step 2: Initialize Model and Tokenizer ---
38
+ MODEL_NAME = "Gregniuki/Qwen3-0.6B-translate-EN-PL-EN"
39
+ #MODEL_NAME = "Gregniuki/Qwen3-1.7B-translatate-En-Pl-En"
40
  print(f"--- Loading model from Hugging Face Hub: {MODEL_NAME} ---")
41
 
42
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
43
  dtype = torch.bfloat16 if device.type == "cuda" else torch.float32
44
  print(f"--- Using device: {device}, dtype: {dtype} ---")
45
 
46
+ #try:
47
+ # tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
48
+ # model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=dtype, trust_remote_code=True).to(device)
49
+ # model.eval()
50
+ # print("--- Model and Tokenizer Loaded Successfully ---")
51
+ #except Exception as e:
52
+ # raise RuntimeError(f"FATAL: Could not load components. Error: {e}")
53
  try:
54
  tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, trust_remote_code=True)
55
+
56
+ # --- FIX: Manually set the chat template for the Qwen3 model ---
57
+ qwen3_chat_template = """{% for message in messages %}{% if message['role'] == 'system' %}{{ '<|im_start|>system\n' + message['content'] + '<|im_end|>\n' }}{% elif message['role'] == 'user' %}{{ '<|im_start|>user\n' + message['content'] + '<|im_end|>\n' }}{% elif message['role'] == 'assistant' %}{{ '<|im_start|>assistant\n' + message['content'] + '<|im_end|>\n' }}{% endif %}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant\n' }}{% endif %}"""
58
+ tokenizer.chat_template = qwen3_chat_template
59
+ # --- END FIX ---
60
+
61
  model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, torch_dtype=dtype, trust_remote_code=True).to(device)
62
  model.eval()
63
  print("--- Model and Tokenizer Loaded Successfully ---")
64
  except Exception as e:
65
  raise RuntimeError(f"FATAL: Could not load components. Error: {e}")
 
66
  # --- Helper Functions ---
67
  def chunk_text(text: str, max_size: int) -> list[str]:
68
  if not text: return []