Spaces:
Running
Running
Update train.py
Browse files
train.py
CHANGED
|
@@ -27,7 +27,7 @@ Key features:
|
|
| 27 |
Python is used for web development, data science, AI, automation, and more!
|
| 28 |
|
| 29 |
<USER> How do I print hello world?
|
| 30 |
-
<ASSISTANT> To print
|
| 31 |
|
| 32 |
<CODE>
|
| 33 |
print("Hello, World!")
|
|
@@ -42,9 +42,8 @@ When you run this, it displays: Hello, World!
|
|
| 42 |
def add(a, b):
|
| 43 |
return a + b
|
| 44 |
|
| 45 |
-
# Example usage
|
| 46 |
result = add(5, 3)
|
| 47 |
-
print(result)
|
| 48 |
<ENDCODE>
|
| 49 |
|
| 50 |
This function takes two parameters and returns their sum.
|
|
@@ -63,8 +62,8 @@ def function_name(parameters):
|
|
| 63 |
Parts of a function:
|
| 64 |
- def: keyword to define a function
|
| 65 |
- function_name: what you call your function
|
| 66 |
-
- parameters: input values
|
| 67 |
-
- return: sends back a result
|
| 68 |
|
| 69 |
Example:
|
| 70 |
|
|
@@ -73,7 +72,7 @@ def greet(name):
|
|
| 73 |
message = f"Hello, {name}!"
|
| 74 |
return message
|
| 75 |
|
| 76 |
-
print(greet("Alice"))
|
| 77 |
<ENDCODE>
|
| 78 |
|
| 79 |
<USER> What is a list?
|
|
@@ -82,10 +81,7 @@ print(greet("Alice")) # Output: Hello, Alice!
|
|
| 82 |
Creating lists:
|
| 83 |
|
| 84 |
<CODE>
|
| 85 |
-
# Empty list
|
| 86 |
my_list = []
|
| 87 |
-
|
| 88 |
-
# List with items
|
| 89 |
numbers = [1, 2, 3, 4, 5]
|
| 90 |
fruits = ["apple", "banana", "cherry"]
|
| 91 |
mixed = [1, "hello", 3.14, True]
|
|
@@ -96,27 +92,21 @@ Common operations:
|
|
| 96 |
<CODE>
|
| 97 |
fruits = ["apple", "banana"]
|
| 98 |
|
| 99 |
-
# Add items
|
| 100 |
fruits.append("cherry")
|
| 101 |
fruits.insert(0, "apricot")
|
| 102 |
|
| 103 |
-
|
| 104 |
-
print(fruits[
|
| 105 |
-
print(fruits[-1]) # Last item
|
| 106 |
|
| 107 |
-
# Remove items
|
| 108 |
fruits.remove("banana")
|
| 109 |
fruits.pop()
|
| 110 |
|
| 111 |
-
# Length
|
| 112 |
print(len(fruits))
|
| 113 |
<ENDCODE>
|
| 114 |
|
| 115 |
<USER> Write a fibonacci function
|
| 116 |
<ASSISTANT> Here is a Fibonacci function:
|
| 117 |
|
| 118 |
-
Recursive approach:
|
| 119 |
-
|
| 120 |
<CODE>
|
| 121 |
def fibonacci(n):
|
| 122 |
if n <= 0:
|
|
@@ -125,13 +115,11 @@ def fibonacci(n):
|
|
| 125 |
return 1
|
| 126 |
return fibonacci(n-1) + fibonacci(n-2)
|
| 127 |
|
| 128 |
-
# Print first 10 numbers
|
| 129 |
for i in range(10):
|
| 130 |
print(fibonacci(i), end=" ")
|
| 131 |
-
# Output: 0 1 1 2 3 5 8 13 21 34
|
| 132 |
<ENDCODE>
|
| 133 |
|
| 134 |
-
Iterative
|
| 135 |
|
| 136 |
<CODE>
|
| 137 |
def fibonacci_iterative(n):
|
|
@@ -153,29 +141,28 @@ Key components:
|
|
| 153 |
1. Base case - stops the recursion
|
| 154 |
2. Recursive case - function calls itself
|
| 155 |
|
| 156 |
-
Example
|
| 157 |
|
| 158 |
<CODE>
|
| 159 |
def countdown(n):
|
| 160 |
-
if n <= 0:
|
| 161 |
print("Done!")
|
| 162 |
return
|
| 163 |
print(n)
|
| 164 |
-
countdown(n - 1)
|
| 165 |
|
| 166 |
countdown(5)
|
| 167 |
-
# Output: 5, 4, 3, 2, 1, Done!
|
| 168 |
<ENDCODE>
|
| 169 |
|
| 170 |
-
|
| 171 |
|
| 172 |
<CODE>
|
| 173 |
def factorial(n):
|
| 174 |
-
if n <= 1:
|
| 175 |
return 1
|
| 176 |
-
return n * factorial(n - 1)
|
| 177 |
|
| 178 |
-
print(factorial(5))
|
| 179 |
<ENDCODE>
|
| 180 |
|
| 181 |
<USER> Write bubble sort
|
|
@@ -198,18 +185,12 @@ def bubble_sort(arr):
|
|
| 198 |
|
| 199 |
return arr
|
| 200 |
|
| 201 |
-
# Example
|
| 202 |
numbers = [64, 34, 25, 12, 22, 11, 90]
|
| 203 |
-
print("Original:", numbers)
|
| 204 |
print("Sorted:", bubble_sort(numbers.copy()))
|
| 205 |
<ENDCODE>
|
| 206 |
|
| 207 |
-
Output:
|
| 208 |
-
Original: [64, 34, 25, 12, 22, 11, 90]
|
| 209 |
-
Sorted: [11, 12, 22, 25, 34, 64, 90]
|
| 210 |
-
|
| 211 |
<USER> Write binary search
|
| 212 |
-
<ASSISTANT> Here is
|
| 213 |
|
| 214 |
<CODE>
|
| 215 |
def binary_search(arr, target):
|
|
@@ -226,19 +207,14 @@ def binary_search(arr, target):
|
|
| 226 |
else:
|
| 227 |
right = mid - 1
|
| 228 |
|
| 229 |
-
return -1
|
| 230 |
|
| 231 |
-
# Example
|
| 232 |
numbers = [1, 3, 5, 7, 9, 11, 13, 15]
|
| 233 |
-
print(binary_search(numbers, 7))
|
| 234 |
-
print(binary_search(numbers, 6)) # Output: -1
|
| 235 |
<ENDCODE>
|
| 236 |
|
| 237 |
-
Important: The array must be sorted!
|
| 238 |
-
Time complexity: O(log n)
|
| 239 |
-
|
| 240 |
<USER> What is a class?
|
| 241 |
-
<ASSISTANT> A class is a blueprint for creating objects
|
| 242 |
|
| 243 |
<CODE>
|
| 244 |
class Dog:
|
|
@@ -248,57 +224,26 @@ class Dog:
|
|
| 248 |
|
| 249 |
def bark(self):
|
| 250 |
return f"{self.name} says Woof!"
|
| 251 |
-
|
| 252 |
-
def get_info(self):
|
| 253 |
-
return f"{self.name} is {self.age} years old"
|
| 254 |
|
| 255 |
-
# Create objects
|
| 256 |
my_dog = Dog("Buddy", 3)
|
| 257 |
-
print(my_dog.bark())
|
| 258 |
-
print(my_dog.get_info()) # Buddy is 3 years old
|
| 259 |
<ENDCODE>
|
| 260 |
|
| 261 |
-
Key concepts:
|
| 262 |
-
- __init__: constructor, runs when object is created
|
| 263 |
-
- self: refers to the current object
|
| 264 |
-
- Attributes: variables inside the class
|
| 265 |
-
- Methods: functions inside the class
|
| 266 |
-
|
| 267 |
<USER> How do I read a file?
|
| 268 |
-
<ASSISTANT> Here is how to read files
|
| 269 |
|
| 270 |
<CODE>
|
| 271 |
-
# Read entire file
|
| 272 |
with open("filename.txt", "r") as file:
|
| 273 |
content = file.read()
|
| 274 |
print(content)
|
| 275 |
|
| 276 |
-
# Read line by line
|
| 277 |
with open("filename.txt", "r") as file:
|
| 278 |
for line in file:
|
| 279 |
print(line.strip())
|
| 280 |
-
|
| 281 |
-
# Read all lines into list
|
| 282 |
-
with open("filename.txt", "r") as file:
|
| 283 |
-
lines = file.readlines()
|
| 284 |
-
<ENDCODE>
|
| 285 |
-
|
| 286 |
-
With error handling:
|
| 287 |
-
|
| 288 |
-
<CODE>
|
| 289 |
-
def read_file_safely(filename):
|
| 290 |
-
try:
|
| 291 |
-
with open(filename, "r") as file:
|
| 292 |
-
return file.read()
|
| 293 |
-
except FileNotFoundError:
|
| 294 |
-
print(f"File '{filename}' not found")
|
| 295 |
-
return None
|
| 296 |
-
|
| 297 |
-
content = read_file_safely("myfile.txt")
|
| 298 |
<ENDCODE>
|
| 299 |
|
| 300 |
<USER> How do I handle errors?
|
| 301 |
-
<ASSISTANT>
|
| 302 |
|
| 303 |
<CODE>
|
| 304 |
try:
|
|
@@ -321,56 +266,26 @@ except Exception as e:
|
|
| 321 |
print(f"Error: {e}")
|
| 322 |
<ENDCODE>
|
| 323 |
|
| 324 |
-
Complete structure:
|
| 325 |
-
|
| 326 |
-
<CODE>
|
| 327 |
-
try:
|
| 328 |
-
result = risky_operation()
|
| 329 |
-
except SomeError:
|
| 330 |
-
print("Handle error")
|
| 331 |
-
else:
|
| 332 |
-
print("Success!")
|
| 333 |
-
finally:
|
| 334 |
-
print("Always runs")
|
| 335 |
-
<ENDCODE>
|
| 336 |
-
|
| 337 |
<USER> Thanks!
|
| 338 |
-
<ASSISTANT> You are welcome!
|
|
|
|
|
|
|
|
|
|
| 339 |
|
| 340 |
<USER> What is the difference between list and tuple?
|
| 341 |
<ASSISTANT> The main difference is mutability:
|
| 342 |
-
- Lists are mutable (can
|
| 343 |
-
- Tuples are immutable (cannot
|
| 344 |
-
|
| 345 |
-
Syntax:
|
| 346 |
|
| 347 |
<CODE>
|
| 348 |
-
# List - square brackets
|
| 349 |
my_list = [1, 2, 3]
|
| 350 |
-
|
| 351 |
-
# Tuple - parentheses
|
| 352 |
my_tuple = (1, 2, 3)
|
| 353 |
-
<ENDCODE>
|
| 354 |
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
<CODE>
|
| 358 |
-
# Lists can be modified
|
| 359 |
-
my_list = [1, 2, 3]
|
| 360 |
-
my_list[0] = 100 # OK
|
| 361 |
-
my_list.append(4) # OK
|
| 362 |
-
|
| 363 |
-
# Tuples cannot be modified
|
| 364 |
-
my_tuple = (1, 2, 3)
|
| 365 |
-
# my_tuple[0] = 100 # ERROR!
|
| 366 |
<ENDCODE>
|
| 367 |
|
| 368 |
-
|
| 369 |
-
- List: data that will change
|
| 370 |
-
- Tuple: fixed data like coordinates
|
| 371 |
-
|
| 372 |
-
<USER> Hi
|
| 373 |
-
<ASSISTANT> Hello! How can I help you today? I can write code, explain concepts, or answer your programming questions!
|
| 374 |
'''
|
| 375 |
|
| 376 |
|
|
@@ -500,12 +415,8 @@ if __name__ == "__main__":
|
|
| 500 |
trainer = VedaTrainer()
|
| 501 |
trainer.train(epochs=20)
|
| 502 |
|
| 503 |
-
print("\
|
| 504 |
-
|
| 505 |
-
print("="*50)
|
| 506 |
-
|
| 507 |
-
tests = ["Hello!", "What is a function?", "Write a function to reverse a string"]
|
| 508 |
-
|
| 509 |
for test in tests:
|
| 510 |
-
print(f"
|
| 511 |
print(f"Assistant: {trainer.generate_response(test)}")
|
|
|
|
| 27 |
Python is used for web development, data science, AI, automation, and more!
|
| 28 |
|
| 29 |
<USER> How do I print hello world?
|
| 30 |
+
<ASSISTANT> To print Hello World in Python, use the print function:
|
| 31 |
|
| 32 |
<CODE>
|
| 33 |
print("Hello, World!")
|
|
|
|
| 42 |
def add(a, b):
|
| 43 |
return a + b
|
| 44 |
|
|
|
|
| 45 |
result = add(5, 3)
|
| 46 |
+
print(result)
|
| 47 |
<ENDCODE>
|
| 48 |
|
| 49 |
This function takes two parameters and returns their sum.
|
|
|
|
| 62 |
Parts of a function:
|
| 63 |
- def: keyword to define a function
|
| 64 |
- function_name: what you call your function
|
| 65 |
+
- parameters: input values
|
| 66 |
+
- return: sends back a result
|
| 67 |
|
| 68 |
Example:
|
| 69 |
|
|
|
|
| 72 |
message = f"Hello, {name}!"
|
| 73 |
return message
|
| 74 |
|
| 75 |
+
print(greet("Alice"))
|
| 76 |
<ENDCODE>
|
| 77 |
|
| 78 |
<USER> What is a list?
|
|
|
|
| 81 |
Creating lists:
|
| 82 |
|
| 83 |
<CODE>
|
|
|
|
| 84 |
my_list = []
|
|
|
|
|
|
|
| 85 |
numbers = [1, 2, 3, 4, 5]
|
| 86 |
fruits = ["apple", "banana", "cherry"]
|
| 87 |
mixed = [1, "hello", 3.14, True]
|
|
|
|
| 92 |
<CODE>
|
| 93 |
fruits = ["apple", "banana"]
|
| 94 |
|
|
|
|
| 95 |
fruits.append("cherry")
|
| 96 |
fruits.insert(0, "apricot")
|
| 97 |
|
| 98 |
+
print(fruits[0])
|
| 99 |
+
print(fruits[-1])
|
|
|
|
| 100 |
|
|
|
|
| 101 |
fruits.remove("banana")
|
| 102 |
fruits.pop()
|
| 103 |
|
|
|
|
| 104 |
print(len(fruits))
|
| 105 |
<ENDCODE>
|
| 106 |
|
| 107 |
<USER> Write a fibonacci function
|
| 108 |
<ASSISTANT> Here is a Fibonacci function:
|
| 109 |
|
|
|
|
|
|
|
| 110 |
<CODE>
|
| 111 |
def fibonacci(n):
|
| 112 |
if n <= 0:
|
|
|
|
| 115 |
return 1
|
| 116 |
return fibonacci(n-1) + fibonacci(n-2)
|
| 117 |
|
|
|
|
| 118 |
for i in range(10):
|
| 119 |
print(fibonacci(i), end=" ")
|
|
|
|
| 120 |
<ENDCODE>
|
| 121 |
|
| 122 |
+
Iterative version:
|
| 123 |
|
| 124 |
<CODE>
|
| 125 |
def fibonacci_iterative(n):
|
|
|
|
| 141 |
1. Base case - stops the recursion
|
| 142 |
2. Recursive case - function calls itself
|
| 143 |
|
| 144 |
+
Example:
|
| 145 |
|
| 146 |
<CODE>
|
| 147 |
def countdown(n):
|
| 148 |
+
if n <= 0:
|
| 149 |
print("Done!")
|
| 150 |
return
|
| 151 |
print(n)
|
| 152 |
+
countdown(n - 1)
|
| 153 |
|
| 154 |
countdown(5)
|
|
|
|
| 155 |
<ENDCODE>
|
| 156 |
|
| 157 |
+
Factorial example:
|
| 158 |
|
| 159 |
<CODE>
|
| 160 |
def factorial(n):
|
| 161 |
+
if n <= 1:
|
| 162 |
return 1
|
| 163 |
+
return n * factorial(n - 1)
|
| 164 |
|
| 165 |
+
print(factorial(5))
|
| 166 |
<ENDCODE>
|
| 167 |
|
| 168 |
<USER> Write bubble sort
|
|
|
|
| 185 |
|
| 186 |
return arr
|
| 187 |
|
|
|
|
| 188 |
numbers = [64, 34, 25, 12, 22, 11, 90]
|
|
|
|
| 189 |
print("Sorted:", bubble_sort(numbers.copy()))
|
| 190 |
<ENDCODE>
|
| 191 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
<USER> Write binary search
|
| 193 |
+
<ASSISTANT> Here is Binary Search:
|
| 194 |
|
| 195 |
<CODE>
|
| 196 |
def binary_search(arr, target):
|
|
|
|
| 207 |
else:
|
| 208 |
right = mid - 1
|
| 209 |
|
| 210 |
+
return -1
|
| 211 |
|
|
|
|
| 212 |
numbers = [1, 3, 5, 7, 9, 11, 13, 15]
|
| 213 |
+
print(binary_search(numbers, 7))
|
|
|
|
| 214 |
<ENDCODE>
|
| 215 |
|
|
|
|
|
|
|
|
|
|
| 216 |
<USER> What is a class?
|
| 217 |
+
<ASSISTANT> A class is a blueprint for creating objects:
|
| 218 |
|
| 219 |
<CODE>
|
| 220 |
class Dog:
|
|
|
|
| 224 |
|
| 225 |
def bark(self):
|
| 226 |
return f"{self.name} says Woof!"
|
|
|
|
|
|
|
|
|
|
| 227 |
|
|
|
|
| 228 |
my_dog = Dog("Buddy", 3)
|
| 229 |
+
print(my_dog.bark())
|
|
|
|
| 230 |
<ENDCODE>
|
| 231 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
<USER> How do I read a file?
|
| 233 |
+
<ASSISTANT> Here is how to read files:
|
| 234 |
|
| 235 |
<CODE>
|
|
|
|
| 236 |
with open("filename.txt", "r") as file:
|
| 237 |
content = file.read()
|
| 238 |
print(content)
|
| 239 |
|
|
|
|
| 240 |
with open("filename.txt", "r") as file:
|
| 241 |
for line in file:
|
| 242 |
print(line.strip())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
<ENDCODE>
|
| 244 |
|
| 245 |
<USER> How do I handle errors?
|
| 246 |
+
<ASSISTANT> Use try-except blocks:
|
| 247 |
|
| 248 |
<CODE>
|
| 249 |
try:
|
|
|
|
| 266 |
print(f"Error: {e}")
|
| 267 |
<ENDCODE>
|
| 268 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 269 |
<USER> Thanks!
|
| 270 |
+
<ASSISTANT> You are welcome! Feel free to ask me anything anytime! Happy coding!
|
| 271 |
+
|
| 272 |
+
<USER> Hi
|
| 273 |
+
<ASSISTANT> Hello! How can I help you today? I can write code, explain concepts, or answer your programming questions!
|
| 274 |
|
| 275 |
<USER> What is the difference between list and tuple?
|
| 276 |
<ASSISTANT> The main difference is mutability:
|
| 277 |
+
- Lists are mutable (can change)
|
| 278 |
+
- Tuples are immutable (cannot change)
|
|
|
|
|
|
|
| 279 |
|
| 280 |
<CODE>
|
|
|
|
| 281 |
my_list = [1, 2, 3]
|
|
|
|
|
|
|
| 282 |
my_tuple = (1, 2, 3)
|
|
|
|
| 283 |
|
| 284 |
+
my_list[0] = 100
|
| 285 |
+
my_list.append(4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
<ENDCODE>
|
| 287 |
|
| 288 |
+
Use lists for changing data, tuples for fixed data.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
'''
|
| 290 |
|
| 291 |
|
|
|
|
| 415 |
trainer = VedaTrainer()
|
| 416 |
trainer.train(epochs=20)
|
| 417 |
|
| 418 |
+
print("\nTesting:")
|
| 419 |
+
tests = ["Hello!", "What is a function?"]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 420 |
for test in tests:
|
| 421 |
+
print(f"User: {test}")
|
| 422 |
print(f"Assistant: {trainer.generate_response(test)}")
|