Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,239 +1,471 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import tensorflow as tf
|
| 3 |
-
import numpy as np
|
| 4 |
import os
|
| 5 |
import json
|
|
|
|
| 6 |
|
| 7 |
from model import VedaProgrammingLLM
|
| 8 |
from tokenizer import VedaTokenizer
|
|
|
|
|
|
|
|
|
|
| 9 |
from train import VedaTrainer, SAMPLE_CODE
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
tokenizer = None
|
| 14 |
|
| 15 |
-
def
|
| 16 |
-
"""Initialize
|
| 17 |
-
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
)
|
| 40 |
-
|
| 41 |
-
dummy = tf.zeros((1, config['max_length']), dtype=tf.int32)
|
| 42 |
-
model(dummy)
|
| 43 |
-
model.load_weights(os.path.join(model_path, "weights.h5"))
|
| 44 |
-
print("Model loaded!")
|
| 45 |
else:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
max_length=128,
|
| 51 |
-
batch_size=8
|
| 52 |
-
)
|
| 53 |
-
trainer.train(epochs=5, save_path=model_path)
|
| 54 |
-
model = trainer.model
|
| 55 |
-
tokenizer = trainer.tokenizer
|
| 56 |
-
print("Model trained!")
|
| 57 |
-
|
| 58 |
-
except Exception as e:
|
| 59 |
-
print(f"Error: {e}")
|
| 60 |
-
print("Creating fresh model...")
|
| 61 |
-
trainer = VedaTrainer()
|
| 62 |
-
trainer.train(epochs=5)
|
| 63 |
-
model = trainer.model
|
| 64 |
-
tokenizer = trainer.tokenizer
|
| 65 |
|
| 66 |
-
def generate_code(
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
if model is None
|
| 71 |
-
return "Model
|
| 72 |
|
| 73 |
try:
|
| 74 |
if not prompt.strip():
|
| 75 |
-
return "Please enter a prompt."
|
| 76 |
-
|
| 77 |
-
tokens = tokenizer.encode(prompt)
|
| 78 |
-
if len(tokens) == 0:
|
| 79 |
-
tokens = [2] # START token
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
| 84 |
temperature=float(temperature),
|
|
|
|
| 85 |
top_k=int(top_k)
|
| 86 |
)
|
| 87 |
|
| 88 |
-
result =
|
| 89 |
-
return result
|
| 90 |
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
try:
|
| 99 |
-
with open("programming.txt", 'w') as f:
|
| 100 |
-
f.write(training_data)
|
| 101 |
-
|
| 102 |
-
trainer = VedaTrainer(
|
| 103 |
-
data_path="programming.txt",
|
| 104 |
-
vocab_size=3000,
|
| 105 |
-
max_length=128,
|
| 106 |
-
batch_size=8
|
| 107 |
)
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
model = trainer.model
|
| 112 |
-
tokenizer = trainer.tokenizer
|
| 113 |
-
|
| 114 |
-
final_loss = history.history['loss'][-1]
|
| 115 |
-
final_acc = history.history.get('accuracy', [0])[-1]
|
| 116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
return f"""β
Training Complete!
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
|
|
|
| 123 |
"""
|
| 124 |
-
|
| 125 |
-
return f"β Training Error: {
|
| 126 |
|
| 127 |
-
def
|
| 128 |
-
"""
|
| 129 |
-
|
|
|
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
return f"""## ποΈ Veda Programming LLM
|
| 138 |
|
|
|
|
|
|
|
| 139 |
| Property | Value |
|
| 140 |
|----------|-------|
|
| 141 |
-
| Vocabulary Size | {config['vocab_size']} |
|
| 142 |
-
| Max Length | {config['max_length']} |
|
| 143 |
-
| Model Dimension | {config['d_model']} |
|
| 144 |
-
| Attention Heads | {config['num_heads']} |
|
| 145 |
-
| Transformer Layers | {config['num_layers']} |
|
| 146 |
-
| FFN Dimension | {config['ff_dim']} |
|
| 147 |
-
| **Total Parameters** | **{params:,}** |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
"""
|
| 149 |
|
| 150 |
-
#
|
| 151 |
def create_app():
|
| 152 |
-
with gr.Blocks(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
gr.Markdown("""
|
| 155 |
# ποΈ Veda Programming LLM
|
| 156 |
-
###
|
|
|
|
|
|
|
| 157 |
""")
|
| 158 |
|
| 159 |
with gr.Tabs():
|
| 160 |
-
# Generation Tab
|
| 161 |
with gr.TabItem("π» Generate Code"):
|
| 162 |
with gr.Row():
|
| 163 |
-
with gr.Column():
|
| 164 |
prompt = gr.Textbox(
|
| 165 |
-
label="Code Prompt",
|
| 166 |
-
placeholder="
|
| 167 |
-
lines=
|
| 168 |
-
value="def
|
| 169 |
)
|
| 170 |
|
| 171 |
with gr.Row():
|
| 172 |
-
max_tokens = gr.Slider(
|
| 173 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
-
gen_btn = gr.Button("π Generate", variant="primary")
|
| 178 |
|
| 179 |
-
with gr.Column():
|
| 180 |
-
output = gr.Code(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
|
|
|
| 182 |
gen_btn.click(
|
| 183 |
generate_code,
|
| 184 |
-
inputs=[prompt, max_tokens, temperature, top_k],
|
| 185 |
-
outputs=output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
)
|
| 187 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
gr.Examples(
|
| 189 |
examples=[
|
| 190 |
-
["def fibonacci(n):",
|
| 191 |
-
["def bubble_sort(arr):",
|
| 192 |
-
["class Calculator:",
|
| 193 |
-
["def binary_search(",
|
| 194 |
],
|
| 195 |
-
inputs=[prompt, max_tokens, temperature, top_k]
|
| 196 |
-
outputs=output,
|
| 197 |
-
fn=generate_code
|
| 198 |
)
|
| 199 |
|
| 200 |
-
# Training Tab
|
| 201 |
-
with gr.TabItem("π
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
-
|
| 212 |
-
train_output = gr.Textbox(label="Training Results", lines=8)
|
| 213 |
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
-
# Info Tab
|
| 221 |
with gr.TabItem("βΉοΈ Model Info"):
|
| 222 |
info_output = gr.Markdown()
|
| 223 |
-
|
| 224 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 225 |
|
| 226 |
gr.Markdown("""
|
| 227 |
---
|
| 228 |
-
|
|
|
|
| 229 |
""")
|
| 230 |
|
| 231 |
return app
|
| 232 |
|
| 233 |
-
# Main
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
print("π Starting Gradio...")
|
| 238 |
-
app = create_app()
|
| 239 |
-
app.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Gradio interface for Veda Programming LLM with continuous learning"""
|
| 2 |
+
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
import os
|
| 5 |
import json
|
| 6 |
+
from datetime import datetime
|
| 7 |
|
| 8 |
from model import VedaProgrammingLLM
|
| 9 |
from tokenizer import VedaTokenizer
|
| 10 |
+
from data_collector import collector
|
| 11 |
+
from continuous_trainer import trainer
|
| 12 |
+
from database import db
|
| 13 |
from train import VedaTrainer, SAMPLE_CODE
|
| 14 |
+
from config import (
|
| 15 |
+
MODEL_DIR, DEFAULT_TEMPERATURE, DEFAULT_MAX_TOKENS,
|
| 16 |
+
DEFAULT_REPETITION_PENALTY, DEFAULT_TOP_K
|
| 17 |
+
)
|
| 18 |
|
| 19 |
+
# Current interaction tracking
|
| 20 |
+
current_interaction_id = None
|
|
|
|
| 21 |
|
| 22 |
+
def initialize():
|
| 23 |
+
"""Initialize the system"""
|
| 24 |
+
print("ποΈ Initializing Veda Programming LLM...")
|
| 25 |
+
print("=" * 50)
|
| 26 |
|
| 27 |
+
# Try to load existing model
|
| 28 |
+
if trainer.load_model():
|
| 29 |
+
print("β
Existing model loaded")
|
| 30 |
+
else:
|
| 31 |
+
print("π Training initial model...")
|
| 32 |
+
# Initial training
|
| 33 |
+
initial_trainer = VedaTrainer(
|
| 34 |
+
data_path="programming.txt",
|
| 35 |
+
vocab_size=5000,
|
| 36 |
+
max_length=256,
|
| 37 |
+
batch_size=8
|
| 38 |
+
)
|
| 39 |
+
initial_trainer.train(epochs=10, save_path=MODEL_DIR)
|
| 40 |
+
|
| 41 |
+
# Load the trained model into continuous trainer
|
| 42 |
+
trainer.load_model()
|
| 43 |
|
| 44 |
+
# Start auto-training scheduler
|
| 45 |
+
trainer.start_auto_training()
|
| 46 |
+
|
| 47 |
+
print("=" * 50)
|
| 48 |
+
print("β
System ready!")
|
| 49 |
+
|
| 50 |
+
def clean_output(text: str) -> str:
|
| 51 |
+
"""Clean generated output"""
|
| 52 |
+
lines = text.split('\n')
|
| 53 |
+
cleaned = []
|
| 54 |
+
empty_count = 0
|
| 55 |
+
|
| 56 |
+
for line in lines:
|
| 57 |
+
if line.strip() == '':
|
| 58 |
+
empty_count += 1
|
| 59 |
+
if empty_count <= 2:
|
| 60 |
+
cleaned.append(line)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
else:
|
| 62 |
+
empty_count = 0
|
| 63 |
+
cleaned.append(line)
|
| 64 |
+
|
| 65 |
+
return '\n'.join(cleaned)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
def generate_code(
|
| 68 |
+
prompt: str,
|
| 69 |
+
max_tokens: int,
|
| 70 |
+
temperature: float,
|
| 71 |
+
repetition_penalty: float,
|
| 72 |
+
top_k: int
|
| 73 |
+
) -> tuple:
|
| 74 |
+
"""Generate code and track interaction"""
|
| 75 |
+
global current_interaction_id
|
| 76 |
|
| 77 |
+
if trainer.model is None:
|
| 78 |
+
return "β³ Model loading...", -1
|
| 79 |
|
| 80 |
try:
|
| 81 |
if not prompt.strip():
|
| 82 |
+
return "β οΈ Please enter a prompt.", -1
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# Generate
|
| 85 |
+
result = trainer.generate(
|
| 86 |
+
prompt=prompt,
|
| 87 |
+
max_tokens=int(max_tokens),
|
| 88 |
temperature=float(temperature),
|
| 89 |
+
repetition_penalty=float(repetition_penalty),
|
| 90 |
top_k=int(top_k)
|
| 91 |
)
|
| 92 |
|
| 93 |
+
result = clean_output(result)
|
|
|
|
| 94 |
|
| 95 |
+
# Save interaction
|
| 96 |
+
current_interaction_id = collector.collect_interaction(
|
| 97 |
+
prompt=prompt,
|
| 98 |
+
generated_code=result,
|
| 99 |
+
temperature=temperature,
|
| 100 |
+
max_tokens=max_tokens
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
)
|
| 102 |
|
| 103 |
+
return result, current_interaction_id
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
except Exception as e:
|
| 106 |
+
import traceback
|
| 107 |
+
traceback.print_exc()
|
| 108 |
+
return f"β Error: {str(e)}", -1
|
| 109 |
+
|
| 110 |
+
def submit_feedback(interaction_id: int, is_positive: bool, edited_code: str = None):
|
| 111 |
+
"""Submit feedback for generated code"""
|
| 112 |
+
if interaction_id < 0:
|
| 113 |
+
return "β οΈ No interaction to rate"
|
| 114 |
+
|
| 115 |
+
collector.record_feedback(
|
| 116 |
+
interaction_id=interaction_id,
|
| 117 |
+
is_positive=is_positive,
|
| 118 |
+
edited_code=edited_code if edited_code and edited_code.strip() else None
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
emoji = "π" if is_positive else "π"
|
| 122 |
+
pending = collector.get_pending_count()
|
| 123 |
+
|
| 124 |
+
msg = f"{emoji} Feedback recorded! Thank you for helping improve the model.\n"
|
| 125 |
+
msg += f"π Approved samples pending training: {pending}"
|
| 126 |
+
|
| 127 |
+
if trainer.should_retrain():
|
| 128 |
+
msg += "\nπ Enough samples collected - model will be retrained soon!"
|
| 129 |
+
|
| 130 |
+
return msg
|
| 131 |
+
|
| 132 |
+
def positive_feedback(interaction_id, code):
|
| 133 |
+
return submit_feedback(int(interaction_id), True, code)
|
| 134 |
+
|
| 135 |
+
def negative_feedback(interaction_id, code):
|
| 136 |
+
return submit_feedback(int(interaction_id), False, code)
|
| 137 |
+
|
| 138 |
+
def manual_train(epochs: int):
|
| 139 |
+
"""Manually trigger training"""
|
| 140 |
+
if trainer.is_training:
|
| 141 |
+
return "β³ Training already in progress..."
|
| 142 |
+
|
| 143 |
+
result = trainer.train(epochs=int(epochs))
|
| 144 |
+
|
| 145 |
+
if result['status'] == 'success':
|
| 146 |
return f"""β
Training Complete!
|
| 147 |
|
| 148 |
+
π Results:
|
| 149 |
+
- Version: {result['version']}
|
| 150 |
+
- Loss: {result['loss']:.4f}
|
| 151 |
+
- Accuracy: {result['accuracy']:.4f}
|
| 152 |
+
- Samples Used: {result['samples_used']}
|
| 153 |
"""
|
| 154 |
+
else:
|
| 155 |
+
return f"β Training Error: {result['message']}"
|
| 156 |
|
| 157 |
+
def add_training_code(code: str, category: str):
|
| 158 |
+
"""Add code directly to training data"""
|
| 159 |
+
if not code.strip():
|
| 160 |
+
return "β οΈ Please enter some code"
|
| 161 |
|
| 162 |
+
collector.add_training_sample(code, category)
|
| 163 |
+
return f"β
Code added to training data!\nCategory: {category}"
|
| 164 |
+
|
| 165 |
+
def get_statistics():
|
| 166 |
+
"""Get system statistics"""
|
| 167 |
+
stats = collector.get_statistics()
|
| 168 |
+
status = trainer.get_status()
|
| 169 |
|
| 170 |
+
return f"""## π System Statistics
|
| 171 |
+
|
| 172 |
+
### Model Status
|
| 173 |
+
| Property | Value |
|
| 174 |
+
|----------|-------|
|
| 175 |
+
| π€ Model Version | {status['model_version']} |
|
| 176 |
+
| π Currently Training | {'Yes' if status['is_training'] else 'No'} |
|
| 177 |
+
| π Training Progress | {status['training_progress']:.0f}% |
|
| 178 |
+
| β° Last Training | {status['last_training'] or 'Never'} |
|
| 179 |
+
|
| 180 |
+
### Learning Data
|
| 181 |
+
| Metric | Count |
|
| 182 |
+
|--------|-------|
|
| 183 |
+
| π¬ Total Interactions | {stats['total_interactions']} |
|
| 184 |
+
| π Positive Feedback | {stats['positive_feedback']} |
|
| 185 |
+
| π Negative Feedback | {stats['negative_feedback']} |
|
| 186 |
+
| β
Approved Samples | {stats['approved_samples']} |
|
| 187 |
+
| π Pending for Training | {status['pending_samples']} |
|
| 188 |
+
| π― Min Samples to Retrain | {status['min_samples_for_training']} |
|
| 189 |
+
|
| 190 |
+
### Training History
|
| 191 |
+
| Metric | Value |
|
| 192 |
+
|--------|-------|
|
| 193 |
+
| π Total Training Runs | {stats['training_runs']} |
|
| 194 |
+
| π Code Samples | {stats['code_samples']} |
|
| 195 |
+
|
| 196 |
+
### Last 7 Days
|
| 197 |
+
| Metric | Count |
|
| 198 |
+
|--------|-------|
|
| 199 |
+
| π’ Generations | {stats['recent_generations']} |
|
| 200 |
+
| π Positive | {stats['recent_positive']} |
|
| 201 |
+
| π Negative | {stats['recent_negative']} |
|
| 202 |
+
| π Approval Rate | {stats['approval_rate']:.1f}% |
|
| 203 |
+
"""
|
| 204 |
+
|
| 205 |
+
def get_recent_interactions():
|
| 206 |
+
"""Get recent interactions for review"""
|
| 207 |
+
interactions = db.get_recent_interactions(limit=10)
|
| 208 |
+
|
| 209 |
+
if not interactions:
|
| 210 |
+
return "No interactions yet."
|
| 211 |
+
|
| 212 |
+
md = "## Recent Interactions\n\n"
|
| 213 |
+
|
| 214 |
+
for item in interactions:
|
| 215 |
+
feedback = "π" if item['feedback'] > 0 else ("π" if item['feedback'] < 0 else "β³")
|
| 216 |
+
md += f"""### {item['timestamp']}
|
| 217 |
+
**Prompt:** `{item['prompt'][:50]}...`
|
| 218 |
+
**Feedback:** {feedback}
|
| 219 |
+
|
| 220 |
+
---
|
| 221 |
+
"""
|
| 222 |
+
|
| 223 |
+
return md
|
| 224 |
+
|
| 225 |
+
def get_training_history():
|
| 226 |
+
"""Get training history"""
|
| 227 |
+
history = db.get_training_history(limit=10)
|
| 228 |
+
|
| 229 |
+
if not history:
|
| 230 |
+
return "No training history yet."
|
| 231 |
+
|
| 232 |
+
md = "## Training History\n\n"
|
| 233 |
+
md += "| Date | Version | Samples | Loss | Accuracy |\n"
|
| 234 |
+
md += "|------|---------|---------|------|----------|\n"
|
| 235 |
+
|
| 236 |
+
for item in history:
|
| 237 |
+
md += f"| {item['timestamp'][:10]} | {item['model_version']} | "
|
| 238 |
+
md += f"{item['samples_used']} | {item['final_loss']:.4f} | {item['final_accuracy']:.4f} |\n"
|
| 239 |
+
|
| 240 |
+
return md
|
| 241 |
+
|
| 242 |
+
def get_model_info():
|
| 243 |
+
"""Get model architecture info"""
|
| 244 |
+
if trainer.model is None:
|
| 245 |
+
return "β³ Model not loaded"
|
| 246 |
+
|
| 247 |
+
config = trainer.model.get_config()
|
| 248 |
+
params = trainer.model.count_params()
|
| 249 |
|
| 250 |
return f"""## ποΈ Veda Programming LLM
|
| 251 |
|
| 252 |
+
### Architecture
|
| 253 |
+
|
| 254 |
| Property | Value |
|
| 255 |
|----------|-------|
|
| 256 |
+
| π Vocabulary Size | {config['vocab_size']:,} |
|
| 257 |
+
| π Max Sequence Length | {config['max_length']} |
|
| 258 |
+
| π§ Model Dimension | {config['d_model']} |
|
| 259 |
+
| ποΈ Attention Heads | {config['num_heads']} |
|
| 260 |
+
| π¦ Transformer Layers | {config['num_layers']} |
|
| 261 |
+
| π§ FFN Dimension | {config['ff_dim']} |
|
| 262 |
+
| β‘ **Total Parameters** | **{params:,}** |
|
| 263 |
+
|
| 264 |
+
### Features
|
| 265 |
+
- β
Continuous Learning from User Feedback
|
| 266 |
+
- β
Automatic Retraining
|
| 267 |
+
- β
Repetition Penalty
|
| 268 |
+
- β
Top-K & Top-P Sampling
|
| 269 |
+
- β
Temperature Control
|
| 270 |
+
- β
Model Versioning
|
| 271 |
"""
|
| 272 |
|
| 273 |
+
# Create the interface
|
| 274 |
def create_app():
|
| 275 |
+
with gr.Blocks(
|
| 276 |
+
title="Veda Programming LLM",
|
| 277 |
+
theme=gr.themes.Soft(),
|
| 278 |
+
css="""
|
| 279 |
+
.feedback-btn { min-width: 100px; }
|
| 280 |
+
.positive { background-color: #4CAF50 !important; }
|
| 281 |
+
.negative { background-color: #f44336 !important; }
|
| 282 |
+
"""
|
| 283 |
+
) as app:
|
| 284 |
+
|
| 285 |
+
# Hidden state for interaction tracking
|
| 286 |
+
interaction_id = gr.State(value=-1)
|
| 287 |
|
| 288 |
gr.Markdown("""
|
| 289 |
# ποΈ Veda Programming LLM
|
| 290 |
+
### AI Code Generation with Continuous Learning
|
| 291 |
+
|
| 292 |
+
This model learns from your feedback! Rate generated code to help improve it.
|
| 293 |
""")
|
| 294 |
|
| 295 |
with gr.Tabs():
|
| 296 |
+
# ============ Generation Tab ============
|
| 297 |
with gr.TabItem("π» Generate Code"):
|
| 298 |
with gr.Row():
|
| 299 |
+
with gr.Column(scale=1):
|
| 300 |
prompt = gr.Textbox(
|
| 301 |
+
label="π Code Prompt",
|
| 302 |
+
placeholder="Enter your code prompt...",
|
| 303 |
+
lines=4,
|
| 304 |
+
value="def fibonacci(n):"
|
| 305 |
)
|
| 306 |
|
| 307 |
with gr.Row():
|
| 308 |
+
max_tokens = gr.Slider(
|
| 309 |
+
10, 300, value=DEFAULT_MAX_TOKENS,
|
| 310 |
+
step=10, label="π Max Tokens"
|
| 311 |
+
)
|
| 312 |
+
temperature = gr.Slider(
|
| 313 |
+
0.1, 1.5, value=DEFAULT_TEMPERATURE,
|
| 314 |
+
step=0.1, label="π‘οΈ Temperature"
|
| 315 |
+
)
|
| 316 |
|
| 317 |
+
with gr.Row():
|
| 318 |
+
repetition_penalty = gr.Slider(
|
| 319 |
+
1.0, 2.0, value=DEFAULT_REPETITION_PENALTY,
|
| 320 |
+
step=0.1, label="π Repetition Penalty"
|
| 321 |
+
)
|
| 322 |
+
top_k = gr.Slider(
|
| 323 |
+
10, 100, value=DEFAULT_TOP_K,
|
| 324 |
+
step=5, label="π― Top-K"
|
| 325 |
+
)
|
| 326 |
|
| 327 |
+
gen_btn = gr.Button("π Generate Code", variant="primary", size="lg")
|
| 328 |
|
| 329 |
+
with gr.Column(scale=1):
|
| 330 |
+
output = gr.Code(
|
| 331 |
+
label="π Generated Code (Edit if needed before rating)",
|
| 332 |
+
language="python",
|
| 333 |
+
lines=15,
|
| 334 |
+
interactive=True
|
| 335 |
+
)
|
| 336 |
+
|
| 337 |
+
gr.Markdown("### π Rate this output to help improve the model:")
|
| 338 |
+
|
| 339 |
+
with gr.Row():
|
| 340 |
+
good_btn = gr.Button("π Good", variant="primary", elem_classes=["feedback-btn", "positive"])
|
| 341 |
+
bad_btn = gr.Button("π Bad", variant="secondary", elem_classes=["feedback-btn", "negative"])
|
| 342 |
+
|
| 343 |
+
feedback_output = gr.Textbox(label="Feedback Status", lines=2)
|
| 344 |
|
| 345 |
+
# Wire up generation
|
| 346 |
gen_btn.click(
|
| 347 |
generate_code,
|
| 348 |
+
inputs=[prompt, max_tokens, temperature, repetition_penalty, top_k],
|
| 349 |
+
outputs=[output, interaction_id]
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
+
# Wire up feedback
|
| 353 |
+
good_btn.click(
|
| 354 |
+
positive_feedback,
|
| 355 |
+
inputs=[interaction_id, output],
|
| 356 |
+
outputs=feedback_output
|
| 357 |
)
|
| 358 |
|
| 359 |
+
bad_btn.click(
|
| 360 |
+
negative_feedback,
|
| 361 |
+
inputs=[interaction_id, output],
|
| 362 |
+
outputs=feedback_output
|
| 363 |
+
)
|
| 364 |
+
|
| 365 |
+
# Examples
|
| 366 |
+
gr.Markdown("### π‘ Example Prompts")
|
| 367 |
gr.Examples(
|
| 368 |
examples=[
|
| 369 |
+
["def fibonacci(n):", 100, 0.7, 1.2, 50],
|
| 370 |
+
["def bubble_sort(arr):", 120, 0.7, 1.2, 50],
|
| 371 |
+
["class Calculator:", 150, 0.8, 1.3, 40],
|
| 372 |
+
["def binary_search(arr, target):", 100, 0.7, 1.2, 50],
|
| 373 |
],
|
| 374 |
+
inputs=[prompt, max_tokens, temperature, repetition_penalty, top_k]
|
|
|
|
|
|
|
| 375 |
)
|
| 376 |
|
| 377 |
+
# ============ Training Tab ============
|
| 378 |
+
with gr.TabItem("π Training"):
|
| 379 |
+
with gr.Row():
|
| 380 |
+
with gr.Column():
|
| 381 |
+
gr.Markdown("### π Manual Training")
|
| 382 |
+
gr.Markdown("Trigger training on collected approved samples.")
|
| 383 |
+
|
| 384 |
+
train_epochs = gr.Slider(1, 20, value=5, step=1, label="Epochs")
|
| 385 |
+
train_btn = gr.Button("π― Start Training", variant="primary")
|
| 386 |
+
train_output = gr.Textbox(label="Training Output", lines=8)
|
| 387 |
+
|
| 388 |
+
train_btn.click(manual_train, inputs=[train_epochs], outputs=train_output)
|
| 389 |
+
|
| 390 |
+
with gr.Column():
|
| 391 |
+
gr.Markdown("### π Add Training Code")
|
| 392 |
+
gr.Markdown("Contribute code directly to the training dataset.")
|
| 393 |
+
|
| 394 |
+
code_input = gr.Textbox(
|
| 395 |
+
label="Code",
|
| 396 |
+
placeholder="Paste your Python code here...",
|
| 397 |
+
lines=10
|
| 398 |
+
)
|
| 399 |
+
|
| 400 |
+
category = gr.Dropdown(
|
| 401 |
+
choices=["function", "class", "algorithm", "utility", "other"],
|
| 402 |
+
value="function",
|
| 403 |
+
label="Category"
|
| 404 |
+
)
|
| 405 |
+
|
| 406 |
+
add_btn = gr.Button("β Add to Training Data")
|
| 407 |
+
add_output = gr.Textbox(label="Status")
|
| 408 |
+
|
| 409 |
+
add_btn.click(add_training_code, inputs=[code_input, category], outputs=add_output)
|
| 410 |
+
|
| 411 |
+
# ============ Statistics Tab ============
|
| 412 |
+
with gr.TabItem("π Statistics"):
|
| 413 |
+
stats_output = gr.Markdown()
|
| 414 |
+
refresh_stats = gr.Button("π Refresh Statistics")
|
| 415 |
+
refresh_stats.click(get_statistics, outputs=stats_output)
|
| 416 |
|
| 417 |
+
gr.Markdown("---")
|
|
|
|
| 418 |
|
| 419 |
+
with gr.Row():
|
| 420 |
+
with gr.Column():
|
| 421 |
+
interactions_output = gr.Markdown()
|
| 422 |
+
refresh_interactions = gr.Button("π Refresh Interactions")
|
| 423 |
+
refresh_interactions.click(get_recent_interactions, outputs=interactions_output)
|
| 424 |
+
|
| 425 |
+
with gr.Column():
|
| 426 |
+
history_output = gr.Markdown()
|
| 427 |
+
refresh_history = gr.Button("π Refresh History")
|
| 428 |
+
refresh_history.click(get_training_history, outputs=history_output)
|
| 429 |
|
| 430 |
+
# ============ Model Info Tab ============
|
| 431 |
with gr.TabItem("βΉοΈ Model Info"):
|
| 432 |
info_output = gr.Markdown()
|
| 433 |
+
refresh_info = gr.Button("π Refresh Info")
|
| 434 |
+
refresh_info.click(get_model_info, outputs=info_output)
|
| 435 |
+
|
| 436 |
+
gr.Markdown("""
|
| 437 |
+
### π§ How Continuous Learning Works
|
| 438 |
+
|
| 439 |
+
1. **You generate code** using the model
|
| 440 |
+
2. **You rate the output** (π or π)
|
| 441 |
+
3. **Good outputs are saved** for training
|
| 442 |
+
4. **When enough samples collect**, the model retrains
|
| 443 |
+
5. **The model improves** based on your feedback!
|
| 444 |
+
|
| 445 |
+
### π‘ Tips
|
| 446 |
+
|
| 447 |
+
- Rate outputs honestly to help the model learn
|
| 448 |
+
- Edit code before rating if it's close but not perfect
|
| 449 |
+
- The more you use it, the better it gets!
|
| 450 |
+
- Contribute your own code samples for faster learning
|
| 451 |
+
""")
|
| 452 |
|
| 453 |
gr.Markdown("""
|
| 454 |
---
|
| 455 |
+
**ποΈ Veda Programming LLM** | Continuous Learning System |
|
| 456 |
+
Built with TensorFlow & Gradio
|
| 457 |
""")
|
| 458 |
|
| 459 |
return app
|
| 460 |
|
| 461 |
+
# Main execution
|
| 462 |
+
if __name__ == "__main__":
|
| 463 |
+
initialize()
|
| 464 |
+
|
| 465 |
+
print("\nπ Starting Gradio Interface...")
|
| 466 |
+
app = create_app()
|
| 467 |
+
app.launch(
|
| 468 |
+
server_name="0.0.0.0",
|
| 469 |
+
server_port=7860,
|
| 470 |
+
show_error=True
|
| 471 |
+
)
|