Spaces:
Runtime error
Runtime error
Isabel Gwara
commited on
Commit
·
73cca1b
1
Parent(s):
f2a948e
add interface and file reading to app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from tensorflow.keras.models import load_model
|
| 2 |
-
import gradio as gr
|
| 3 |
import numpy as np
|
| 4 |
-
|
| 5 |
-
import h5py
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
model = load_model('model.h5') # single file model
|
| 8 |
labels = ['cheetah', 'hyena', 'jaguar', 'tiger'] # will need to be loaded from file in the order of your directories
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def preprocess(image):
|
| 11 |
image = np.array(image) / 255
|
| 12 |
# image = transform.resize(image, (300, 300, 3))
|
|
@@ -25,4 +102,4 @@ def match_prediction_with_label(test_img):
|
|
| 25 |
image = gr.inputs.Image(shape=(300, 300), label="Upload Your Image Here")
|
| 26 |
label = gr.outputs.Label(num_top_classes=4)
|
| 27 |
|
| 28 |
-
gr.Interface(fn=match_prediction_with_label, inputs=image, outputs=label, capture_session=True).launch(share=True, debug=True)
|
|
|
|
| 1 |
+
### ----------------------------- ###
|
| 2 |
+
### libraries ###
|
| 3 |
+
### ----------------------------- ###
|
| 4 |
+
|
| 5 |
from tensorflow.keras.models import load_model
|
| 6 |
+
import gradio as gr # remove later
|
| 7 |
import numpy as np
|
| 8 |
+
from yattag import Doc
|
| 9 |
+
import h5py # remove later
|
| 10 |
+
|
| 11 |
+
### -------------------------------- ###
|
| 12 |
+
### model loading ###
|
| 13 |
+
### -------------------------------- ###
|
| 14 |
|
| 15 |
model = load_model('model.h5') # single file model
|
| 16 |
labels = ['cheetah', 'hyena', 'jaguar', 'tiger'] # will need to be loaded from file in the order of your directories
|
| 17 |
|
| 18 |
+
## -------------------------------- ###
|
| 19 |
+
### file reading ###
|
| 20 |
+
### -------------------------------- ###
|
| 21 |
+
# placeholders in case info.txt does not exist
|
| 22 |
+
placeholder = "please create an info.txt to customize this text"
|
| 23 |
+
title = bkgd = data_collection = priv_cons = bias_cons = ident_cons = img_src = membs = placeholder
|
| 24 |
+
description = "An AI project created by [name], [name], and [name]"
|
| 25 |
+
# check if info.txt is present
|
| 26 |
+
if os.path.isfile("info.txt"):
|
| 27 |
+
# open info.txt in read mode
|
| 28 |
+
info = open("info.txt", "r")
|
| 29 |
+
|
| 30 |
+
# each line to a string
|
| 31 |
+
title = info.readline()
|
| 32 |
+
bkgd = info.readline()
|
| 33 |
+
data_collection = info.readline()
|
| 34 |
+
priv_cons = info.readline()
|
| 35 |
+
bias_cons = info.readline()
|
| 36 |
+
ident_cons = info.readline()
|
| 37 |
+
img_src = info.readline()
|
| 38 |
+
membs = info.readline()
|
| 39 |
+
|
| 40 |
+
# close file
|
| 41 |
+
info.close()
|
| 42 |
+
|
| 43 |
+
# use yattag library to generate html
|
| 44 |
+
doc, tag, text, line = Doc().ttl()
|
| 45 |
+
# create html based on info.txt
|
| 46 |
+
with tag('div'):
|
| 47 |
+
with tag('div', klass='my-div'):
|
| 48 |
+
line('h2', 'Project Background')
|
| 49 |
+
line('p', bkgd)
|
| 50 |
+
with tag('div', klass='my-div'):
|
| 51 |
+
line('h2', 'Data Collection')
|
| 52 |
+
line('p', data_collection)
|
| 53 |
+
with tag('div', klass='my-div'):
|
| 54 |
+
line('h2', 'Ethical Considerations')
|
| 55 |
+
with tag('ul'):
|
| 56 |
+
line('li', priv_cons)
|
| 57 |
+
line('li', bias_cons)
|
| 58 |
+
line('li', ident_cons)
|
| 59 |
+
with tag('div', klass='my-div'):
|
| 60 |
+
line('h2', 'Our Team')
|
| 61 |
+
line('p', membs)
|
| 62 |
+
doc.stag('img', src=img_src)
|
| 63 |
+
|
| 64 |
+
my_css = '''
|
| 65 |
+
.my-div {
|
| 66 |
+
border: 2px solid black;
|
| 67 |
+
text-align: center;
|
| 68 |
+
margin: 10px;
|
| 69 |
+
padding: 5%;
|
| 70 |
+
}
|
| 71 |
+
ul {
|
| 72 |
+
display: inline-block;
|
| 73 |
+
text-align: left;
|
| 74 |
+
}
|
| 75 |
+
img {
|
| 76 |
+
display: block;
|
| 77 |
+
margin: auto;
|
| 78 |
+
}
|
| 79 |
+
.description {
|
| 80 |
+
text-align: center;
|
| 81 |
+
}
|
| 82 |
+
'''
|
| 83 |
+
### ------------------------------- ###
|
| 84 |
+
### interface creation ###
|
| 85 |
+
### ------------------------------- ###
|
| 86 |
+
|
| 87 |
def preprocess(image):
|
| 88 |
image = np.array(image) / 255
|
| 89 |
# image = transform.resize(image, (300, 300, 3))
|
|
|
|
| 102 |
image = gr.inputs.Image(shape=(300, 300), label="Upload Your Image Here")
|
| 103 |
label = gr.outputs.Label(num_top_classes=4)
|
| 104 |
|
| 105 |
+
gr.Interface(fn=match_prediction_with_label, inputs=image, outputs=label, capture_session=True, article=doc.getvalue(), css=my_css, theme='huggingface', title=title, allow_flagging=False, description=description).launch(share=True, debug=True)
|