[python] sdsewwewqe
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- import string
- import bcrypt
- from flask import Flask, redirect, render_template, url_for, request, Markup
- from flask_sqlalchemy import SQLAlchemy
- from flask_login import UserMixin, login_user, LoginManager, login_required, logout_user, current_user
- from wtforms import StringField, PasswordField, SubmitField
- from wtforms.validators import InputRequired, Length, ValidationError
- from flask_wtf import FlaskForm
- from flask_bcrypt import Bcrypt
- from datetime import datetime
- import requests
- import numpy as np
- import pandas as pd
- import config
- import pickle
- import io
- import torch
- from torchvision import transforms
- from PIL import Image
- from utils.model import ResNet9
- from utils.fertilizer import fertilizer_dic
- from utils.disease import disease_dic
- # -------------------------LOADING THE TRAINED MODELS -----------------------------------------------
- # Loading crop recommendation model
- crop_recommendation_model_path = 'models/RandomForest.pkl'
- crop_recommendation_model = pickle.load(
- open(crop_recommendation_model_path, 'rb'))
- # Loading plant disease classification model
- disease_classes = ['Apple___Apple_scab',
- 'Apple___Black_rot',
- 'Apple___Cedar_apple_rust',
- 'Apple___healthy',
- 'Blueberry___healthy',
- 'Cherry_(including_sour)___Powdery_mildew',
- 'Cherry_(including_sour)___healthy',
- 'Corn_(maize)___Cercospora_leaf_spot Gray_leaf_spot',
- 'Corn_(maize)___Common_rust_',
- 'Corn_(maize)___Northern_Leaf_Blight',
- 'Corn_(maize)___healthy',
- 'Grape___Black_rot',
- 'Grape___Esca_(Black_Measles)',
- 'Grape___Leaf_blight_(Isariopsis_Leaf_Spot)',
- 'Grape___healthy',
- 'Orange___Haunglongbing_(Citrus_greening)',
- 'Peach___Bacterial_spot',
- 'Peach___healthy',
- 'Pepper,_bell___Bacterial_spot',
- 'Pepper,_bell___healthy',
- 'Potato___Early_blight',
- 'Potato___Late_blight',
- 'Potato___healthy',
- 'Raspberry___healthy',
- 'Soybean___healthy',
- 'Squash___Powdery_mildew',
- 'Strawberry___Leaf_scorch',
- 'Strawberry___healthy',
- 'Tomato___Bacterial_spot',
- 'Tomato___Early_blight',
- 'Tomato___Late_blight',
- 'Tomato___Leaf_Mold',
- 'Tomato___Septoria_leaf_spot',
- 'Tomato___Spider_mites Two-spotted_spider_mite',
- 'Tomato___Target_Spot',
- 'Tomato___Tomato_Yellow_Leaf_Curl_Virus',
- 'Tomato___Tomato_mosaic_virus',
- 'Tomato___healthy']
- # disease prediction
- disease_model_path = 'models/plant_disease_model.pth'
- disease_model = ResNet9(3, len(disease_classes))
- disease_model.load_state_dict(torch.load(
- disease_model_path, map_location=torch.device('cpu')))
- disease_model.eval()
- def weather_fetch(city_name):
- """
- Fetch and returns the temperature and humidity of a city
- :params: city_name
- :return: temperature, humidity
- """
- api_key = config.weather_api_key
- base_url = "http://api.openweathermap.org/data/2.5/weather?"
- complete_url = base_url + "appid=" + api_key + "&q=" + city_name
- response = requests.get(complete_url)
- x = response.json()
- if x["cod"] != "404":
- y = x["main"]
- temperature = round((y["temp"] - 273.15), 2)
- humidity = y["humidity"]
- return temperature, humidity
- else:
- return None
- def predict_image(img, model=disease_model):
- """
- Transforms image to tensor and predicts disease label
- :params: image
- :return: prediction (string)
- """
- transform = transforms.Compose([
- transforms.Resize(256),
- transforms.ToTensor(),
- ])
- image = Image.open(io.BytesIO(img))
- img_t = transform(image)
- img_u = torch.unsqueeze(img_t, 0)
- # Get predictions from model
- yb = model(img_u)
- # Pick index with highest probability
- _, preds = torch.max(yb, dim=1)
- prediction = disease_classes[preds[0].item()]
- # Retrieve the class label
- return prediction
- app = Flask(__name__)
- db = SQLAlchemy(app)
- bcrypt = Bcrypt(app)
- app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///database.db"
- app.config["SECRET_KEY"] = 'thisissecretkey'
- login_manager = LoginManager()
- login_manager.init_app(app)
- login_manager.login_view = "login"
- @login_manager.user_loader
- def load_user(user_id):
- return User.query.get(int(user_id))
- class User(db.Model,UserMixin):
- id = db.Column(db.Integer,primary_key=True)
- username = db.Column(db.String(20), nullable=False, unique=True)
- password = db.Column(db.String(80), nullable=False)
- crop_results = db.relationship('CropResult', backref='user', lazy=True)
- class UserAdmin(db.Model,UserMixin):
- id = db.Column(db.Integer,primary_key=True)
- username = db.Column(db.String(20), nullable=False, unique=True)
- password = db.Column(db.String(80), nullable=False)
- class RegisterForm(FlaskForm):
- username=StringField(validators=[InputRequired(),Length(min=5,max=20)],render_kw={"placeholder":"username"})
- password=PasswordField(validators=[InputRequired(),Length(min=5,max=20)],render_kw={"placeholder":"password"})
- submit = SubmitField("Register")
- def validate_username(self, username):
- existing_user_username = User.query.filter_by(username=username.data).first()
- if existing_user_username:
- raise ValidationError("That username already exist. please choose different one.")
- class LoginForm(FlaskForm):
- username=StringField(validators=[InputRequired(),Length(min=5,max=20)],render_kw={"placeholder":"username"})
- password=PasswordField(validators=[InputRequired(),Length(min=5,max=20)],render_kw={"placeholder":"password"})
- submit = SubmitField("Login")
- https://www.madisonjournal.com/sites/default/files/inline-files/DSDSD.pdf
- https://www.madisonjournal.com/sites/default/files/inline-files/Untitled%20document.pdf
- https://www.madisonjournal.com/sites/default/files/inline-files/Gran%20Turismo_0.pdf
- https://www.madisonjournal.com/sites/default/files/inline-files/Sound%20of%20Freedom_1.pdf
- https://www.madisonjournal.com/sites/default/files/inline-files/The%20Equalizer%203.pdf
- https://www.madisonjournal.com/sites/default/files/inline-files/The%20Black%20Book.pdf
- https://www.madisonjournal.com/sites/default/files/inline-files/Operation%20Napoleon.pdf
- https://www.madisonjournal.com/sites/default/files/inline-files/Saw%20X%203.pdf
- https://www.madisonjournal.com/sites/default/files/inline-files/Nowhere.pdf
- https://www.madisonjournal.com/sites/default/files/inline-files/Gran%20Turismo_0.pdf
- https://www.madisonjournal.com/sites/default/files/inline-files/Concrete%20Utopia.pdf
- https://socialprotection.org/sites/default/files/database_files/The%20Nightmare%20Before%20Christmas.pdf
- https://www.enterprise-journal.com/sites/default/files/inline-files/The%20Beasts.pdf
- https://www.enterprise-journal.com/sites/default/files/inline-files/Jujutsu%20Kaisen.pdf
- https://www.enterprise-journal.com/sites/default/files/inline-files/Concrete%20Utopia.pdf
- https://www.enterprise-journal.com/sites/default/files/inline-files/Loki.pdf
- https://www.enterprise-journal.com/sites/default/files/inline-files/Jeanne%20du%20Barry.pdf
- https://baskadia.com/post/7drn
- https://baskadia.com/post/7drp
- class CropResult(db.Model):
- id = db.Column(db.Integer, primary_key=True)
- user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
- crop_name = db.Column(db.String(50), nullable=False)
- result = db.Column(db.String(50), nullable=False)
- date_created = db.Column(db.DateTime, default=datetime.utcnow)
- with app.app_context():
- db.create_all()
Editor
You can edit this paste and save as new: