Spaces:
Sleeping
Sleeping
Upload 7 files
Browse files- app.py +53 -0
- digit_model.pkl +3 -0
- optdigits.tra +0 -0
- output_file.csv +0 -0
- pca.ipynb +0 -0
- pca_transformed_data.csv +0 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pickle
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
with open("digit_model.pkl", "rb") as f:
|
| 8 |
+
pipeline = pickle.load(f)
|
| 9 |
+
|
| 10 |
+
def predict_manual(*features):
|
| 11 |
+
features = np.array(features, dtype=float).reshape(1, -1)
|
| 12 |
+
prediction = pipeline.predict(features)
|
| 13 |
+
return int(prediction[0])
|
| 14 |
+
|
| 15 |
+
def predict_image(img):
|
| 16 |
+
|
| 17 |
+
display_img = img.resize((200, 200))
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
img_small = img.convert("L").resize((8, 8), resample=Image.Resampling.LANCZOS)
|
| 21 |
+
img_array = np.array(img_small, dtype=float)
|
| 22 |
+
|
| 23 |
+
img_array = (16 - (img_array / 255.0 * 16)).flatten().reshape(1, -1)
|
| 24 |
+
|
| 25 |
+
prediction = pipeline.predict(img_array)
|
| 26 |
+
return display_img, int(prediction[0])
|
| 27 |
+
|
| 28 |
+
manual_inputs = [gr.Number(label=f"Pixel {i}", value=0) for i in range(64)]
|
| 29 |
+
|
| 30 |
+
with gr.Blocks(title="Digit Recognition App") as demo:
|
| 31 |
+
gr.Markdown("## Digit Recognition using PCA + KNN Pipeline")
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
with gr.Tab("Manual Input"):
|
| 35 |
+
gr.Markdown("Enter the 64 pixel values manually (0–16 scale like sklearn digits):")
|
| 36 |
+
for inp in manual_inputs:
|
| 37 |
+
inp.render()
|
| 38 |
+
manual_output = gr.Label(label="Predicted Digit")
|
| 39 |
+
manual_button = gr.Button("Predict Digit")
|
| 40 |
+
manual_button.click(fn=predict_manual, inputs=manual_inputs, outputs=manual_output)
|
| 41 |
+
|
| 42 |
+
with gr.Tab("Upload Image"):
|
| 43 |
+
gr.Markdown("Upload an image of a digit (grayscale or color):")
|
| 44 |
+
with gr.Row():
|
| 45 |
+
image_input = gr.Image(type="pil", label="Upload Digit Image")
|
| 46 |
+
with gr.Column():
|
| 47 |
+
image_output = gr.Image(label="Resized Image for Display")
|
| 48 |
+
digit_output = gr.Label(label="Predicted Digit")
|
| 49 |
+
|
| 50 |
+
image_button = gr.Button("Predict Digit")
|
| 51 |
+
image_button.click(fn=predict_image, inputs=image_input, outputs=[image_output, digit_output])
|
| 52 |
+
|
| 53 |
+
demo.launch()
|
digit_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:800d4f07bcca675c7ff3e4d3459f3f548d580d62192ff7264dad6104f98074f5
|
| 3 |
+
size 1053218
|
optdigits.tra
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
output_file.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
pca.ipynb
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
pca_transformed_data.csv
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
numpy
|
| 3 |
+
matplotlib
|
| 4 |
+
seaborn
|
| 5 |
+
scikit-learn
|