File size: 14,485 Bytes
41b3c40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
from flask import Flask, render_template, request, jsonify, redirect, url_for, session
from keras.models import load_model
from PIL import Image, ImageOps
import numpy as np
import io
import base64
from groq import Groq
from deep_translator import GoogleTranslator
import os
from functools import wraps

app = Flask(__name__)
app.secret_key = 'agrovet_secret_key_2024'  # Change this to a random secret key

# Load model and labels (with error handling for missing files)
try:
    model = load_model("s3/keras_model.h5", compile=False)
    class_names = open("s3/labels.txt", "r").readlines()
    print("✓ Model loaded successfully")
    print(f"✓ Loaded {len(class_names)} disease classes")
except Exception as e:
    print(f"✗ Error loading model: {e}")
    model = None
    class_names = []

# Medication suggestions for specific diseases
medications = {
    "1 LUMPY SKIN": "Tips for Lumpy Skin Disease (LSD):"
                    "Isolation: Immediately isolate affected animals to prevent the spread of the disease to other livestock."
                    "Hygiene: Maintain a clean environment by disinfecting animal housing, feeding equipment, and water troughs regularly."
                    "Supportive Care: Ensure animals are well-fed with a nutritious diet to strengthen their immune systems. Providing fresh, clean water is essential."
                    "Reduce Stress: Keep the animals in a calm and stress-free environment. Stress can worsen disease symptoms."
                    "Monitor for Secondary Infections: Keep an eye on skin lesions and prevent further infection by cleaning any open wounds.",
    "4 RINGWORM": "Management Tips for Ringworm:"
                    "Isolate Infected Animals to prevent spread."
                    "Maintain Cleanliness by changing bedding and disinfecting equipment."
                    "Improve Nutrition with a balanced diet and supplements like zinc and vitamin A."
                    "Use Topical Antifungal Ointments like clotrimazole or sulfur dips."
                    "Sun Exposure can help kill fungi."
                    "Groom Regularly to remove scabs and infected fur."
                    "Reduce Stress by providing a comfortable environment."
                    "Monitor Other Animals for signs and consult a vet if needed.",
    "6 MASTITIS": "Tips and management for Mastitis"
                    "Frequent Milking: Regular milking helps prevent milk build-up and reduces the chances of infection."
                    "Proper Hygiene: Clean the udder and teats before and after milking to prevent bacterial entry. Use sanitized towels and gloves."
                    "Dry Cow Therapy: After the lactating period, administer dry cow therapy to prevent mastitis and keep the udder healthy."
                    "Diet Management: Ensure a balanced nutrition with adequate amounts of vitamins and minerals to maintain good udder health."
                    "Massaging: Gently massage the udder to improve circulation and promote milk flow, which can help reduce the risk of mastitis.",
    "3 SHEEP SCABIES": "Tips for Sheep Scabies (For Mouth Area)"
                        "Keep Sheep Clean: Regularly bathe the sheep in mild antiseptic solutions to help remove mites from the skin."
                        "Maintain Bedding: Change bedding frequently to prevent mite infestations. Clean the stalls with a disinfectant to kill any remaining mites."
                        "Improve Nutrition: Provide a balanced diet with minerals and vitamins that help strengthen the immune system and promote skin healing."
                        "Reduce Stress: Avoid overcrowding, and provide adequate space, ventilation, and quiet surroundings to reduce stress, which can worsen scabies."
                        "Skin Care: Apply soothing oils like neem oil or coconut oil to the affected areas, especially around the mouth, to help calm irritation and moisturize the skin."
                        "Monitor and Isolate: Isolate any affected animals to prevent the spread of the disease to healthy sheep.",
    "10 GREASY DISEASE": "Prevention and Management Tips for Greasy Disease:"
                        "Clean and Dry Conditions: Keep pens and bedding dry to prevent bacterial growth."
                        "Isolate: Separate infected pigs to avoid spreading."
                        "Boost Immunity: Provide a balanced diet with vitamins A, E, and omega-3 fatty acids."
                        "Skin Care: Clean affected areas with antiseptic and apply antibacterial ointments."
                        "Herbal Remedies: Use neem oil and turmeric paste for their antibacterial properties."
                        "Hydration: Ensure access to fresh, clean water."
                        "Monitor: Watch for early signs and isolate any affected animals."
}

