import gradio as gr from transformers import AutoModelForCausalLM, AutoTokenizer # 模型和分词器的名称 model_name = "Qwen/Qwen2.5-7B-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=100) 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()