rajkhanke commited on
Commit
2c9c281
·
verified ·
1 Parent(s): a7374f4

Update app.py

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