# Initialize Groq client (Replace with your actual API key)
API_KEY = "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"
client = Groq(api_key=API_KEY)

def translate_text(text, target_language):
    """Translates text to the specified target language."""
    try:
        return GoogleTranslator(source="auto", target=target_language).translate(text)
    except:
        return text

@app.route("/")
def index():
    return redirect(url_for('login'))

@app.route("/login")
def login():
    return render_template("login.html")

@app.route("/signup")
def signup():
    return render_template("signup.html")

@app.route("/home")
def home():
    return render_template("home.html")

@app.route("/rearing-guidance")
def rearing_guidance():
    return render_template('s1.html')

@app.route('/result', methods=['POST'])
def result():
    # Get user input
    animal_type = request.form['animal_type']
    number_of_animals = int(request.form['number_of_animals'])
    
    # Map animal type and count to required features
    area_size, feed_type, feed_quantity, cost_in_inr, water_supply, hygiene_tips, seasonal_tips = get_animal_data(animal_type, number_of_animals)

    return render_template('s1.html',
                           area_size=area_size,
                           feed_type=feed_type,
                           feed_quantity=feed_quantity,
                           cost_in_inr=cost_in_inr,
                           water_supply=water_supply,
                           hygiene_tips=hygiene_tips,
                           seasonal_tips=seasonal_tips,
                           animal_type=animal_type,
                           number_of_animals=number_of_animals,
                           )

# Helper function to map animal type and count to required values
def get_animal_data(animal_type, count):
    if animal_type == 'Cow':
        area_size = 19.81 * count
        feed_type = 'Green Feed'
        feed_quantity = 24.85 * count
        cost_in_inr = 6652.15 * count
        water_supply = 68.52 * count
        hygiene_tips = 'Clean barn daily, vaccinate regularly'
        seasonal_tips = 'Summer: Provide shade and cool water, Winter: Ensure warmth, Rainy: Avoid damp areas.'
    elif animal_type == 'Buffalo':
        area_size = 24.58 * count
        feed_type = 'Dry Feed'
        feed_quantity = 31.45 * count
        cost_in_inr = 7483.52 * count
        water_supply = 81.23 * count
        hygiene_tips = 'Ensure proper ventilation'
        seasonal_tips = 'Summer: Provide shade and cool water, Winter: Provide shelter, Rainy: Dry bedding is essential.'
    elif animal_type == 'Pig':
        area_size = 15.25 * count
        feed_type = 'Concentrates'
        feed_quantity = 5.15 * count
        cost_in_inr = 4989.25 * count
        water_supply = 50.60 * count
        hygiene_tips = 'Use disinfectants weekly'
        seasonal_tips = 'Summer: Keep cool and dry, Winter: Ensure warmth, Rainy: Keep pens dry.'
    elif animal_type == 'Goat':
        area_size = 10.21 * count
        feed_type = 'Green Feed'
        feed_quantity = 3.25 * count
        cost_in_inr = 3325.60 * count
        water_supply = 20.55 * count
        hygiene_tips = 'Provide clean bedding'
        seasonal_tips = 'Summer: Provide fresh water and shade, Winter: Ensure dry and warm shelter, Rainy: Avoid damp areas.'
    elif animal_type == 'Sheep':
        area_size = 12.50 * count
        feed_type = 'Green Feed'
        feed_quantity = 4.75 * count
        cost_in_inr = 3741.75 * count
        water_supply = 25.80 * count
        hygiene_tips = 'Check for parasites regularly'
        seasonal_tips = 'Summer: Provide plenty of water, Winter: Keep warm with shelter, Rainy: Keep dry and prevent wool rot.'
    elif animal_type == 'Poultry':
        area_size = 1.5 * count
        feed_type = 'Grain-based Feed'
        feed_quantity = 0.25 * count
        cost_in_inr = 120 * count
        water_supply = 0.5 * count
        hygiene_tips = 'Keep the coop clean and free from pests'
        seasonal_tips = 'Summer: Ensure proper ventilation, Winter: Provide heat and protection, Rainy: Keep the coop dry.'
    elif animal_type == 'Bee Hiving':
        area_size = 10.25  # In square feet, for a beekeeping area
        feed_type = 'Sugar Syrup'
        feed_quantity = 0.5 * count  # For hives
        cost_in_inr = 1500.55 * count  # Cost per hive
        water_supply = 0.25 * count  # Water requirement per hive
        hygiene_tips = 'Check for pests like mites and wax moths'
        seasonal_tips = 'Summer: Provide ample water sources, Winter: Ensure the bees have enough food stores, Rainy: Avoid disturbances.'
    elif animal_type == 'Sericulture':
        area_size = 4.85 * count  # In square feet, for silkworms
        feed_type = 'Mulberry Leaves'
        feed_quantity = 0.1 * count  # Per silkworm
        cost_in_inr = 198.25 * count  # Cost per silkworm
        water_supply = 0.2 * count  # Water per silkworm
        hygiene_tips = 'Maintain clean environment, avoid humidity'
        seasonal_tips = 'Summer: Maintain cool and dry conditions, Winter: Keep warm, Rainy: Ensure adequate airflow.'

    return area_size, feed_type, feed_quantity, cost_in_inr, water_supply, hygiene_tips, seasonal_tips

