mountainrock commited on
Commit
3bcfe8f
·
verified ·
1 Parent(s): 4d1bf24

Upload ./gradio_app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. gradio_app.py +141 -0
gradio_app.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Gradio Web UI for Vadakayil LLM
3
+ Upload this to Hugging Face Spaces to run interactively
4
+ """
5
+
6
+ import gradio as gr
7
+ import torch
8
+ import json
9
+ import os
10
+ from pathlib import Path
11
+
12
+ # Try to import local modules
13
+ try:
14
+ from model import TinyLLM
15
+ from tokenizer import Tokenizer
16
+ LOCAL_MODE = True
17
+ except ImportError:
18
+ LOCAL_MODE = False
19
+
20
+
21
+ def load_model():
22
+ """Load the trained model."""
23
+ # Check if running on Spaces
24
+ if os.path.exists("model.pt"):
25
+ model_path = "model.pt"
26
+ tokenizer_path = "tokenizer.json"
27
+ else:
28
+ # Local path
29
+ model_path = "./output/vadakayil_model/model.pt"
30
+ tokenizer_path = "./output/vadakayil_model/tokenizer.json"
31
+
32
+ # Load tokenizer
33
+ tokenizer = Tokenizer.load(tokenizer_path)
34
+
35
+ # Load model config
36
+ with open("config.json" if os.path.exists("config.json") else "./output/vadakayil_model/config.json") as f:
37
+ config = json.load(f)
38
+
39
+ # Create model
40
+ model = TinyLLM(
41
+ vocab_size=config.get("vocab_size", 74),
42
+ d_model=config.get("d_model", 128),
43
+ num_heads=config.get("num_heads", 2),
44
+ num_layers=config.get("num_layers", 2),
45
+ d_ff=config.get("d_ff", 256),
46
+ max_seq_len=config.get("max_seq_len", 512),
47
+ dropout=0.1,
48
+ pad_token_id=0
49
+ )
50
+
51
+ # Load weights
52
+ checkpoint = torch.load(model_path, map_location="cpu", weights_only=False)
53
+ model.load_state_dict(checkpoint["model_state_dict"])
54
+ model.eval()
55
+
56
+ return model, tokenizer
57
+
58
+
59
+ def generate_text(prompt, max_tokens, temperature, top_k):
60
+ """Generate text from prompt."""
61
+ if not hasattr(generate_text, 'model'):
62
+ generate_text.model, generate_text.tokenizer = load_model()
63
+
64
+ model = generate_text.model
65
+ tokenizer = generate_text.tokenizer
66
+
67
+ # Encode prompt
68
+ input_ids = tokenizer.encode(prompt, add_special_tokens=False)
69
+ input_ids = torch.tensor([input_ids], dtype=torch.long)
70
+
71
+ # Get EOS token
72
+ eos_token_id = tokenizer.token_to_id.get(tokenizer.eos_token, None)
73
+
74
+ # Generate
75
+ with torch.no_grad():
76
+ output_ids = model.generate(
77
+ input_ids,
78
+ max_new_tokens=max_tokens,
79
+ temperature=temperature,
80
+ top_k=top_k if top_k > 0 else None,
81
+ eos_token_id=eos_token_id
82
+ )
83
+
84
+ # Decode
85
+ generated_text = tokenizer.decode(output_ids[0].tolist(), skip_special_tokens=True)
86
+ return generated_text
87
+
88
+
89
+ # Create Gradio Interface
90
+ with gr.Blocks(title="Vadakayil LLM", theme=gr.themes.Soft()) as demo:
91
+ gr.Markdown("""
92
+ # 🧘 Vadakayil LLM
93
+
94
+ A tiny character-level LLM trained on Capt Ajit Vadakayil's writings about:
95
+ - Mach 0.3 and fluid dynamics
96
+ - Consciousness and Vedic philosophy
97
+ - Silent Kalki Revolution
98
+ - Evidence and Witness
99
+
100
+ **Model**: [mountainrock/vadakayil-llm-tiny](https://huggingface.co/mountainrock/vadakayil-llm-tiny)
101
+ """)
102
+
103
+ with gr.Row():
104
+ with gr.Column():
105
+ prompt_input = gr.Textbox(
106
+ label="Enter your question",
107
+ placeholder="What is Mach 0.3?",
108
+ lines=3
109
+ )
110
+
111
+ with gr.Accordion("Advanced Settings", open=False):
112
+ max_tokens = gr.Slider(50, 300, value=150, step=10, label="Max Tokens")
113
+ temperature = gr.Slider(0.1, 2.0, value=0.8, step=0.1, label="Temperature")
114
+ top_k = gr.Slider(0, 100, value=50, step=5, label="Top-K (0 = disabled)")
115
+
116
+ generate_btn = gr.Button("Generate", variant="primary")
117
+
118
+ with gr.Column():
119
+ output_text = gr.Textbox(label="Generated Answer", lines=5)
120
+
121
+ # Example prompts
122
+ gr.Examples(
123
+ examples=[
124
+ "What is Mach 0.3 and why is it significant?",
125
+ "Why is Mach 0.3 called the Paradox Rekha?",
126
+ "What is the Silent Kalki Revolution of Consciousness?",
127
+ "What does the movie Thondi Muthalum Driksakshiyum represent?",
128
+ "What is the paradox of holding on versus letting go?",
129
+ ],
130
+ inputs=prompt_input
131
+ )
132
+
133
+ generate_btn.click(
134
+ fn=generate_text,
135
+ inputs=[prompt_input, max_tokens, temperature, top_k],
136
+ outputs=output_text
137
+ )
138
+
139
+
140
+ if __name__ == "__main__":
141
+ demo.launch()