Upload use.py
Browse files
use.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 3 |
+
from threading import Thread
|
| 4 |
+
|
| 5 |
+
MODEL_PATH = "VDrontGPT50m-Base"
|
| 6 |
+
TEMPERATURE = 0.5
|
| 7 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def load_model_and_tokenizer(model_path):
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=False)
|
| 12 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
+
model_path,
|
| 14 |
+
torch_dtype=torch.float16 if DEVICE == "cuda" else torch.float32,
|
| 15 |
+
device_map="auto",
|
| 16 |
+
trust_remote_code=False
|
| 17 |
+
)
|
| 18 |
+
if tokenizer.pad_token is None:
|
| 19 |
+
tokenizer.pad_token = tokenizer.eos_token
|
| 20 |
+
return model, tokenizer
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def generate_stream(model, tokenizer, prompt, temperature=0.4, max_new_tokens=128):
|
| 24 |
+
# Text continuation generator without chat history
|
| 25 |
+
inputs = tokenizer(prompt, return_tensors="pt", truncation=True, max_length=2048)
|
| 26 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
| 27 |
+
|
| 28 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 29 |
+
generation_kwargs = dict(
|
| 30 |
+
**inputs,
|
| 31 |
+
max_new_tokens=max_new_tokens,
|
| 32 |
+
temperature=temperature,
|
| 33 |
+
do_sample=True,
|
| 34 |
+
top_p=0.96,
|
| 35 |
+
repetition_penalty=1.1,
|
| 36 |
+
pad_token_id=tokenizer.pad_token_id,
|
| 37 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 38 |
+
streamer=streamer,
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 42 |
+
thread.start()
|
| 43 |
+
|
| 44 |
+
for new_text in streamer:
|
| 45 |
+
yield new_text
|
| 46 |
+
|
| 47 |
+
thread.join()
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def interactive_chat(model, tokenizer, temperature):
|
| 51 |
+
print(f"Simple text continuation (temp={temperature})")
|
| 52 |
+
print("Enter the beginning of the text, and the model will continue it.")
|
| 53 |
+
print("Commands: 'exit' or 'quit' — exit.")
|
| 54 |
+
|
| 55 |
+
while True:
|
| 56 |
+
try:
|
| 57 |
+
user_input = input("\nYou: ").strip()
|
| 58 |
+
except (KeyboardInterrupt, EOFError):
|
| 59 |
+
print("\nGoodbye!")
|
| 60 |
+
break
|
| 61 |
+
|
| 62 |
+
if user_input.lower() in ["exit", "quit"]:
|
| 63 |
+
print("Goodbye!")
|
| 64 |
+
break
|
| 65 |
+
|
| 66 |
+
if not user_input:
|
| 67 |
+
continue
|
| 68 |
+
|
| 69 |
+
print() # empty line before continuation
|
| 70 |
+
for token in generate_stream(model, tokenizer, user_input, temperature=temperature):
|
| 71 |
+
print(token, end="", flush=True)
|
| 72 |
+
print() # finish the line
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
model, tokenizer = load_model_and_tokenizer(MODEL_PATH)
|
| 77 |
+
interactive_chat(model, tokenizer, temperature=TEMPERATURE)
|