saritha commited on
Commit
8f6a2da
·
verified ·
1 Parent(s): 0fff4e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -42,7 +42,17 @@ class_names = ['BacterialBlights', 'Healthy', 'Mosaic', 'RedRot', 'Rust', 'Yello
42
  # Load AI response generator (using a local GPT pipeline or OpenAI's GPT-3/4 API)
43
  ai_pipeline = pipeline("text-generation", model="gpt2", tokenizer="gpt2")
44
 
45
- # Function to predict disease type from an image
 
 
 
 
 
 
 
 
 
 
46
  def predict_disease(image):
47
  # Apply transformations to the image
48
  img_tensor = transform(image).unsqueeze(0) # Add batch dimension
@@ -55,13 +65,13 @@ def predict_disease(image):
55
  # Get the predicted label
56
  predicted_label = class_names[predicted_class.item()]
57
 
58
- # Generate a detailed response for the detected disease
59
- prompt = f"The detected sugarcane disease is '{predicted_label}'. Provide detailed advice for managing this condition."
60
- ai_response = ai_pipeline(prompt, max_length=100, num_return_sequences=1, truncation=True)[0]['generated_text']
61
-
62
- # Post-process the AI response to ensure it ends with a complete sentence
63
- if not ai_response.endswith(('.', '!', '?')):
64
- ai_response = ai_response.rsplit('.', 1)[0] + '.'
65
 
66
  # Create a styled HTML output
67
  output_message = f"""
@@ -73,13 +83,13 @@ def predict_disease(image):
73
  if predicted_label != "Healthy":
74
  output_message += f"""
75
  <p style='font-size: 16px; color: #757575;'>
76
- {ai_response}
77
  </p>
78
  """
79
  else:
80
  output_message += f"""
81
  <p style='font-size: 16px; color: #757575;'>
82
- The sugarcane crop is <strong>healthy</strong>. Keep monitoring for potential risks.
83
  </p>
84
  """
85
 
 
42
  # Load AI response generator (using a local GPT pipeline or OpenAI's GPT-3/4 API)
43
  ai_pipeline = pipeline("text-generation", model="gpt2", tokenizer="gpt2")
44
 
45
+ # Knowledge base for sugarcane diseases (example data from the website)
46
+ knowledge_base = {
47
+ 'BacterialBlights': "Bacterial blights cause water-soaked lesions on leaves, leading to yellowing and withering. To manage, apply copper-based fungicides and ensure proper drainage.",
48
+ 'Mosaic': "Mosaic disease results in streaked and mottled leaves, reducing photosynthesis. Use disease-resistant varieties and control aphids to prevent spread.",
49
+ 'RedRot': "Red rot is identified by reddening and rotting of stalks. Remove infected plants and treat soil with appropriate fungicides.",
50
+ 'Rust': "Rust appears as orange pustules on leaves. Apply systemic fungicides and maintain optimal field conditions to reduce spread.",
51
+ 'Yellow': "Yellowing indicates nutrient deficiencies or initial disease stages. Test soil and provide balanced fertilizers.",
52
+ 'Healthy': "The sugarcane crop is healthy. Continue regular monitoring and good agronomic practices."
53
+ }
54
+
55
+ # Update the predict_disease function
56
  def predict_disease(image):
57
  # Apply transformations to the image
58
  img_tensor = transform(image).unsqueeze(0) # Add batch dimension
 
65
  # Get the predicted label
66
  predicted_label = class_names[predicted_class.item()]
67
 
68
+ # Retrieve response from knowledge base
69
+ if predicted_label in knowledge_base:
70
+ detailed_response = knowledge_base[predicted_label]
71
+ else:
72
+ # Fallback to AI-generated response
73
+ prompt = f"The detected sugarcane disease is '{predicted_label}'. Provide detailed advice for managing this condition."
74
+ detailed_response = ai_pipeline(prompt, max_length=100, num_return_sequences=1, truncation=True)[0]['generated_text']
75
 
76
  # Create a styled HTML output
77
  output_message = f"""
 
83
  if predicted_label != "Healthy":
84
  output_message += f"""
85
  <p style='font-size: 16px; color: #757575;'>
86
+ {detailed_response}
87
  </p>
88
  """
89
  else:
90
  output_message += f"""
91
  <p style='font-size: 16px; color: #757575;'>
92
+ {detailed_response}
93
  </p>
94
  """
95