Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,18 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
LOCAL_MODEL_PATH = "./model" # 上传到 Space 的模型文件夹
|
| 9 |
-
# 方案 B:使用公开 HuggingFace Hub 模型
|
| 10 |
HUB_MODEL_NAME = "TheBloke/llama-7b-hf"
|
| 11 |
|
| 12 |
-
# 是否使用本地模型
|
| 13 |
-
USE_LOCAL_MODEL = True # True 使用本地模型,False 使用 Hub 模型
|
| 14 |
-
|
| 15 |
if USE_LOCAL_MODEL:
|
| 16 |
model_name = LOCAL_MODEL_PATH
|
| 17 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
model_name,
|
| 20 |
-
torch_dtype=torch.float16
|
|
|
|
| 21 |
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 22 |
else:
|
| 23 |
model_name = HUB_MODEL_NAME
|
|
@@ -27,7 +22,6 @@ else:
|
|
| 27 |
torch_dtype=torch.float16
|
| 28 |
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 29 |
|
| 30 |
-
# ======= 聊天函数 =======
|
| 31 |
def chat(user_input):
|
| 32 |
inputs = tokenizer(user_input, return_tensors="pt").to(model.device)
|
| 33 |
outputs = model.generate(
|
|
@@ -40,13 +34,11 @@ def chat(user_input):
|
|
| 40 |
)
|
| 41 |
return tokenizer.decode(outputs[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
| 42 |
|
| 43 |
-
# ======= Gradio 网页界面 =======
|
| 44 |
interface = gr.Interface(
|
| 45 |
fn=chat,
|
| 46 |
inputs=gr.Textbox(lines=3, label="输入你的问题"),
|
| 47 |
-
outputs=gr.Textbox(lines=10, label="模型回答")
|
| 48 |
-
title="我的AI模型",
|
| 49 |
-
description="在 HuggingFace Space 上运行的 LLaMA/GPT 模型网页"
|
| 50 |
)
|
| 51 |
|
| 52 |
interface.launch()
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
+
USE_LOCAL_MODEL = True
|
| 6 |
+
LOCAL_MODEL_PATH = "./model"
|
|
|
|
|
|
|
| 7 |
HUB_MODEL_NAME = "TheBloke/llama-7b-hf"
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
if USE_LOCAL_MODEL:
|
| 10 |
model_name = LOCAL_MODEL_PATH
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, local_files_only=True)
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
model_name,
|
| 14 |
+
torch_dtype=torch.float16,
|
| 15 |
+
local_files_only=True
|
| 16 |
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 17 |
else:
|
| 18 |
model_name = HUB_MODEL_NAME
|
|
|
|
| 22 |
torch_dtype=torch.float16
|
| 23 |
).to("cuda" if torch.cuda.is_available() else "cpu")
|
| 24 |
|
|
|
|
| 25 |
def chat(user_input):
|
| 26 |
inputs = tokenizer(user_input, return_tensors="pt").to(model.device)
|
| 27 |
outputs = model.generate(
|
|
|
|
| 34 |
)
|
| 35 |
return tokenizer.decode(outputs[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
| 36 |
|
|
|
|
| 37 |
interface = gr.Interface(
|
| 38 |
fn=chat,
|
| 39 |
inputs=gr.Textbox(lines=3, label="输入你的问题"),
|
| 40 |
+
outputs=gr.Textbox(lines=10, label="模型回答")
|
|
|
|
|
|
|
| 41 |
)
|
| 42 |
|
| 43 |
interface.launch()
|
| 44 |
+
|