Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,9 @@
|
|
| 1 |
-
import
|
| 2 |
-
import hashlib
|
| 3 |
-
import time
|
| 4 |
-
import threading
|
| 5 |
import requests
|
| 6 |
-
from flask import Flask, render_template, request, jsonify, Response, send_file, url_for
|
| 7 |
from bs4 import BeautifulSoup
|
| 8 |
-
from flask_caching import Cache
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
| 12 |
-
# Configure in-memory caching for non-image routes
|
| 13 |
-
app.config['CACHE_TYPE'] = 'SimpleCache'
|
| 14 |
-
app.config['CACHE_DEFAULT_TIMEOUT'] = 300 # Cache responses for 5 minutes
|
| 15 |
-
cache = Cache(app)
|
| 16 |
-
|
| 17 |
-
# Directory to store cached images
|
| 18 |
-
CACHE_DIR = "cache_images"
|
| 19 |
-
if not os.path.exists(CACHE_DIR):
|
| 20 |
-
os.makedirs(CACHE_DIR)
|
| 21 |
-
|
| 22 |
-
def get_cache_filename(url):
|
| 23 |
-
"""Generate a unique filename for a given URL."""
|
| 24 |
-
h = hashlib.md5(url.encode('utf-8')).hexdigest()
|
| 25 |
-
# Here we assume JPEG. You might want to derive extension from url or headers.
|
| 26 |
-
return os.path.join(CACHE_DIR, f"{h}.jpg")
|
| 27 |
-
|
| 28 |
# Internal mapping of crops to pests (for the form)
|
| 29 |
CROP_TO_PESTS = {
|
| 30 |
"Sorgum": ["FallArmyWorm"],
|
|
@@ -50,19 +29,48 @@ CROP_MAPPING = {
|
|
| 50 |
"Soybean": "2",
|
| 51 |
"Sugarcane": "8",
|
| 52 |
"Tur": "5",
|
| 53 |
-
"Sorgum": "6"
|
| 54 |
}
|
| 55 |
|
| 56 |
# Map our internal pest names to external page values per crop.
|
| 57 |
PEST_MAPPING = {
|
| 58 |
-
"Cotton": {
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
"
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
}
|
| 67 |
|
| 68 |
# Parameter codes and labels for the final image URL
|
|
@@ -83,13 +91,13 @@ def index():
|
|
| 83 |
week = request.args.get('week', '')
|
| 84 |
param = request.args.get('param', '')
|
| 85 |
|
| 86 |
-
|
| 87 |
if crop and pest and year and week and param:
|
| 88 |
# Build the external image URL (using HTTP)
|
| 89 |
base_url = f"http://www.icar-crida.res.in:8080/naip/gisimages/{crop}/{year}/{pest}_"
|
| 90 |
external_image_url = f"{base_url}{param}{week}.jpg"
|
| 91 |
-
# Instead of
|
| 92 |
-
|
| 93 |
|
| 94 |
return render_template('index.html',
|
| 95 |
crops=list(CROP_TO_PESTS.keys()),
|
|
@@ -101,11 +109,9 @@ def index():
|
|
| 101 |
selected_year=year,
|
| 102 |
selected_week=week,
|
| 103 |
selected_param=param,
|
| 104 |
-
|
| 105 |
|
| 106 |
-
# Cache this route based on its query string.
|
| 107 |
@app.route('/fetch_weeks')
|
| 108 |
-
@cache.cached(timeout=300, query_string=True)
|
| 109 |
def fetch_weeks():
|
| 110 |
crop = request.args.get('crop', '')
|
| 111 |
pest = request.args.get('pest', '')
|
|
@@ -134,27 +140,19 @@ def fetch_weeks():
|
|
| 134 |
weeks = [str(i) for i in range(1, 53)]
|
| 135 |
return jsonify({"weeks": weeks})
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
external_url = request.args.get('url')
|
| 141 |
if not external_url:
|
| 142 |
return "Missing URL", 400
|
| 143 |
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
if resp.status_code == 200:
|
| 151 |
-
with open(cache_filename, "wb") as f:
|
| 152 |
-
f.write(resp.content)
|
| 153 |
-
return send_file(cache_filename, mimetype=resp.headers.get('Content-Type', 'image/jpeg'))
|
| 154 |
-
else:
|
| 155 |
-
return "Error downloading image", resp.status_code
|
| 156 |
-
except Exception as e:
|
| 157 |
-
return str(e), 500
|
| 158 |
|
| 159 |
if __name__ == '__main__':
|
| 160 |
-
app.run(debug=True)
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify, Response
|
|
|
|
|
|
|
|
|
|
| 2 |
import requests
|
|
|
|
| 3 |
from bs4 import BeautifulSoup
|
|
|
|
| 4 |
|
| 5 |
app = Flask(__name__)
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Internal mapping of crops to pests (for the form)
|
| 8 |
CROP_TO_PESTS = {
|
| 9 |
"Sorgum": ["FallArmyWorm"],
|
|
|
|
| 29 |
"Soybean": "2",
|
| 30 |
"Sugarcane": "8",
|
| 31 |
"Tur": "5",
|
| 32 |
+
"Sorgum": "6" # Adjust if needed
|
| 33 |
}
|
| 34 |
|
| 35 |
# Map our internal pest names to external page values per crop.
|
| 36 |
PEST_MAPPING = {
|
| 37 |
+
"Cotton": {
|
| 38 |
+
"FallArmyWorm": "71"
|
| 39 |
+
},
|
| 40 |
+
"Gram": {
|
| 41 |
+
"H.armigera": "72",
|
| 42 |
+
"Wilt": "73"
|
| 43 |
+
},
|
| 44 |
+
"Maize": {
|
| 45 |
+
"FallArmyWorm": "74"
|
| 46 |
+
},
|
| 47 |
+
"Rice": {
|
| 48 |
+
"Blast": "75",
|
| 49 |
+
"GallMidge": "76",
|
| 50 |
+
"YSB": "77",
|
| 51 |
+
"PlantHopper": "78",
|
| 52 |
+
"BlueBeetle": "79",
|
| 53 |
+
"BacterialLeafBlight": "80"
|
| 54 |
+
},
|
| 55 |
+
"Soybean": {
|
| 56 |
+
"Girdlebeetle": "81",
|
| 57 |
+
"H.armigera": "82",
|
| 58 |
+
"Semilooper": "83",
|
| 59 |
+
"Spodoptera": "84",
|
| 60 |
+
"StemFLy": "85"
|
| 61 |
+
},
|
| 62 |
+
"Tur": {
|
| 63 |
+
"Wilt": "86",
|
| 64 |
+
"Webbed_Leaves": "87",
|
| 65 |
+
"Pod_damage": "88"
|
| 66 |
+
},
|
| 67 |
+
"Sugarcane": {
|
| 68 |
+
"FallArmyGrub": "89",
|
| 69 |
+
"WhiteGrub": "90"
|
| 70 |
+
},
|
| 71 |
+
"Sorgum": {
|
| 72 |
+
"FallArmyWorm": "91"
|
| 73 |
+
}
|
| 74 |
}
|
| 75 |
|
| 76 |
# Parameter codes and labels for the final image URL
|
|
|
|
| 91 |
week = request.args.get('week', '')
|
| 92 |
param = request.args.get('param', '')
|
| 93 |
|
| 94 |
+
image_url = ""
|
| 95 |
if crop and pest and year and week and param:
|
| 96 |
# Build the external image URL (using HTTP)
|
| 97 |
base_url = f"http://www.icar-crida.res.in:8080/naip/gisimages/{crop}/{year}/{pest}_"
|
| 98 |
external_image_url = f"{base_url}{param}{week}.jpg"
|
| 99 |
+
# Instead of using the external HTTP URL directly, we build our proxy URL
|
| 100 |
+
image_url = f"/proxy-image?url={external_image_url}"
|
| 101 |
|
| 102 |
return render_template('index.html',
|
| 103 |
crops=list(CROP_TO_PESTS.keys()),
|
|
|
|
| 109 |
selected_year=year,
|
| 110 |
selected_week=week,
|
| 111 |
selected_param=param,
|
| 112 |
+
image_url=image_url)
|
| 113 |
|
|
|
|
| 114 |
@app.route('/fetch_weeks')
|
|
|
|
| 115 |
def fetch_weeks():
|
| 116 |
crop = request.args.get('crop', '')
|
| 117 |
pest = request.args.get('pest', '')
|
|
|
|
| 140 |
weeks = [str(i) for i in range(1, 53)]
|
| 141 |
return jsonify({"weeks": weeks})
|
| 142 |
|
| 143 |
+
@app.route('/proxy-image')
|
| 144 |
+
def proxy_image():
|
| 145 |
+
# Get the external URL from the query parameter
|
| 146 |
external_url = request.args.get('url')
|
| 147 |
if not external_url:
|
| 148 |
return "Missing URL", 400
|
| 149 |
|
| 150 |
+
try:
|
| 151 |
+
# Fetch the image from the external server
|
| 152 |
+
resp = requests.get(external_url, timeout=10)
|
| 153 |
+
return Response(resp.content, mimetype=resp.headers.get('Content-Type', 'image/jpeg'))
|
| 154 |
+
except Exception as e:
|
| 155 |
+
return str(e), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
if __name__ == '__main__':
|
| 158 |
+
app.run(debug=True)
|