Spaces:
Running
Running
Create hf_model.py
Browse files- hf_model.py +58 -0
hf_model.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
+
import torch
|
4 |
+
from datetime import datetime
|
5 |
+
import os
|
6 |
+
|
7 |
+
class HFModel:
|
8 |
+
def __init__(self, model_name):
|
9 |
+
parts = model_name.split("/")
|
10 |
+
self.friendly_name = parts[1]
|
11 |
+
self.model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True, torch_dtype=torch.bfloat16).cuda()
|
12 |
+
self.tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
13 |
+
self.chat_history = []
|
14 |
+
self.log_file = f"chat_log_{datetime.now().strftime('%Y%m%d_%H%M%S')}.md"
|
15 |
+
|
16 |
+
def generate_response(self, input_text, max_length=100, skip_special_tokens=True):
|
17 |
+
inputs = self.tokenizer(input_text, return_tensors="pt").to(self.model.device)
|
18 |
+
outputs = self.model.generate(**inputs, max_length=max_length)
|
19 |
+
response = self.tokenizer.decode(outputs[0], skip_special_tokens=skip_special_tokens).strip()
|
20 |
+
return response
|
21 |
+
|
22 |
+
def stream_response(self, input_text, max_length=100, skip_special_tokens=True):
|
23 |
+
inputs = self.tokenizer(input_text, return_tensors="pt").to(self.model.device)
|
24 |
+
for output in self.model.generate(**inputs, max_length=max_length, do_stream=True):
|
25 |
+
response = self.tokenizer.decode(output, skip_special_tokens=skip_special_tokens).strip()
|
26 |
+
yield response
|
27 |
+
|
28 |
+
def chat(self, user_input, max_length=100, skip_special_tokens=True):
|
29 |
+
# Add user input to chat history
|
30 |
+
self.chat_history.append({"role": "user", "content": user_input})
|
31 |
+
|
32 |
+
# Generate model response
|
33 |
+
model_response = self.generate_response(user_input, max_length=max_length, skip_special_tokens=skip_special_tokens)
|
34 |
+
|
35 |
+
# Add model response to chat history
|
36 |
+
self.chat_history.append({"role": "assistant", "content": model_response})
|
37 |
+
|
38 |
+
# Save chat log
|
39 |
+
self.save_chat_log()
|
40 |
+
|
41 |
+
return model_response
|
42 |
+
|
43 |
+
def save_chat_log(self):
|
44 |
+
with open(self.log_file, "a", encoding="utf-8") as f:
|
45 |
+
for entry in self.chat_history[-2:]: # Save only the latest interaction
|
46 |
+
role = entry["role"]
|
47 |
+
content = entry["content"]
|
48 |
+
f.write(f"**{role.capitalize()}:**\n\n{content}\n\n---\n\n")
|
49 |
+
|
50 |
+
def clear_chat_history(self):
|
51 |
+
self.chat_history = []
|
52 |
+
print("Chat history cleared.")
|
53 |
+
|
54 |
+
def print_chat_history(self):
|
55 |
+
for entry in self.chat_history:
|
56 |
+
role = entry["role"]
|
57 |
+
content = entry["content"]
|
58 |
+
print(f"{role.capitalize()}: {content}\n")
|