File size: 8,138 Bytes
eb6bb5f 07103d9 | 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 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 | ---
datasets:
- TeichAI/gpt-5.1-codex-max-1000x
- TeichAI/gpt-5.1-high-reasoning-1000x
base_model:
- openai-community/gpt2-medium
---
Below is a standalone Python GUI application that loads the gss1147/GPT5.1-high-reasoning-codex-0.4B model from Hugging Face and provides a simple interface for text generation. The GUI is built with tkinter and uses transformers and torch for model inference. Generation runs in a separate thread to keep the interface responsive.
Features
· Load the model once at startup.
· Input text box for user prompts.
· Adjustable generation parameters: max new tokens, temperature, top‑p, and repetition penalty.
· Generate button with a progress indicator (text changes to "Generating..." while busy).
· Output displayed in a scrollable text area.
· Error handling for missing dependencies and CUDA issues.
Requirements
Install the required packages before running the script:
```bash
pip install transformers torch
```
(If you have a GPU, ensure you install the appropriate version of PyTorch with CUDA support. The script will automatically use GPU if available.)
The Code
```python
import tkinter as tk
from tkinter import scrolledtext, messagebox, ttk
import threading
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
# Model name
MODEL_NAME = "gss1147/GPT5.1-high-reasoning-codex-0.4B"
# Global variables for model and tokenizer (loaded once)
model = None
tokenizer = None
def load_model():
"""Load the model and tokenizer from Hugging Face."""
global model, tokenizer
try:
# Show loading message in the GUI (if root already exists)
if 'root' in globals():
status_label.config(text="Loading model... This may take a while.")
root.update()
device = "cuda" if torch.cuda.is_available() else "cpu"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
model = AutoModelForCausalLM.from_pretrained(
MODEL_NAME,
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
device_map="auto" if device == "cuda" else None
)
if device == "cpu":
model.to(device)
model.eval()
if 'root' in globals():
status_label.config(text="Model loaded. Ready.")
generate_button.config(state=tk.NORMAL)
except Exception as e:
messagebox.showerror("Error", f"Failed to load model:\n{e}")
if 'root' in globals():
status_label.config(text="Loading failed.")
raise
def generate_text():
"""Start generation in a separate thread."""
thread = threading.Thread(target=generate_thread)
thread.start()
def generate_thread():
"""Run generation and update the GUI when done."""
# Disable the generate button and show progress
generate_button.config(state=tk.DISABLED, text="Generating...")
status_label.config(text="Generating...")
root.update()
try:
# Get parameters from the GUI
prompt = input_text.get("1.0", tk.END).strip()
if not prompt:
messagebox.showwarning("Warning", "Please enter a prompt.")
return
max_new = int(max_new_tokens_var.get())
temp = float(temperature_var.get())
top_p_val = float(top_p_var.get())
rep_penalty = float(rep_penalty_var.get())
# Tokenize input
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
# Generate
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_new,
temperature=temp,
top_p=top_p_val,
repetition_penalty=rep_penalty,
do_sample=True,
pad_token_id=tokenizer.eos_token_id
)
# Decode and display
generated = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Remove the input prompt from the output if desired
if generated.startswith(prompt):
generated = generated[len(prompt):].lstrip()
# Update output in GUI (thread-safe via after)
root.after(0, lambda: display_output(generated))
except Exception as e:
root.after(0, lambda: messagebox.showerror("Error", f"Generation failed:\n{e}"))
finally:
# Re-enable button and reset status
root.after(0, lambda: generate_button.config(state=tk.NORMAL, text="Generate"))
root.after(0, lambda: status_label.config(text="Ready."))
def display_output(text):
"""Insert generated text into the output box."""
output_text.delete("1.0", tk.END)
output_text.insert(tk.END, text)
# ------------------- GUI Setup -------------------
root = tk.Tk()
root.title("GPT-5.1 0.4B Text Generator")
root.geometry("800x700")
root.resizable(True, True)
# Style
root.option_add("*Font", "Arial 11")
# Status bar at the top
status_label = tk.Label(root, text="Initializing...", bd=1, relief=tk.SUNKEN, anchor=tk.W)
status_label.pack(fill=tk.X, padx=5, pady=2)
# Main frame
main_frame = ttk.Frame(root, padding="10")
main_frame.pack(fill=tk.BOTH, expand=True)
# Input label and text area
ttk.Label(main_frame, text="Input Prompt:").grid(row=0, column=0, sticky=tk.W, pady=5)
input_text = scrolledtext.ScrolledText(main_frame, height=8, wrap=tk.WORD)
input_text.grid(row=1, column=0, columnspan=3, sticky=(tk.W, tk.E, tk.N, tk.S), pady=5)
# Generation parameters
param_frame = ttk.LabelFrame(main_frame, text="Generation Parameters", padding="10")
param_frame.grid(row=2, column=0, columnspan=3, sticky=(tk.W, tk.E), pady=10)
# Max new tokens
ttk.Label(param_frame, text="Max New Tokens:").grid(row=0, column=0, sticky=tk.W, padx=5)
max_new_tokens_var = tk.StringVar(value="100")
ttk.Entry(param_frame, textvariable=max_new_tokens_var, width=8).grid(row=0, column=1, sticky=tk.W)
# Temperature
ttk.Label(param_frame, text="Temperature:").grid(row=0, column=2, sticky=tk.W, padx=5)
temperature_var = tk.StringVar(value="0.7")
ttk.Entry(param_frame, textvariable=temperature_var, width=8).grid(row=0, column=3, sticky=tk.W)
# Top-p
ttk.Label(param_frame, text="Top-p:").grid(row=1, column=0, sticky=tk.W, padx=5)
top_p_var = tk.StringVar(value="0.9")
ttk.Entry(param_frame, textvariable=top_p_var, width=8).grid(row=1, column=1, sticky=tk.W)
# Repetition penalty
ttk.Label(param_frame, text="Rep. Penalty:").grid(row=1, column=2, sticky=tk.W, padx=5)
rep_penalty_var = tk.StringVar(value="1.1")
ttk.Entry(param_frame, textvariable=rep_penalty_var, width=8).grid(row=1, column=3, sticky=tk.W)
# Generate button
generate_button = ttk.Button(main_frame, text="Generate", command=generate_text, state=tk.DISABLED)
generate_button.grid(row=3, column=0, pady=10)
# Output label and text area
ttk.Label(main_frame, text="Generated Output:").grid(row=4, column=0, sticky=tk.W, pady=5)
output_text = scrolledtext.ScrolledText(main_frame, height=15, wrap=tk.WORD)
output_text.grid(row=5, column=0, columnspan=3, sticky=(tk.W, tk.E, tk.N, tk.S), pady=5)
# Configure grid weights so text areas expand
main_frame.columnconfigure(0, weight=1)
main_frame.rowconfigure(1, weight=1)
main_frame.rowconfigure(5, weight=1)
# Start loading the model in the background after GUI is up
root.after(100, lambda: threading.Thread(target=load_model, daemon=True).start())
root.mainloop()
```
How to Use
1. Save the code as gpt_gui.py.
2. Install the dependencies: pip install transformers torch.
3. Run the script: python gpt_gui.py.
4. Wait for the model to load (a status bar shows progress).
5. Enter a prompt, adjust parameters if needed, and click Generate.
6. The generated text appears in the lower box.
Notes
· The model is about 0.4 B parameters, so it requires roughly 1–2 GB of RAM/VRAM.
· If you have a GPU with sufficient memory, the script will automatically use it (via device_map="auto").
· Generation can take a few seconds to tens of seconds depending on your hardware and the number of tokens requested.
· The GUI remains responsive during generation because the work is done in a separate thread.
Enjoy experimenting with the model! |