JbIPS
commited on
Commit
·
642a5f0
1
Parent(s):
6a64fc1
Initial commit
Browse files- app.py +68 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import requests
|
| 3 |
+
import numpy as np
|
| 4 |
+
import streamlit as st
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
from huggingface_hub import hf_hub_url, cached_download
|
| 7 |
+
from tf.keras.preprocessing.image import img_to_array
|
| 8 |
+
from tf.keras.models import load_model
|
| 9 |
+
from PIL import Image
|
| 10 |
+
from io import BytesIO
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def make_prediction(url, model, race_names):
|
| 14 |
+
response = requests.get(url)
|
| 15 |
+
img = Image.open(BytesIO(response.content)).resize((180, 180))
|
| 16 |
+
img_array = img_to_array(img)
|
| 17 |
+
img_array = tf.expand_dims(img_array, 0) # Create batch axis
|
| 18 |
+
|
| 19 |
+
predictions = model.predict(img_array)
|
| 20 |
+
|
| 21 |
+
top4 = predictions.argsort()[0, -1:-5:-1]
|
| 22 |
+
|
| 23 |
+
breakdown = []
|
| 24 |
+
for race, acc in zip(np.array(race_names)[top4], predictions[0, top4]):
|
| 25 |
+
breakdown.append(f'{race} at {acc:.2%}')
|
| 26 |
+
return breakdown
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def main():
|
| 30 |
+
|
| 31 |
+
race_file = open('race_names.json', 'r')
|
| 32 |
+
race_names = json.load(race_file)
|
| 33 |
+
# Load model
|
| 34 |
+
model = load_model(cached_download(
|
| 35 |
+
hf_hub_url('JbIPS/DogRace', 'saved_model')
|
| 36 |
+
))
|
| 37 |
+
|
| 38 |
+
st.set_page_config("Who let's the dogs out")
|
| 39 |
+
st.title('Quelle est ta race de chien totem ?')
|
| 40 |
+
|
| 41 |
+
st.text('''
|
| 42 |
+
Pour découvrir ta race de chien, colle l'adresse d'une photo.
|
| 43 |
+
''')
|
| 44 |
+
url = st.text_input('URL de la photo')
|
| 45 |
+
predict_btn = st.button('Prédire')
|
| 46 |
+
if predict_btn:
|
| 47 |
+
pred = make_prediction(url, model, race_names)
|
| 48 |
+
main_race = pred[0].split(' at')[0].lower()
|
| 49 |
+
main_race = '/'.join(reversed(main_race.replace('-', '').split(' ')))
|
| 50 |
+
if main_race.startswith('husky'):
|
| 51 |
+
main_race = main_race.split('/')[0]
|
| 52 |
+
col1, col2 = st.columns(2)
|
| 53 |
+
with col1:
|
| 54 |
+
st.image(url)
|
| 55 |
+
with col2:
|
| 56 |
+
response = requests.get(url=f'https://dog.ceo/api/breed/{main_race}/images/random').json()
|
| 57 |
+
if response['status'] == 'success':
|
| 58 |
+
st.image(response['message'])
|
| 59 |
+
else:
|
| 60 |
+
st.text(main_race)
|
| 61 |
+
st.text(response)
|
| 62 |
+
st.write('Les races qui te correspondent le plus sont :')
|
| 63 |
+
for race in pred:
|
| 64 |
+
st.write(race)
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
if __name__ == '__main__':
|
| 68 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit==1.5.1
|
| 2 |
+
huggingface==0.5.1
|
| 3 |
+
tensorflow==2.8.0
|
| 4 |
+
pillow==9.0.1
|
| 5 |
+
|