Spaces:
Runtime error
Runtime error
File size: 2,495 Bytes
eee38ad 8086eb7 eee38ad 08863c9 eee38ad 08863c9 8086eb7 08863c9 6e29a0e 8086eb7 08863c9 8086eb7 6e29a0e 8086eb7 6e29a0e 08863c9 6e29a0e 8086eb7 6e29a0e efc5651 6e29a0e efc5651 6e29a0e efc5651 6e29a0e 8086eb7 |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import gradio as gr
# ๅ ่ฝฝๆไปคๆจกๅ
model = AutoModelForCausalLM.from_pretrained(
"MediaTek-Research/Breeze-7B-Instruct-v1_0",
device_map="auto",
torch_dtype=torch.bfloat16,
)
# ๅ ่ฝฝๅ่ฏๅจ
tokenizer = AutoTokenizer.from_pretrained("MediaTek-Research/Breeze-7B-Instruct-v1_0")
# ๅฎไนSYS_PROMPT
SYS_PROMPT = "You are a helpful AI assistant built by MediaTek Research. The user you are helping speaks Traditional Chinese and comes from Taiwan."
# ๅฎไนๅฏน่ฏๅๅฒ่ฎฐๅฝๅ้
chat_history = []
def generate_response(user_input):
global chat_history
# ๅฐ็จๆท่พๅ
ฅๆทปๅ ๅฐๅฏน่ฏๅๅฒ่ฎฐๅฝ
chat_history.append({"role": "user", "content": user_input})
# ๅบ็จ่ๅคฉๆจกๆฟ
prompt = tokenizer.apply_chat_template(chat_history, tokenize=False)
full_prompt = f"<s>{SYS_PROMPT} [INST] {prompt} [/INST]"
# ็ๆๆๆฌ
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
outputs = model.generate(
inputs["input_ids"],
max_new_tokens=128,
top_p=0.95,
top_k=50,
repetition_penalty=1.1,
temperature=0.7,
do_sample=True, # ๅฏ็จ sample-based ็ๆๆจกๅผ
attention_mask=inputs["attention_mask"], # ่ฎพ็ฝฎ attention mask
)
# ่งฃ็ ่พๅบ
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
# ๅฐ็ๆ็ๆๆฌๆทปๅ ๅฐๅฏน่ฏๅๅฒ่ฎฐๅฝ
chat_history.append({"role": "assistant", "content": generated_text})
# ๅชไฟ็ๆๆฐ็ไบไธช้ฎ็ญๅฏน่ฏ
if len(chat_history) > 10:
chat_history = chat_history[-10:]
return generated_text
# ๅๅปบGradio็้ข
def chat_interface(user_input, history):
response = generate_response(user_input)
history.append({"role": "user", "content": user_input})
history.append({"role": "assistant", "content": response})
return "", history
iface = gr.Blocks()
with iface:
gr.Markdown("# ้ซ็ๅ็ญๅฉๆ\n้ๆฏไธๅๅบๆผ MediaTek-Research/Breeze-7B-Instruct-v1_0 ๆจกๅ็้ซ็ๅ็ญๅฉๆใ")
chatbot = gr.Chatbot(type="messages") # ไฝฟ็จOpenAI้ฃๆ ผ็ๅญๅ
ธๆ ผๅผ
with gr.Row():
txt = gr.Textbox(
show_label=False,
placeholder="่ซ่ผธๅ
ฅไฝ ็ๅ้ก...",
lines=1,
)
txt.submit(chat_interface, [txt, chatbot], [txt, chatbot])
# ๅฏๅจGradio็้ขๅนถๅ
ฑไบซ้พๆฅ
iface.launch(share=True)
|