AI-RESEARCHER-2024 commited on
Commit
989a16b
·
verified ·
1 Parent(s): 4fb9b36

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+
5
+ # Load the trained model
6
+ model = tf.keras.models.load_model('model.h5')
7
+ print("Model loaded successfully!")
8
+
9
+ def preprocess_image(image):
10
+ """Process the input image to match MNIST format"""
11
+ # Convert to grayscale
12
+ image = image.convert('L')
13
+ # Resize to 28x28
14
+ image = image.resize((28, 28))
15
+ # Convert to numpy array and normalize
16
+ image_array = np.array(image)
17
+ image_array = image_array / 255.0
18
+ # Reshape to match model input
19
+ image_array = np.expand_dims(image_array, axis=0)
20
+ return image_array
21
+
22
+ def predict_digit(image):
23
+ if image is None:
24
+ return None
25
+
26
+ # Preprocess the image
27
+ processed_image = preprocess_image(image)
28
+
29
+ # Make prediction
30
+ predictions = model.predict(processed_image)
31
+ pred_scores = tf.nn.softmax(predictions[0]).numpy()
32
+ pred_class = np.argmax(pred_scores)
33
+
34
+ # Create result string
35
+ result = f"Prediction: {pred_class}"
36
+
37
+ return result
38
+
39
+ # Create Gradio interface
40
+ demo = gr.Interface(
41
+ fn=predict_digit,
42
+ inputs=gr.Image(type="pil"),
43
+ outputs=gr.Textbox(label="Result"),
44
+ title="MNIST Digit Recognizer",
45
+ description="Upload a digit from 0-9 and the model will predict which digit it is.",
46
+ examples=None,
47
+ )
48
+
49
+ if __name__ == "__main__":
50
+ demo.launch()