quancute commited on
Commit
8674962
·
verified ·
1 Parent(s): 4796137

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py CHANGED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForCausalLM
2
+ from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training
3
+ import torch
4
+ # from huggingface_hub import log
5
+
6
+ # Cấu hình mô hình
7
+ MODEL = "Viet-Mistral/Vistral-7B-Chat"
8
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
9
+ print('device =', device)
10
+
11
+ # Load mô hình và tokenizer
12
+ model = AutoModelForCausalLM.from_pretrained(
13
+ 'Viet-Mistral/Vistral-7B-Chat',
14
+ torch_dtype=torch.bfloat16, # change to torch.float16 if you're using V100
15
+ device_map="auto",
16
+ use_cache=True,
17
+ cache_dir='./hf_cache'
18
+ )
19
+
20
+ tokenizer = AutoTokenizer.from_pretrained(MODEL, cache_dir='./hf_cache')
21
+
22
+ lora_config = LoraConfig.from_pretrained(
23
+ "thviet79/model-QA-medical", # Thay bằng đường dẫn đến mô hình LoRA trên Hugging Face
24
+ cache_dir='/workspace/thviet/hf_cache'
25
+ )
26
+
27
+ # Áp dụng cấu hình LoRA vào mô hình
28
+ model = get_peft_model(model, lora_config)
29
+
30
+ # def generate_output(input_text:str,
31
+ # top_p:float=0.95,
32
+ # top_k:int=40,
33
+ # temperature:float=0.1,
34
+ # repetition_penalty:float=1.05,
35
+ # max_new_tokens:int=768):
36
+ # system_prompt = "Bạn là một trợ lí ảo Tiếng Việt về lĩnh vực y tế."
37
+ # conversation = [{"role": "system", "content": system_prompt }]
38
+ # human = f"Vui lòng trả lời câu hỏi sau: {input_text}"
39
+ # conversation.append({"role": "user", "content": human })
40
+
41
+ # # Chuyển các tensor đầu vào sang đúng thiết bị
42
+ # input_ids = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(device)
43
+
44
+ # # Tạo đầu ra từ mô hình
45
+ # out_ids = model.generate(
46
+ # input_ids=input_ids,
47
+ # max_new_tokens=768,
48
+ # do_sample=True,
49
+ # top_p=0.95,
50
+ # top_k=40,
51
+ # temperature=0.1,
52
+ # repetition_penalty=1.05,
53
+ # )
54
+
55
+ # # Giải mã và in kết quả
56
+ # assistant = tokenizer.batch_decode(out_ids[:, input_ids.size(1):], skip_special_tokens=True)[0].strip()
57
+ # return assistant
58
+
59
+ def respond(
60
+ message,
61
+ history: list[tuple[str, str]],
62
+ system_message: str,
63
+ max_tokens,
64
+ temperature,
65
+ top_p,
66
+ ):
67
+ sys_prompt = "Bạn là một trợ lí ảo Tiếng Việt về lĩnh vực y tế."
68
+ conversation = [{"role": "system", "content": sys_prompt}]
69
+
70
+
71
+ for val in history:
72
+ if val[0]:
73
+ conversation.append({"role": "user", "content": val[0]})
74
+ if val[1]:
75
+ messages.append({"role": "assistant", "content": val[1]})
76
+
77
+ conversation.append({"role": "user", "content": message})
78
+
79
+ input_ids_list = tokenizer.apply_chat_template(conversation, return_tensors="pt").to(device)
80
+ response = ""
81
+
82
+ for message in tokenizer.batch_decode(model.generate(
83
+ input_ids=input_ids,
84
+ max_new_tokens=max_tokens,
85
+ do_sample=True,
86
+ top_p=top_p,
87
+ temperature=temperature,
88
+ )[:, input_ids_list.size(1):], skip_special_tokens=True):
89
+ token = message.strip()
90
+ response += token
91
+ yield response
92
+
93
+ demo = gr.ChatInterface(
94
+ respond,
95
+ additional_inputs=[
96
+ gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
97
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
98
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
99
+ gr.Slider(
100
+ minimum=0.1,
101
+ maximum=1.0,
102
+ value=0.95,
103
+ step=0.05,
104
+ label="Top-p (nucleus sampling)",
105
+ ),
106
+ ],
107
+ )
108
+
109
+
110
+ if __name__ == "__main__":
111
+ demo.launch()