Update app.py
Browse files
app.py
CHANGED
@@ -1,41 +1,43 @@
|
|
1 |
-
|
2 |
-
import gradio as gr
|
3 |
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
# Load the image classification pipeline
|
6 |
-
try:
|
7 |
-
pipeline = pipeline(task="image-classification", model="Hemg/Melanoma-Cancer-Image-Classification")
|
8 |
-
except ValueError:
|
9 |
-
pipeline = None
|
10 |
-
|
11 |
-
def predict(input_img):
|
12 |
-
if pipeline is None:
|
13 |
-
result = {"error": "Model not available"}
|
14 |
-
else:
|
15 |
-
try:
|
16 |
-
# Use the pipeline to make predictions
|
17 |
-
predictions = pipeline(input_img)
|
18 |
-
# Process the predictions
|
19 |
-
result = {p["label"]: p["score"] for p in predictions}
|
20 |
-
# Check if the labels are "Benign" or "Malignant
|
21 |
-
if not any(label in result for label in ["Benign", "Malignant"]):
|
22 |
-
result = "This is out of context image"
|
23 |
-
except:
|
24 |
-
# If an exception occurs, return a default result
|
25 |
-
result = "no data provided!!"
|
26 |
-
|
27 |
-
# Return the input image and the result
|
28 |
-
return input_img, result
|
29 |
-
|
30 |
-
# Create a Gradio interface
|
31 |
-
gradio_app = gr.Interface(
|
32 |
-
predict,
|
33 |
-
inputs=gr.Image(label="Upload Image", sources=['upload', 'webcam'], type="pil"),
|
34 |
-
outputs=[gr.Image(label="Processed Image"), gr.Label(label="Result", num_top_classes=2)],
|
35 |
-
title="Benign or Malignant?"
|
36 |
-
)
|
37 |
-
|
38 |
-
# Launch the Gradio app
|
39 |
-
if __name__ == "__main__":
|
40 |
-
gradio_app.launch()
|
41 |
|
|
|
|
|
|
|
|
1 |
from transformers import pipeline
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
|
5 |
+
modelName = "Melanoma-Cancer-Image-Classification"
|
6 |
+
hfUser = "Hemg"
|
7 |
+
|
8 |
+
|
9 |
+
def prediction_function(inputFile):
|
10 |
+
# get user name of their hugging face
|
11 |
+
modelPath = hfUser + "/" + modelName
|
12 |
+
# takes some time
|
13 |
+
classifier = pipeline("image-classification", model=modelPath)
|
14 |
+
|
15 |
+
try:
|
16 |
+
result = classifier(inputFile)
|
17 |
+
predictions = dict()
|
18 |
+
labels = []
|
19 |
+
for eachLabel in result:
|
20 |
+
predictions[eachLabel["label"]] = eachLabel["score"]
|
21 |
+
labels.append(eachLabel["label"])
|
22 |
+
result = predictions
|
23 |
+
|
24 |
+
# Check if the image is out of context
|
25 |
+
if "out of context image" in result:
|
26 |
+
raise ValueError("Out of context image provided")
|
27 |
+
except Exception as e:
|
28 |
+
result = "no data provided!!"
|
29 |
+
|
30 |
+
return result
|
31 |
+
|
32 |
+
|
33 |
+
# change modelName parameter
|
34 |
+
def create_demo():
|
35 |
+
demo = gr.Interface(
|
36 |
+
fn=prediction_function,
|
37 |
+
inputs=gr.Image(type="pil"),
|
38 |
+
outputs=gr.Label(num_top_classes=2),
|
39 |
+
)
|
40 |
+
demo.launch(debug=True)
|
41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
+
create_demo()
|