File size: 700 Bytes
7d45100
0bdce7a
7d45100
0bdce7a
8ee4ade
0bdce7a
 
 
 
 
 
 
 
1a56177
0bdce7a
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer

# 模型和分词器的名称
model_name = "Qwen/Qwen2.5-0.5B-Instruct"

# 加载模型和分词器
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

# 定义生成文本的函数
def generate_text(input_text):
    inputs = tokenizer(input_text, return_tensors="pt")
    outputs = model.generate(**inputs, max_length=200)
    generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return generated_text

# 创建 Gradio 接口
iface = gr.Interface(fn=generate_text, inputs="text", outputs="text")

# 启动 Gradio 接口
iface.launch()