RahulJain19 commited on
Commit
4e7dde8
1 Parent(s): 764b272

blog article demo

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from tensorflow.keras.preprocessing.image import img_to_array
5
+ from keras.datasets import mnist
6
+
7
+ (X_train, y_train), (X_test, y_test) = mnist.load_data()
8
+
9
+ X_train = X_train/255.0
10
+ X_test = X_test/255.0
11
+
12
+ model = tf.keras.models.Sequential([
13
+ tf.keras.layers.
14
+ tf.keras.layers.Flatten(input_shape=(28, 28)),
15
+ tf.keras.layers.Dense(units=128, activation='relu'),
16
+ tf.keras.layers.Dropout(0.5),
17
+ tf.keras.layers.Dense(units=10,activation="softmax")
18
+ ])
19
+
20
+ model.compile(optimizer='adam',
21
+ loss='sparse_categorical_crossentropy',
22
+ metrics=['accuracy']
23
+ )
24
+ history = model.fit(X_train,y_train,epochs=10,validation_data=(X_test,y_test))
25
+
26
+ def predict(img):
27
+ x = img_to_array(img)
28
+ x = np.expand_dims(x,axis=0)
29
+ target = model.predict(x)
30
+ target = np.argmax(target)
31
+ return target
32
+
33
+ demo = gr.Interface(fn=predict,
34
+ inputs="sketchpad",
35
+ outputs="number",
36
+ live=True, streaming=True)
37
+
38
+ demo.launch(share=True)