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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
OpenLLM Demo App - Works without external model dependencies
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import torch
|
| 8 |
+
import torch.nn as nn
|
| 9 |
+
import torch.nn.functional as F
|
| 10 |
+
import json
|
| 11 |
+
import random
|
| 12 |
+
import logging
|
| 13 |
+
|
| 14 |
+
# Set up logging
|
| 15 |
+
logging.basicConfig(level=logging.INFO)
|
| 16 |
+
logger = logging.getLogger(__name__)
|
| 17 |
+
|
| 18 |
+
class DemoGPT(nn.Module):
|
| 19 |
+
"""Demo GPT model for testing"""
|
| 20 |
+
|
| 21 |
+
def __init__(self, vocab_size=1000, n_layer=2, n_head=4, n_embd=128):
|
| 22 |
+
super().__init__()
|
| 23 |
+
self.config = type('Config', (), {
|
| 24 |
+
'vocab_size': vocab_size,
|
| 25 |
+
'n_layer': n_layer,
|
| 26 |
+
'n_head': n_head,
|
| 27 |
+
'n_embd': n_embd,
|
| 28 |
+
'block_size': 256
|
| 29 |
+
})()
|
| 30 |
+
|
| 31 |
+
self.transformer = nn.ModuleDict(dict(
|
| 32 |
+
wte = nn.Embedding(vocab_size, n_embd),
|
| 33 |
+
wpe = nn.Embedding(256, n_embd),
|
| 34 |
+
drop = nn.Dropout(0.1),
|
| 35 |
+
h = nn.ModuleList([nn.TransformerEncoderLayer(
|
| 36 |
+
d_model=n_embd,
|
| 37 |
+
nhead=n_head,
|
| 38 |
+
dim_feedforward=4 * n_embd,
|
| 39 |
+
dropout=0.1,
|
| 40 |
+
batch_first=True
|
| 41 |
+
) for _ in range(n_layer)]),
|
| 42 |
+
ln_f = nn.LayerNorm(n_embd),
|
| 43 |
+
))
|
| 44 |
+
self.lm_head = nn.Linear(n_embd, vocab_size)
|
| 45 |
+
|
| 46 |
+
# Initialize with random weights
|
| 47 |
+
self.apply(self._init_weights)
|
| 48 |
+
|
| 49 |
+
def _init_weights(self, module):
|
| 50 |
+
if isinstance(module, nn.Linear):
|
| 51 |
+
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 52 |
+
if module.bias is not None:
|
| 53 |
+
torch.nn.init.zeros_(module.bias)
|
| 54 |
+
elif isinstance(module, nn.Embedding):
|
| 55 |
+
torch.nn.init.normal_(module.weight, mean=0.0, std=0.02)
|
| 56 |
+
|
| 57 |
+
def forward(self, idx, targets=None):
|
| 58 |
+
b, t = idx.size()
|
| 59 |
+
pos = torch.arange(0, t, dtype=torch.long, device=idx.device).unsqueeze(0)
|
| 60 |
+
|
| 61 |
+
tok_emb = self.transformer.wte(idx)
|
| 62 |
+
pos_emb = self.transformer.wpe(pos)
|
| 63 |
+
x = self.transformer.drop(tok_emb + pos_emb)
|
| 64 |
+
|
| 65 |
+
for block in self.transformer.h:
|
| 66 |
+
x = block(x)
|
| 67 |
+
x = self.transformer.ln_f(x)
|
| 68 |
+
|
| 69 |
+
if targets is not None:
|
| 70 |
+
logits = self.lm_head(x)
|
| 71 |
+
loss = F.cross_entropy(logits.view(-1, logits.size(-1)), targets.view(-1), ignore_index=-1)
|
| 72 |
+
else:
|
| 73 |
+
logits = self.lm_head(x[:, [-1], :])
|
| 74 |
+
loss = None
|
| 75 |
+
|
| 76 |
+
return logits, loss
|
| 77 |
+
|
| 78 |
+
class DemoInferenceEngine:
|
| 79 |
+
"""Demo inference engine that works without external models"""
|
| 80 |
+
|
| 81 |
+
def __init__(self):
|
| 82 |
+
self.models = {}
|
| 83 |
+
self.current_model = None
|
| 84 |
+
|
| 85 |
+
# Demo model configurations
|
| 86 |
+
self.model_configs = {
|
| 87 |
+
"demo-4k": {
|
| 88 |
+
"name": "Demo Model (4k steps)",
|
| 89 |
+
"description": "Demo model simulating 4,000 training steps",
|
| 90 |
+
"steps": 4000
|
| 91 |
+
},
|
| 92 |
+
"demo-6k": {
|
| 93 |
+
"name": "Demo Model (6k steps)",
|
| 94 |
+
"description": "Demo model simulating 6,000 training steps",
|
| 95 |
+
"steps": 6000
|
| 96 |
+
},
|
| 97 |
+
"demo-7k": {
|
| 98 |
+
"name": "Demo Model (7k steps)",
|
| 99 |
+
"description": "Demo model simulating 7,000 training steps",
|
| 100 |
+
"steps": 7000
|
| 101 |
+
},
|
| 102 |
+
"demo-8k": {
|
| 103 |
+
"name": "Demo Model (8k steps)",
|
| 104 |
+
"description": "Demo model simulating 8,000 training steps",
|
| 105 |
+
"steps": 8000
|
| 106 |
+
},
|
| 107 |
+
"demo-9k": {
|
| 108 |
+
"name": "Demo Model (9k steps)",
|
| 109 |
+
"description": "Demo model simulating 9,000 training steps",
|
| 110 |
+
"steps": 9000
|
| 111 |
+
}
|
| 112 |
+
}
|
| 113 |
+
|
| 114 |
+
logger.info("π Demo OpenLLM Inference Engine initialized")
|
| 115 |
+
|
| 116 |
+
def load_model(self, model_id: str) -> bool:
|
| 117 |
+
"""Load a demo model"""
|
| 118 |
+
try:
|
| 119 |
+
config = self.model_configs.get(model_id)
|
| 120 |
+
if not config:
|
| 121 |
+
logger.error(f"β Unknown model ID: {model_id}")
|
| 122 |
+
return False
|
| 123 |
+
|
| 124 |
+
logger.info(f"π₯ Loading demo model: {model_id}")
|
| 125 |
+
|
| 126 |
+
# Create a demo model
|
| 127 |
+
model = DemoGPT()
|
| 128 |
+
model.eval()
|
| 129 |
+
self.models[model_id] = model
|
| 130 |
+
self.current_model = model_id
|
| 131 |
+
|
| 132 |
+
logger.info(f"β
Successfully loaded demo model: {model_id}")
|
| 133 |
+
return True
|
| 134 |
+
|
| 135 |
+
except Exception as e:
|
| 136 |
+
logger.error(f"β Failed to load demo model {model_id}: {e}")
|
| 137 |
+
return False
|
| 138 |
+
|
| 139 |
+
def generate_text(self, prompt: str, max_length: int = 100,
|
| 140 |
+
temperature: float = 0.7, top_k: int = 50,
|
| 141 |
+
top_p: float = 0.9) -> str:
|
| 142 |
+
"""Generate demo text"""
|
| 143 |
+
if not self.current_model or self.current_model not in self.models:
|
| 144 |
+
return "β No model loaded. Please select a model first."
|
| 145 |
+
|
| 146 |
+
try:
|
| 147 |
+
model = self.models[self.current_model]
|
| 148 |
+
config = self.model_configs[self.current_model]
|
| 149 |
+
|
| 150 |
+
# Create demo response based on prompt and parameters
|
| 151 |
+
demo_responses = [
|
| 152 |
+
f"Based on your prompt '{prompt[:50]}...', here's a demo response from the {config['name']} model. This is a simulated output that demonstrates how the interface would work with real models.",
|
| 153 |
+
f"The {config['name']} model (trained for {config['steps']} steps) would generate: '{prompt}' followed by additional context and continuation text.",
|
| 154 |
+
f"Demo generation with temperature={temperature}, top_k={top_k}, top_p={top_p}: The model processes your input and produces coherent text based on the training patterns it has learned.",
|
| 155 |
+
f"Simulated response from {config['name']}: Your prompt '{prompt}' is interesting. Let me provide a thoughtful continuation that builds upon your input while maintaining context and relevance."
|
| 156 |
+
]
|
| 157 |
+
|
| 158 |
+
# Select response based on parameters
|
| 159 |
+
response = random.choice(demo_responses)
|
| 160 |
+
|
| 161 |
+
# Add some variation based on parameters
|
| 162 |
+
if temperature > 1.0:
|
| 163 |
+
response += " (Higher temperature makes responses more creative and varied)"
|
| 164 |
+
elif temperature < 0.5:
|
| 165 |
+
response += " (Lower temperature produces more focused and deterministic output)"
|
| 166 |
+
|
| 167 |
+
if max_length > 200:
|
| 168 |
+
response += " With a longer generation length, the model would continue with more detailed elaboration and context."
|
| 169 |
+
|
| 170 |
+
return response
|
| 171 |
+
|
| 172 |
+
except Exception as e:
|
| 173 |
+
error_msg = f"β Demo generation failed: {str(e)}"
|
| 174 |
+
logger.error(error_msg)
|
| 175 |
+
return error_msg
|
| 176 |
+
|
| 177 |
+
# Initialize the demo inference engine
|
| 178 |
+
inference_engine = DemoInferenceEngine()
|
| 179 |
+
|
| 180 |
+
def load_model_info(model_id: str) -> str:
|
| 181 |
+
"""Get information about a specific model"""
|
| 182 |
+
config = inference_engine.model_configs.get(model_id)
|
| 183 |
+
if config:
|
| 184 |
+
return f"**{config['name']}**\n\n{config['description']}"
|
| 185 |
+
return "β Model not found"
|
| 186 |
+
|
| 187 |
+
def generate_text_interface(model_id: str, prompt: str, max_length: int,
|
| 188 |
+
temperature: float, top_k: int, top_p: float) -> str:
|
| 189 |
+
"""Gradio interface function for text generation"""
|
| 190 |
+
try:
|
| 191 |
+
# Load model if not already loaded
|
| 192 |
+
if model_id not in inference_engine.models:
|
| 193 |
+
logger.info(f"π Loading model: {model_id}")
|
| 194 |
+
success = inference_engine.load_model(model_id)
|
| 195 |
+
if not success:
|
| 196 |
+
return f"β Failed to load model: {model_id}"
|
| 197 |
+
|
| 198 |
+
# Generate text
|
| 199 |
+
result = inference_engine.generate_text(
|
| 200 |
+
prompt=prompt,
|
| 201 |
+
max_length=max_length,
|
| 202 |
+
temperature=temperature,
|
| 203 |
+
top_k=top_k,
|
| 204 |
+
top_p=top_p
|
| 205 |
+
)
|
| 206 |
+
|
| 207 |
+
return result
|
| 208 |
+
|
| 209 |
+
except Exception as e:
|
| 210 |
+
error_msg = f"β Error in generation interface: {str(e)}"
|
| 211 |
+
logger.error(error_msg)
|
| 212 |
+
return error_msg
|
| 213 |
+
|
| 214 |
+
# Create Gradio interface
|
| 215 |
+
def create_interface():
|
| 216 |
+
"""Create the Gradio interface"""
|
| 217 |
+
|
| 218 |
+
with gr.Blocks(
|
| 219 |
+
title="π OpenLLM Demo Space",
|
| 220 |
+
theme=gr.themes.Soft()
|
| 221 |
+
) as interface:
|
| 222 |
+
|
| 223 |
+
# Header
|
| 224 |
+
gr.Markdown("""
|
| 225 |
+
# π OpenLLM Demo Space
|
| 226 |
+
|
| 227 |
+
Welcome to the OpenLLM Demo Space! This is a demonstration interface showing how the OpenLLM inference would work.
|
| 228 |
+
|
| 229 |
+
## π― Demo Models
|
| 230 |
+
|
| 231 |
+
We provide **5 different demo models** simulating varying training steps:
|
| 232 |
+
|
| 233 |
+
| Model | Training Steps | Description |
|
| 234 |
+
|-------|---------------|-------------|
|
| 235 |
+
| **Demo 4k** | 4,000 | Early training stage simulation |
|
| 236 |
+
| **Demo 6k** | 6,000 | Improved coherence simulation |
|
| 237 |
+
| **Demo 7k** | 7,000 | Enhanced quality simulation |
|
| 238 |
+
| **Demo 8k** | 8,000 | Sophisticated understanding simulation |
|
| 239 |
+
| **Demo 9k** | 9,000 | Best performing model simulation |
|
| 240 |
+
|
| 241 |
+
---
|
| 242 |
+
""")
|
| 243 |
+
|
| 244 |
+
with gr.Row():
|
| 245 |
+
with gr.Column(scale=1):
|
| 246 |
+
# Model selection
|
| 247 |
+
model_dropdown = gr.Dropdown(
|
| 248 |
+
choices=list(inference_engine.model_configs.keys()),
|
| 249 |
+
value="demo-9k",
|
| 250 |
+
label="π― Select Model",
|
| 251 |
+
info="Choose the demo model to use"
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
# Model information display
|
| 255 |
+
model_info = gr.Markdown(
|
| 256 |
+
value=load_model_info("demo-9k"),
|
| 257 |
+
label="π Model Information"
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
# Update model info when selection changes
|
| 261 |
+
model_dropdown.change(
|
| 262 |
+
fn=load_model_info,
|
| 263 |
+
inputs=[model_dropdown],
|
| 264 |
+
outputs=[model_info]
|
| 265 |
+
)
|
| 266 |
+
|
| 267 |
+
with gr.Column(scale=2):
|
| 268 |
+
# Input prompt
|
| 269 |
+
prompt_input = gr.Textbox(
|
| 270 |
+
lines=5,
|
| 271 |
+
label="π Input Prompt",
|
| 272 |
+
placeholder="Enter your text prompt here...",
|
| 273 |
+
info="The text that will be used as input for generation"
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
# Generation parameters
|
| 277 |
+
with gr.Row():
|
| 278 |
+
max_length = gr.Slider(
|
| 279 |
+
minimum=10,
|
| 280 |
+
maximum=500,
|
| 281 |
+
value=100,
|
| 282 |
+
step=10,
|
| 283 |
+
label="π Max Length",
|
| 284 |
+
info="Maximum number of tokens to generate"
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
temperature = gr.Slider(
|
| 288 |
+
minimum=0.1,
|
| 289 |
+
maximum=2.0,
|
| 290 |
+
value=0.7,
|
| 291 |
+
step=0.1,
|
| 292 |
+
label="π‘οΈ Temperature",
|
| 293 |
+
info="Controls randomness (higher = more random)"
|
| 294 |
+
)
|
| 295 |
+
|
| 296 |
+
with gr.Row():
|
| 297 |
+
top_k = gr.Slider(
|
| 298 |
+
minimum=1,
|
| 299 |
+
maximum=100,
|
| 300 |
+
value=50,
|
| 301 |
+
step=1,
|
| 302 |
+
label="π Top-K",
|
| 303 |
+
info="Number of highest probability tokens to consider"
|
| 304 |
+
)
|
| 305 |
+
|
| 306 |
+
top_p = gr.Slider(
|
| 307 |
+
minimum=0.1,
|
| 308 |
+
maximum=1.0,
|
| 309 |
+
value=0.9,
|
| 310 |
+
step=0.1,
|
| 311 |
+
label="π Top-P",
|
| 312 |
+
info="Nucleus sampling parameter"
|
| 313 |
+
)
|
| 314 |
+
|
| 315 |
+
# Generate button
|
| 316 |
+
generate_btn = gr.Button(
|
| 317 |
+
"π Generate Text",
|
| 318 |
+
variant="primary",
|
| 319 |
+
size="lg"
|
| 320 |
+
)
|
| 321 |
+
|
| 322 |
+
# Output
|
| 323 |
+
output_text = gr.Textbox(
|
| 324 |
+
lines=10,
|
| 325 |
+
label="π― Generated Text",
|
| 326 |
+
info="The generated text will appear here"
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
+
# Connect the generate button
|
| 330 |
+
generate_btn.click(
|
| 331 |
+
fn=generate_text_interface,
|
| 332 |
+
inputs=[model_dropdown, prompt_input, max_length, temperature, top_k, top_p],
|
| 333 |
+
outputs=[output_text]
|
| 334 |
+
)
|
| 335 |
+
|
| 336 |
+
# Footer
|
| 337 |
+
gr.Markdown("""
|
| 338 |
+
---
|
| 339 |
+
|
| 340 |
+
## π§ Technical Details
|
| 341 |
+
|
| 342 |
+
- **Architecture**: GPT-style transformer decoder (demo)
|
| 343 |
+
- **Model Size**: Small demo models for testing
|
| 344 |
+
- **Framework**: PyTorch with embedded demo code
|
| 345 |
+
- **Status**: Demo mode - shows interface functionality
|
| 346 |
+
|
| 347 |
+
**This is a demo version showing the interface. Real models would be loaded from Hugging Face repositories.**
|
| 348 |
+
""")
|
| 349 |
+
|
| 350 |
+
return interface
|
| 351 |
+
|
| 352 |
+
# Create and launch the interface
|
| 353 |
+
if __name__ == "__main__":
|
| 354 |
+
interface = create_interface()
|
| 355 |
+
interface.launch(
|
| 356 |
+
server_name="0.0.0.0",
|
| 357 |
+
server_port=7860,
|
| 358 |
+
share=False,
|
| 359 |
+
debug=True
|
| 360 |
+
)
|