簡嘉琳 commited on
Commit
bb42d34
·
2 Parent(s): 81827e0 c4a5299

Merge branch 'main' of https://huggingface.co/spaces/unausagi/chatbot

Browse files
Files changed (2) hide show
  1. app.py +45 -21
  2. requirements.txt +7 -1
app.py CHANGED
@@ -1,15 +1,15 @@
1
  import gradio as gr
2
- from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
  import os
5
 
6
- # 預先定義 Hugging Face 模型
7
  MODEL_NAMES = {
8
- "DeepSeek-V3": "deepseek-ai/DeepSeek-V3",
9
- "DeepSeek-R1": "deepseek-ai/DeepSeek-R1",
10
  }
11
 
12
  HF_TOKEN = os.getenv("HF_TOKEN")
 
13
 
14
 
15
  def load_model(model_path):
@@ -19,36 +19,60 @@ def load_model(model_path):
19
  model = AutoModelForCausalLM.from_pretrained(
20
  model_path, torch_dtype=torch.float16, trust_remote_code=True, token=HF_TOKEN
21
  ).cuda()
22
- return model, tokenizer
23
 
 
 
 
 
 
24
 
25
- # 預設載入 DeepSeek-V3
26
- current_model, current_tokenizer = load_model("deepseek-ai/DeepSeek-V3")
 
 
 
 
 
 
 
 
27
 
 
 
 
28
 
29
  def chat(message, history, model_name):
30
- """處理聊天訊息"""
31
- global current_model, current_tokenizer
32
 
33
- # 若模型不同則切換
34
- if model_name != current_model:
35
- current_model, current_tokenizer = load_model(model_name)
 
36
 
37
- inputs = current_tokenizer(message, return_tensors="pt").to("cuda")
 
38
  outputs = current_model.generate(**inputs, max_length=1024)
39
  response = current_tokenizer.decode(outputs[0], skip_special_tokens=True)
40
 
41
  return response
42
 
43
-
44
  with gr.Blocks() as app:
45
  gr.Markdown("## Chatbot with DeepSeek Models")
46
 
47
- with gr.Row():
48
- chat_interface = gr.ChatInterface(chat, streaming=True, save_history=True)
49
- model_selector = gr.Dropdown(
50
- choices=list(MODEL_NAMES.keys()), value="DeepSeek-V3", label="Select Model"
51
- )
 
 
 
 
 
 
 
 
 
52
 
53
- chat_interface.append(model_selector)
54
- app.launch()
 
1
  import gradio as gr
2
+ from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
3
  import torch
4
  import os
5
 
 
6
  MODEL_NAMES = {
7
+ "DeepSeek-R1-Distill-Qwen-7B": "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
8
+ "DeepSeek-R1-Distill-Llama-8B": "deepseek-ai/DeepSeek-R1-Distill-Llama-8B",
9
  }
10
 
11
  HF_TOKEN = os.getenv("HF_TOKEN")
12
+ <<<<<<< HEAD
13
 
14
 
15
  def load_model(model_path):
 
19
  model = AutoModelForCausalLM.from_pretrained(
20
  model_path, torch_dtype=torch.float16, trust_remote_code=True, token=HF_TOKEN
21
  ).cuda()
22
+ =======
23
 
24
+ def load_model(model_path):
25
+ tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True, token=HF_TOKEN)
26
+ config = AutoConfig.from_pretrained(model_path, trust_remote_code=True, token=HF_TOKEN)
27
+ if hasattr(config, "quantization_config"):
28
+ del config.quantization_config # 刪除量化配置,避免使用 FP8
29
 
30
+ model = AutoModelForCausalLM.from_pretrained(
31
+ model_path,
32
+ config=config,
33
+ trust_remote_code=True,
34
+ token=HF_TOKEN,
35
+ torch_dtype=torch.float16,
36
+ device_map="auto",
37
+ )
38
+ >>>>>>> c4a529954b9cc11b7b8c2dcdac51f3ef3a83c616
39
+ return model, tokenizer
40
 
41
+ # 初始化預設模型
42
+ current_model_name = "DeepSeek-R1-Distill-Llama-8B"
43
+ current_model, current_tokenizer = load_model(MODEL_NAMES[current_model_name])
44
 
45
  def chat(message, history, model_name):
46
+ global current_model, current_tokenizer, current_model_name
 
47
 
48
+ # 檢查是否需要更換模型
49
+ if model_name != current_model_name:
50
+ current_model_name = model_name
51
+ current_model, current_tokenizer = load_model(MODEL_NAMES[model_name])
52
 
53
+ device = "cuda" if torch.cuda.is_available() else "cpu"
54
+ inputs = current_tokenizer(message, return_tensors="pt").to(device)
55
  outputs = current_model.generate(**inputs, max_length=1024)
56
  response = current_tokenizer.decode(outputs[0], skip_special_tokens=True)
57
 
58
  return response
59
 
 
60
  with gr.Blocks() as app:
61
  gr.Markdown("## Chatbot with DeepSeek Models")
62
 
63
+ model_selector = gr.Dropdown(
64
+ choices=list(MODEL_NAMES.keys()),
65
+ value=current_model_name,
66
+ label="Select Model",
67
+ )
68
+
69
+ chat_interface = gr.ChatInterface(
70
+ fn=lambda message, history: chat(message, history, model_selector.value),
71
+ type="messages",
72
+ flagging_mode="manual",
73
+ save_history=True,
74
+ )
75
+
76
+ model_selector.change(fn=lambda model_name: None, inputs=[model_selector], outputs=[])
77
 
78
+ app.launch()
 
requirements.txt CHANGED
@@ -1,2 +1,8 @@
 
1
  transformers>=4.37.2
2
- torch>=2.1.0
 
 
 
 
 
 
1
+ <<<<<<< HEAD
2
  transformers>=4.37.2
3
+ torch>=2.1.0
4
+ =======
5
+ transformers>=4.38.0
6
+ torch>=2.1.0
7
+ accelerate
8
+ >>>>>>> c4a529954b9cc11b7b8c2dcdac51f3ef3a83c616