File size: 2,113 Bytes
658af68
ee7c5db
 
658af68
ee7c5db
 
658af68
ee7c5db
 
658af68
ee7c5db
 
 
 
 
 
 
658af68
ee7c5db
 
 
658af68
ee7c5db
 
658af68
ee7c5db
 
 
 
 
658af68
ee7c5db
 
658af68
ee7c5db
658af68
ee7c5db
 
 
 
 
658af68
ee7c5db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
658af68
ee7c5db
 
 
 
658af68
 
ee7c5db
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

# 定义模型名称(替换为您上传的模型名称)
model_name = "larry1129/WooWoof_AI"  # 替换为您的模型名称

# 加载分词器
tokenizer = AutoTokenizer.from_pretrained(model_name)

# 加载模型
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    device_map="auto",
    torch_dtype=torch.float16,
    trust_remote_code=True  # 如果你的模型使用自定义代码,请保留此参数
)

# 设置 pad_token
tokenizer.pad_token = tokenizer.eos_token
model.config.pad_token_id = tokenizer.pad_token_id

# 切换到评估模式
model.eval()

# 定义提示生成函数
def generate_prompt(instruction, input_text=""):
    if input_text:
        prompt = f"""### Instruction:
{instruction}

### Input:
{input_text}

### Response:
"""
    else:
        prompt = f"""### Instruction:
{instruction}

### Response:
"""
    return prompt

# 定义生成响应的函数
def generate_response(instruction, input_text):
    prompt = generate_prompt(instruction, input_text)
    inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
    
    with torch.no_grad():
        outputs = model.generate(
            input_ids=inputs["input_ids"],
            attention_mask=inputs["attention_mask"],
            max_new_tokens=128,
            temperature=0.7,
            top_p=0.95,
            do_sample=True,
        )
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    response = response.split("### Response:")[-1].strip()
    return response

# 创建 Gradio 接口
iface = gr.Interface(
    fn=generate_response,
    inputs=[
        gr.inputs.Textbox(lines=2, placeholder="请输入指令...", label="Instruction"),
        gr.inputs.Textbox(lines=2, placeholder="如果有额外输入,请在此填写...", label="Input (可选)")
    ],
    outputs="text",
    title="WooWoof AI 交互式聊天",
    description="基于 LLAMA 3.1 的大语言模型,支持指令和可选输入。",
    allow_flagging="never"
)

# 启动 Gradio 接口
iface.launch()