hsuwill000 commited on
Commit
d4c56cf
·
verified ·
1 Parent(s): 76da388

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -37
app.py CHANGED
@@ -8,60 +8,45 @@ model_id = "hsuwill000/DeepSeek-R1-Distill-Qwen-1.5B-openvino"
8
  model = OVModelForCausalLM.from_pretrained(model_id, device_map="auto")
9
  tokenizer = AutoTokenizer.from_pretrained(model_id)
10
 
11
- def respond(prompt, history):
12
- # 建立初始的系統訊息
 
 
 
 
 
 
13
  messages = [
14
- {"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."}
 
15
  ]
16
-
17
- # 將歷史對話內容整合進 messages(注意這裡轉換成字典)
18
- for msg in history:
19
- # 假設 history 中每個元素原本是字典格式 (如果不是,請自行轉換)
20
- messages.append({"role": "user", "content": msg["user"]})
21
- messages.append({"role": "assistant", "content": msg["assistant"]})
22
-
23
- # 加入當前用戶輸入
24
- messages.append({"role": "user", "content": prompt})
25
-
26
- # 構造模型輸入並生成回覆
27
  text = tokenizer.apply_chat_template(
28
  messages,
29
  tokenize=False,
30
  add_generation_prompt=True
31
  )
32
- model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
33
  generated_ids = model.generate(
34
  **model_inputs,
35
  max_new_tokens=512
36
- )
 
 
 
 
37
  generated_ids = [
38
  output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
39
  ]
40
- response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
41
-
 
42
  print(f"Messages: {messages}")
43
  print(f"Reply: {response}")
44
  return response
45
- '''''
46
- # 更新並返回完整的聊天歷史(改為字典列表)
47
- new_history = history.copy()
48
- new_history.append({"user": prompt, "assistant": response})
49
- # 最後返回的是一個消息列表,每一條消息為字典格式(可進一步轉換為 ChatMessage 格式)
50
- final_messages = []
51
- # 如果需要顯示完整對話,可將歷史中每一條對話分拆成兩條消息
52
- for item in new_history:
53
- final_messages.append({"role": "user", "content": item["user"]})
54
- final_messages.append({"role": "assistant", "content": item["assistant"]})
55
- return final_messages
56
- '''''
57
-
58
  # 設定 Gradio 的聊天界面
59
- demo = gr.ChatInterface(
60
- fn=respond,
61
- title="DeepSeek-R1-Distill-Qwen-1.5B-openvino Chat",
62
- description="Chat with DeepSeek-R1-Distill-Qwen-1.5B-openvino model.",
63
- type="messages"
64
- )
65
 
66
  if __name__ == "__main__":
67
  demo.launch()
 
 
8
  model = OVModelForCausalLM.from_pretrained(model_id, device_map="auto")
9
  tokenizer = AutoTokenizer.from_pretrained(model_id)
10
 
11
+ # 建立生成管道
12
+ #pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
13
+
14
+ def respond(prompt , history):
15
+ # 將當前訊息與歷史訊息合併
16
+ #input_text = message if not history else history[-1]["content"] + " " + message
17
+ #input_text = message+",(450字內回覆)"
18
+
19
  messages = [
20
+ {"role": "system", "content": "You are Qwen, created by Alibaba Cloud. You are a helpful assistant."},
21
+ {"role": "user", "content": prompt }
22
  ]
 
 
 
 
 
 
 
 
 
 
 
23
  text = tokenizer.apply_chat_template(
24
  messages,
25
  tokenize=False,
26
  add_generation_prompt=True
27
  )
28
+ model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
29
  generated_ids = model.generate(
30
  **model_inputs,
31
  max_new_tokens=512
32
+ )
33
+
34
+ # 獲取模型的回應
35
+ #response = pipe(input_text, max_length=512, truncation=True, num_return_sequences=1)
36
+ #reply = response[0]['generated_text']
37
  generated_ids = [
38
  output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
39
  ]
40
+ response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
41
+
42
+ # 返回新的消息格式
43
  print(f"Messages: {messages}")
44
  print(f"Reply: {response}")
45
  return response
46
+
 
 
 
 
 
 
 
 
 
 
 
 
47
  # 設定 Gradio 的聊天界面
48
+ demo = gr.ChatInterface(fn=respond, title="Qwen2.5-0.5B-Instruct-openvino-4bit", description="Qwen2.5-0.5B-Instruct-openvino-4bit", type='messages')
 
 
 
 
 
49
 
50
  if __name__ == "__main__":
51
  demo.launch()
52
+