-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathai_code.py
More file actions
63 lines (40 loc) · 1.11 KB
/
Copy pathai_code.py
File metadata and controls
63 lines (40 loc) · 1.11 KB
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
import os
import warnings
import torch
from transformers import pipeline, GenerationConfig
from transformers.utils import logging
warnings.filterwarnings("ignore")
logging.set_verbosity_error()
pipe = pipeline(
"text-generation",
model="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
device_map="auto",
torch_dtype=torch.float16
)
gen_config = GenerationConfig(
max_new_tokens=40,
max_length=None,
do_sample=False
)
print("Chatbot ready! Type quit() to exit.\n")
conversation = ""
while True:
user_input = input("You: ")
if user_input.lower() == "quit()":
break
conversation += f"<|user|>\n{user_input}\n<|assistant|>\n"
prompt = f"""<|system|>
You are a helpful, friendly chatbot. Keep responses short and relevant.
{conversation}
"""
print("Generating...")
output = pipe(
prompt,
generation_config=gen_config,
return_full_text=False
)
reply = output[0]["generated_text"].strip()
print("Bot:", reply)
conversation += reply + "\n"
if len(conversation) > 2000:
conversation = conversation[-2000:]