Spaces:
Sleeping
Sleeping
Upload app.py (#1)
Browse files- Upload app.py (12896f33a216402a1d302a6279a7123bbb1cc929)
Co-authored-by: Lontsi hermann <[email protected]>
app.py
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import tensorflow as tf
|
|
|
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from PIL import Image
|
| 5 |
from tensorflow.keras.utils import CustomObjectScope
|
| 6 |
from tensorflow.keras.layers.experimental.preprocessing import RandomHeight
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
with CustomObjectScope({'RandomHeight': RandomHeight}):
|
| 9 |
model_0 = tf.keras.models.load_model('bestmodel.h5')
|
| 10 |
|
|
@@ -21,14 +28,37 @@ if gpus:
|
|
| 21 |
except RuntimeError as e:
|
| 22 |
print(e)
|
| 23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def classify_image(inp):
|
| 26 |
# Convert to PIL Image if we have a numpy array
|
| 27 |
image = None
|
|
|
|
| 28 |
if isinstance(inp, np.ndarray):
|
| 29 |
image = Image.fromarray(inp)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
else:
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
# Resize image to 224x224
|
| 34 |
image = image.resize((224, 224), Image.Resampling.LANCZOS)
|
|
@@ -62,8 +92,6 @@ def classify_image(inp):
|
|
| 62 |
return output
|
| 63 |
|
| 64 |
|
| 65 |
-
image = gr.Image(height=224, width=224)
|
| 66 |
-
|
| 67 |
gr.Interface(
|
| 68 |
-
fn=classify_image, inputs=image, outputs="text",live=True,title="API de détection des images violentes",
|
| 69 |
-
).launch()
|
|
|
|
| 1 |
+
from io import BytesIO
|
| 2 |
import gradio as gr
|
| 3 |
import tensorflow as tf
|
| 4 |
+
import logging
|
| 5 |
+
import requests
|
| 6 |
import numpy as np
|
| 7 |
from PIL import Image
|
| 8 |
from tensorflow.keras.utils import CustomObjectScope
|
| 9 |
from tensorflow.keras.layers.experimental.preprocessing import RandomHeight
|
| 10 |
|
| 11 |
+
logging.basicConfig(
|
| 12 |
+
level=logging.WARNING,
|
| 13 |
+
format="%(asctime)s - %(levelname)s - %(message)s",
|
| 14 |
+
)
|
| 15 |
with CustomObjectScope({'RandomHeight': RandomHeight}):
|
| 16 |
model_0 = tf.keras.models.load_model('bestmodel.h5')
|
| 17 |
|
|
|
|
| 28 |
except RuntimeError as e:
|
| 29 |
print(e)
|
| 30 |
|
| 31 |
+
def clean_url(input_string):
|
| 32 |
+
if "http" in input_string:
|
| 33 |
+
return input_string[input_string.find("http"):]
|
| 34 |
+
return input_string
|
| 35 |
+
|
| 36 |
|
| 37 |
def classify_image(inp):
|
| 38 |
# Convert to PIL Image if we have a numpy array
|
| 39 |
image = None
|
| 40 |
+
logging.warning("entree dans ckassify_image")
|
| 41 |
if isinstance(inp, np.ndarray):
|
| 42 |
image = Image.fromarray(inp)
|
| 43 |
+
logging.warning("1")
|
| 44 |
+
|
| 45 |
+
if isinstance(inp, str) :
|
| 46 |
+
logging.warning("2")
|
| 47 |
+
|
| 48 |
+
inp = clean_url(inp)
|
| 49 |
+
response = requests.get(inp)
|
| 50 |
+
response.raise_for_status()
|
| 51 |
+
image = Image.open(BytesIO(response.content))
|
| 52 |
+
if isinstance(inp, str) and (inp.startswith("http://") or inp.startswith("https://")):
|
| 53 |
+
logging.warning("3")
|
| 54 |
+
response = requests.get(inp)
|
| 55 |
+
response.raise_for_status()
|
| 56 |
+
image = Image.open(BytesIO(response.content))
|
| 57 |
+
|
| 58 |
else:
|
| 59 |
+
logging.warning("4")
|
| 60 |
+
|
| 61 |
+
image = inp
|
| 62 |
|
| 63 |
# Resize image to 224x224
|
| 64 |
image = image.resize((224, 224), Image.Resampling.LANCZOS)
|
|
|
|
| 92 |
return output
|
| 93 |
|
| 94 |
|
|
|
|
|
|
|
| 95 |
gr.Interface(
|
| 96 |
+
fn=classify_image, inputs=gr.Text(label="Url de l image"), outputs="text",live=True,title="API de détection des images violentes",
|
| 97 |
+
).launch(share=True)
|