Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,9 +4,12 @@ from flask import Flask, render_template, request, jsonify, Response
|
|
4 |
from google import genai
|
5 |
import markdown
|
6 |
import os
|
|
|
7 |
app = Flask(__name__)
|
8 |
|
9 |
-
|
|
|
|
|
10 |
client = genai.Client(api_key=api_key)
|
11 |
|
12 |
def validate_coordinates(lat, lon):
|
@@ -149,7 +152,6 @@ def get_soil_properties():
|
|
149 |
except Exception as e:
|
150 |
return jsonify({"error": str(e)}), 500
|
151 |
|
152 |
-
|
153 |
def call_gemini_api(input_data):
|
154 |
"""
|
155 |
Enhanced prompt: We request a visually appealing Markdown report WITHOUT
|
@@ -168,8 +170,7 @@ Create a visually appealing, farmer-friendly pest outbreak report in Markdown wi
|
|
168 |
- Pest Name
|
169 |
- Predicted Outbreak Month(s)
|
170 |
- Severity
|
171 |
-
-
|
172 |
-
- Precautionary Measures against damages
|
173 |
5. Provide bullet points for best practices.
|
174 |
6. Use a friendly color scheme, with subtle hovers or highlights for rows, and consistent fonts.
|
175 |
7. Avoid printing any raw code blocks.
|
@@ -193,18 +194,35 @@ Important details from the user:
|
|
193 |
- Wind Speed: {input_data.get('wind_speed')}
|
194 |
- Cloud Cover: {input_data.get('cloud_cover')}
|
195 |
10. also i want specific reocmmendation on pest control (seprate than precuatuonary measure below it in bullet pooints),best agriculktual practices,not generlaized one , but speicifc as perstudiyng each input paprmaetenrindetial okay,poepr sltying tbale should be rendered porpelry etc.. porper bold heaidng ,big fotn,left allgiemnd jsutified text
|
196 |
-
11.again order first title the lat,long,location derive form lat long(ex : nagpur,india) then below it agiruclturla oinput parmeter analysis, then pest tbale then pest avidnaces practice in dpeth
|
197 |
-
12. i want short sentences and easily understandbale language for farmers big size font and colorful for subheadings , dont include paprmagrpash ,include short easily undertabsdbale senetences and contne tby farmers and porper ui in first run itself,for ui imorvement currently i have to rerun the modoel so tkae care of that, also hightlight important point sby yellow higlighters alll hihglighters pijtns should hihglihgt like key damages,reocmendations,pest,,key season like this.,also note that in first rnedering proepr well formatetd stlyed,wellr endered tbale s etc display and yellow hihglighters i want not yellow text,text is blakc highloght key poitns by yellow
|
198 |
"""
|
199 |
response = client.models.generate_content(
|
200 |
model="gemini-2.0-flash",
|
|
|
201 |
)
|
202 |
return response.text
|
203 |
|
204 |
-
|
205 |
@app.route('/predict', methods=['POST'])
|
206 |
def predict():
|
207 |
form_data = request.form.to_dict()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
208 |
background: linear-gradient(120deg, #f7f7f7 0%, #e3f2fd 100%);
|
209 |
font-family: 'Segoe UI', Tahoma, sans-serif;
|
210 |
}}
|
@@ -212,6 +230,13 @@ def predict():
|
|
212 |
.report-container {{
|
213 |
max-width: 1000px;
|
214 |
margin: 0 auto;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
transform: translateY(-4px);
|
216 |
box-shadow: 0 12px 24px rgba(0,0,0,0.15);
|
217 |
}}
|
@@ -219,6 +244,11 @@ def predict():
|
|
219 |
/* Gradient heading for H1 */
|
220 |
.report-container h1 {{
|
221 |
text-align: center;
|
|
|
|
|
|
|
|
|
|
|
222 |
border-radius: 6px;
|
223 |
box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
|
224 |
}}
|
@@ -226,6 +256,8 @@ def predict():
|
|
226 |
/* Secondary headings (H2, H3) */
|
227 |
.report-container h2,
|
228 |
.report-container h3 {{
|
|
|
|
|
229 |
color: #2c3e50;
|
230 |
text-align: left;
|
231 |
}}
|
@@ -233,6 +265,7 @@ def predict():
|
|
233 |
/* Paragraphs */
|
234 |
.report-container p {{
|
235 |
margin-bottom: 1rem;
|
|
|
236 |
text-align: justify;
|
237 |
line-height: 1.6;
|
238 |
}}
|
@@ -240,6 +273,7 @@ def predict():
|
|
240 |
/* Lists */
|
241 |
.report-container ul,
|
242 |
.report-container ol {{
|
|
|
243 |
margin-bottom: 1rem;
|
244 |
color: #555555;
|
245 |
}}
|
@@ -247,6 +281,20 @@ def predict():
|
|
247 |
/* Table styling */
|
248 |
.report-container table {{
|
249 |
width: 100%;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
250 |
.report-container tbody tr:hover {{
|
251 |
background-color: #f9f9f9;
|
252 |
}}
|
@@ -254,7 +302,41 @@ def predict():
|
|
254 |
/* Responsive table for smaller screens */
|
255 |
@media (max-width: 768px) {{
|
256 |
.report-container table,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
257 |
|
258 |
|
259 |
if __name__ == '__main__':
|
260 |
-
app.run(debug=True)
|
|
|
4 |
from google import genai
|
5 |
import markdown
|
6 |
import os
|
7 |
+
|
8 |
app = Flask(__name__)
|
9 |
|
10 |
+
|
11 |
+
api_key = os.getenv('GEMINI_API_KEY')
|
12 |
+
# Replace with your actual Gemini API key
|
13 |
client = genai.Client(api_key=api_key)
|
14 |
|
15 |
def validate_coordinates(lat, lon):
|
|
|
152 |
except Exception as e:
|
153 |
return jsonify({"error": str(e)}), 500
|
154 |
|
|
|
155 |
def call_gemini_api(input_data):
|
156 |
"""
|
157 |
Enhanced prompt: We request a visually appealing Markdown report WITHOUT
|
|
|
170 |
- Pest Name
|
171 |
- Predicted Outbreak Month(s)
|
172 |
- Severity
|
173 |
+
- Precautionary Measures
|
|
|
174 |
5. Provide bullet points for best practices.
|
175 |
6. Use a friendly color scheme, with subtle hovers or highlights for rows, and consistent fonts.
|
176 |
7. Avoid printing any raw code blocks.
|
|
|
194 |
- Wind Speed: {input_data.get('wind_speed')}
|
195 |
- Cloud Cover: {input_data.get('cloud_cover')}
|
196 |
10. also i want specific reocmmendation on pest control (seprate than precuatuonary measure below it in bullet pooints),best agriculktual practices,not generlaized one , but speicifc as perstudiyng each input paprmaetenrindetial okay,poepr sltying tbale should be rendered porpelry etc.. porper bold heaidng ,big fotn,left allgiemnd jsutified text
|
197 |
+
11.again order first title the lat,long,location derive form lat long(ex : nagpur,india) then below it agiruclturla oinput parmeter analysis, then pest tbale then pest avidnaces practice in dpeth 10-12 with bueet pint safter that spciific agrficulturla best practices as per input parameters, after the damage of predicted pest some contnest and more dept contnext this shoudl be order
|
|
|
198 |
"""
|
199 |
response = client.models.generate_content(
|
200 |
model="gemini-2.0-flash",
|
201 |
+
contents=prompt,
|
202 |
)
|
203 |
return response.text
|
204 |
|
|
|
205 |
@app.route('/predict', methods=['POST'])
|
206 |
def predict():
|
207 |
form_data = request.form.to_dict()
|
208 |
+
report_md = call_gemini_api(form_data)
|
209 |
+
|
210 |
+
# Convert raw markdown to HTML
|
211 |
+
report_html = markdown.markdown(report_md)
|
212 |
+
|
213 |
+
# Inject advanced, colorful styling into the final HTML
|
214 |
+
html_output = f"""<!DOCTYPE html>
|
215 |
+
<html lang="en">
|
216 |
+
<head>
|
217 |
+
<meta charset="UTF-8">
|
218 |
+
<title>Pest Outbreak Dashboard Report</title>
|
219 |
+
<!-- Tailwind for utility classes -->
|
220 |
+
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
|
221 |
+
<style>
|
222 |
+
/* Overall page background with a subtle gradient */
|
223 |
+
body {{
|
224 |
+
margin: 0;
|
225 |
+
padding: 2rem;
|
226 |
background: linear-gradient(120deg, #f7f7f7 0%, #e3f2fd 100%);
|
227 |
font-family: 'Segoe UI', Tahoma, sans-serif;
|
228 |
}}
|
|
|
230 |
.report-container {{
|
231 |
max-width: 1000px;
|
232 |
margin: 0 auto;
|
233 |
+
background-color: #ffffff;
|
234 |
+
border-radius: 8px;
|
235 |
+
box-shadow: 0 4px 12px rgba(0,0,0,0.1);
|
236 |
+
padding: 2rem;
|
237 |
+
transition: transform 0.3s ease, box-shadow 0.3s ease;
|
238 |
+
}}
|
239 |
+
.report-container:hover {{
|
240 |
transform: translateY(-4px);
|
241 |
box-shadow: 0 12px 24px rgba(0,0,0,0.15);
|
242 |
}}
|
|
|
244 |
/* Gradient heading for H1 */
|
245 |
.report-container h1 {{
|
246 |
text-align: center;
|
247 |
+
font-size: 2rem;
|
248 |
+
margin-bottom: 1.5rem;
|
249 |
+
color: #ffffff;
|
250 |
+
background: linear-gradient(to right, #81c784, #388e3c);
|
251 |
+
padding: 1rem;
|
252 |
border-radius: 6px;
|
253 |
box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
|
254 |
}}
|
|
|
256 |
/* Secondary headings (H2, H3) */
|
257 |
.report-container h2,
|
258 |
.report-container h3 {{
|
259 |
+
margin-top: 1.5rem;
|
260 |
+
margin-bottom: 0.75rem;
|
261 |
color: #2c3e50;
|
262 |
text-align: left;
|
263 |
}}
|
|
|
265 |
/* Paragraphs */
|
266 |
.report-container p {{
|
267 |
margin-bottom: 1rem;
|
268 |
+
color: #555555;
|
269 |
text-align: justify;
|
270 |
line-height: 1.6;
|
271 |
}}
|
|
|
273 |
/* Lists */
|
274 |
.report-container ul,
|
275 |
.report-container ol {{
|
276 |
+
margin-left: 1.5rem;
|
277 |
margin-bottom: 1rem;
|
278 |
color: #555555;
|
279 |
}}
|
|
|
281 |
/* Table styling */
|
282 |
.report-container table {{
|
283 |
width: 100%;
|
284 |
+
border-collapse: collapse;
|
285 |
+
margin: 1.5rem 0;
|
286 |
+
}}
|
287 |
+
.report-container thead tr {{
|
288 |
+
background: linear-gradient(to right, #81c784, #388e3c);
|
289 |
+
color: #ffffff;
|
290 |
+
}}
|
291 |
+
.report-container th,
|
292 |
+
.report-container td {{
|
293 |
+
border: 1px solid #ddd;
|
294 |
+
padding: 12px 15px;
|
295 |
+
text-align: left;
|
296 |
+
transition: background-color 0.2s ease;
|
297 |
+
}}
|
298 |
.report-container tbody tr:hover {{
|
299 |
background-color: #f9f9f9;
|
300 |
}}
|
|
|
302 |
/* Responsive table for smaller screens */
|
303 |
@media (max-width: 768px) {{
|
304 |
.report-container table,
|
305 |
+
.report-container thead,
|
306 |
+
.report-container tbody,
|
307 |
+
.report-container th,
|
308 |
+
.report-container td,
|
309 |
+
.report-container tr {{
|
310 |
+
display: block;
|
311 |
+
width: 100%;
|
312 |
+
}}
|
313 |
+
.report-container thead tr {{
|
314 |
+
display: none;
|
315 |
+
}}
|
316 |
+
.report-container td {{
|
317 |
+
border: none;
|
318 |
+
border-bottom: 1px solid #ddd;
|
319 |
+
position: relative;
|
320 |
+
padding-left: 50%;
|
321 |
+
text-align: left;
|
322 |
+
}}
|
323 |
+
.report-container td:before {{
|
324 |
+
content: attr(data-label);
|
325 |
+
position: absolute;
|
326 |
+
left: 15px;
|
327 |
+
font-weight: bold;
|
328 |
+
}}
|
329 |
+
}}
|
330 |
+
</style>
|
331 |
+
</head>
|
332 |
+
<body>
|
333 |
+
<div class="report-container">
|
334 |
+
{report_html}
|
335 |
+
</div>
|
336 |
+
</body>
|
337 |
+
</html>"""
|
338 |
+
return Response(html_output, mimetype="text/html")
|
339 |
|
340 |
|
341 |
if __name__ == '__main__':
|
342 |
+
app.run(debug=True)
|