Spaces:
Build error
Build error
Commit ·
2ed5309
1
Parent(s): 10618ca
Started develop API. Upload images for ex. and upload model.
Browse files- .gitignore +1 -0
- app.py +96 -0
- example_images/01_test.jpg +0 -0
- example_images/02_test.jpg +0 -0
- example_images/03_test.jpg +0 -0
- example_images/04_test.jpg +0 -0
- requirements.txt +5 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
*.keras
|
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# For loading files
|
| 4 |
+
from joblib import dump, load
|
| 5 |
+
|
| 6 |
+
# Model hub
|
| 7 |
+
import tensorflow_hub as hub
|
| 8 |
+
|
| 9 |
+
# Neural networks
|
| 10 |
+
import tensorflow as tf
|
| 11 |
+
from tensorflow import keras
|
| 12 |
+
from tensorflow.keras import layers
|
| 13 |
+
from keras.applications.vgg16 import preprocess_input
|
| 14 |
+
|
| 15 |
+
# Image processing
|
| 16 |
+
import PIL
|
| 17 |
+
|
| 18 |
+
#------------------------------------------
|
| 19 |
+
|
| 20 |
+
# Loading files
|
| 21 |
+
path = './trained_models/'
|
| 22 |
+
filename_model_VGG16 = 'vgg16_best.keras'
|
| 23 |
+
example_images_path = './example_images'
|
| 24 |
+
|
| 25 |
+
# Loading model
|
| 26 |
+
model = keras.models.load_model(path + filename_model_VGG16)
|
| 27 |
+
|
| 28 |
+
# Defining parameters
|
| 29 |
+
breed_names_norm = [Chihuahua, papillon, beagle,
|
| 30 |
+
Yorkshire_terrier, Australian_terrier,
|
| 31 |
+
Scotch_terrier, golden_retriever,
|
| 32 |
+
malinois, kelpie, Doberman, miniature_pinscher,
|
| 33 |
+
Great_Dane, Pomeranian, standard_poodle, Mexican_hairless]
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# Importing stopwords
|
| 37 |
+
with open('./stopwords/stopwords.txt') as file:
|
| 38 |
+
my_stopwords = {line.rstrip() for line in file}
|
| 39 |
+
|
| 40 |
+
# Function definitions
|
| 41 |
+
|
| 42 |
+
def load_img_path(img_path, target_size, show=False):
|
| 43 |
+
"""
|
| 44 |
+
Loads an image in jpg format into PIL format from a path.
|
| 45 |
+
"""
|
| 46 |
+
img = keras.preprocessing.image.load_img(
|
| 47 |
+
img_path, target_size=target_size
|
| 48 |
+
)
|
| 49 |
+
if show == True:
|
| 50 |
+
img.show()
|
| 51 |
+
|
| 52 |
+
return img
|
| 53 |
+
|
| 54 |
+
def predict(model, img, show=False):
|
| 55 |
+
"""
|
| 56 |
+
Returns a dictionnary: predicted_breeds, where the
|
| 57 |
+
keys are [1,2,3] for the first, second and third more probable
|
| 58 |
+
breed for the dog image. Each value is a dictionnary with keys
|
| 59 |
+
['idx', 'name', 'confidence'] and their corresponding values.
|
| 60 |
+
|
| 61 |
+
Parameters:
|
| 62 |
+
img: returned by the function load_img_path
|
| 63 |
+
"""
|
| 64 |
+
img_array = keras.preprocessing.image.img_to_array(img)
|
| 65 |
+
img_array = tf.expand_dims(img_array, 0) # Creates a batch axis
|
| 66 |
+
|
| 67 |
+
predictions = model.predict(img_array, verbose=0)
|
| 68 |
+
scores = predictions[0]
|
| 69 |
+
|
| 70 |
+
# Keep the 3 more probable breeds
|
| 71 |
+
predicted_breeds = {}
|
| 72 |
+
for i in [1,2,3]:
|
| 73 |
+
idx = scores.argsort()[-i] # First breed is the last proba when sorted
|
| 74 |
+
name = breed_names_norm[idx]
|
| 75 |
+
confidence = round(scores[idx]*100,2)
|
| 76 |
+
predicted_breeds[i] = {'idx':idx,'name':name,'confidence':confidence}
|
| 77 |
+
|
| 78 |
+
return predicted_breeds
|
| 79 |
+
|
| 80 |
+
# --------------------------------------------------
|
| 81 |
+
|
| 82 |
+
examples = [
|
| 83 |
+
["Jquery/Javascript Opacity animation with scroll <p>I'm looking to change the opacity on an object (and have the transition be animated) based on a users scroll.\nexample(http://davegamache.com/)</p>\n\n<p>I've searched everywhere\nlike here, but it ends up pointing me to the waypoints plugin (http://stackoverflow.com/questions/6316757/opacity-based-on-scroll-position)</p>\n\n<p>I've implemented the [waypoints][1] plugin and have the object fading once it's higher than 100px. [Using the offet attribute] but would like to basically control the opacity of an object and have the animation be visible like the above example.</p>\n\n<p>I've searched all over- this is my last resort.\nAny help is greatly appreciated.</p>\n"],
|
| 84 |
+
['Setting cross-domain cookies in Safari <p>I have to call domain A.com (which sets the cookies with http) from domain B.com.\nAll I do on domain B.com is (javascript): </p>\n\n<pre><code>var head = document.getElementsByTagName("head")[0];\nvar script = document.createElement("script");\nscript.src = "A.com/setCookie?cache=1231213123";\nhead.appendChild(script);\n</code></pre>\n\n<p>This sets the cookie on A.com on every browser I\'ve tested, except Safari.\nAmazingly this works in IE6, even without the P3P headers.</p>\n\n<p>Is there any way to make this work in Safari?</p>\n'],
|
| 85 |
+
['Database migrations for SQL Server <p>I need a database migration framework for SQL Server, capable of managing both schema changes and data migrations.</p>\n\n<p>I guess I am looking for something similar to django\'s <a href="http://south.aeracode.org/" rel="noreferrer">South</a> framework here.</p>\n\n<p>Given the fact that South is tightly coupled with django\'s ORM, and the fact that there\'s so many ORMs for SQL Server I guess having just a generic migration framework, enabling you to write and execute in controlled and sequential manner SQL data/schema change scripts should be sufficient.</p>\n'],
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
demo = gr.Interface(fn=tag_suggestion,
|
| 89 |
+
inputs="text",
|
| 90 |
+
outputs=["text"],
|
| 91 |
+
examples=examples)
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
demo.launch()
|
| 96 |
+
|
example_images/01_test.jpg
ADDED
|
example_images/02_test.jpg
ADDED
|
example_images/03_test.jpg
ADDED
|
example_images/04_test.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
joblib
|
| 3 |
+
tensorflow
|
| 4 |
+
tensorflow_hub
|
| 5 |
+
scikit-learn
|