Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,15 @@ from transformers import AutoTokenizer, AutoModelForCausalLM
|
|
| 4 |
|
| 5 |
model_name = "ICEPVP8977/Uncensored_Qwen2.5_Coder_3B"
|
| 6 |
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
model_name,
|
|
|
|
| 10 |
torch_dtype=torch.float32,
|
| 11 |
low_cpu_mem_usage=True
|
| 12 |
)
|
|
@@ -14,36 +20,33 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 14 |
model.eval()
|
| 15 |
|
| 16 |
def generate(prompt):
|
| 17 |
-
messages = [
|
| 18 |
-
{"role": "user", "content": prompt}
|
| 19 |
-
]
|
| 20 |
-
|
| 21 |
text = tokenizer.apply_chat_template(
|
| 22 |
messages,
|
| 23 |
tokenize=False,
|
| 24 |
add_generation_prompt=True
|
| 25 |
)
|
| 26 |
|
| 27 |
-
inputs = tokenizer(text, return_tensors="pt")
|
| 28 |
|
| 29 |
with torch.no_grad():
|
| 30 |
outputs = model.generate(
|
| 31 |
**inputs,
|
| 32 |
max_new_tokens=256,
|
|
|
|
| 33 |
temperature=0.7,
|
| 34 |
-
|
| 35 |
)
|
| 36 |
|
| 37 |
-
|
| 38 |
outputs[0][inputs["input_ids"].shape[-1]:],
|
| 39 |
skip_special_tokens=True
|
| 40 |
)
|
| 41 |
-
|
| 42 |
-
return response
|
| 43 |
|
| 44 |
gr.Interface(
|
| 45 |
fn=generate,
|
| 46 |
-
inputs=gr.Textbox(lines=4),
|
| 47 |
-
outputs="
|
| 48 |
-
title="
|
| 49 |
).launch()
|
|
|
|
| 4 |
|
| 5 |
model_name = "ICEPVP8977/Uncensored_Qwen2.5_Coder_3B"
|
| 6 |
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 8 |
+
model_name,
|
| 9 |
+
trust_remote_code=True,
|
| 10 |
+
use_fast=False
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
model_name,
|
| 15 |
+
trust_remote_code=True,
|
| 16 |
torch_dtype=torch.float32,
|
| 17 |
low_cpu_mem_usage=True
|
| 18 |
)
|
|
|
|
| 20 |
model.eval()
|
| 21 |
|
| 22 |
def generate(prompt):
|
| 23 |
+
messages = [{"role": "user", "content": prompt}]
|
|
|
|
|
|
|
|
|
|
| 24 |
text = tokenizer.apply_chat_template(
|
| 25 |
messages,
|
| 26 |
tokenize=False,
|
| 27 |
add_generation_prompt=True
|
| 28 |
)
|
| 29 |
|
| 30 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
|
| 31 |
|
| 32 |
with torch.no_grad():
|
| 33 |
outputs = model.generate(
|
| 34 |
**inputs,
|
| 35 |
max_new_tokens=256,
|
| 36 |
+
do_sample=True,
|
| 37 |
temperature=0.7,
|
| 38 |
+
pad_token_id=tokenizer.eos_token_id
|
| 39 |
)
|
| 40 |
|
| 41 |
+
result = tokenizer.decode(
|
| 42 |
outputs[0][inputs["input_ids"].shape[-1]:],
|
| 43 |
skip_special_tokens=True
|
| 44 |
)
|
| 45 |
+
return result.strip()
|
|
|
|
| 46 |
|
| 47 |
gr.Interface(
|
| 48 |
fn=generate,
|
| 49 |
+
inputs=gr.Textbox(lines=4, label="Tanya apa saja"),
|
| 50 |
+
outputs=gr.Textbox(label="Jawaban"),
|
| 51 |
+
title="AI Assistant",
|
| 52 |
).launch()
|