File size: 7,451 Bytes
51e8dee 2d10538 51e8dee | 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 | import os
import sys
import torch
import gradio as gr
# Add project root to path for imports
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from src.model.config import MiniGPTConfig
from src.model.gpt import MiniGPT
from src.tokenizer.minigpt_tokenizer import MiniGPTTokenizer
# -----------------------------------------------------------------------------
# Global setup
# -----------------------------------------------------------------------------
device = "cuda" if torch.cuda.is_available() else "cpu"
# Paths adjusted for root-level app.py
DEFAULT_CHECKPOINT = os.path.join("checkpoints", "ckpt.pt")
DEFAULT_TOKENIZER = os.path.join("data", "tokenizer", "tokenizer.json")
model = None
tokenizer = None
def load_resources():
global model, tokenizer
if not os.path.exists(DEFAULT_CHECKPOINT):
return f"❌ Checkpoint not found at {DEFAULT_CHECKPOINT}"
try:
# Load checkpoint
checkpoint = torch.load(DEFAULT_CHECKPOINT, map_location=device, weights_only=False)
config = checkpoint['config']
# Initialize model and load weights
model = MiniGPT(config)
model.load_state_dict(checkpoint['model'])
model.to(device)
model.eval()
# Load Tokenizer
if os.path.exists(DEFAULT_TOKENIZER):
tokenizer = MiniGPTTokenizer(DEFAULT_TOKENIZER)
else:
return f"⚠️ Model loaded, but Tokenizer file not found at {DEFAULT_TOKENIZER}"
return f"✅ Successfully loaded model from {DEFAULT_CHECKPOINT} ({sum(p.numel() for p in model.parameters())/1e6:.1f}M parameters)."
except Exception as e:
return f"❌ Error loading model: {str(e)}"
def generate_text(prompt, max_new_tokens, temperature, top_k):
if model is None or tokenizer is None:
return "Model and tokenizer are not loaded yet. Please wait..."
if not prompt.strip():
# Use End of Text token as default prompt
idx = torch.tensor([[tokenizer.eot_id]], dtype=torch.long, device=device)
else:
tokens = tokenizer.encode(prompt, return_tensors="pt")
idx = tokens.unsqueeze(0).to(device) # Add batch dimension -> (1, T)
# Generate
with torch.no_grad():
generated_idx = model.generate(
idx,
max_new_tokens=int(max_new_tokens),
temperature=float(temperature),
top_k=int(top_k) if top_k > 0 else None
)
# Decode
return tokenizer.decode(generated_idx[0].tolist())
# -----------------------------------------------------------------------------
# Gradio UI Design
# -----------------------------------------------------------------------------
custom_css = """
/* Professional Deep Dark Theme */
body {
background-color: #0d1117;
color: #c9d1d9;
}
.gradio-container {
background: radial-gradient(circle at 50% -20%, #1c2128, #0d1117) !important;
border: none !important;
}
#header {
text-align: center;
margin-bottom: 2rem;
padding: 2rem;
background: rgba(255, 255, 255, 0.03);
border-radius: 15px;
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.1);
}
h1 {
font-family: 'Inter', system-ui, -apple-system, sans-serif;
font-weight: 800;
font-size: 2.8rem !important;
background: linear-gradient(90deg, #7928CA, #FF0080);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 0.5rem !important;
letter-spacing: -0.02em;
}
.info-text {
font-size: 1.1rem;
color: #8b949e;
}
/* Glassmorphism for panels */
.panel-style {
background: rgba(22, 27, 34, 0.7) !important;
border-radius: 16px !important;
border: 1px solid rgba(255, 255, 255, 0.08) !important;
padding: 1.5rem !important;
backdrop-filter: blur(12px) saturate(180%);
box-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.3);
}
/* Primary Button Styling */
button.primary-btn {
background: linear-gradient(135deg, #6e40c9 0%, #d83bd2 100%) !important;
border: none !important;
color: white !important;
font-weight: 700 !important;
font-size: 1.1rem !important;
padding: 0.8rem !important;
border-radius: 12px !important;
transition: all 0.3s ease !important;
cursor: pointer;
box-shadow: 0 4px 15px rgba(110, 64, 201, 0.3);
}
button.primary-btn:hover {
transform: translateY(-2px);
box-shadow: 0 6px 20px rgba(110, 64, 201, 0.5);
filter: brightness(1.1);
}
/* Slider and Inputs */
.gr-box {
border-radius: 10px !important;
}
input[type="range"] {
accent-color: #d83bd2;
}
"""
with gr.Blocks(css=custom_css, title="MiniGPT | Nano-Scale Intelligence") as demo:
with gr.Column(elem_id="container"):
with gr.Column(elem_id="header"):
gr.Markdown("# ⚡ MiniGPT: Nano-Scale Intelligence")
gr.Markdown("A custom-trained GPT model optimized for efficiency and performance. | [GitHub Repository](https://github.com/mrshibly/MiniGPT-from-Scratch)", elem_classes="info-text")
with gr.Row():
with gr.Column(scale=3):
with gr.Group(elem_classes="panel-style"):
prompt = gr.Textbox(
label="Input Prompt",
placeholder="What would you like to ask the model?",
lines=5,
info="Type a starting phrase or a question."
)
with gr.Row():
tokens_count = gr.Slider(minimum=1, maximum=512, value=128, step=1, label="Response Length")
temp = gr.Slider(minimum=0.1, maximum=2.0, value=0.8, step=0.1, label="Creativity (Temp)")
top_k = gr.Slider(minimum=0, maximum=200, value=50, step=1, label="Focus (Top-K)")
generate_btn = gr.Button("✨ Generate Response", variant="primary", elem_classes="primary-btn")
gr.Examples(
examples=[
["The future of artificial intelligence is", 128, 0.8, 50],
["Once upon a time in a digital world,", 200, 0.9, 45],
["To be, or not to be, that is the question:", 100, 0.7, 40],
["Python code to sort a list:", 150, 0.1, 10]
],
inputs=[prompt, tokens_count, temp, top_k]
)
with gr.Column(scale=2):
with gr.Group(elem_classes="panel-style"):
output = gr.Textbox(
label="AI Response",
lines=16,
placeholder="Model output will appear here...",
show_copy_button=True,
interactive=False
)
status = gr.Markdown("*Status: Ready*", elem_id="status-display")
# Footer
gr.Markdown("---")
gr.Markdown("Built with PyTorch & Gradio. This Space showcases a custom Transformer architecture trained from scratch.", elem_classes="info-text")
# Wire up events
generate_btn.click(
fn=generate_text,
inputs=[prompt, tokens_count, temp, top_k],
outputs=output,
api_name="generate"
)
if __name__ == "__main__":
load_resources()
demo.launch()
|