Update launch.py
Browse files
launch.py
CHANGED
|
@@ -1,106 +1,64 @@
|
|
| 1 |
import os
|
| 2 |
-
import sys
|
| 3 |
import torch
|
| 4 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
-
import glob
|
| 6 |
-
from prompt_toolkit import PromptSession
|
| 7 |
-
from prompt_toolkit.keys import Keys
|
| 8 |
-
from prompt_toolkit.application import Application
|
| 9 |
-
from prompt_toolkit.layout import Layout, HSplit, Window
|
| 10 |
-
from prompt_toolkit.widgets import Frame, Label
|
| 11 |
-
from prompt_toolkit.key_binding import KeyBindings
|
| 12 |
-
from prompt_toolkit.styles import Style
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
if len(files) == 1:
|
| 27 |
-
return files[0]
|
| 28 |
-
selected = 0
|
| 29 |
-
style = Style.from_dict({"selected": "fg:ansiblack bg:ansigreen"})
|
| 30 |
-
kb = KeyBindings()
|
| 31 |
-
|
| 32 |
-
@kb.add("up")
|
| 33 |
-
def up(event):
|
| 34 |
-
nonlocal selected
|
| 35 |
-
selected = (selected - 1) % len(files)
|
| 36 |
-
|
| 37 |
-
@kb.add("down")
|
| 38 |
-
def down(event):
|
| 39 |
-
nonlocal selected
|
| 40 |
-
selected = (selected + 1) % len(files)
|
| 41 |
-
|
| 42 |
-
@kb.add("enter")
|
| 43 |
-
def enter(event):
|
| 44 |
-
event.app.exit(result=files[selected])
|
| 45 |
-
|
| 46 |
-
@kb.add("c-c")
|
| 47 |
-
def cancel(event):
|
| 48 |
-
event.app.exit(result=None)
|
| 49 |
-
|
| 50 |
-
body = HSplit([])
|
| 51 |
-
for idx, file in enumerate(files):
|
| 52 |
-
if idx == selected:
|
| 53 |
-
body.children.append(Window(Label(text=f"[x] {file}"), style="selected"))
|
| 54 |
-
else:
|
| 55 |
-
body.children.append(Window(Label(text=f"[ ] {file}")))
|
| 56 |
-
|
| 57 |
-
container = Frame(body=body, title="Select model (up/down, enter)")
|
| 58 |
-
layout = Layout(container)
|
| 59 |
-
app = Application(layout=layout, key_bindings=kb, style=style, full_screen=False)
|
| 60 |
-
return app.run()
|
| 61 |
-
|
| 62 |
-
class ChatClient:
|
| 63 |
-
def __init__(self, model_path):
|
| 64 |
-
self.tokenizer = AutoTokenizer.from_pretrained(".", local_files_only=True)
|
| 65 |
-
self.model = AutoModelForCausalLM.from_pretrained(".", local_files_only=True)
|
| 66 |
-
self.history = []
|
| 67 |
-
|
| 68 |
-
def generate_response(self, prompt):
|
| 69 |
-
inputs = self.tokenizer(prompt, return_tensors="pt")
|
| 70 |
-
with torch.no_grad():
|
| 71 |
-
outputs = self.model.generate(**inputs, max_new_tokens=256, do_sample=True, temperature=0.7)
|
| 72 |
-
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 73 |
-
response = response[len(prompt):].strip()
|
| 74 |
-
return response
|
| 75 |
-
|
| 76 |
-
def chat(self):
|
| 77 |
-
session = PromptSession()
|
| 78 |
-
print("Chat with model. Type 'exit' to quit.")
|
| 79 |
-
print("Type 'clear' to clear history.")
|
| 80 |
-
while True:
|
| 81 |
-
try:
|
| 82 |
-
user_input = session.prompt("You: ")
|
| 83 |
-
if user_input.lower() in ["exit", "quit"]:
|
| 84 |
-
break
|
| 85 |
-
if user_input.lower() == "clear":
|
| 86 |
-
self.history = []
|
| 87 |
-
print("History cleared.")
|
| 88 |
-
continue
|
| 89 |
-
self.history.append("User: " + user_input)
|
| 90 |
-
context = "\n".join(self.history[-10:])
|
| 91 |
-
response = self.generate_response(context + "\nAssistant:")
|
| 92 |
-
print("Assistant:", response)
|
| 93 |
-
self.history.append("Assistant: " + response)
|
| 94 |
-
except KeyboardInterrupt:
|
| 95 |
-
print("\nExiting...")
|
| 96 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
if __name__ == "__main__":
|
| 99 |
-
|
| 100 |
-
selected_file = select_file_with_prompt(files)
|
| 101 |
-
if selected_file:
|
| 102 |
-
print(f"Loading {selected_file}...")
|
| 103 |
-
client = ChatClient(selected_file)
|
| 104 |
-
client.chat()
|
| 105 |
-
else:
|
| 106 |
-
print("No model selected.")
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
def main():
|
| 6 |
+
# Use the current directory where the model files are located
|
| 7 |
+
model_path = os.path.dirname(os.path.abspath(__file__))
|
| 8 |
+
|
| 9 |
+
print("Loading model and tokenizer...")
|
| 10 |
+
try:
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 12 |
+
|
| 13 |
+
# Load the model. trust_remote_code=True is included in case of custom architectures
|
| 14 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
+
model_path,
|
| 16 |
+
trust_remote_code=True,
|
| 17 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32
|
| 18 |
+
)
|
| 19 |
+
|
| 20 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 21 |
+
model.to(device)
|
| 22 |
+
print(f"Model loaded successfully on {device.upper()}!\n")
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"Error loading model: {e}")
|
| 25 |
+
return
|
| 26 |
|
| 27 |
+
print("--- Chat Session Started (Type 'exit' or 'quit' to end) ---")
|
| 28 |
+
|
| 29 |
+
while True:
|
| 30 |
+
try:
|
| 31 |
+
user_input = input("You: ").strip()
|
| 32 |
+
if not user_input:
|
| 33 |
+
continue
|
| 34 |
+
if user_input.lower() in ["exit", "quit"]:
|
| 35 |
+
print("Ending session. Goodbye!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
break
|
| 37 |
+
|
| 38 |
+
# Tokenize input
|
| 39 |
+
inputs = tokenizer(user_input, return_tensors="pt").to(device)
|
| 40 |
+
|
| 41 |
+
# Generate response
|
| 42 |
+
with torch.no_grad():
|
| 43 |
+
outputs = model.generate(
|
| 44 |
+
**inputs,
|
| 45 |
+
max_new_tokens=128,
|
| 46 |
+
do_sample=True,
|
| 47 |
+
temperature=0.6,
|
| 48 |
+
top_k=40,
|
| 49 |
+
top_p=0.9,
|
| 50 |
+
pad_token_id=tokenizer.eos_token_id if tokenizer.eos_token_id is not None else 0
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# Decode only the newly generated tokens
|
| 54 |
+
input_length = inputs.input_ids.shape[1]
|
| 55 |
+
response = tokenizer.decode(outputs[0][input_length:], skip_special_tokens=True)
|
| 56 |
+
|
| 57 |
+
print(f"Model: {response.strip()}\n")
|
| 58 |
+
|
| 59 |
+
except KeyboardInterrupt:
|
| 60 |
+
print("\nSession interrupted. Goodbye!")
|
| 61 |
+
break
|
| 62 |
|
| 63 |
if __name__ == "__main__":
|
| 64 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|