Spaces:
Runtime error
Runtime error
first commit
Browse files- app.py +91 -0
- categories.txt +1 -0
- defective.jpeg +0 -0
- model.h5 +3 -0
- okay.jpeg +0 -0
- requirements.txt +3 -0
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
### -------------------------------- ###
|
2 |
+
### libraries ###
|
3 |
+
### -------------------------------- ###
|
4 |
+
import gradio as gr
|
5 |
+
import numpy as np
|
6 |
+
import os
|
7 |
+
from tensorflow.keras.models import load_model
|
8 |
+
|
9 |
+
### -------------------------------- ###
|
10 |
+
### model loading ###
|
11 |
+
### -------------------------------- ###
|
12 |
+
model = load_model('model.h5') # single file model from colab
|
13 |
+
|
14 |
+
## --------------------------------- ###
|
15 |
+
### reading: categories.txt ###
|
16 |
+
### -------------------------------- ###
|
17 |
+
labels = ['please upload categories.txt' for i in range(10)] # placeholder
|
18 |
+
|
19 |
+
if os.path.isfile("categories.txt"):
|
20 |
+
# open categories.txt in read mode
|
21 |
+
categories = open("categories.txt", "r")
|
22 |
+
labels = categories.readline().split()
|
23 |
+
|
24 |
+
## --------------------------------- ###
|
25 |
+
### rendering: info.html ###
|
26 |
+
### -------------------------------- ###
|
27 |
+
# borrow file reading functionality from reader.py
|
28 |
+
# info =
|
29 |
+
description = "A Hugging Space demo created by datasith"
|
30 |
+
title = "Cast parts: Deffective or Okay?"
|
31 |
+
# css = \
|
32 |
+
# '''
|
33 |
+
# .div {
|
34 |
+
# border: 2px solid black;
|
35 |
+
# margin: 10px;
|
36 |
+
# padding: 5%;
|
37 |
+
# }
|
38 |
+
# ul {
|
39 |
+
# display: inline-block;
|
40 |
+
# text-align: left;
|
41 |
+
# }
|
42 |
+
# img {
|
43 |
+
# display: block;
|
44 |
+
# margin: auto;
|
45 |
+
# }
|
46 |
+
# .description {
|
47 |
+
# text-align: center;
|
48 |
+
# }
|
49 |
+
# '''
|
50 |
+
|
51 |
+
article = \
|
52 |
+
'''
|
53 |
+
Deffective or Okay? Demo app including a binary classification model for casted parts
|
54 |
+
This is a test project to get familiar with Hugging Face!
|
55 |
+
The space includes the necessary files for everything to run smoothly on HF's `Spaces`:
|
56 |
+
- `app.py`
|
57 |
+
- `reader.py`
|
58 |
+
- `requirements.txt`
|
59 |
+
- `model.h5` (TensorFlow/Keras)
|
60 |
+
- `categories.txt`
|
61 |
+
- `info.txt`
|
62 |
+
The data used to train the model is available as [Kaggle dataset](https://www.kaggle.com/datasets/ravirajsinh45/real-life-industrial-dataset-of-casting-product).
|
63 |
+
The space was inspired by @Isabel's wonderful [cat or pug](https://huggingface.co/spaces/isabel/pug-or-cat-image-classifier) one. Enjoy!d
|
64 |
+
'''
|
65 |
+
|
66 |
+
|
67 |
+
### -------------------------------- ###
|
68 |
+
### interface creation ###
|
69 |
+
### -------------------------------- ###
|
70 |
+
samples = ['defective.jpeg', 'okay.jpeg']
|
71 |
+
|
72 |
+
def preprocess(image):
|
73 |
+
image = np.array(image) / 255
|
74 |
+
image = np.expand_dims(image, axis=0)
|
75 |
+
return image
|
76 |
+
|
77 |
+
def predict_image(image):
|
78 |
+
pred = model.predict(preprocess(image))
|
79 |
+
results = {}
|
80 |
+
for row in pred:
|
81 |
+
for idx, item in enumerate(row):
|
82 |
+
results[labels[idx]] = float(item)
|
83 |
+
return results
|
84 |
+
|
85 |
+
# generate img input and text label output
|
86 |
+
image = gr.inputs.Image(shape=(300, 300), label="Upload Your Image Here")
|
87 |
+
label = gr.outputs.Label(num_top_classes=len(labels))
|
88 |
+
|
89 |
+
# generate and launch interface
|
90 |
+
interface = gr.Interface(fn=predict_image, inputs=image, outputs=label, article=article, theme='default', title=title, allow_flagging='never', description=description, examples=samples)
|
91 |
+
interface.launch()
|
categories.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
def_front ok_front
|
defective.jpeg
ADDED
model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:aacf5ddc63a89f828b15fbb0fe87b7c28fce1b521271fe7a2ac563809ac23a9c
|
3 |
+
size 134670360
|
okay.jpeg
ADDED
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
tensorflow>=2.6.1
|
2 |
+
keras>=2.6.0
|
3 |
+
yattag==1.14.0
|