| import numpy as np |
| import pandas as pd |
| from sklearn.preprocessing import LabelEncoder |
| from sklearn.model_selection import train_test_split |
| from tensorflow.keras.models import Sequential |
| from tensorflow.keras.layers import Dense, LSTM, Embedding |
| from tensorflow.keras.optimizers import Adam |
| from tensorflow.keras.utils import to_categorical |
| from tensorflow.keras.callbacks import EarlyStopping |
| import gradio as gr |
|
|
| |
| data = [ |
| "Double big 12", "Single big 11", "Single big 13", "Double big 12", "Double small 10", |
| "Double big 12", "Double big 12", "Single small 7", "Single small 5", "Single small 9", |
| "Single big 13", "Double small 8", "Single small 5", "Double big 14", "Single big 11", |
| "Double big 14", "Single big 17", "Triple 9", "Double small 6", "Single big 13", |
| "Double big 14", "Double small 8", "Double small 8", "Single big 13", "Single small 9", |
| "Double small 8", "Double small 8", "Single big 12", "Double small 8", "Double big 14", |
| "Double small 10", "Single big 13", "Single big 11", "Double big 14", "Double big 14" |
| ] |
|
|
| |
| encoder = LabelEncoder() |
| encoded_data = encoder.fit_transform(data) |
|
|
| |
| sequence_length = 5 |
| X, y = [], [] |
| for i in range(len(encoded_data) - sequence_length): |
| X.append(encoded_data[i:i + sequence_length]) |
| y.append(encoded_data[i + sequence_length]) |
|
|
| X = np.array(X) |
| y = to_categorical(y, num_classes=len(encoder.classes_)) |
|
|
| |
| def build_model(vocab_size, sequence_length): |
| model = Sequential([ |
| Embedding(vocab_size, 50, input_length=sequence_length), |
| LSTM(100), |
| Dense(vocab_size, activation='softmax') |
| ]) |
| model.compile(loss='categorical_crossentropy', optimizer=Adam(learning_rate=0.001), metrics=['accuracy']) |
| return model |
|
|
| |
| vocab_size = len(encoder.classes_) |
| model = build_model(vocab_size, sequence_length) |
|
|
| |
| early_stopping = EarlyStopping(monitor='val_loss', patience=10, restore_best_weights=True) |
| history = model.fit(X, y, epochs=100, validation_split=0.2, callbacks=[early_stopping], verbose=0) |
|
|
| def predict_next(model, data, sequence_length, encoder): |
| last_sequence = data[-sequence_length:] |
| last_sequence = np.array(encoder.transform(last_sequence)).reshape((1, sequence_length)) |
| prediction = model.predict(last_sequence) |
| predicted_label = encoder.inverse_transform([np.argmax(prediction)]) |
| return predicted_label[0] |
|
|
| def update_data(data, new_outcome): |
| data.append(new_outcome) |
| return data |
|
|
| def retrain_model(model, X, y, epochs=10): |
| early_stopping = EarlyStopping(monitor='val_loss', patience=5, restore_best_weights=True) |
| model.fit(X, y, epochs=epochs, validation_split=0.2, callbacks=[early_stopping], verbose=0) |
| return model |
|
|
| def gradio_predict(outcome): |
| global data, model |
|
|
| if outcome not in encoder.classes_: |
| return "Invalid outcome. Please try again." |
|
|
| data = update_data(data, outcome) |
|
|
| if len(data) < sequence_length: |
| return "Not enough data to make a prediction." |
|
|
| predicted_next = predict_next(model, data, sequence_length, encoder) |
| return f'Predicted next outcome: {predicted_next}' |
|
|
| def gradio_update(actual_next): |
| global data, X, y, model |
|
|
| if actual_next not in encoder.classes_: |
| return "Invalid outcome. Please try again." |
|
|
| data = update_data(data, actual_next) |
|
|
| if len(data) < sequence_length + 1: |
| return "Not enough data to update the model." |
|
|
| |
| new_X = [] |
| new_y = [] |
| for i in range(len(data) - sequence_length): |
| new_X.append(encoder.transform(data[i:i + sequence_length])) |
| new_y.append(encoder.transform([data[i + sequence_length]])[0]) |
|
|
| X = np.array(new_X) |
| y = to_categorical(new_y, num_classes=len(encoder.classes_)) |
|
|
| |
| model = retrain_model(model, X, y, epochs=10) |
|
|
| return "Model updated with new data." |
|
|
| |
| with gr.Blocks() as demo: |
| gr.Markdown("## Outcome Prediction Model") |
| with gr.Row(): |
| outcome_input = gr.Textbox(label="Current Outcome") |
| predict_button = gr.Button("Predict Next") |
| predicted_output = gr.Textbox(label="Predicted Next Outcome") |
| with gr.Row(): |
| actual_input = gr.Textbox(label="Actual Next Outcome") |
| update_button = gr.Button("Update Model") |
| update_output = gr.Textbox(label="Update Status") |
|
|
| predict_button.click(gradio_predict, inputs=outcome_input, outputs=predicted_output) |
| update_button.click(gradio_update, inputs=actual_input, outputs=update_output) |
|
|
| demo.launch() |