Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -17,16 +17,13 @@ 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:
|
@@ -34,48 +31,97 @@ def generate_prompt(instruction, input_text="", output_text=None):
|
|
34 |
return prompt
|
35 |
|
36 |
# 定义生成响应的函数,并使用 @spaces.GPU 装饰
|
37 |
-
@spaces.GPU(duration=
|
38 |
def generate_response(instruction, input_text):
|
39 |
global model, tokenizer
|
40 |
|
41 |
if model is None:
|
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 |
else:
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
print("模型或分词器未正确加载。")
|
78 |
-
raise ValueError("模型或分词器未正确加载。")
|
79 |
|
80 |
# 生成提示
|
81 |
prompt = generate_prompt(instruction, input_text)
|
|
|
17 |
if input_text:
|
18 |
prompt = f"""### Instruction:
|
19 |
{instruction}
|
|
|
20 |
### Input:
|
21 |
{input_text}
|
|
|
22 |
### Response:
|
23 |
"""
|
24 |
else:
|
25 |
prompt = f"""### Instruction:
|
26 |
{instruction}
|
|
|
27 |
### Response:
|
28 |
"""
|
29 |
if output_text:
|
|
|
31 |
return prompt
|
32 |
|
33 |
# 定义生成响应的函数,并使用 @spaces.GPU 装饰
|
34 |
+
@spaces.GPU(duration=30)
|
35 |
def generate_response(instruction, input_text):
|
36 |
global model, tokenizer
|
37 |
|
38 |
if model is None:
|
39 |
+
# 在函数内部导入需要 GPU 的库
|
40 |
+
import torch
|
41 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
42 |
+
|
43 |
+
# 加载分词器
|
44 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=hf_token)
|
45 |
+
|
46 |
+
# 加载模型
|
47 |
+
model = AutoModelForCausalLM.from_pretrained(
|
48 |
+
model_name,
|
49 |
+
device_map="auto",
|
50 |
+
torch_dtype=torch.float16,
|
51 |
+
use_auth_token=hf_token,
|
52 |
+
)
|
53 |
+
|
54 |
+
# 设置 pad_token
|
55 |
+
tokenizer.pad_token = tokenizer.eos_token
|
56 |
+
model.config.pad_token_id = tokenizer.pad_token_id
|
57 |
+
|
58 |
+
# 切换到评估模式
|
59 |
+
model.eval()
|
60 |
+
else:import spaces # 必须在最顶部导入
|
61 |
+
import gradio as gr
|
62 |
+
import os
|
63 |
+
|
64 |
+
# 获取 Hugging Face 访问令牌
|
65 |
+
hf_token = os.getenv("HF_API_TOKEN")
|
66 |
+
|
67 |
+
# 定义基础模型名称
|
68 |
+
base_model_name = "unsloth/meta-llama-3.1-8b-bnb-4bit"
|
69 |
+
|
70 |
+
# 定义 adapter 模型名称
|
71 |
+
adapter_model_name = "larry1129/WooWoof_AI"
|
72 |
+
|
73 |
+
# 定义全局变量用于缓存模型和分词器
|
74 |
+
model = None
|
75 |
+
tokenizer = None
|
76 |
+
|
77 |
+
# 定义提示生成函数
|
78 |
+
def generate_prompt(instruction, input_text=""):
|
79 |
+
if input_text:
|
80 |
+
prompt = f"""### Instruction:
|
81 |
+
{instruction}
|
82 |
+
### Input:
|
83 |
+
{input_text}
|
84 |
+
### Response:
|
85 |
+
"""
|
86 |
else:
|
87 |
+
prompt = f"""### Instruction:
|
88 |
+
{instruction}
|
89 |
+
### Response:
|
90 |
+
"""
|
91 |
+
return prompt
|
92 |
+
|
93 |
+
# 定义生成响应的函数,并使用 @spaces.GPU 装饰
|
94 |
+
@spaces.GPU(duration=120)
|
95 |
+
def generate_response(instruction, input_text):
|
96 |
+
global model, tokenizer
|
97 |
+
|
98 |
+
if model is None:
|
99 |
+
# 检查 bitsandbytes 是否已安装
|
100 |
+
import importlib.util
|
101 |
+
if importlib.util.find_spec("bitsandbytes") is None:
|
102 |
+
import subprocess
|
103 |
+
subprocess.call(["pip", "install", "--upgrade", "bitsandbytes"])
|
104 |
+
|
105 |
+
# 在函数内部导入需要 GPU 的库
|
106 |
import torch
|
107 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
108 |
+
from peft import PeftModel
|
109 |
+
|
110 |
+
# 创建量化配置
|
111 |
+
bnb_config = BitsAndBytesConfig(
|
112 |
+
load_in_4bit=True,
|
113 |
+
bnb_4bit_use_double_quant=True,
|
114 |
+
bnb_4bit_quant_type="nf4",
|
115 |
+
bnb_4bit_compute_dtype=torch.float16
|
116 |
+
)
|
117 |
+
|
118 |
+
# 加载分词器
|
119 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name, use_auth_token=hf_token)
|
120 |
+
|
121 |
+
# 加载基础模型
|
122 |
|
123 |
+
# 在函数内部导入需要的库
|
124 |
+
import torch
|
|
|
|
|
125 |
|
126 |
# 生成提示
|
127 |
prompt = generate_prompt(instruction, input_text)
|