Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,68 @@ import os
|
|
5 |
# 获取 Hugging Face 访问令牌
|
6 |
hf_token = os.getenv("HF_API_TOKEN")
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
# 定义基础模型名称
|
9 |
base_model_name = "larry1129/meta-llama-3.1-8b-bnb-4bit"
|
10 |
|
@@ -60,50 +122,7 @@ def generate_response(instruction, input_text):
|
|
60 |
tokenizer = AutoTokenizer.from_pretrained(base_model_name, use_auth_token=hf_token)
|
61 |
|
62 |
# 加载基础模型
|
63 |
-
base_model = AutoModelForCausalLM.from_pretrained(
|
64 |
-
base_model_name,
|
65 |
-
quantization_config=bnb_config,
|
66 |
-
device_map="auto",
|
67 |
-
use_auth_token=hf_token,
|
68 |
-
trust_remote_code=True
|
69 |
-
)
|
70 |
-
|
71 |
-
# 加载 adapter 并将其应用到基础模型上
|
72 |
-
# 加载适配器模型时,忽略未知参数
|
73 |
-
try:
|
74 |
-
model = PeftModel.from_pretrained(
|
75 |
-
base_model,
|
76 |
-
adapter_model_name,
|
77 |
-
torch_dtype=torch.float16,
|
78 |
-
use_auth_token=hf_token
|
79 |
-
)
|
80 |
-
except TypeError as e:
|
81 |
-
if "got an unexpected keyword argument 'model_type'" in str(e):
|
82 |
-
# 手动修改配置,移除 'model_type' 参数
|
83 |
-
import json
|
84 |
-
config_path = os.path.join(adapter_model_name, 'adapter_config.json')
|
85 |
-
with open(config_path, 'r') as f:
|
86 |
-
config = json.load(f)
|
87 |
-
config.pop('model_type', None)
|
88 |
-
with open(config_path, 'w') as f:
|
89 |
-
json.dump(config, f)
|
90 |
-
# 重新加载模型
|
91 |
-
model = PeftModel.from_pretrained(
|
92 |
-
base_model,
|
93 |
-
adapter_model_name,
|
94 |
-
torch_dtype=torch.float16,
|
95 |
-
use_auth_token=hf_token
|
96 |
-
)
|
97 |
-
else:
|
98 |
-
raise e
|
99 |
|
100 |
-
# 设置 pad_token
|
101 |
-
tokenizer.pad_token = tokenizer.eos_token
|
102 |
-
model.config.pad_token_id = tokenizer.pad_token_id
|
103 |
-
|
104 |
-
# 切换到评估模式
|
105 |
-
model.eval()
|
106 |
-
else:
|
107 |
# 在函数内部导入需要的库
|
108 |
import torch
|
109 |
|
|
|
5 |
# 获取 Hugging Face 访问令牌
|
6 |
hf_token = os.getenv("HF_API_TOKEN")
|
7 |
|
8 |
+
# 定义模型名称
|
9 |
+
model_name = "larry1129/WooWoof_AI"
|
10 |
+
|
11 |
+
# 定义全局变量用于缓存模型和分词器
|
12 |
+
model = None
|
13 |
+
tokenizer = None
|
14 |
+
|
15 |
+
# 定义提示生成函数
|
16 |
+
def generate_prompt(instruction, input_text="", output_text=None):
|
17 |
+
if input_text:
|
18 |
+
prompt = f"""### Instruction:
|
19 |
+
{instruction}
|
20 |
+
|
21 |
+
### Input:
|
22 |
+
{input_text}
|
23 |
+
|
24 |
+
### Response:
|
25 |
+
"""
|
26 |
+
else:
|
27 |
+
prompt = f"""### Instruction:
|
28 |
+
{instruction}
|
29 |
+
|
30 |
+
### Response:
|
31 |
+
"""
|
32 |
+
if output_text:
|
33 |
+
prompt += f"{output_text}{tokenizer.eos_token}"
|
34 |
+
return prompt
|
35 |
+
|
36 |
+
# 定义生成响应的函数,并使用 @spaces.GPU 装饰
|
37 |
+
@spaces.GPU(duration=120)
|
38 |
+
def generate_response(instruction, input_text):
|
39 |
+
global model, tokenizer
|
40 |
+
|
41 |
+
if model is None:
|
42 |
+
# 在函数内部导入需要 GPU 的库
|
43 |
+
import torch
|
44 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
45 |
+
|
46 |
+
# 加载分词器
|
47 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=hf_token)
|
48 |
+
|
49 |
+
# 加载模型
|
50 |
+
model = AutoModelForCausalLM.from_pretrained(
|
51 |
+
model_name,
|
52 |
+
device_map="auto",
|
53 |
+
torch_dtype=torch.float16,
|
54 |
+
use_auth_token=hf_token,
|
55 |
+
)
|
56 |
+
|
57 |
+
# 设置 pad_token
|
58 |
+
tokenizer.pad_token = tokenizer.eos_token
|
59 |
+
model.config.pad_token_id = tokenizer.pad_token_id
|
60 |
+
|
61 |
+
# 切换到评估模式
|
62 |
+
model.eval()
|
63 |
+
else:import spaces # 必须在最顶部导入
|
64 |
+
import gradio as gr
|
65 |
+
import os
|
66 |
+
|
67 |
+
# 获取 Hugging Face 访问令牌
|
68 |
+
hf_token = os.getenv("HF_API_TOKEN")
|
69 |
+
|
70 |
# 定义基础模型名称
|
71 |
base_model_name = "larry1129/meta-llama-3.1-8b-bnb-4bit"
|
72 |
|
|
|
122 |
tokenizer = AutoTokenizer.from_pretrained(base_model_name, use_auth_token=hf_token)
|
123 |
|
124 |
# 加载基础模型
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
# 在函数内部导入需要的库
|
127 |
import torch
|
128 |
|