File size: 1,310 Bytes
4fa4548
d7715fb
bd74359
4fa4548
5bbd84c
10c9c2c
bd74359
d10cab6
10c9c2c
ceccab9
d10cab6
d7715fb
 
d10cab6
bd74359
 
323e777
d7715fb
323e777
b788d04
d10cab6
5bbd84c
 
 
 
d7715fb
d10cab6
d7715fb
5bbd84c
bd74359
d7715fb
d10cab6
d7715fb
d10cab6
68f792f
d10cab6
5bbd84c
 
 
d10cab6
4fa4548
 
b788d04
d10cab6
 
 
4fa4548
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
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

model_name = "ICEPVP8977/Uncensored_Qwen2.5_Coder_3B"
tokenizer_name = "Qwen/Qwen2.5-Coder-3B-Instruct"  # ambil tokenizer dari sini

tokenizer = AutoTokenizer.from_pretrained(
    tokenizer_name,
    trust_remote_code=True
)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    trust_remote_code=True,
    torch_dtype=torch.float32,
    low_cpu_mem_usage=True
)
model.eval()

def generate(prompt):
    messages = [{"role": "user", "content": prompt}]
    text = tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True
    )
    inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
    with torch.no_grad():
        outputs = model.generate(
            **inputs,
            max_new_tokens=256,
            do_sample=True,
            temperature=0.7,
            pad_token_id=tokenizer.eos_token_id
        )
    result = tokenizer.decode(
        outputs[0][inputs["input_ids"].shape[-1]:],
        skip_special_tokens=True
    )
    return result.strip()

gr.Interface(
    fn=generate,
    inputs=gr.Textbox(lines=4, label="Tanya apa saja"),
    outputs=gr.Textbox(label="Jawaban"),
    title="AI Assistant",
).launch()