SarowarSaurav commited on
Commit
90d35fd
1 Parent(s): 5fbb974

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -45
app.py CHANGED
@@ -1,54 +1,32 @@
1
  import gradio as gr
2
- import torch
3
- from transformers import ViTFeatureExtractor, ViTForImageClassification
4
  from PIL import Image
5
- import requests
6
 
7
- # Load a pre-trained Vision Transformer model from Hugging Face
8
- model_name = "nateraw/vit-base-patch16-224-in21k" # Replace with the model you've trained or a similar model
9
- model = ViTForImageClassification.from_pretrained(model_name)
10
- feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
11
 
12
- # Define the disease labels (placeholders)
13
- labels = {
14
- 0: "Healthy",
15
- 1: "Tobacco Mosaic Virus",
16
- 2: "Brown Spot",
17
- 3: "Frog Eye Leaf Spot",
18
- 4: "Other"
19
- }
20
-
21
- # Define a function for disease detection
22
- def detect_disease(image):
23
- # Preprocess the image
24
- inputs = feature_extractor(images=image, return_tensors="pt")
25
-
26
- # Run the model
27
- with torch.no_grad():
28
- outputs = model(**inputs)
29
- logits = outputs.logits
30
- predicted_class = logits.argmax().item()
31
 
32
- # Get disease name from label dictionary
33
- disease_name = labels.get(predicted_class, "Unknown Disease")
34
 
35
- return f"Disease Detected: {disease_name}"
36
-
37
- # Build Gradio Interface
38
- title = "Tobacco Leaf Disease Detection"
39
- description = """
40
- Upload or take a real-time picture of a tobacco leaf, and the app will detect the disease (if any).
41
- """
42
 
43
- # Create Gradio interface with camera and real-time processing
44
- iface = gr.Interface(
45
- fn=detect_disease,
46
- inputs=gr.Image(source="camera", type="pil", tool="editor"),
47
- outputs="text",
48
- title=title,
49
- description=description,
50
- live=True # Enables real-time processing
 
 
51
  )
52
 
53
- # Launch Gradio app
54
- iface.launch()
 
1
  import gradio as gr
2
+ from transformers import pipeline
 
3
  from PIL import Image
 
4
 
5
+ # Load the Hugging Face image classification pipeline with EfficientNetB0
6
+ # This model is generic for plant disease, so if you have a specific tobacco disease model, replace it accordingly
7
+ classifier = pipeline("image-classification", model="nateraw/efficientnet-b0")
 
8
 
9
+ def identify_disease(image):
10
+ # Use the classifier to predict the disease
11
+ predictions = classifier(image)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
+ # Format the output to include disease name and confidence score
14
+ results = [{"Disease": pred["label"], "Confidence": f"{pred['score'] * 100:.2f}%"} for pred in predictions]
15
 
16
+ # Return the uploaded image along with the results
17
+ return image, results
 
 
 
 
 
18
 
19
+ # Define Gradio interface
20
+ interface = gr.Interface(
21
+ fn=identify_disease,
22
+ inputs=gr.inputs.Image(type="pil"),
23
+ outputs=[
24
+ gr.outputs.Image(type="pil", label="Uploaded Image"),
25
+ gr.outputs.Dataframe(type="pandas", label="Predictions")
26
+ ],
27
+ title="Tobacco Plant Disease Identifier",
28
+ description="Upload an image of a tobacco plant, and this tool will identify the disease along with the confidence score."
29
  )
30
 
31
+ # Launch the app
32
+ interface.launch()