File size: 1,251 Bytes
a785e67
de6e209
2972c89
de6e209
a785e67
 
 
2972c89
 
 
de6e209
 
2972c89
 
 
 
 
 
 
 
 
de6e209
 
 
 
 
 
 
 
 
 
 
500b722
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
import os
import gradio as gr
from ctransformers import LlamaForCausalLM, LlamaTokenizer

# Lấy secret key từ biến môi trường
secret_key = os.getenv("HUGGING_FACE_SECRET_KEY")

# Khởi tạo model và tokenizer
model = LlamaForCausalLM.from_pretrained("Arrcttacsrks/Llama-3.2-3B-Instruct_gguf", revision="Llama-3.2-3B-Instruct-Q8_0.gguf")
tokenizer = LlamaTokenizer.from_pretrained("Arrcttacsrks/Llama-3.2-3B-Instruct_gguf", revision="Llama-3.2-3B-Instruct-Q8_0.gguf")

def chat_with_model(user_input):
    # Mã hóa đầu vào
    input_ids = tokenizer.encode(user_input, return_tensors="pt")
    
    # Tạo phản hồi
    output = model.generate(input_ids, max_length=200, num_return_sequences=1, do_sample=True, top_k=50, top_p=0.95, num_beams=1)
    
    # Giải mã phản hồi
    response = tokenizer.decode(output[0], skip_special_tokens=True)
    return response

# Tạo giao diện Gradio
interface = gr.Interface(
    fn=chat_with_model,
    inputs=gr.Textbox(placeholder="Nhập câu hỏi của bạn..."),
    outputs=gr.Textbox(),
    title="Chat với LLaMA 3.2",
    description="Nhập câu hỏi để nhận phản hồi từ mô hình LLaMA 3.2 3B Instruct."
)

# Khởi động ứng dụng
interface.launch()