Spaces:
Runtime error
Runtime error
File size: 2,060 Bytes
be111c3 c946ef8 47d626a be111c3 c946ef8 be111c3 c946ef8 be111c3 c946ef8 b859f61 c946ef8 47d626a c946ef8 be111c3 c946ef8 be111c3 c946ef8 |
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 50 51 |
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers import AutoTokenizer, pipeline
import gradio as gr
import os
# Initialize message history array
message_history = []
initial_message = "Please write your prompt here and press 'enter'"
device = "cuda" # the device to load the model onto
model = AutoModelForCausalLM.from_pretrained(
"DuongTrongChi/Rikka-1.8B-v2.2",
torch_dtype="auto",
device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained("DuongTrongChi/Rikka-1.8B-v2.2")
eos_token = tokenizer("<|im_end|>",add_special_tokens=False)["input_ids"][0]
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
def chat(prompt):
def chat_template(prompt):
system_prompt = """Bạn là một trợ lý hữu ích, tôn trọng và trung thực. Luôn trả lời một cách hữu ích nhất có thể trong khi vẫn an toàn. Câu trả lời của bạn không được bao gồm bất kỳ nội dung có hại, phi đạo đức, phân biệt chủng tộc, phân biệt giới tính, độc hại, nguy hiểm hoặc bất hợp pháp. """
return [
{"role": "system", "content": system_prompt},
{"role": "question", "content": prompt}
]
prompt = pipe.tokenizer.apply_chat_template(chat_template(prompt), tokenize=False, add_generation_prompt=True)
outputs = pipe(prompt, max_new_tokens=512, do_sample=True, temperature=0.3, top_k=50, top_p=0.95, eos_token_id=eos_token, pad_token_id=eos_token)
return outputs[0]['generated_text'][len(prompt):].strip()
with gr.Blocks(theme='abidlabs/dracula_test') as chatblock:
gr.Markdown("<h1><center>Welcome to my personal AI assistant (powered by OpenAI)</center></h1>")
Chatbot = gr.Chatbot()
with gr.Row():
txt = gr.Textbox(
show_label=False,
placeholder = initial_message).style(container=False)
state = gr.State()
txt.submit(chat, txt, Chatbot)
txt.submit(None, None, txt, _js="() => {''}")
chatblock.launch() |