Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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="Weather-Based Farming Insights", layout="wide")
|
12 |
+
st.title("π¦οΈ Weather-Based Farming Insights")
|
13 |
+
st.write("Fetching your location to provide farming recommendations!")
|
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 |
+
# Optional Crop Input
|
27 |
+
crop_name = st.text_input("πΎ Enter the crop you're growing (optional):", "")
|
28 |
+
|
29 |
+
# Fetch Weather Data
|
30 |
+
def fetch_weather_data(lat, lon):
|
31 |
+
url = f"https://api.ambeedata.com/weather/latest/by-lat-lng?lat={lat}&lng={lon}"
|
32 |
+
headers = {
|
33 |
+
"x-api-key": os.getenv('WEATHER'),
|
34 |
+
"Content-type": "application/json"
|
35 |
+
}
|
36 |
+
response = requests.get(url, headers=headers)
|
37 |
+
return response.json() if response.status_code == 200 else None
|
38 |
+
|
39 |
+
# Generate Farming Report
|
40 |
+
def generate_farming_report(weather_json, crop):
|
41 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
42 |
+
|
43 |
+
# Adjust prompt based on crop input
|
44 |
+
if crop:
|
45 |
+
prompt = f"""
|
46 |
+
Analyze the given weather data and generate a **farmer-friendly** report in simple terms.
|
47 |
+
Provide insights on:
|
48 |
+
- **Impact of Current Weather on {crop}**: Any risks or benefits.
|
49 |
+
- **Precautions for Farmers Growing {crop}**: How to protect against weather-related risks.
|
50 |
+
- **Market Price Trends**: Whether the weather may affect future crop prices.
|
51 |
+
|
52 |
+
**Weather Data:**
|
53 |
+
{weather_json}
|
54 |
+
"""
|
55 |
+
else:
|
56 |
+
prompt = f"""
|
57 |
+
Analyze the given weather data and generate a **farmer-friendly** report in simple terms.
|
58 |
+
Provide guidance on:
|
59 |
+
- **Impact on Crops**: How current weather affects growing crops.
|
60 |
+
- **Best Crops to Grow**: Based on temperature, air quality, and humidity.
|
61 |
+
- **Precautions for Farmers**: Weather-related risks and farming safety measures.
|
62 |
+
|
63 |
+
**Weather Data:**
|
64 |
+
{weather_json}
|
65 |
+
"""
|
66 |
+
|
67 |
+
response = model.generate_content(prompt)
|
68 |
+
return response.text if response else "Could not generate report."
|
69 |
+
|
70 |
+
# Fetch and Process Weather Data
|
71 |
+
report_text = None # Initialize variable
|
72 |
+
|
73 |
+
if latitude and longitude and st.button("Get Farming Report"):
|
74 |
+
with st.spinner("Fetching weather data... β³"):
|
75 |
+
weather_data = fetch_weather_data(latitude, longitude)
|
76 |
+
|
77 |
+
if weather_data:
|
78 |
+
report_text = generate_farming_report(weather_data, crop_name)
|
79 |
+
st.subheader("π Weather-Based Farming Report")
|
80 |
+
st.write(report_text)
|
81 |
+
|
82 |
+
# Option to download report
|
83 |
+
st.download_button("Download Report", report_text, file_name="Farming_Report.txt")
|
84 |
+
else:
|
85 |
+
st.error("Failed to fetch weather data. Please try again later.")
|