Upload app.py
Browse files
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 |
|
@@ -27,6 +34,10 @@ def classify_image(inp):
|
|
27 |
image = None
|
28 |
if isinstance(inp, np.ndarray):
|
29 |
image = Image.fromarray(inp)
|
|
|
|
|
|
|
|
|
30 |
else:
|
31 |
image = inp
|
32 |
|
@@ -62,8 +73,12 @@ def classify_image(inp):
|
|
62 |
return output
|
63 |
|
64 |
|
65 |
-
|
|
|
|
|
|
|
|
|
66 |
|
67 |
gr.Interface(
|
68 |
-
fn=classify_image, inputs=
|
69 |
).launch()
|
|
|
1 |
import gradio as gr
|
2 |
import tensorflow as tf
|
3 |
+
import requests
|
4 |
import numpy as np
|
5 |
from PIL import Image
|
6 |
from tensorflow.keras.utils import CustomObjectScope
|
7 |
from tensorflow.keras.layers.experimental.preprocessing import RandomHeight
|
8 |
|
9 |
+
import os
|
10 |
+
from fastapi import FastAPI
|
11 |
+
from fastapi.responses import JSONResponse
|
12 |
+
|
13 |
+
app = FastAPI()
|
14 |
+
|
15 |
with CustomObjectScope({'RandomHeight': RandomHeight}):
|
16 |
model_0 = tf.keras.models.load_model('bestmodel.h5')
|
17 |
|
|
|
34 |
image = None
|
35 |
if isinstance(inp, np.ndarray):
|
36 |
image = Image.fromarray(inp)
|
37 |
+
if isinstance(inp, str) and (inp.startswith("http://") or inp.startswith("https://")):
|
38 |
+
response = requests.get(inp)
|
39 |
+
response.raise_for_status()
|
40 |
+
image = Image.open(BytesIO(response.content))
|
41 |
else:
|
42 |
image = inp
|
43 |
|
|
|
73 |
return output
|
74 |
|
75 |
|
76 |
+
@app.get("/predict2")
|
77 |
+
async def predict_custom_controller(image_url: str):
|
78 |
+
return JSONResponse(content=classify_image(image_url))
|
79 |
+
|
80 |
+
|
81 |
|
82 |
gr.Interface(
|
83 |
+
fn=classify_image, inputs=gr.Image(height=224, width=224,type="filepath",label="Image ou Url"), outputs="text",live=True,title="API de détection des images violentes",
|
84 |
).launch()
|