File size: 1,493 Bytes
8d12dce
e6de937
2cf69dd
d4d4818
54ca5bc
e6de937
2cf69dd
 
 
a4a1608
2cf69dd
 
d4d4818
a4a1608
 
 
2cf69dd
 
 
 
714d90d
d4d4818
 
54ca5bc
d4d4818
 
 
 
 
 
 
 
 
 
54ca5bc
 
d4d4818
 
54ca5bc
d4d4818
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import gradio as gr
from transformers import pipeline
from huggingface_hub import hf_hub_download
import json
import numpy as np

# Define model repository details
model_repo = "KhadijaAsehnoune12/LeafDiseaseDetector"
config_filename = "config.json"

# Download the config.json file from the model repository
config_path = hf_hub_download(repo_id=model_repo, filename=config_filename)

# Load the config.json file
with open(config_path, "r", encoding="utf-8") as f:
    config = json.load(f)

# Initialize the pipeline
pipe = pipeline(task="image-classification", model=model_repo)

# Define a custom prediction function
def predict(image):
    # Get the predictions from the pipeline
    predictions = pipe(image)
    # Get the predicted label index
    predicted_index = predictions[0]['label']
    # Map the index to the corresponding disease name using id2label
    label_name = config["id2label"][str(predicted_index)]
    # Optionally, you can add the confidence score as well
    confidence_score = predictions[0]['score']
    return f"{label_name} ({confidence_score:.2f})"

# Create Gradio interface
iface = gr.Interface(fn=predict,
                     inputs=gr.inputs.Image(type="numpy"),
                     outputs=gr.outputs.Textbox(),
                     title="Orange Disease Image Classification",
                     description="Detect diseases in orange leaves and fruits.",
                     examples=[np.random.rand(224, 224, 3)])

# Launch the app
iface.launch()