File size: 1,551 Bytes
0000172
15ac307
7354c9e
 
15ac307
 
 
 
 
 
 
 
7354c9e
28fd5b9
7354c9e
 
 
15ac307
7354c9e
 
 
0000172
7354c9e
 
 
 
 
0000172
 
7354c9e
 
 
 
 
 
 
 
 
 
 
0000172
7354c9e
28fd5b9
 
0000172
 
 
7354c9e
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
import torch

# Настройка 4-bit квантизации
quant_config = BitsAndBytesConfig(
    load_in_4bit=True,
    bnb_4bit_compute_dtype=torch.float16,
    bnb_4bit_quant_type="nf4",
    bnb_4bit_use_double_quant=True
)

# Загружаем модель и токенизатор
model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    quantization_config=quant_config,
    device_map="auto",
    low_cpu_mem_usage=True
)

def chat(message, history):
    messages = [{"role": "system", "content": "You are a friendly Chatbot."}]
    for user_msg, assistant_msg in history:
        messages.append({"role": "user", "content": user_msg})
        messages.append({"role": "assistant", "content": assistant_msg})
    messages.append({"role": "user", "content": message})

    inputs = tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=True, return_tensors="pt")
    outputs = model.generate(
        inputs,
        max_new_tokens=512,
        do_sample=True,
        temperature=0.7,
        top_p=0.95
    )
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return response

chatbot = gr.ChatInterface(
    fn=chat,
    title="TinyLlama 1.1B Chatbot",
    description="Chat with TinyLlama-1.1B-Chat-v1.0 (4-bit quantized)"
)

if __name__ == "__main__":
    chatbot.launch()