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:
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
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
- Save the code as gpt_gui.py.
- Install the dependencies: pip install transformers torch.
- Run the script: python gpt_gui.py.
- Wait for the model to load (a status bar shows progress).
- Enter a prompt, adjust parameters if needed, and click Generate.
- 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!
- Downloads last month
- 100