Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,25 +3,44 @@ 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 =
|
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 |
-
#
|
16 |
-
|
17 |
latitude, longitude = None, None
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
else:
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
# User Input for Crop
|
27 |
crop_planted = st.text_input("πΎ Enter the crop you are currently growing:", "")
|
@@ -38,7 +57,6 @@ 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.
|
@@ -46,7 +64,6 @@ def summarize_soil_report(soil_json, crop):
|
|
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 |
"""
|
@@ -62,10 +79,13 @@ if latitude and longitude and st.button("Get Soil Report"):
|
|
62 |
summary = summarize_soil_report(soil_data, crop_planted)
|
63 |
|
64 |
st.subheader("π Simplified Soil Report Summary")
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
67 |
|
68 |
# Option to download summary
|
69 |
-
st.download_button("Download Summary as Text",
|
70 |
else:
|
71 |
st.error("Failed to fetch soil data. Please try again later.")
|
|
|
3 |
import google.generativeai as genai
|
4 |
from streamlit_js_eval import get_geolocation
|
5 |
import os
|
6 |
+
|
7 |
# Configure Google Gemini API
|
8 |
+
GEMINI_API_KEY = os.getenv("GEMINI")
|
9 |
genai.configure(api_key=GEMINI_API_KEY)
|
10 |
|
11 |
# Streamlit UI
|
12 |
st.set_page_config(page_title="Soil Report Analyzer", layout="wide")
|
13 |
st.title("π± Soil Report Analyzer")
|
14 |
+
st.write("Fetching your current location or enter an address to generate soil insights!")
|
15 |
|
16 |
+
# User Input for Address
|
17 |
+
manual_address = st.text_input("π Enter your location (or leave blank to use device location):", "")
|
18 |
latitude, longitude = None, None
|
19 |
|
20 |
+
# Function to get coordinates from address
|
21 |
+
def get_coordinates_from_address(address):
|
22 |
+
url = f"https://nominatim.openstreetmap.org/search?q={address}&format=json&limit=1"
|
23 |
+
response = requests.get(url)
|
24 |
+
if response.status_code == 200 and response.json():
|
25 |
+
location_data = response.json()[0]
|
26 |
+
return float(location_data["lat"]), float(location_data["lon"])
|
27 |
+
return None, None
|
28 |
+
|
29 |
+
# Fetch User Location if no manual address is provided
|
30 |
+
if manual_address:
|
31 |
+
latitude, longitude = get_coordinates_from_address(manual_address)
|
32 |
+
if latitude and longitude:
|
33 |
+
st.success(f"π Detected Location: {manual_address} (Lat: {latitude}, Lon: {longitude})")
|
34 |
+
else:
|
35 |
+
st.error("Could not fetch coordinates for the given address. Try refining your input.")
|
36 |
else:
|
37 |
+
location = get_geolocation()
|
38 |
+
if location:
|
39 |
+
latitude = location["coords"]["latitude"]
|
40 |
+
longitude = location["coords"]["longitude"]
|
41 |
+
st.success(f"π Detected Location: Latitude {latitude}, Longitude {longitude}")
|
42 |
+
else:
|
43 |
+
st.warning("Could not fetch location. Please enable location access or enter an address.")
|
44 |
|
45 |
# User Input for Crop
|
46 |
crop_planted = st.text_input("πΎ Enter the crop you are currently growing:", "")
|
|
|
57 |
model = genai.GenerativeModel("gemini-1.5-flash")
|
58 |
prompt = f"""
|
59 |
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.
|
|
|
60 |
### **Key Insights to Include:**
|
61 |
1. **Soil pH Level** - Determine if the soil is acidic, neutral, or alkaline and suggest corrections.
|
62 |
2. **Nutrient Content** - Assess nitrogen, organic carbon, and other nutrients; suggest improvements if needed.
|
|
|
64 |
4. **Moisture Content & Irrigation Needs** - Provide irrigation best practices based on soil type.
|
65 |
5. **Crop Suitability Analysis** - Determine if '{crop}' is a good fit and suggest better alternatives if necessary.
|
66 |
6. **Soil Improvement Tips** - Offer actionable suggestions for farmers.
|
|
|
67 |
### **Soil Data Input:**
|
68 |
{soil_json}
|
69 |
"""
|
|
|
79 |
summary = summarize_soil_report(soil_data, crop_planted)
|
80 |
|
81 |
st.subheader("π Simplified Soil Report Summary")
|
82 |
+
filtered_summary = "\n".join(
|
83 |
+
line for line in summary.split("\n")
|
84 |
+
if "Coordinates:" not in line # Exclude lines containing 'Coordinates:'
|
85 |
+
)
|
86 |
+
st.write(filtered_summary)
|
87 |
|
88 |
# Option to download summary
|
89 |
+
st.download_button("Download Summary as Text", filtered_summary, file_name="Soil_Report.txt")
|
90 |
else:
|
91 |
st.error("Failed to fetch soil data. Please try again later.")
|