"""Training for Veda Programming Assistant""" import tensorflow as tf from tensorflow import keras import numpy as np import os import json from model import VedaProgrammingLLM from tokenizer import VedaTokenizer from config import MODEL_DIR, VOCAB_SIZE, MAX_LENGTH, BATCH_SIZE TRAINING_DATA = ''' Hello! Hello! Welcome to Veda Programming Assistant! I can help you with coding, explain concepts, or answer programming questions. What would you like to learn today? What is Python? Python is a high-level, interpreted programming language created by Guido van Rossum in 1991. It is known for its simple, readable syntax. Key features: - Easy to learn and read - Dynamic typing - Large standard library - Great for beginners and experts Python is used for web development, data science, AI, automation, and more! How do I print hello world? To print Hello World in Python, use the print function: print("Hello, World!") When you run this, it displays: Hello, World! Write a function to add two numbers Here is a simple function to add two numbers: def add(a, b): return a + b result = add(5, 3) print(result) This function takes two parameters and returns their sum. Explain what a function is A function is a reusable block of code that performs a specific task. Functions help organize code and avoid repetition. Here is the basic structure: def function_name(parameters): # Code block return result Parts of a function: - def: keyword to define a function - function_name: what you call your function - parameters: input values - return: sends back a result Example: def greet(name): message = f"Hello, {name}!" return message print(greet("Alice")) What is a list? A list is an ordered, mutable collection in Python that can hold items of any type. Creating lists: my_list = [] numbers = [1, 2, 3, 4, 5] fruits = ["apple", "banana", "cherry"] mixed = [1, "hello", 3.14, True] Common operations: fruits = ["apple", "banana"] fruits.append("cherry") fruits.insert(0, "apricot") print(fruits[0]) print(fruits[-1]) fruits.remove("banana") fruits.pop() print(len(fruits)) Write a fibonacci function Here is a Fibonacci function: def fibonacci(n): if n <= 0: return 0 if n == 1: return 1 return fibonacci(n-1) + fibonacci(n-2) for i in range(10): print(fibonacci(i), end=" ") Iterative version: def fibonacci_iterative(n): if n <= 0: return 0 if n == 1: return 1 a, b = 0, 1 for _ in range(2, n + 1): a, b = b, a + b return b What is recursion? Recursion is when a function calls itself to solve a problem. Key components: 1. Base case - stops the recursion 2. Recursive case - function calls itself Example: def countdown(n): if n <= 0: print("Done!") return print(n) countdown(n - 1) countdown(5) Factorial example: def factorial(n): if n <= 1: return 1 return n * factorial(n - 1) print(factorial(5)) Write bubble sort Here is the Bubble Sort algorithm: def bubble_sort(arr): n = len(arr) for i in range(n): swapped = False for j in range(0, n - i - 1): if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] swapped = True if not swapped: break return arr numbers = [64, 34, 25, 12, 22, 11, 90] print("Sorted:", bubble_sort(numbers.copy())) Write binary search Here is Binary Search: def binary_search(arr, target): left = 0 right = len(arr) - 1 while left <= right: mid = (left + right) // 2 if arr[mid] == target: return mid elif arr[mid] < target: left = mid + 1 else: right = mid - 1 return -1 numbers = [1, 3, 5, 7, 9, 11, 13, 15] print(binary_search(numbers, 7)) What is a class? A class is a blueprint for creating objects: class Dog: def __init__(self, name, age): self.name = name self.age = age def bark(self): return f"{self.name} says Woof!" my_dog = Dog("Buddy", 3) print(my_dog.bark()) How do I read a file? Here is how to read files: with open("filename.txt", "r") as file: content = file.read() print(content) with open("filename.txt", "r") as file: for line in file: print(line.strip()) How do I handle errors? Use try-except blocks: try: result = 10 / 0 except ZeroDivisionError: print("Cannot divide by zero!") Multiple exceptions: try: number = int(input("Enter number: ")) result = 10 / number except ValueError: print("Not a valid number!") except ZeroDivisionError: print("Cannot divide by zero!") except Exception as e: print(f"Error: {e}") Thanks! You are welcome! Feel free to ask me anything anytime! Happy coding! Hi Hello! How can I help you today? I can write code, explain concepts, or answer your programming questions! What is the difference between list and tuple? The main difference is mutability: - Lists are mutable (can change) - Tuples are immutable (cannot change) my_list = [1, 2, 3] my_tuple = (1, 2, 3) my_list[0] = 100 my_list.append(4) Use lists for changing data, tuples for fixed data. ''' class VedaTrainer: """Trainer for Veda Programming Assistant""" def __init__(self, vocab_size: int = 8000, max_length: int = 512, batch_size: int = 4): self.vocab_size = vocab_size self.max_length = max_length self.batch_size = batch_size self.tokenizer = VedaTokenizer(vocab_size=vocab_size) self.model = None def prepare_data(self, extra_data: str = ""): """Prepare training data""" data = TRAINING_DATA if extra_data: data += "\n\n" + extra_data if os.path.exists("programming.txt"): with open("programming.txt", 'r', encoding='utf-8') as f: code_data = f.read() data += "\n\n" + code_data self.tokenizer.fit([data]) all_tokens = self.tokenizer.encode(data) print(f"Total tokens: {len(all_tokens)}") sequences = [] stride = self.max_length // 2 for i in range(0, len(all_tokens) - self.max_length - 1, stride): seq = all_tokens[i:i + self.max_length + 1] if len(seq) == self.max_length + 1: sequences.append(seq) if len(sequences) < 10: stride = self.max_length // 4 sequences = [] for i in range(0, len(all_tokens) - self.max_length - 1, stride): seq = all_tokens[i:i + self.max_length + 1] if len(seq) == self.max_length + 1: sequences.append(seq) print(f"Created {len(sequences)} training sequences") sequences = np.array(sequences) X = sequences[:, :-1] y = sequences[:, 1:] dataset = tf.data.Dataset.from_tensor_slices((X, y)) dataset = dataset.shuffle(1000).batch(self.batch_size).prefetch(1) return dataset def build_model(self): """Build the model""" self.model = VedaProgrammingLLM( vocab_size=self.tokenizer.vocabulary_size, max_length=self.max_length, d_model=256, num_heads=8, num_layers=4, ff_dim=512 ) self.model.compile( optimizer=keras.optimizers.Adam(1e-4), loss=keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=['accuracy'] ) dummy = tf.zeros((1, self.max_length), dtype=tf.int32) self.model(dummy) return self.model def train(self, epochs: int = 15, save_path: str = None, extra_data: str = ""): """Train the model""" if save_path is None: save_path = MODEL_DIR dataset = self.prepare_data(extra_data) self.build_model() self.model.summary() os.makedirs(save_path, exist_ok=True) history = self.model.fit(dataset, epochs=epochs, verbose=1) self.model.save_weights(os.path.join(save_path, "weights.h5")) self.tokenizer.save(os.path.join(save_path, "tokenizer.json")) config = self.model.get_config() with open(os.path.join(save_path, "config.json"), 'w') as f: json.dump(config, f) print(f"Model saved to {save_path}") return history def generate_response(self, user_input: str, max_tokens: int = 200, temperature: float = 0.7) -> str: """Generate a response""" prompt = f" {user_input}\n" tokens = self.tokenizer.encode(prompt) generated = self.model.generate( tokens, max_new_tokens=max_tokens, temperature=temperature, repetition_penalty=1.2 ) response = self.tokenizer.decode(generated) if "" in response: response = response.split("")[-1].strip() if "" in response: response = response.split("")[0].strip() return response if __name__ == "__main__": trainer = VedaTrainer() trainer.train(epochs=20) print("\nTesting:") tests = ["Hello!", "What is a function?"] for test in tests: print(f"User: {test}") print(f"Assistant: {trainer.generate_response(test)}")