|
import gradio as gr |
|
from transformers import AutoModelForCausalLM, AutoTokenizer |
|
import torch |
|
|
|
MODEL_NAME = "Qwen/Qwen1.5-4B" |
|
|
|
|
|
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) |
|
model = AutoModelForCausalLM.from_pretrained( |
|
MODEL_NAME, |
|
torch_dtype=torch.float32 |
|
) |
|
|
|
|
|
def chat_with_qwen(prompt): |
|
inputs = tokenizer(prompt, return_tensors="pt") |
|
output = model.generate(**inputs, max_new_tokens=100) |
|
response = tokenizer.decode(output[0], skip_special_tokens=True) |
|
return response |
|
|
|
|
|
iface = gr.Interface( |
|
fn=chat_with_qwen, |
|
inputs=gr.Textbox(lines=2, placeholder="سوال خود را اینجا بنویسید..."), |
|
outputs="text", |
|
title="Qwen 1.5 4B Chatbot", |
|
description="چتبات با مدل سبکتر برای منابع رایگان", |
|
) |
|
|
|
iface.launch() |
|
|