File size: 1,564 Bytes
2c8d1ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

# 預先定義 Hugging Face 模型
MODEL_NAMES = {
    "DeepSeek-V3": "deepseek-ai/DeepSeek-V3",
    "DeepSeek-R1": "deepseek-ai/DeepSeek-R1"
}

def load_model(model_name):
    """載入 Hugging Face 模型與 tokenizer"""
    model_path = MODEL_NAMES.get(model_name, "deepseek-ai/DeepSeek-V3")
    tokenizer = AutoTokenizer.from_pretrained(model_path)
    model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.float16).cuda()
    return model, tokenizer

# 預設載入 DeepSeek-V3
current_model, current_tokenizer = load_model("DeepSeek-V3")

def chat(message, history, model_name):
    """處理聊天訊息"""
    global current_model, current_tokenizer
    
    # 若模型不同則切換
    if model_name != current_model:
        current_model, current_tokenizer = load_model(model_name)
    
    inputs = current_tokenizer(message, return_tensors="pt").to("cuda")
    outputs = current_model.generate(**inputs, max_length=1024)
    response = current_tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    return response

with gr.Blocks() as app:
    gr.Markdown("## Chatbot with DeepSeek Models")
    
    with gr.Row():
        chat_interface = gr.ChatInterface(chat, streaming=True, save_history=True)
        model_selector = gr.Dropdown(
            choices=list(MODEL_NAMES.keys()),
            value="DeepSeek-V3",
            label="Select Model"
        )
    
    chat_interface.append(model_selector)
    app.launch()