hellokawei commited on
Commit
6a3c032
·
verified ·
1 Parent(s): e27dbdf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -24
app.py CHANGED
@@ -1,48 +1,41 @@
1
  import os
2
  import torch
3
  import gradio as gr
4
- from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
5
- from typing import List, Tuple, Dict
6
 
7
- # 模型配置 - 全部使用无访问限制的公开模型
 
 
 
 
 
8
  MODELS = {
9
  "Zephyr 7B Beta": {
10
  "model_id": "HuggingFaceH4/zephyr-7b-beta",
11
  "kwargs": {"torch_dtype": torch.float16}
12
  },
13
- "Mistral 7B Instruct": {
14
- "model_id": "mistralai/Mistral-7B-Instruct-v0.2",
15
- "kwargs": {"torch_dtype": torch.float16}
16
- },
17
- "OpenHermes 2.5": {
18
- "model_id": "teknium/OpenHermes-2.5-Mistral-7B",
19
- "kwargs": {"torch_dtype": torch.float16}
20
- },
21
  "Falcon 7B Instruct": {
22
  "model_id": "tiiuae/falcon-7b-instruct",
23
- "kwargs": {"torch_dtype": torch.float16}
24
  }
25
  }
26
 
27
- # 加载模型和分词器
28
  def load_model(model_name):
29
  model_config = MODELS[model_name]
30
- tokenizer = AutoTokenizer.from_pretrained(model_config["model_id"])
31
-
32
- # 处理特殊模型参数(如需要)
33
- if "Falcon" in model_name:
34
- model_config["kwargs"]["trust_remote_code"] = True
35
-
36
  model = AutoModelForCausalLM.from_pretrained(
37
  model_config["model_id"],
 
38
  **model_config["kwargs"]
39
  )
40
-
41
- # 移动到可用设备
42
  device = "cuda" if torch.cuda.is_available() else "cpu"
43
- model = model.to(device)
44
-
45
- return model, tokenizer, device
46
 
47
  # 初始化模型
48
  loaded_models = {}
 
1
  import os
2
  import torch
3
  import gradio as gr
4
+ from transformers import AutoModelForCausalLM, AutoTokenizer
 
5
 
6
+ # 从环境变量获取Hugging Face Token
7
+ hf_token = os.environ.get("HUGGINGFACE_HUB_TOKEN")
8
+ if not hf_token:
9
+ raise EnvironmentError("未找到HUGGINGFACE_HUB_TOKEN环境变量,请在Space设置中添加")
10
+
11
+ # 模型配置 - 使用公开模型
12
  MODELS = {
13
  "Zephyr 7B Beta": {
14
  "model_id": "HuggingFaceH4/zephyr-7b-beta",
15
  "kwargs": {"torch_dtype": torch.float16}
16
  },
 
 
 
 
 
 
 
 
17
  "Falcon 7B Instruct": {
18
  "model_id": "tiiuae/falcon-7b-instruct",
19
+ "kwargs": {"torch_dtype": torch.float16, "trust_remote_code": True}
20
  }
21
  }
22
 
23
+ # 加载模型
24
  def load_model(model_name):
25
  model_config = MODELS[model_name]
26
+ tokenizer = AutoTokenizer.from_pretrained(
27
+ model_config["model_id"],
28
+ use_auth_token=hf_token
29
+ )
 
 
30
  model = AutoModelForCausalLM.from_pretrained(
31
  model_config["model_id"],
32
+ use_auth_token=hf_token,
33
  **model_config["kwargs"]
34
  )
 
 
35
  device = "cuda" if torch.cuda.is_available() else "cpu"
36
+ return model.to(device), tokenizer, device
37
+
38
+ # 其余代码(界面构建和交互逻辑)保持不变...
39
 
40
  # 初始化模型
41
  loaded_models = {}