-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
33 lines (27 loc) · 987 Bytes
/
app.py
File metadata and controls
33 lines (27 loc) · 987 Bytes
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
import torch
import gradio as gr
from model import Model
from train import encoder, decoder
# Device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load model
model = Model().to(device)
model.load_state_dict(torch.load("nanogpt_model.pth", map_location=device))
model.eval()
# Generation function
def generate_text(prompt, max_tokens):
idx = torch.tensor(encoder(prompt), dtype=torch.long, device=device).unsqueeze(0)
generated = model.generate(idx, max_new_tokens=max_tokens)[0].tolist()
return decoder(generated)
# Gradio interface
iface = gr.Interface(
fn=generate_text,
inputs=[
gr.Textbox(lines=2, placeholder="Enter a prompt...", label="Prompt"),
gr.Slider(10, 500, value=200, step=10, label="Max Tokens")
],
outputs=gr.Textbox(label="Generated Output"),
title="🧠 NanoGPT from Scratch",
description="A tiny GPT model trained on Shakespeare. Try your luck by giving it a prompt!"
)
iface.launch()