HOANGHUAN commited on
Commit
fb3a430
·
verified ·
1 Parent(s): ec01530

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -21
app.py CHANGED
@@ -1,32 +1,38 @@
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
-
5
- # Load model (sẽ tải từ repo của ông)
6
- model_id = "HOANGHUAN/dulich"
7
- tokenizer = AutoTokenizer.from_pretrained(model_id)
8
  model = AutoModelForCausalLM.from_pretrained(
9
- model_id,
10
  torch_dtype=torch.float16,
11
  device_map="auto"
12
  )
13
-
14
- def chat(message):
15
- prompt = f"### Human: {message}\n### Assistant:"
 
 
 
 
16
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
17
- outputs = model.generate(**inputs, max_new_tokens=512, temperature=0.7, do_sample=True)
 
 
 
 
 
 
 
 
18
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
19
- # Cắt phần prompt đi
20
- if prompt in response:
21
- response = response[len(prompt):].strip()
22
  return response
23
-
24
- # Tạo giao diện + API endpoint
25
- demo = gr.Interface(
26
  fn=chat,
27
- inputs=gr.Textbox(label="Câu hỏi"),
28
- outputs=gr.Textbox(label="Trả lời"),
29
- title="Vietnam Tourism Chatbot"
30
- )
31
-
32
- demo.launch()
 
1
  import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
+ # Load model
5
+ model_name = "HOANGHUAN/dulich"
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
 
7
  model = AutoModelForCausalLM.from_pretrained(
8
+ model_name,
9
  torch_dtype=torch.float16,
10
  device_map="auto"
11
  )
12
+ def chat(message, history):
13
+ messages = [
14
+ {"role": "system", "content": "Bạn là Vistours Assistant."},
15
+ {"role": "user", "content": message}
16
+ ]
17
+
18
+ prompt = tokenizer.apply_chat_template(messages, tokenize=False)
19
  inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
20
+
21
+ outputs = model.generate(
22
+ **inputs,
23
+ max_new_tokens=256,
24
+ temperature=0.7,
25
+ top_p=0.9,
26
+ do_sample=True
27
+ )
28
+
29
  response = tokenizer.decode(outputs[0], skip_special_tokens=True)
30
+ # Extract assistant response
31
+ if "assistant" in response:
32
+ response = response.split("assistant")[-1].strip()
33
  return response
34
+ gr.ChatInterface(
 
 
35
  fn=chat,
36
+ title="Vistours Chatbot",
37
+ description="AI Assistant cho công ty du lịch Vistours"
38
+ ).launch()