Skip to content

Commit 727a0aa

Browse files
.
1 parent 420bac2 commit 727a0aa

File tree

2 files changed

+20
-28
lines changed
  • 3rd-party/openai-api
  • projects/spaceshooter-item-detection

2 files changed

+20
-28
lines changed

3rd-party/openai-api/run.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
##
2-
#
3-
#
2+
# API access snippet.
43
#
54
# Source origin
65
# - DeepLearning.ai / OpenAI course: https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers
@@ -13,6 +12,9 @@
1312
_ = load_dotenv(find_dotenv())
1413
openai.api_key = os.getenv('OPENAI_API_KEY')
1514

15+
16+
# ---------------------------------------------------------------------------------------- FUNCTIONS
17+
1618
def get_completion(user_prompt, model="gpt-3.5-turbo"):
1719
messages = [{"role": "user", "content": user_prompt}]
1820
ai_response = openai.ChatCompletion.create(
@@ -22,6 +24,9 @@ def get_completion(user_prompt, model="gpt-3.5-turbo"):
2224
)
2325
return ai_response.choices[0].message["content"]
2426

27+
28+
# -------------------------------------------------------------------------------------------- TESTS
29+
2530
prompt = "Name five reasons to become a developer"
2631
result = get_completion(prompt)
2732

projects/spaceshooter-item-detection/run.py

+13-26
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
from sklearn.compose import ColumnTransformer
1313

1414

15-
# ------------------------------------------------------------------------------
16-
17-
# DATA
15+
# --------------------------------------------------------------------------------------------- DATA
1816

1917
# Raw values
2018
# - Index = 0 is also expected output
@@ -29,16 +27,12 @@
2927
])
3028

3129

32-
# ------------------------------------------------------------------------------
33-
34-
# TRANSFORM
30+
# ---------------------------------------------------------------------------------------- TRANSFORM
3531

3632
# - Raw data index 1-3 (ignore index 0)
3733
# - Raw data index 0
3834
inputs = data[:, 1:]
3935
outputs = data[:, 0].astype(int)
40-
#print(inputs)
41-
#print(outputs)
4236

4337

4438
# Strings become integers (here: Asteroid=0, Particle=1)
@@ -52,13 +46,11 @@
5246
transformers=[("encoder", OneHotEncoder(), [0])],
5347
remainder="passthrough"
5448
)
55-
inputs = np.array(ct.fit_transform(inputs), dtype=float)
56-
#print(inputs)
5749

50+
inputs = np.array(ct.fit_transform(inputs), dtype=float)
5851

59-
# ------------------------------------------------------------------------------
6052

61-
# MODEL
53+
# -------------------------------------------------------------------------------------------- MODEL
6254

6355
# Hidden layers = Between in/out, not visible/usable?
6456
# - 8 neurons with 4 inputs
@@ -78,33 +70,28 @@
7870
model.fit(inputs, outputs, epochs=1000, batch_size=2)
7971

8072

81-
# ------------------------------------------------------------------------------
82-
# PREDICTIONS
73+
# -------------------------------------------------------------------------------------- PREDICTIONS
8374

8475
def predict_collision(category: str, pos_x_item: int, pos_x_player: int):
8576
# Convert the input data into a NumPy array
8677
input_data = np.array([[category, pos_x_item, pos_x_player]])
87-
#print(input_data)
8878

8979
# Transform the input data using the ColumnTransformer
9080
input_data_transformed = ct.transform(input_data).astype(float)
91-
#print(input_data_transformed)
9281

9382
# Use the model to predict the probability of a collision
9483
prediction = model.predict(input_data_transformed)
95-
#print(prediction)
9684

9785
return prediction[0][0] > 0.5
9886

9987

100-
# ------------------------------------------------------------------------------
101-
# TESTS
88+
# -------------------------------------------------------------------------------------------- TESTS
10289

10390
# Test model with examples
104-
print(predict_collision("ASTEROID", 100, 100)) # Expected output: True
105-
print(predict_collision("ASTEROID", 0, 100)) # Expected output: False
106-
print(predict_collision("ASTEROID", 100, 0)) # Expected output: False
107-
print(predict_collision("PARTICLE", 100, 100)) # Expected output: False
108-
print(predict_collision("PARTICLE", 0, 100)) # Expected output: False
109-
print(predict_collision("PARTICLE", 100, 0)) # Expected output: False
110-
print(predict_collision("ASTEROID", 0, 0)) # Expected output: True
91+
print(predict_collision("ASTEROID", 100, 100)) # Expected output: True
92+
print(predict_collision("ASTEROID", 0, 100)) # Expected output: False
93+
print(predict_collision("ASTEROID", 100, 0)) # Expected output: False
94+
print(predict_collision("PARTICLE", 100, 100)) # Expected output: False
95+
print(predict_collision("PARTICLE", 0, 100)) # Expected output: False
96+
print(predict_collision("PARTICLE", 100, 0)) # Expected output: False
97+
print(predict_collision("ASTEROID", 0, 0)) # Expected output: True

0 commit comments

Comments
 (0)