bhoomika28 commited on
Commit
72404d6
·
1 Parent(s): dedf27f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -62
app.py CHANGED
@@ -1,75 +1,41 @@
1
- import streamlit as st
 
 
2
  import numpy as np
3
- import matplotlib.pyplot as plt
4
- from sklearn.linear_model import LinearRegression, Ridge
5
- from sklearn.preprocessing import PolynomialFeatures
6
- from sklearn.metrics import mean_squared_error
7
 
8
- st.subheader("Ridge Demo")
9
- col1, col2 = st.columns(2)
10
 
11
- degree = st.slider('Degree', 2, 40, 1)
12
- alpha = st.slider('Lambda (Regularisation)', 0, 500, 1)
13
 
14
 
15
- with col1:
16
- st.markdown("#### Un-regularized")
17
-
18
- with col2:
19
- st.markdown("#### Regularized")
20
-
21
- x = np.linspace(-1., 1., 100)
22
- y = 4 + 3*x + 2*np.sin(x) + 2*np.random.randn(len(x))
23
 
24
 
25
- poly = PolynomialFeatures(degree=degree, include_bias=False)
26
- x_new = poly.fit_transform(x.reshape(-1, 1))
27
 
28
- lr = LinearRegression()
29
- lr.fit(x_new, y)
30
- y_pred = lr.predict(x_new)
31
 
 
 
 
32
 
33
- ri = Ridge(alpha = alpha)
34
- ri.fit(x_new, y)
35
- y_pred_ri = ri.predict(x_new)
36
 
 
37
 
38
- fig1, ax1 = plt.subplots()
39
- fig2, ax2 = plt.subplots()
40
 
41
- ax1.scatter(x, y)
42
- ax1.plot(x, y_pred)
43
-
44
- ax2.scatter(x, y)
45
- ax2.plot(x, y_pred_ri)
46
-
47
- for ax in [ax1, ax2]:
48
- ax.spines['right'].set_visible(False)
49
- ax.spines['top'].set_visible(False)
50
-
51
- # Only show ticks on the left and bottom spines
52
- ax.yaxis.set_ticks_position('left')
53
- ax.xaxis.set_ticks_position('bottom')
54
-
55
- ax.set_xlabel("x")
56
- ax.set_ylabel("y")
57
-
58
- rmse = np.round(np.sqrt(mean_squared_error(y_pred, y)), 2)
59
- ax1.set_title(f"Train RMSE: {rmse}")
60
-
61
- rmse_ri = np.round(np.sqrt(mean_squared_error(y_pred_ri, y)), 2)
62
- ax2.set_title(f"Train RMSE: {rmse_ri}")
63
-
64
- with col1:
65
- st.pyplot(fig1)
66
-
67
- with col2:
68
- st.pyplot(fig2)
69
- hide_streamlit_style = """
70
- <style>
71
- #MainMenu {visibility: hidden;}
72
- footer {visibility: hidden;}
73
- </style>
74
- """
75
- st.markdown(hide_streamlit_style, unsafe_allow_html=True)
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ from huggingface_hub import from_pretrained_keras
4
  import numpy as np
 
 
 
 
5
 
6
+ model = from_pretrained_keras("keras-io/semi-supervised-classification-simclr")
 
7
 
8
+ labels = ["airplane", "bird", "car", "cat", "deer", "dog", "horse", "monkey", "ship", "truck"]
 
9
 
10
 
11
+ def infer(test_image):
12
+ image = tf.constant(test_image)
13
+ image = tf.reshape(image, [-1, 96, 96, 3])
14
+ pred = model.predict(image)
15
+ pred_list = pred[0, :]
16
+ pred_softmax = np.exp(pred_list)/np.sum(np.exp(pred_list))
17
+ softmax_list = pred_softmax.tolist()
18
+ return {labels[i]: softmax_list[i] for i in range(10)}
19
 
20
 
21
+ image = gr.inputs.Image(shape=(96, 96))
22
+ label = gr.outputs.Label(num_top_classes=3)
23
 
 
 
 
24
 
25
+ article = """<center>
26
+ Authors: <a href='https://twitter.com/johko990' target='_blank'>Johannes Kolbe</a> after an example by András Béres at
27
+ <a href='https://keras.io/examples/vision/semisupervised_simclr/' target='_blank'>keras.io</a>"""
28
 
 
 
 
29
 
30
+ description = """Image classification with a model trained via Semi-supervised Contrastive Learning """
31
 
 
 
32
 
33
+ Iface = gr.Interface(
34
+ fn=infer,
35
+ inputs=image,
36
+ outputs=label,
37
+ examples=[["examples/monkey.jpeg"], ["examples/titanic.jpg"], ["examples/truck.jpg"]],
38
+ title="Semi-Supervised Contrastive Learning Classification",
39
+ article=article,
40
+ description=description,
41
+ ).launch()