Spaces:
Runtime error
Runtime error
File size: 1,490 Bytes
c83b1e4 |
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 48 |
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
# 加载模型和分词器
model_path = "meta-llama/Llama-3.2-1B-Instruct-QLORA_INT4_EO8"
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.float16,
device_map="auto",
)
def generate_text(prompt, max_length=512, temperature=0.7, top_p=0.9):
# 准备输入
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
# 生成回答
with torch.no_grad():
outputs = model.generate(
**inputs,
max_length=max_length,
temperature=temperature,
top_p=top_p,
pad_token_id=tokenizer.pad_token_id,
eos_token_id=tokenizer.eos_token_id,
)
# 解码输出
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return response
# 创建Gradio界面
iface = gr.Interface(
fn=generate_text,
inputs=[
gr.Textbox(lines=5, label="输入提示"),
gr.Slider(minimum=64, maximum=1024, value=512, label="最大长度"),
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, label="温度"),
gr.Slider(minimum=0.1, maximum=1.0, value=0.9, label="Top-p"),
],
outputs=gr.Textbox(lines=5, label="生成的文本"),
title="Llama-3.2-1B-Instruct 演示",
description="输入提示,获取AI生成的回答",
)
# 启动应用
iface.launch() |