-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathllama2-adventure-game.py
160 lines (129 loc) · 6.1 KB
/
llama2-adventure-game.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from multiprocessing import Process
import threading
import asyncio
import random
import requests
import time
import base64
from datetime import datetime
import io
import sys
from concurrent.futures import ThreadPoolExecutor
from PIL import Image, ImageTk
import customtkinter
import os
import tkinter as tk
from llama_cpp import Llama
script_dir = os.path.dirname(os.path.realpath(__file__))
model_path = os.path.join(script_dir, "llama-2-7b-chat.ggmlv3.q8_0.bin")
llm = Llama(model_path=model_path, n_ctx=3999)
executor = ThreadPoolExecutor(max_workers=1)
async def llama_generate_async(prompt):
loop = asyncio.get_event_loop()
output = await loop.run_in_executor(executor, lambda: llm(prompt, max_tokens=3999))
return output
def word_by_word_insert(text_box, message):
for word in message.split(' '):
text_box.insert(tk.END, f"{word} ")
text_box.update_idletasks()
text_box.after(100)
text_box.insert(tk.END, '\n')
text_box.see(tk.END)
class App(customtkinter.CTk):
def __init__(self):
super().__init__()
self.setup_gui()
threading.Thread(target=self.delayed_initialize_story).start() # Delay the story initialization
def delayed_initialize_story(self):
time.sleep(120) # Wait for 2 minutes (120 seconds)
self.initialize_story()
def initialize_story(self):
story_prompt = "Once upon a time in a faraway kingdom, a mysterious event has thrown the realm into chaos. You, a brave adventurer, have just arrived at the castle gates. What will you do?"
self.text_box.insert(tk.END, f"Story: {story_prompt}\n")
self.text_box.see(tk.END)
threading.Thread(target=self.generate_response, args=(story_prompt,)).start()
def setup_gui(self):
# Configure window
self.title("OneLoveIPFS AI")
self.geometry(f"{1820}x{880}")
# Configure grid layout (4x4)
self.grid_columnconfigure(1, weight=1)
self.grid_columnconfigure((2, 3), weight=0)
self.grid_rowconfigure((0, 1, 2), weight=1)
# Create a label to display the image with a grey background
self.image_label = tk.Label(self, bg='#202225')
self.image_label.grid(row=4, column=1, columnspan=2, padx=(20, 0), pady=(20, 20), sticky="nsew")
# Create text box
self.text_box = customtkinter.CTkTextbox(self, bg_color="white", text_color="white", border_width=0, height=20, width=50, font=customtkinter.CTkFont(size=13))
self.text_box.grid(row=0, column=1, rowspan=3, columnspan=3, padx=(20, 20), pady=(20, 20), sticky="nsew")
# Create main entry and button
self.entry = customtkinter.CTkEntry(self, placeholder_text="Chat With Llama")
self.entry.grid(row=3, column=1, columnspan=2, padx=(20, 0), pady=(20, 20), sticky="nsew")
self.send_button = customtkinter.CTkButton(self, text="Send", command=self.on_submit)
self.send_button.grid(row=3, column=3, padx=(0, 20), pady=(20, 20), sticky="nsew")
self.entry.bind('<Return>', self.on_submit)
def on_submit(self, event=None):
message = self.entry.get().strip()
if message:
self.entry.delete(0, tk.END)
self.text_box.insert(tk.END, f"You: {message}\n")
self.text_box.see(tk.END)
threading.Thread(target=self.generate_response, args=(message,)).start()
threading.Thread(target=self.generate_images, args=(message,)).start()
def generate_response(self, message):
# Capture the last 18 lines from the text box for context
all_text = self.text_box.get('1.0', tk.END).split('\n')[:-1] # Removing the last empty line
last_18_lines = all_text[-18:]
# Add timestamps and [PASTCONTEXT] label
context_with_timestamps = []
for line in last_18_lines:
timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
context_with_timestamps.append(f"[{timestamp}][PASTCONTEXT] {line}")
# Combine context with new message
full_prompt = '\n'.join(context_with_timestamps) + f'\nYou: {message}'
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
full_response = loop.run_until_complete(llama_generate_async(full_prompt))
# Ensure full_response is a dictionary and has the expected keys
if not isinstance(full_response, dict):
print(f"Unexpected type for full_response: {type(full_response)}. Expected dict.")
return
if 'choices' not in full_response or not full_response['choices']:
print("No 'choices' key or empty 'choices' in full_response")
return
# Extract the text from the first choice
response_text = full_response['choices'][0].get('text', '')
# Chunking the response
max_chunk_size = 2000
response_chunks = [response_text[i:i + max_chunk_size] for i in range(0, len(response_text), max_chunk_size)]
for chunk in response_chunks:
word_by_word_insert(self.text_box, f"AI: {chunk}") # Update word by word
def generate_images(self, message):
url = 'http://127.0.0.1:7860/sdapi/v1/txt2img'
payload = {
"prompt": message,
"steps": 9,
"seed": random.randrange(sys.maxsize),
"enable_hr": "false",
"denoising_strength": "0.7",
"cfg_scale": "7",
"width": 1000,
"height": 427,
}
response = requests.post(url, json=payload)
if response.status_code == 200:
try:
r = response.json()
for i in r['images']:
image = Image.open(io.BytesIO(base64.b64decode(i.split(",", 1)[0])))
img_tk = ImageTk.PhotoImage(image)
self.image_label.config(image=img_tk)
self.image_label.image = img_tk
except ValueError as e:
print("Error processing image data: ", e)
else:
print("Error generating image: ", response.status_code)
if __name__ == "__main__":
# Run your tkinter app
app = App()
app.mainloop()