Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import google.generativeai as genai
|
4 |
+
from streamlit_js_eval import get_geolocation
|
5 |
+
import os
|
6 |
+
# Configure Google Gemini API
|
7 |
+
GEMINI_API_KEY = os.getenv("GEMINI")
|
8 |
+
genai.configure(api_key=GEMINI_API_KEY)
|
9 |
+
|
10 |
+
# Streamlit UI
|
11 |
+
st.set_page_config(page_title="Soil Report Analyzer", layout="wide")
|
12 |
+
st.title("π± Soil Report Analyzer")
|
13 |
+
st.write("Fetching your current location to generate soil insights!")
|
14 |
+
|
15 |
+
# Fetch User Location
|
16 |
+
location = get_geolocation()
|
17 |
+
latitude, longitude = None, None
|
18 |
+
|
19 |
+
if location:
|
20 |
+
latitude = location["coords"]["latitude"]
|
21 |
+
longitude = location["coords"]["longitude"]
|
22 |
+
st.success(f"π Detected Location: Latitude {latitude}, Longitude {longitude}")
|
23 |
+
else:
|
24 |
+
st.warning("Could not fetch location. Please enable location access.")
|
25 |
+
|
26 |
+
# User Input for Crop
|
27 |
+
crop_planted = st.text_input("πΎ Enter the crop you are currently growing:", "")
|
28 |
+
|
29 |
+
# API Call Function
|
30 |
+
def fetch_soil_data(lat, lon):
|
31 |
+
url = f"https://rest.isric.org/soilgrids/v2.0/properties/query?lon={lon}&lat={lat}&property=bdod&property=cfvo&property=clay&property=nitrogen&property=ocd&property=ocs&property=phh2o&property=sand&property=silt&property=soc&property=wv0010&property=wv0033&property=wv1500&depth=5-15cm&depth=15-30cm&value=Q0.05&value=mean"
|
32 |
+
headers = {'accept': 'application/json'}
|
33 |
+
response = requests.get(url, headers=headers)
|
34 |
+
return response.json() if response.status_code == 200 else None
|
35 |
+
|
36 |
+
# Summarize Soil Data using Gemini Flash
|
37 |
+
def summarize_soil_report(soil_json, crop):
|
38 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
39 |
+
prompt = f"""
|
40 |
+
Analyze the given soil data and generate a **farmer-friendly** soil analysis report. Also, evaluate the suitability of growing '{crop}' based on the soil conditions and suggest better crops if needed.
|
41 |
+
|
42 |
+
### **Key Insights to Include:**
|
43 |
+
1. **Soil pH Level** - Determine if the soil is acidic, neutral, or alkaline and suggest corrections.
|
44 |
+
2. **Nutrient Content** - Assess nitrogen, organic carbon, and other nutrients; suggest improvements if needed.
|
45 |
+
3. **Soil Texture & Water Retention** - Use clay, sand, and silt content to determine water retention.
|
46 |
+
4. **Moisture Content & Irrigation Needs** - Provide irrigation best practices based on soil type.
|
47 |
+
5. **Crop Suitability Analysis** - Determine if '{crop}' is a good fit and suggest better alternatives if necessary.
|
48 |
+
6. **Soil Improvement Tips** - Offer actionable suggestions for farmers.
|
49 |
+
|
50 |
+
### **Soil Data Input:**
|
51 |
+
{soil_json}
|
52 |
+
"""
|
53 |
+
response = model.generate_content(prompt)
|
54 |
+
return response.text if response else "Summary could not be generated."
|
55 |
+
|
56 |
+
# Fetch and Process Soil Data
|
57 |
+
if latitude and longitude and st.button("Get Soil Report"):
|
58 |
+
with st.spinner("Fetching soil data... β³"):
|
59 |
+
soil_data = fetch_soil_data(latitude, longitude)
|
60 |
+
|
61 |
+
if soil_data:
|
62 |
+
summary = summarize_soil_report(soil_data, crop_planted)
|
63 |
+
|
64 |
+
st.subheader("π Raw Soil Data")
|
65 |
+
st.json(soil_data, expanded=False)
|
66 |
+
|
67 |
+
st.subheader("π Simplified Soil Report Summary")
|
68 |
+
st.write(summary)
|
69 |
+
|
70 |
+
# Option to download summary
|
71 |
+
st.download_button("Download Summary as Text", summary, file_name="Soil_Report_Summary.txt")
|
72 |
+
else:
|
73 |
+
st.error("Failed to fetch soil data. Please try again later.")
|