Maulidaaa commited on
Commit
79939d7
·
1 Parent(s): 0def9a2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -8
app.py CHANGED
@@ -1,7 +1,7 @@
1
  import os
2
  import numpy as np
3
  import pandas as pd
4
- from flask import Flask, request, jsonify
5
  from tensorflow.keras.models import load_model
6
  from tensorflow.keras.utils import load_img, img_to_array
7
  from math import radians, cos, sin, sqrt, atan2
@@ -98,10 +98,8 @@ def get_farming_tips(df, crop_name):
98
  }
99
  return None
100
 
 
101
  def get_location_name(lat, lon):
102
- """
103
- Mengambil nama lokasi (reverse geocoding) dari OpenStreetMap Nominatim.
104
- """
105
  try:
106
  url = f"https://nominatim.openstreetmap.org/reverse?lat={lat}&lon={lon}&format=json"
107
  headers = {"User-Agent": "soil-api/1.0"}
@@ -114,7 +112,31 @@ def get_location_name(lat, lon):
114
  except Exception:
115
  return "Tidak ditemukan"
116
 
117
- # Endpoint utama
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118
  @app.route('/analyze', methods=['POST'])
119
  def analyze():
120
  if 'image' not in request.files:
@@ -141,9 +163,7 @@ def analyze():
141
  if nearest_data is None:
142
  return jsonify({"error": "Data jenis tanah tidak ditemukan"}), 404
143
 
144
- # Ambil nama lokasi dari OSM
145
  location_name = get_location_name(nearest_data['latitude'], nearest_data['longitude'])
146
-
147
  weather = get_weather_data(lat, lon)
148
  if not weather:
149
  return jsonify({"error": "Gagal mengambil data cuaca"}), 500
@@ -182,7 +202,7 @@ def analyze():
182
  "nearest_soil_data": {
183
  "latitude": nearest_data['latitude'],
184
  "longitude": nearest_data['longitude'],
185
- "location_name": location_name, # Tambahkan nama lokasi di sini
186
  "pH": nearest_data['pH'],
187
  "N": nearest_data['N'],
188
  "P": nearest_data['P'],
 
1
  import os
2
  import numpy as np
3
  import pandas as pd
4
+ from flask import Flask, request, jsonify, render_template_string
5
  from tensorflow.keras.models import load_model
6
  from tensorflow.keras.utils import load_img, img_to_array
7
  from math import radians, cos, sin, sqrt, atan2
 
98
  }
99
  return None
100
 
101
+ # Fungsi reverse geocoding
102
  def get_location_name(lat, lon):
 
 
 
103
  try:
104
  url = f"https://nominatim.openstreetmap.org/reverse?lat={lat}&lon={lon}&format=json"
105
  headers = {"User-Agent": "soil-api/1.0"}
 
112
  except Exception:
113
  return "Tidak ditemukan"
114
 
115
+ # Halaman landing
116
+ @app.route('/')
117
+ def index():
118
+ html_content = """
119
+ <!DOCTYPE html>
120
+ <html>
121
+ <head>
122
+ <title>Soil & Crop Recommender</title>
123
+ </head>
124
+ <body style="font-family: Arial; text-align: center; padding: 50px;">
125
+ <h1>Welcome to the Soil & Crop Recommendation API 🌱</h1>
126
+ <p>Use the <code>/analyze</code> endpoint with an image and coordinates to get soil predictions and crop suggestions.</p>
127
+ <p><strong>POST /analyze</strong> with form-data:</p>
128
+ <ul style="list-style: none;">
129
+ <li><code>image</code>: soil image file</li>
130
+ <li><code>lat</code>: latitude</li>
131
+ <li><code>lon</code>: longitude</li>
132
+ </ul>
133
+ <p>Happy Farming! 🌾</p>
134
+ </body>
135
+ </html>
136
+ """
137
+ return render_template_string(html_content)
138
+
139
+ # Endpoint analisis utama
140
  @app.route('/analyze', methods=['POST'])
141
  def analyze():
142
  if 'image' not in request.files:
 
163
  if nearest_data is None:
164
  return jsonify({"error": "Data jenis tanah tidak ditemukan"}), 404
165
 
 
166
  location_name = get_location_name(nearest_data['latitude'], nearest_data['longitude'])
 
167
  weather = get_weather_data(lat, lon)
168
  if not weather:
169
  return jsonify({"error": "Gagal mengambil data cuaca"}), 500
 
202
  "nearest_soil_data": {
203
  "latitude": nearest_data['latitude'],
204
  "longitude": nearest_data['longitude'],
205
+ "location_name": location_name,
206
  "pH": nearest_data['pH'],
207
  "N": nearest_data['N'],
208
  "P": nearest_data['P'],