Instructions to use eunyuOffice/simcse_chat with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- PEFT
How to use eunyuOffice/simcse_chat with PEFT:
from peft import PeftModel from transformers import AutoModelForCausalLM base_model = AutoModelForCausalLM.from_pretrained("TinyLlama/TinyLlama-1.1B-Chat-v1.0") model = PeftModel.from_pretrained(base_model, "eunyuOffice/simcse_chat") - Notebooks
- Google Colab
- Kaggle
Upload handler.py with huggingface_hub
Browse files- handler.py +85 -79
handler.py
CHANGED
|
@@ -2,93 +2,99 @@ import torch
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
from peft import PeftModel
|
| 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 |
-
if "<|assistant|>" in decoded:
|
| 71 |
-
response = decoded.split("<|assistant|>")[-1].strip()
|
| 72 |
-
else:
|
| 73 |
-
response = decoded.split(prompt)[-1].strip()
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
return {"response": response}
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
from peft import PeftModel
|
| 4 |
|
| 5 |
+
def EndpointHandler(path=""):
|
| 6 |
+
"""
|
| 7 |
+
Inference Endpointsμμ μ¬μ©ν νΈλ€λ¬ ν¨μ
|
| 8 |
+
"""
|
| 9 |
+
class Handler:
|
| 10 |
+
def __init__(self, path=""):
|
| 11 |
+
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 12 |
+
self.base_model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
|
| 13 |
+
self.adapter_path = path or "./tinyllama-qa-news"
|
| 14 |
+
|
| 15 |
+
# λͺ¨λΈκ³Ό ν ν¬λμ΄μ λ‘λ
|
| 16 |
+
print("πΉ λͺ¨λΈ λ‘λ© μ€...")
|
| 17 |
+
self.base_model = AutoModelForCausalLM.from_pretrained(
|
| 18 |
+
self.base_model_id,
|
| 19 |
+
torch_dtype=torch.float32,
|
| 20 |
+
device_map="auto",
|
| 21 |
+
load_in_8bit=False
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
print("πΉ LoRA μ΄λν° λ‘λ© μ€...")
|
| 25 |
+
self.model = PeftModel.from_pretrained(
|
| 26 |
+
self.base_model,
|
| 27 |
+
self.adapter_path,
|
| 28 |
+
is_trainable=False,
|
| 29 |
+
device_map="auto"
|
| 30 |
+
)
|
| 31 |
+
self.model.eval()
|
| 32 |
|
| 33 |
+
self.tokenizer = AutoTokenizer.from_pretrained(self.base_model_id)
|
| 34 |
+
self.tokenizer.pad_token = self.tokenizer.eos_token
|
| 35 |
+
self.tokenizer.padding_side = "right"
|
| 36 |
|
| 37 |
+
def generate_response(self, user_input: str):
|
| 38 |
+
try:
|
| 39 |
+
messages = [
|
| 40 |
+
{"role": "system", "content": "λΉμ μ 곡κ°μ μ΄κ³ μ΄ν΄μ¬ κΉμ κ°μ μλ΄ μ±λ΄μ
λλ€. μ¬μ©μμ κ°μ κ³Ό μκ°μ μ΄ν΄νκ³ κ³΅κ°νλ©°, μ μ ν μ‘°μΈμ μ 곡ν©λλ€."},
|
| 41 |
+
{"role": "user", "content": user_input}
|
| 42 |
+
]
|
| 43 |
|
| 44 |
+
# ν둬ννΈ μμ±
|
| 45 |
+
prompt = self.tokenizer.apply_chat_template(
|
| 46 |
+
messages,
|
| 47 |
+
tokenize=False,
|
| 48 |
+
add_generation_prompt=True,
|
| 49 |
+
return_tensors=None
|
| 50 |
+
)
|
| 51 |
|
| 52 |
+
# ν ν¬λμ΄μ§
|
| 53 |
+
inputs = self.tokenizer(prompt, return_tensors="pt", padding=True)
|
| 54 |
+
inputs = {k: v.to(self.device) for k, v in inputs.items()}
|
| 55 |
|
| 56 |
+
with torch.no_grad():
|
| 57 |
+
output = self.model.generate(
|
| 58 |
+
**inputs,
|
| 59 |
+
max_new_tokens=256,
|
| 60 |
+
temperature=0.8,
|
| 61 |
+
top_p=0.95,
|
| 62 |
+
do_sample=True,
|
| 63 |
+
pad_token_id=self.tokenizer.eos_token_id,
|
| 64 |
+
repetition_penalty=1.1,
|
| 65 |
+
length_penalty=1.0,
|
| 66 |
+
num_return_sequences=1,
|
| 67 |
+
num_beams=1,
|
| 68 |
+
early_stopping=False
|
| 69 |
+
)
|
| 70 |
|
| 71 |
+
decoded = self.tokenizer.decode(output[0], skip_special_tokens=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
# μλ΅ μΆμΆ
|
| 74 |
+
if "<|assistant|>" in decoded:
|
| 75 |
+
response = decoded.split("<|assistant|>")[-1].strip()
|
| 76 |
+
else:
|
| 77 |
+
response = decoded.split(prompt)[-1].strip()
|
| 78 |
+
|
| 79 |
+
if not response:
|
| 80 |
+
response = "λ€, μ΄ μν©μμ κΈμ μ μΈ κ°μ μ λλΌλ κ²μ΄ μμ°μ€λ½μ΅λλ€. μ’μ μκ°μ μ μ¦κΈ°μκ³ κ³μΈμ."
|
| 81 |
+
|
| 82 |
+
return response
|
| 83 |
|
| 84 |
+
except Exception as e:
|
| 85 |
+
print(f"β οΈ μ€λ₯ λ°μ: {str(e)}")
|
| 86 |
+
return "μ£μ‘ν©λλ€. μλ΅ μμ± μ€ μ€λ₯κ° λ°μνμ΅λλ€."
|
| 87 |
|
| 88 |
+
def __call__(self, data):
|
| 89 |
+
"""
|
| 90 |
+
Inference Endpointsμμ νΈμΆλλ λ©μΈ ν¨μ
|
| 91 |
+
"""
|
| 92 |
+
if not data or "inputs" not in data:
|
| 93 |
+
return {"error": "μ
λ ₯ λ°μ΄ν°κ° μμ΅λλ€."}
|
| 94 |
+
|
| 95 |
+
user_input = data["inputs"]
|
| 96 |
+
response = self.generate_response(user_input)
|
| 97 |
|
| 98 |
+
return {"response": response}
|
| 99 |
+
|
| 100 |
+
return Handler(path)
|
|
|