File size: 933 Bytes
713a291
eeca8d3
 
f2980b3
6224565
713a291
eeca8d3
 
 
 
6224565
eeca8d3
713a291
fa5e4da
eeca8d3
0fca800
6224565
eeca8d3
 
 
6224565
eeca8d3
 
fa5e4da
eeca8d3
6224565
 
eeca8d3
 
fa5e4da
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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  # روی CPU اجرا می‌شه
)

# تابع چت‌بات
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

# رابط Gradio
iface = gr.Interface(
    fn=chat_with_qwen,
    inputs=gr.Textbox(lines=2, placeholder="سوال خود را اینجا بنویسید..."),
    outputs="text",
    title="Qwen 1.5 4B Chatbot",
    description="چت‌بات با مدل سبک‌تر برای منابع رایگان",
)

iface.launch()