Upload 4 files
Browse files- Dockerfile +23 -0
- app.py +39 -0
- modelCNN.h5 +3 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.8-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
# Install system dependencies
|
| 6 |
+
RUN apt-get update && apt-get install -y \
|
| 7 |
+
libglib2.0-0 \
|
| 8 |
+
libsm6 \
|
| 9 |
+
libxrender-dev \
|
| 10 |
+
libxext6 \
|
| 11 |
+
ffmpeg
|
| 12 |
+
|
| 13 |
+
# Install Python dependencies
|
| 14 |
+
COPY requirements.txt requirements.txt
|
| 15 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 16 |
+
|
| 17 |
+
# Copy application files
|
| 18 |
+
COPY . .
|
| 19 |
+
|
| 20 |
+
# Expose port
|
| 21 |
+
EXPOSE 7860
|
| 22 |
+
|
| 23 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
from fastapi import FastAPI, File, UploadFile
|
| 3 |
+
from pydantic import BaseModel
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import tensorflow as tf
|
| 8 |
+
|
| 9 |
+
# Load the trained model
|
| 10 |
+
modelCNN = tf.keras.models.load_model('modelCNN.h5')
|
| 11 |
+
|
| 12 |
+
def predict(sketch):
|
| 13 |
+
try:
|
| 14 |
+
if 'layers' in sketch:
|
| 15 |
+
# Access the sketch image from the 'layers' key
|
| 16 |
+
sketch_image = np.array(sketch['layers'][0]) * 255
|
| 17 |
+
sketch_image = Image.fromarray(sketch_image.astype('uint8')).convert('L')
|
| 18 |
+
|
| 19 |
+
# Resize the sketch to match MNIST image size
|
| 20 |
+
sketch_image = sketch_image.resize((28, 28))
|
| 21 |
+
|
| 22 |
+
# Convert the image to a numpy array
|
| 23 |
+
img_array = np.array(sketch_image).reshape(1, 28, 28, 1) / 255.0
|
| 24 |
+
|
| 25 |
+
# Make prediction
|
| 26 |
+
prediction = modelCNN.predict(img_array)[0]
|
| 27 |
+
predicted_digit = np.argmax(prediction)
|
| 28 |
+
return str(predicted_digit)
|
| 29 |
+
else:
|
| 30 |
+
# Handle the case where "layers" key is missing
|
| 31 |
+
return "No sketch data found"
|
| 32 |
+
except Exception as e:
|
| 33 |
+
# Print the exception for debugging
|
| 34 |
+
print("Error:", e)
|
| 35 |
+
# Return an error message
|
| 36 |
+
return "An error occurred"
|
| 37 |
+
|
| 38 |
+
# Launch Gradio interface with FastAPI integration
|
| 39 |
+
gr.Interface(fn=predict, inputs="sketchpad", outputs="label").launch()
|
modelCNN.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:04f7344c42077a0d81fa704453010a1aad22ff8718234f46ca3bdca86f01e646
|
| 3 |
+
size 7355048
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
scikit-learn
|
| 3 |
+
torchvision
|
| 4 |
+
seaborn
|