Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,114 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
model_path = "yentinglin/Llama-3-Taiwan-8B-Instruct" # Update this to your model path
|
8 |
-
adapter_path = "netmouse/Llama-3-Taiwan-8B-Instruct-finetuning-by-promisedchat" # Assuming adapter is stored in the same path
|
9 |
|
10 |
-
#
|
11 |
-
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
|
17 |
-
# Ensure that bitsandbytes is not used by removing any reference to 4bit or 8bit
|
18 |
-
base_model = AutoModelForCausalLM.from_pretrained(model_path, config=config, ignore_mismatched_sizes=True)
|
19 |
|
20 |
-
# Load the LoRA adapter
|
21 |
-
model = PeftModel.from_pretrained(base_model, adapter_path)
|
22 |
|
23 |
-
def
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
messages,
|
26 |
-
tokenize
|
27 |
-
add_generation_prompt
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from unsloth import FastLanguageModel
|
2 |
+
import torch
|
3 |
+
max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally!
|
4 |
+
dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+
|
5 |
+
load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False.
|
6 |
+
|
7 |
+
# 4bit pre quantized models we support for 4x faster downloading + no OOMs.
|
8 |
+
fourbit_models = [
|
9 |
+
"unsloth/mistral-7b-v0.3-bnb-4bit", # New Mistral v3 2x faster!
|
10 |
+
"unsloth/mistral-7b-instruct-v0.3-bnb-4bit",
|
11 |
+
"unsloth/llama-3-8b-bnb-4bit", # Llama-3 15 trillion tokens model 2x faster!
|
12 |
+
"unsloth/llama-3-8b-Instruct-bnb-4bit",
|
13 |
+
"unsloth/llama-3-70b-bnb-4bit",
|
14 |
+
"unsloth/Phi-3-mini-4k-instruct", # Phi-3 2x faster!
|
15 |
+
"unsloth/Phi-3-medium-4k-instruct",
|
16 |
+
"unsloth/mistral-7b-bnb-4bit",
|
17 |
+
"unsloth/gemma-7b-bnb-4bit", # Gemma 2.2x faster!
|
18 |
+
#"netmouse/Llama-3-Taiwan-8B-Instruct-finetuning-by-promisedchat", #conversational chat model
|
19 |
+
#"netmouse/Llama-3-Taiwan-8B-finetuning-by-promisedchat-Instruction" #instruction model
|
20 |
+
] # More models at https://huggingface.co/unsloth
|
21 |
+
|
22 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
23 |
+
model_name = "netmouse/Llama-3-Taiwan-8B-finetuning-by-promisedchat-Instruction", # YOUR MODEL YOU USED FOR TRAINING
|
24 |
+
max_seq_length = 2048,
|
25 |
+
dtype = None,
|
26 |
+
load_in_4bit = True,
|
27 |
+
)
|
28 |
+
FastLanguageModel.for_inference(model) # Enable native 2x faster inference
|
29 |
+
|
30 |
+
import transformers
|
31 |
+
message = [
|
32 |
+
{"role": "user", "content": "你是一個在臉書社團「應許之地」的社團成員,大家會互相稱為「應友」"},
|
33 |
+
{"role": "user", "content": "應許的精神就是「混沌」"}
|
34 |
+
]
|
35 |
|
36 |
+
prompt = tokenizer.apply_chat_template(message, add_generation_prompt=True, tokenize=False)
|
|
|
|
|
37 |
|
38 |
+
# Create pipeline
|
39 |
+
pipeline = transformers.pipeline(
|
40 |
+
"text-generation",
|
41 |
+
model=model,
|
42 |
+
tokenizer=tokenizer
|
43 |
+
)
|
44 |
|
45 |
+
terminators = [
|
46 |
+
pipeline.tokenizer.eos_token_id,
|
47 |
+
pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
48 |
+
]
|
49 |
+
|
50 |
+
# Generate text
|
51 |
+
sequences = pipeline(
|
52 |
+
prompt,
|
53 |
+
do_sample=True,
|
54 |
+
temperature=0.7,
|
55 |
+
top_p=0.9,
|
56 |
+
eos_token_id=terminators,
|
57 |
+
num_return_sequences=1,
|
58 |
+
max_length=200,
|
59 |
+
)
|
60 |
+
|
61 |
+
print(sequences[0]['generated_text'][len(prompt):])
|
62 |
+
|
63 |
+
import gradio as gr
|
64 |
|
65 |
+
messages = []
|
|
|
|
|
66 |
|
|
|
|
|
67 |
|
68 |
+
def add_text(history, text):
|
69 |
+
global messages #message[list] is defined globally
|
70 |
+
history = history + [(text,'')]
|
71 |
+
messages = messages + [{"role":'user', 'content': text}]
|
72 |
+
return history, ""
|
73 |
+
|
74 |
+
def generate(history):
|
75 |
+
global messages
|
76 |
+
prompt = pipeline.tokenizer.apply_chat_template(
|
77 |
messages,
|
78 |
+
tokenize=False,
|
79 |
+
add_generation_prompt=True
|
80 |
+
)
|
81 |
+
|
82 |
+
terminators = [
|
83 |
+
pipeline.tokenizer.eos_token_id,
|
84 |
+
pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
85 |
+
]
|
86 |
+
|
87 |
+
outputs = pipeline(
|
88 |
+
prompt,
|
89 |
+
max_new_tokens=256,
|
90 |
+
eos_token_id=terminators,
|
91 |
+
do_sample=True,
|
92 |
+
temperature=0.6,
|
93 |
+
top_p=0.9,
|
94 |
+
)
|
95 |
+
response_msg = outputs[0]["generated_text"][len(prompt):]
|
96 |
+
for char in response_msg:
|
97 |
+
history[-1][1] += char
|
98 |
+
yield history
|
99 |
+
pass
|
100 |
+
|
101 |
+
with gr.Blocks() as demo:
|
102 |
+
|
103 |
+
chatbot = gr.Chatbot(value=[], elem_id="chatbot")
|
104 |
+
with gr.Row():
|
105 |
+
txt = gr.Textbox(
|
106 |
+
show_label=False,
|
107 |
+
placeholder="請輸入聊天內容",
|
108 |
+
)
|
109 |
+
|
110 |
+
txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
111 |
+
generate, inputs =[chatbot,],outputs = chatbot,)
|
112 |
+
|
113 |
+
demo.queue()
|
114 |
+
demo.launch(debug=True)
|