mattcracker commited on
Commit
c83b1e4
1 Parent(s): 689f39f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import torch
4
+
5
+ # 加载模型和分词器
6
+ model_path = "meta-llama/Llama-3.2-1B-Instruct-QLORA_INT4_EO8"
7
+ tokenizer = AutoTokenizer.from_pretrained(model_path)
8
+ model = AutoModelForCausalLM.from_pretrained(
9
+ model_path,
10
+ torch_dtype=torch.float16,
11
+ device_map="auto",
12
+ )
13
+
14
+ def generate_text(prompt, max_length=512, temperature=0.7, top_p=0.9):
15
+ # 准备输入
16
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
17
+
18
+ # 生成回答
19
+ with torch.no_grad():
20
+ outputs = model.generate(
21
+ **inputs,
22
+ max_length=max_length,
23
+ temperature=temperature,
24
+ top_p=top_p,
25
+ pad_token_id=tokenizer.pad_token_id,
26
+ eos_token_id=tokenizer.eos_token_id,
27
+ )
28
+
29
+ # 解码输出
30
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
31
+ return response
32
+
33
+ # 创建Gradio界面
34
+ iface = gr.Interface(
35
+ fn=generate_text,
36
+ inputs=[
37
+ gr.Textbox(lines=5, label="输入提示"),
38
+ gr.Slider(minimum=64, maximum=1024, value=512, label="最大长度"),
39
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.7, label="温度"),
40
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.9, label="Top-p"),
41
+ ],
42
+ outputs=gr.Textbox(lines=5, label="生成的文本"),
43
+ title="Llama-3.2-1B-Instruct 演示",
44
+ description="输入提示,获取AI生成的回答",
45
+ )
46
+
47
+ # 启动应用
48
+ iface.launch()