mrbui1990 commited on
Commit
0dcf288
·
verified ·
1 Parent(s): 7dd5908

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -9
app.py CHANGED
@@ -14,23 +14,44 @@ model = AutoModelForCausalLM.from_pretrained(
14
  )
15
 
16
  @spaces.GPU(duration=60) # cấp GPU tạm cho 1 phút
17
- def chat_with_model(prompt):
18
- inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
19
- output = model.generate(
20
- **inputs,
21
- max_new_tokens=256,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  do_sample=True,
23
  temperature=0.7,
24
  top_p=0.9
25
  )
26
- text = tokenizer.decode(output[0], skip_special_tokens=True)
27
- return text
 
 
28
 
 
29
  demo = gr.Interface(
30
  fn=chat_with_model,
31
- inputs=gr.Textbox(label="Prompt"),
 
 
 
32
  outputs="text",
33
- title="Xyntrai-Mistral 2.5 7B Chat (ZeroGPU)"
34
  )
35
 
36
  if __name__ == "__main__":
 
14
  )
15
 
16
  @spaces.GPU(duration=60) # cấp GPU tạm cho 1 phút
17
+ def chat_with_model(prompt, system_prompt):
18
+
19
+ # 1. Tạo danh sách messages theo chuẩn ChatML
20
+ messages = [
21
+ {"role": "system", "content": system_prompt},
22
+ {"role": "user", "content": prompt}
23
+ ]
24
+
25
+ # 2. Dùng apply_chat_template để tokenizer tự động thêm các thẻ <|im_start|>
26
+ inputs = tokenizer.apply_chat_template(
27
+ messages,
28
+ tokenize=True,
29
+ add_generation_prompt=True, # Thêm thẻ <|im_start|>assistant
30
+ return_tensors="pt"
31
+ ).to(model.device)
32
+
33
+ # 3. Generate
34
+ output_tokens = model.generate(
35
+ inputs,
36
+ max_new_tokens=1024,
37
  do_sample=True,
38
  temperature=0.7,
39
  top_p=0.9
40
  )
41
+
42
+ # 4. Decode (chỉ decode phần nội dung mới, bỏ qua phần prompt)
43
+ response_text = tokenizer.decode(output_tokens[0][inputs.shape[-1]:], skip_special_tokens=True)
44
+ return response_text
45
 
46
+ # Cập nhật Gradio Interface
47
  demo = gr.Interface(
48
  fn=chat_with_model,
49
+ inputs=[
50
+ gr.Textbox(label="Prompt"),
51
+ gr.Textbox(label="System Prompt", value="You are an uncensored and helpful AI assistant.") # Đặt ví dụ 2 làm mặc định
52
+ ],
53
  outputs="text",
54
+ title="Xyntrai-Mistral 2.5 7B Chat"
55
  )
56
 
57
  if __name__ == "__main__":