7.05shixun / app.py
SamZhang1129's picture
Update app.py
003fb42 verified
raw
history blame contribute delete
903 Bytes
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
# 加载模型和分词器
model_name = "Qwen2-0.5B-Instruct"
model = AutoModelForCausalLM.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
def generate_response(input_text):
inputs = tokenizer.encode(input_text, return_tensors="pt")
outputs = model.generate(inputs, max_length=50, num_return_sequences=1)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return response
def chatbot(input_text):
response = generate_response(input_text)
return response
iface = gr.Interface(
fn=chatbot,
inputs=gr.Textbox(lines=2, placeholder="输入你的消息..."),
outputs=gr.Textbox(lines=2),
title="Qwen2-0.5B-Instruct 聊天机器人",
description="基于Qwen2-0.5B-Instruct模型的聊天机器人。"
)
iface.launch()