@app.route("/ai-chatbot")
def ai_chatbot():
    return render_template("s2.html")

@app.route("/chat", methods=["POST"])
def chat():
    data = request.json
    if data is None:
        return jsonify({"response": "Invalid request format. Please send JSON data."}), 400
    
    user_message = data.get("message")  # Get user's message
    target_language = data.get("language", "en")  # Get target language (default: English)

    try:
        # Translate user input to English before sending it to the chatbot
        user_message_en = translate_text(user_message, "en")

        # Use Groq API to get a response from the chatbot
        response = client.chat.completions.create(
            messages=[{"role": "user", "content": user_message_en}],
            model="llama-3.3-70b-versatile"  # Replace with the correct Groq model
        )

        bot_response_en = response.choices[0].message.content  # Extract chatbot's response

        # Translate bot response back to the selected language
        bot_response_translated = translate_text(bot_response_en, target_language)

        return jsonify({"response": bot_response_translated})

    except Exception as e:
        return jsonify({"response": "Sorry, there was an error. Try again later!"})

@app.route("/disease-detection", methods=["GET", "POST"])
def disease_detection():
    prediction = None
    confidence = None
    medication = None
    img_data = None  # Variable to store image data for displaying on the webpage
    if request.method == "POST":
        # Check if image file is provided
        if "file" not in request.files:
            return render_template("s3.html", error="No file part")

        file = request.files["file"]
        if file.filename == "":
            return render_template("s3.html", error="No selected file")

        if file and model is not None:
            # Process the image - read file stream properly
            image = Image.open(io.BytesIO(file.read())).convert("RGB")

            # Resize the image to 224x224 and crop from the center
            size = (224, 224)
            image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)

            # Convert image to numpy array and normalize
            image_array = np.asarray(image)
            normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1

            # Create the array of the right shape to feed into the keras model
            data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
            data[0] = normalized_image_array

            # Predict the model
            prediction = model.predict(data)
            index = np.argmax(prediction)
            class_name = class_names[index].strip()  # Strip any leading/trailing whitespace
            confidence_score = prediction[0][index]

            # Suggest medication if disease detected
            medication = medications.get(class_name, None)

            # Convert image to base64 for rendering on the webpage
            img_byte_array = io.BytesIO()
            image.save(img_byte_array, format="PNG")
            img_data = base64.b64encode(img_byte_array.getvalue()).decode("utf-8")

            return render_template("s3.html", prediction=class_name, confidence=confidence_score, medication=medication, img_data=img_data)
        else:
            return render_template("s3.html", error="Model not available or file processing error")

    return render_template("s3.html", prediction=prediction, confidence=confidence, medication=medication, img_data=img_data)

@app.route("/information")
def information():
    return render_template("information.html")

@app.route("/latest-innovations")
def latest_innovations():
    return render_template("latest-innovations.html")

@app.route("/govt-schemes")
def govt_schemes():
    return render_template("govt-schemes.html")

@app.route("/veterinary_map")
def veterinary_map():
    return render_template("veterinary_map.html")

@app.route("/logout")
def logout():
    return redirect(url_for('login'))

if __name__ == "__main__":
    app.run(debug=True)