File size: 1,344 Bytes
cf1158a
 
03a4ce1
3940861
cf1158a
03a4ce1
 
3940861
 
 
 
 
 
 
 
cf1158a
03a4ce1
 
3940861
 
 
03a4ce1
69b3e47
3940861
cf1158a
1119952
cf1158a
1119952
 
cf1158a
 
1119952
 
cf1158a
3940861
 
 
 
 
 
 
 
 
 
cf1158a
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import gradio as gr
import torch
import os
from transformers import AutoTokenizer, AutoModelForCausalLM

# 本地模型路径(绝对路径)
LOCAL_MODEL_PATH = os.path.join(os.getcwd(), "model")
print("Loading model from:", LOCAL_MODEL_PATH)

# 加载 tokenizer 和模型
tokenizer = AutoTokenizer.from_pretrained(
    LOCAL_MODEL_PATH,
    local_files_only=True,
    trust_remote_code=False
)

model = AutoModelForCausalLM.from_pretrained(
    LOCAL_MODEL_PATH,
    local_files_only=True,
    trust_remote_code=False,
    torch_dtype=torch.float16
).to("cuda" if torch.cuda.is_available() else "cpu")

# 聊天函数
def chat(user_input):
    inputs = tokenizer(user_input, return_tensors="pt").to(model.device)
    outputs = model.generate(
        input_ids=inputs["input_ids"],
        attention_mask=inputs.get("attention_mask"),
        max_new_tokens=200,
        temperature=0.7,
        top_p=0.9,
        do_sample=True
    )
    return tokenizer.decode(outputs[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)

# Gradio 网页界面
interface = gr.Interface(
    fn=chat,
    inputs=gr.Textbox(lines=3, label="输入你的问题"),
    outputs=gr.Textbox(lines=10, label="模型回答"),
    title="我的AI模型",
    description="在 HuggingFace Space 上运行的 LLaMA/GPT 模型网页"
)

interface.launch()