KhadijaAsehnoune12 commited on
Commit
54ca5bc
1 Parent(s): 4755ddc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -27
app.py CHANGED
@@ -2,6 +2,7 @@ import gradio as gr
2
  from transformers import pipeline
3
  from huggingface_hub import hf_hub_download
4
  import json
 
5
 
6
  # Define model repository details
7
  model_repo = "KhadijaAsehnoune12/LeafDiseaseDetector"
@@ -18,30 +19,9 @@ with open(config_path, "r", encoding="utf-8") as f:
18
  pipe = pipeline(task="image-classification", model=model_repo)
19
 
20
  # Define a custom prediction function
21
- import numpy as np
22
-
23
- from PIL import Image
24
- from io import BytesIO
25
- import numpy as np
26
- import base64
27
-
28
  def predict(image):
29
- # Check if the input image is a base64 encoded string
30
- if isinstance(image, str):
31
- # Decode the base64 encoded image string and convert it to a PIL image object
32
- image_data = BytesIO(base64.b64decode(image))
33
- pil_image = Image.open(image_data)
34
- # Convert the PIL image to a numpy array
35
- image_np = np.array(pil_image)
36
- elif isinstance(image, np.ndarray):
37
- # If the input image is already a numpy array, use it directly
38
- image_np = image
39
- else:
40
- # If the input is neither a base64 encoded string nor a numpy array, raise an error
41
- raise ValueError("Input image must be either a base64 encoded string or a numpy array.")
42
-
43
  # Get the predictions from the pipeline
44
- predictions = pipe(image_np)
45
  # Get the predicted label index
46
  predicted_index = predictions[0]['label']
47
  # Map the index to the corresponding disease name using id2label
@@ -50,15 +30,13 @@ def predict(image):
50
  confidence_score = predictions[0]['score']
51
  return f"{label_name} ({confidence_score:.2f})"
52
 
53
-
54
-
55
  # Create Gradio interface
56
  iface = gr.Interface(fn=predict,
57
- inputs=gr.Image(type="numpy"),
58
- outputs=gr.Textbox(),
59
  title="Orange Disease Image Classification",
60
  description="Detect diseases in orange leaves and fruits.",
61
- examples=['MoucheB.jpg', 'verdissement.jpg'])
62
 
63
  # Launch the app
64
  iface.launch()
 
2
  from transformers import pipeline
3
  from huggingface_hub import hf_hub_download
4
  import json
5
+ import numpy as np
6
 
7
  # Define model repository details
8
  model_repo = "KhadijaAsehnoune12/LeafDiseaseDetector"
 
19
  pipe = pipeline(task="image-classification", model=model_repo)
20
 
21
  # Define a custom prediction function
 
 
 
 
 
 
 
22
  def predict(image):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  # Get the predictions from the pipeline
24
+ predictions = pipe(image)
25
  # Get the predicted label index
26
  predicted_index = predictions[0]['label']
27
  # Map the index to the corresponding disease name using id2label
 
30
  confidence_score = predictions[0]['score']
31
  return f"{label_name} ({confidence_score:.2f})"
32
 
 
 
33
  # Create Gradio interface
34
  iface = gr.Interface(fn=predict,
35
+ inputs=gr.inputs.Image(type="numpy"),
36
+ outputs=gr.outputs.Textbox(),
37
  title="Orange Disease Image Classification",
38
  description="Detect diseases in orange leaves and fruits.",
39
+ examples=[np.random.rand(224, 224, 3)])
40
 
41
  # Launch the app
42
  iface.launch()