Spaces:
Sleeping
Sleeping
Download app.py from mnistMLProject/MNISTclassification: direct link, hf CLI and curl.
- Browser
- Download file 10.2 kB
-
https://huggingface.co/spaces/mnistMLProject/MNISTclassification/resolve/main/app.py
- Command line
-
hf download hf://spaces/mnistMLProject/MNISTclassification/app.py
-
curl -L -o app.py https://huggingface.co/spaces/mnistMLProject/MNISTclassification/resolve/main/app.py
10.2 kB
| import gradio as gr | |
| import numpy as np | |
| from skimage.feature import hog | |
| import pickle | |
| import cv2 | |
| loading_errors = {} | |
| def load_pickle_file(file_name): | |
| try: | |
| with open(file_name, "rb") as f: | |
| loaded_file = pickle.load(f) | |
| return loaded_file | |
| except pickle.UnpicklingError: | |
| error_msg = f"{file_name} is not a valid pickle. Run `git lfs pull`." | |
| print(error_msg, flush=True) | |
| loading_errors[file_name] = error_msg | |
| return None | |
| except Exception as exc: | |
| error_msg = f"Could not load {file_name}: {exc}" | |
| print(error_msg, flush=True) | |
| loading_errors[file_name] = error_msg | |
| return None | |
| # def build_knn_from_pca_package(package): | |
| # if package is None: | |
| # return None | |
| # required_keys = {"best_k", "weights", "X_train", "y_train", "pca_mean", "pca_components"} | |
| # missing_keys = required_keys.difference(package) | |
| # if missing_keys: | |
| # print(f"knn_pca.pkl is missing keys: {sorted(missing_keys)}") | |
| # return None | |
| # knn = MultimodeKNN(k=package["best_k"], weights=package["weights"]) | |
| # knn.fit(package["X_train"], package["y_train"]) | |
| # return knn | |
| gnb_package = load_pickle_file("gnb_digit6_package.pkl") | |
| pca_multi = load_pickle_file("pcaMulti.pkl") | |
| rf_model = load_pickle_file("rf_hog_model.pkl") | |
| #pca_KNN = load_pickle_file("knn_pca.pkl") | |
| if pca_multi is not None: | |
| pca_multi = pca_multi['pca'] | |
| if gnb_package is not None: | |
| gnb_binary_model = gnb_package["model"] | |
| pca = gnb_package["pca"] | |
| best_weight = gnb_package["best_weight"] | |
| hog_params = gnb_package["hog_params"] | |
| else: | |
| gnb_binary_model = None | |
| pca = None | |
| best_weight = None | |
| hog_params = None | |
| print("Loading Decision Tree...") | |
| dt_package = load_pickle_file("dt.pkl") | |
| if dt_package is not None: | |
| dt_binary_model = dt_package["model"] | |
| else: | |
| dt_binary_model = None | |
| print("Loading Logistic Regression...") | |
| logreg_binary_model = load_pickle_file("custom_logreg_hog.pkl") | |
| svm_package = load_pickle_file("svm2.pkl") | |
| if svm_package is not None: | |
| svm_binary_model = svm_package["model"] | |
| else: | |
| svm_binary_model = None | |
| svm_scaler_dict = load_pickle_file("svmScaler.pkl") | |
| svm_scaler = svm_scaler_dict.get('scaler', svm_scaler_dict.get('model')) | |
| knn_model_binary = load_pickle_file('knn_final_model.pkl') | |
| knn_multi = load_pickle_file("knn_hog_pca.pkl") | |
| gnb_multimode_model = load_pickle_file('gnbMulti.pkl') | |
| if gnb_multimode_model is not None: | |
| gnb_multimode_model = gnb_multimode_model["model"] | |
| dt_multimode_model = load_pickle_file('dt_Multi.pkl') | |
| if dt_multimode_model is not None: | |
| dt_multimode_model = dt_multimode_model['model'] | |
| svm_multimode_model = load_pickle_file('svm_hog.pkl') | |
| svm_multimode_scaler = load_pickle_file('scaler_hog.pkl') | |
| logreg_multimode_model = load_pickle_file('multiLog2_hog.pkl') | |
| def preprocess_image(image, model_choice, mode_choice): | |
| #image = cv2.resize(image, (28, 28)) | |
| image = np.array(image) | |
| image = image / 255.0 | |
| image_hog = hog( | |
| image, | |
| **hog_params | |
| ) | |
| if mode_choice == "Binary": | |
| if model_choice == "GNB" or model_choice == "KNN": | |
| image_hog = image_hog.reshape(1, -1) | |
| image_pca = pca.transform(image_hog) | |
| return image_pca | |
| elif model_choice == "SVM": | |
| image_r = image.reshape(1,-1) | |
| scaled_img = svm_scaler.transform(image_r) | |
| scaled_img = scaled_img.reshape(28,28) | |
| svm_img_hog = hog(scaled_img, **hog_params) | |
| return svm_img_hog.reshape(1, -1) | |
| else: | |
| image_hog = image_hog.reshape(1, -1) | |
| return image_hog | |
| else: | |
| if model_choice == "GNB": | |
| image_hog = image_hog.reshape(1, -1) | |
| image_pca = pca_multi.transform(image_hog) | |
| return image_pca | |
| elif model_choice == "DT" or model_choice == "Logistic Regression" or model_choice == "SVM": | |
| image_hog = image_hog.reshape(1, -1) | |
| return image_hog | |
| elif model_choice == "KNN": | |
| image_hog = image_hog.reshape(1, -1) | |
| image_pca = pca_multi.transform(image_hog) | |
| return image_pca | |
| def classify_binary_gnb(image): | |
| processed_image = preprocess_image(image, "GNB", "Binary") | |
| prediction = gnb_binary_model.predict( | |
| processed_image, | |
| class_weights={0: 1.0, 1: best_weight} | |
| ) | |
| if prediction[0] == 1: | |
| return "GNB Binary: Is 6" | |
| else: | |
| return "GNB Binary: Not 6" | |
| def classify_binary_dt(image): | |
| processed_image = preprocess_image(image, "DT", "Binary") | |
| prediction = dt_binary_model.predict(processed_image) | |
| if prediction[0] == 1: | |
| return "Decision Tree Binary: Is 6" | |
| else: | |
| return "Decision Tree Binary: Not 6" | |
| def classify_binary_svm(image): | |
| processed_image = preprocess_image(image, "SVM", "Binary") | |
| #scaled = svm_scaler.transform(processed_image) | |
| prediction = svm_binary_model.predict(processed_image) | |
| if prediction[0] == 1: | |
| return "SVM Binary: Is 6" | |
| else: | |
| return "SVM Binary: Not 6" | |
| def classify_binary_logreg(image): | |
| processed_image = preprocess_image(image, "Logistic Regression", "Binary") | |
| prediction = logreg_binary_model.predict(processed_image) | |
| if prediction[0] == 1: | |
| return "Logistic Regression Binary: Is 6" | |
| else: | |
| return "Logistic Regression Binary: Not 6" | |
| def classify_knn(image): | |
| if knn_model_binary is None: | |
| return "KNN Binary model placeholder: model is not inserted yet." | |
| processed_image = preprocess_image(image, "KNN", "Binary") | |
| prediction = knn_model_binary.predict(processed_image) | |
| if prediction[0] == 1: | |
| return "KNN Binary: Is 6" | |
| else: | |
| return "KNN Binary: Not 6" | |
| def classify_multimode_gnb(image): | |
| if gnb_multimode_model is None or pca_multi is None: | |
| return "GNB Multimode model could not be loaded. If this is a Git LFS file, run `git lfs pull` first." | |
| processed_image = preprocess_image(image, "GNB", "Multimode") | |
| prediction = gnb_multimode_model.predict(processed_image) | |
| return "GNB Multimode prediction: " + str(prediction[0]) | |
| def classify_multimode_dt(image): | |
| if dt_multimode_model is None: | |
| return "Decision Tree Multimode model could not be loaded. If this is a Git LFS file, run `git lfs pull` first." | |
| processed_image = preprocess_image(image, "DT", "Multimode") | |
| prediction = dt_multimode_model.predict(processed_image) | |
| return "Decision Tree Multimode prediction: " + str(prediction[0]) | |
| def classify_multimode_svm(image): | |
| if svm_multimode_model is None: | |
| if 'svm_hog.pkl' in loading_errors: | |
| return f"ERROR: {loading_errors['svm_hog.pkl']}" | |
| else: | |
| return "SVM Multimode model placeholder: model is not inserted yet." | |
| processed_image = preprocess_image(image, "SVM", "Multimode") | |
| scaled = svm_multimode_scaler.transform(processed_image) | |
| prediction = svm_multimode_model.predict(scaled) | |
| return "SVM Multimode prediction: " + str(prediction[0]) | |
| def classify_multimode_logreg(image): | |
| if logreg_multimode_model is None: | |
| return "Logistic Regression Multimode model placeholder: model is not inserted yet." | |
| processed_image = preprocess_image(image, "Logistic Regression", "Multimode") | |
| image_scaled = svm_multimode_scaler.transform(processed_image) | |
| prediction = logreg_multimode_model.predict(image_scaled) | |
| return "Logistic Regression Multimode prediction: " + str(prediction[0]) | |
| def classify_multimode_knn(image): | |
| if knn_multi is None: | |
| return "KNN Multimode model could not be loaded. If this is a Git LFS file, run `git lfs pull` first." | |
| processed_image = preprocess_image(image, "KNN", "Multimode") | |
| prediction = knn_multi.predict(processed_image) | |
| return "KNN Multimode prediction: " + str(prediction[0]) | |
| def classify_rf(image): | |
| processed_image = preprocess_image(image , "DT" , "Multimode") | |
| prediction = rf_model.predict(processed_image) | |
| return "RF Prediction: " + str(prediction[0]) | |
| def predict_with_selected_model(image, model_choice, mode_choice): | |
| if image is None: | |
| return "Please upload an image." | |
| if mode_choice == "Binary": | |
| if model_choice == "GNB": | |
| return classify_binary_gnb(image) | |
| elif model_choice == "DT": | |
| return classify_binary_dt(image) | |
| elif model_choice == "SVM": | |
| return classify_binary_svm(image) | |
| elif model_choice == "Logistic Regression": | |
| return classify_binary_logreg(image) | |
| elif model_choice == "KNN": | |
| return classify_knn(image) | |
| else: | |
| return "Unknown binary model selected." | |
| elif mode_choice == "Multimode": | |
| if model_choice == "GNB": | |
| return classify_multimode_gnb(image) | |
| elif model_choice == "DT": | |
| return classify_multimode_dt(image) | |
| elif model_choice == "SVM": | |
| return classify_multimode_svm(image) | |
| elif model_choice == "Logistic Regression": | |
| return classify_multimode_logreg(image) | |
| elif model_choice == "KNN": | |
| return classify_multimode_knn(image) | |
| elif model_choice == "Random Forest": | |
| return classify_rf(image) | |
| else: | |
| return "Unknown multimode model selected." | |
| else: | |
| return "Unknown mode selected." | |
| demo = gr.Interface( | |
| fn=predict_with_selected_model, | |
| inputs=[ | |
| gr.Image( | |
| image_mode="L", | |
| type="numpy", | |
| label="Upload Digit Image" | |
| ), | |
| gr.Radio( | |
| choices=["GNB", "DT", "SVM", "Logistic Regression", "KNN" , "Random Forest"], | |
| value="GNB", | |
| label="Choose Model Type" | |
| ), | |
| gr.Radio( | |
| choices=["Binary", "Multimode"], | |
| value="Binary", | |
| label="Choose Classification Mode" | |
| ) | |
| ], | |
| outputs=gr.Textbox(label="Prediction"), | |
| title="MNIST Classifier", | |
| description="Upload an image, select the model type, choose binary or multimode, and get a prediction." | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |