Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,65 +5,6 @@ import os
|
|
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 |
-
### Input:
|
21 |
-
{input_text}
|
22 |
-
### Response:
|
23 |
-
"""
|
24 |
-
else:
|
25 |
-
prompt = f"""### Instruction:
|
26 |
-
{instruction}
|
27 |
-
### Response:
|
28 |
-
"""
|
29 |
-
if output_text:
|
30 |
-
prompt += f"{output_text}{tokenizer.eos_token}"
|
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 |
|
@@ -91,7 +32,7 @@ def generate_prompt(instruction, input_text=""):
|
|
91 |
return prompt
|
92 |
|
93 |
# 定义生成响应的函数,并使用 @spaces.GPU 装饰
|
94 |
-
@spaces.GPU(duration=
|
95 |
def generate_response(instruction, input_text):
|
96 |
global model, tokenizer
|
97 |
|
|
|
5 |
# 获取 Hugging Face 访问令牌
|
6 |
hf_token = os.getenv("HF_API_TOKEN")
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
# 定义基础模型名称
|
9 |
base_model_name = "unsloth/meta-llama-3.1-8b-bnb-4bit"
|
10 |
|
|
|
32 |
return prompt
|
33 |
|
34 |
# 定义生成响应的函数,并使用 @spaces.GPU 装饰
|
35 |
+
@spaces.GPU(duration=30)
|
36 |
def generate_response(instruction, input_text):
|
37 |
global model, tokenizer
|
38 |
|