VirtualInsight commited on
Commit
b181815
·
verified ·
1 Parent(s): c43aac0

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ import json
4
+ from tokenizers import Tokenizer
5
+ from huggingface_hub import hf_hub_download
6
+ from ModelArchitecture import Transformer, ModelConfig, generate
7
+ from safetensors.torch import load_file
8
+
9
+ # Load model
10
+ device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
11
+ REPO_ID = "VirtualInsight/Lumen"
12
+
13
+ model_path = hf_hub_download(repo_id=REPO_ID, filename="model.safetensors")
14
+ tokenizer_path = hf_hub_download(repo_id=REPO_ID, filename="tokenizer.json")
15
+ config_path = hf_hub_download(repo_id=REPO_ID, filename="config.json")
16
+
17
+ tokenizer = Tokenizer.from_file(tokenizer_path)
18
+ with open(config_path) as f:
19
+ config = ModelConfig(**json.load(f))
20
+
21
+ model = Transformer(config).to(device)
22
+ model.load_state_dict(load_file(model_path, device=str(device)), strict=False)
23
+ model.eval()
24
+
25
+ @torch.no_grad()
26
+ def generate_text(prompt, max_tokens=100, temperature=0.7, top_p=0.9):
27
+ input_ids = torch.tensor(tokenizer.encode(prompt).ids).unsqueeze(0).to(device)
28
+ output_ids = generate(model, input_ids, max_tokens, temperature, top_p=top_p, device=device)
29
+ return tokenizer.decode(output_ids[0, input_ids.size(1):].cpu().tolist())
30
+
31
+ # Gradio Interface
32
+ demo = gr.Interface(
33
+ fn=generate_text,
34
+ inputs=[
35
+ gr.Textbox(label="Prompt", placeholder="Once upon a time...", lines=3),
36
+ gr.Slider(10, 500, value=100, label="Max Tokens"),
37
+ gr.Slider(0.1, 2.0, value=0.7, label="Temperature"),
38
+ gr.Slider(0.1, 1.0, value=0.9, label="Top-p"),
39
+ ],
40
+ outputs=gr.Textbox(label="Generated Text", lines=10),
41
+ title="🌟 Lumen Language Model",
42
+ description="Generate text using the Lumen language model",
43
+ examples=[
44
+ ["Once upon a time", 100, 0.7, 0.9],
45
+ ["The future of AI is", 150, 0.8, 0.95],
46
+ ]
47
+ )
48
+
49
+ if __name__ == "__main__":
50
+ demo.launch()