Views
No views yet
1import gradio as gr
2import torch
3
4# Assuming 'BigramLanguageModel' and 'decode' are defined as in your model code
5
6class GradioInterface:
7 def __init__(self, model_path="lafontaine_gpt_v1.pth"):
8 self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
9 self.model = self.load_model(model_path)
10 self.model.eval()
11
12 def load_model(self, model_path):
13 model = BigramLanguageModel().to(self.device)
14 model.load_state_dict(torch.load(model_path, map_location=self.device))
15 return model
16
17 def generate_text(self, input_text, max_tokens=100):
18 context = torch.tensor([encode(input_text)], dtype=torch.long, device=self.device)
19 output = self.model.generate(context, max_new_tokens=max_tokens)
20 return decode(output[0].tolist())
21
22# Load the model
23model_interface = GradioInterface()
24
25# Define Gradio interface
26gr_interface = gr.Interface(
27 fn=model_interface.generate_text,
28 inputs=["text", gr.Slider(50, 500)],
29 outputs="text",
30 description="Bigram Language Model text generation. Enter some text, and the model will continue it.",
31 examples=[["Once upon a time"]]
32)
33
34# Launch the interface
35gr_interface.launch()