Skip to content

Instantly share code, notes, and snippets.

View jschw's full-sized avatar

Julian Schweigert jschw

  • Germany
View GitHub Profile
@jschw
jschw / Test_RAG_CoT.py
Created January 27, 2024 11:24
This example shows the application of zero-shot Chain-pf-Thoughts method on RAG task. Run script with "python3 Test_RAG_CoT.py config.json"
import sys
import json
import openai
from langchain.text_splitter import Document
# ===== Functions ======
def build_prompt(input_docs, user_input):
from tensorflow import keras
import tensorflow.keras.backend as K
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Dense
import numpy as np
import cv2
import matplotlib
import matplotlib.pyplot as plt
import onnxruntime as onnxrun
# Processing with ONNX Runtime
REAL_Prediction = np.array(sess.run(None, {WORD_InputName: REAL_InputVector}))
REAL_PredictionConfidence = REAL_Prediction[0, 0, np.argmax(REAL_Prediction)]
REAL_PredictionConfidence = round(REAL_PredictionConfidence*100, 2)
DINT_ResultVector = np.argmax(REAL_Prediction)
from tensorflow import keras
import onnxmltools
# Load and convert model to ONNX
model = keras.models.load_model('model_curvetypes.h5')
onnx_model = onnxmltools.convert_keras(model, model.name)
onnxmltools.utils.save_model(onnx_model, 'model_curvetypes.onnx')
PROGRAM PLC_PRG
VAR
BOOL_O_JetsonTrigger : BOOL := FALSE;
DINT_Step : DINT := 0;
DINT_InputVectorLength : DINT := 20;
start_preprocessing = time.time()
prediction = model.predict(predict_arr)
end_preprocessing = time.time()
pred_conf = prediction[0, np.argmax(prediction)]
pred_conf = round(pred_conf*100, 2)
pred_time = round((end_preprocessing-start_preprocessing), 3)
print('Predicted class no.: ' + str(np.argmax(prediction)))
# Specific sample
#predict_arr = X_test[15].reshape(1,20)
#predict_ground_truth = y_onehot_test[15]
# Random sample
rand_index = np.random.choice(len(X_test), 1)
predict_arr = X_test[rand_index].reshape(1,20)
predict_ground_truth = y_onehot_test[rand_index]
def get_model():
model = Sequential()
model.add(Dense(64, input_dim=20, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(32, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(16, activation='relu'))
model.add(Dropout(0.5))
# Split x/y
X = data.values[:,0:-1]
Y = data.values[:,-1]
X = X.astype('float32')
# Encode label vector
encoder = LabelEncoder()
encoded_Y = encoder.fit_transform(Y)
data = pd.read_csv('curve_dataset_train.csv', index_col=False)
data = data.sample(frac=1, random_state=42).reset_index()
data.drop(['index'], 1, inplace=True)
print(data.shape)
data[:9][['shape', 'x1', 'y1','x2', 'y2','x3', 'y3','x4', 'y4','x5', 'y5','x6', 'y6','x7', 'y7','x8', 'y8','x9', 'y9','x10', 'y10']]