lelouch0110 commited on
Commit
364b61e
·
verified ·
1 Parent(s): ab39072

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -20
app.py CHANGED
@@ -1,18 +1,17 @@
1
  from io import BytesIO
2
  import gradio as gr
3
  import tensorflow as tf
 
4
  import requests
5
  import numpy as np
6
  from PIL import Image
7
  from tensorflow.keras.utils import CustomObjectScope
8
  from tensorflow.keras.layers.experimental.preprocessing import RandomHeight
9
 
10
- import os
11
- from fastapi import FastAPI
12
- from fastapi.responses import JSONResponse
13
-
14
- app = FastAPI()
15
-
16
  with CustomObjectScope({'RandomHeight': RandomHeight}):
17
  model_0 = tf.keras.models.load_model('bestmodel.h5')
18
 
@@ -29,25 +28,37 @@ if gpus:
29
  except RuntimeError as e:
30
  print(e)
31
 
 
 
 
 
 
32
 
33
  def classify_image(inp):
34
- Warning(inp)
35
  # Convert to PIL Image if we have a numpy array
36
  image = None
 
37
  if isinstance(inp, np.ndarray):
38
  image = Image.fromarray(inp)
39
- Warning("Image is a numpy array")
40
- Warning(image)
 
 
 
 
 
 
 
41
  if isinstance(inp, str) and (inp.startswith("http://") or inp.startswith("https://")):
 
42
  response = requests.get(inp)
43
  response.raise_for_status()
44
  image = Image.open(BytesIO(response.content))
45
- Warning("Image is url")
46
- Warning(image)
47
  else:
48
- Warning("Image is ???")
49
- Warning(image)
50
- image = inp
51
 
52
  # Resize image to 224x224
53
  image = image.resize((224, 224), Image.Resampling.LANCZOS)
@@ -81,12 +92,6 @@ def classify_image(inp):
81
  return output
82
 
83
 
84
- @app.get("/predict2")
85
- async def predict_custom_controller(image_url: str):
86
- return JSONResponse(content=classify_image(image_url))
87
-
88
-
89
-
90
  gr.Interface(
91
  fn=classify_image, inputs=gr.Image(type="filepath",label="Image ou Url"), outputs="text",live=True,title="API de détection des images violentes",
92
  ).launch(share=True)
 
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.Image(type="filepath",label="Image ou Url"), outputs="text",live=True,title="API de détection des images violentes",
97
  ).launch(share=True)