Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,103 +1,105 @@
|
|
1 |
-
from flask import Flask, render_template, request
|
2 |
-
from markupsafe import Markup
|
3 |
-
from google import genai
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
#
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
-
|
38 |
-
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
-
|
52 |
-
-
|
53 |
-
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
IMPORTANT:
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
|
|
|
|
103 |
app.run(debug=True)
|
|
|
1 |
+
from flask import Flask, render_template, request
|
2 |
+
from markupsafe import Markup
|
3 |
+
from google import genai
|
4 |
+
import os
|
5 |
+
|
6 |
+
app = Flask(__name__)
|
7 |
+
|
8 |
+
api_key = os.getenv('GEMINI_API_KEY')
|
9 |
+
# Initialize the Gemini API client using your API key
|
10 |
+
client = genai.Client(api_key=api_key)
|
11 |
+
|
12 |
+
|
13 |
+
@app.route('/', methods=['GET', 'POST'])
|
14 |
+
def index():
|
15 |
+
if request.method == 'POST':
|
16 |
+
# Retrieve farm details
|
17 |
+
location = request.form.get('location')
|
18 |
+
season = request.form.get('season')
|
19 |
+
total_area = request.form.get('total_area')
|
20 |
+
language = request.form.get('language', 'English') # Get selected language with English as default
|
21 |
+
|
22 |
+
# Retrieve crop details
|
23 |
+
crop_names = request.form.getlist('crop_name')
|
24 |
+
crop_areas = request.form.getlist('crop_area')
|
25 |
+
crops = []
|
26 |
+
for name, area in zip(crop_names, crop_areas):
|
27 |
+
if name.strip() and area.strip():
|
28 |
+
crops.append({'name': name.strip(), 'area': area.strip()})
|
29 |
+
|
30 |
+
# Construct a descriptive summary of the crop details
|
31 |
+
crop_details = ", ".join([f"{crop['name']} ({crop['area']} acres)" for crop in crops])
|
32 |
+
|
33 |
+
# Build the prompt that instructs the API to generate a fully formatted HTML page with enhanced CSS styling.
|
34 |
+
prompt = f"""
|
35 |
+
You are a skilled agriculture expert. I am providing you with the following farm details and crop information:
|
36 |
+
|
37 |
+
- Location: {location}
|
38 |
+
- Season: {season}
|
39 |
+
- Total Farm Area: {total_area} acres
|
40 |
+
- Crop Details: {crop_details}
|
41 |
+
|
42 |
+
Using the above input, please generate a fully formatted HTML page with enhanced CSS styling that includes the following sections:
|
43 |
+
i dont want any spacing remove extra spaicng if any
|
44 |
+
|
45 |
+
1. **Main Heading:**
|
46 |
+
- A centrally aligned, bold heading in green titled "Crop Swapping Strategies" with no extra spacing before the heading or after the report. Use medium overall spacing, clear font sizes for both headings and points, and ensure the report is easily understandable by farmers. Tables should be designed for optimal readability.
|
47 |
+
|
48 |
+
2. **Current Strategy Statistics:**
|
49 |
+
- A left-aligned, blue bold subheading "Current Strategy Statistics".
|
50 |
+
- Below it, include a green table with columns for:
|
51 |
+
- Crop Name
|
52 |
+
- Gross Revenue
|
53 |
+
- Yearly Production
|
54 |
+
- Yearly Expenses
|
55 |
+
- Overall Profit Percentage
|
56 |
+
- Populate the table with realistic hypothetical data based on the provided crop details.
|
57 |
+
|
58 |
+
3. **Year Crops Swapping Strategies (3-Year Forecast):**
|
59 |
+
- A left-aligned, blue bold subheading titled "#Year Crops Swapping Strategies".
|
60 |
+
- Create a green table that displays season-wise cropping strategies over the next three years, ensuring clarity with concise headers and data. Incorporate land optimization details (e.g., tomato: 1.5 acres) and consider different cropping seasons such as Rabi and Kharif.
|
61 |
+
|
62 |
+
4. **Swapping Strategy Statistics:**
|
63 |
+
- A left-aligned, blue bold subheading "Swapping Strategy Statistics".
|
64 |
+
- Add a green table showing new strategy data including:
|
65 |
+
- Production
|
66 |
+
- Gross Income
|
67 |
+
- Profit Percentage
|
68 |
+
- Use realistic figures that align with crop swapping strategies.
|
69 |
+
|
70 |
+
5. **Comparative Analysis:**
|
71 |
+
- Include a section with a blue left-aligned subheading "Comparison of Current and Swapped Strategy Statistics".
|
72 |
+
- Present a comparative table clearly outlining differences between the current strategy and the new swapped strategy.
|
73 |
+
|
74 |
+
6. **Insights and Recommendations:**
|
75 |
+
- At the bottom of the page, include a bullet-point list with short, concise sentences offering insights, recommendations, and questions.
|
76 |
+
- Style each bullet point in bold with yellow highlights on key statistics and numbers.
|
77 |
+
- Optimize spacing and margins to ensure a visually clear layout.
|
78 |
+
|
79 |
+
Additionally, design interactive, beautiful, and colorful tables. Increase the font size for headings and reduce excessive spacing to produce a rich, detailed, and farmer-friendly report using realistic data. Do not include any explanations of the changes.
|
80 |
+
|
81 |
+
IMPORTANT: Give the entire response in {language} language. All text, headings, and content should be in {language}.
|
82 |
+
|
83 |
+
IMPORTANT: Do not add any extra spacing at the beginning or end of the response. Remove any extra spacing.
|
84 |
+
"""
|
85 |
+
|
86 |
+
# Call the Gemini API with the constructed prompt
|
87 |
+
response = client.models.generate_content(
|
88 |
+
model="gemini-2.0-flash",
|
89 |
+
contents=prompt
|
90 |
+
)
|
91 |
+
|
92 |
+
# Extract the generated text (HTML)
|
93 |
+
result_html = response.text
|
94 |
+
|
95 |
+
# Remove any extra spacing at the beginning and end of the response
|
96 |
+
result_html = result_html.strip()
|
97 |
+
|
98 |
+
# Pass the HTML to the template as 'safe' so it's rendered properly
|
99 |
+
return render_template('index.html', result=Markup(result_html))
|
100 |
+
|
101 |
+
return render_template('index.html')
|
102 |
+
|
103 |
+
|
104 |
+
if __name__ == '__main__':
|
105 |
app.run(debug=True)
|