Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -8,7 +8,7 @@ import plotly.io as pio
|
|
8 |
from googletrans import Translator
|
9 |
import numpy as np
|
10 |
|
11 |
-
app = Flask(
|
12 |
|
13 |
# Initialize translator
|
14 |
translator = Translator()
|
@@ -34,6 +34,7 @@ MARATHI_TRANSLATIONS = {
|
|
34 |
'Top 5 Costliest Crops': 'सर्वात महाग 5 पिके'
|
35 |
}
|
36 |
|
|
|
37 |
def translate_to_marathi(text):
|
38 |
"""Translate text to Marathi"""
|
39 |
try:
|
@@ -44,18 +45,19 @@ def translate_to_marathi(text):
|
|
44 |
except:
|
45 |
return text
|
46 |
|
|
|
47 |
def fetch_market_data(state=None, district=None, market=None, commodity=None):
|
48 |
"""Fetch data from the agricultural market API"""
|
49 |
-
api_key = "
|
50 |
print(api_key)
|
51 |
base_url = "https://api.data.gov.in/resource/9ef84268-d588-465a-a308-a864a43d0070"
|
52 |
-
|
53 |
params = {
|
54 |
"api-key": api_key,
|
55 |
"format": "json",
|
56 |
-
"limit":
|
57 |
}
|
58 |
-
|
59 |
# Add filters if provided
|
60 |
if state:
|
61 |
params["filters[state]"] = state
|
@@ -89,28 +91,28 @@ def get_ai_insights(market_data, state, district):
|
|
89 |
try:
|
90 |
# Calculate additional market metrics
|
91 |
district_data = market_data[market_data['district'] == district]
|
92 |
-
|
93 |
# Price trends and volatility
|
94 |
price_trends = district_data.groupby('commodity').agg({
|
95 |
'modal_price': ['mean', 'min', 'max', 'std']
|
96 |
}).round(2)
|
97 |
-
|
98 |
# Calculate price stability (lower std/mean ratio indicates more stable prices)
|
99 |
-
price_trends['price_stability'] = (price_trends['modal_price']['std'] /
|
100 |
-
|
101 |
-
|
102 |
# Identify commodities with consistent high prices
|
103 |
-
high_value_crops = price_trends[price_trends['modal_price']['mean'] >
|
104 |
-
|
105 |
-
|
106 |
# Get seasonal patterns
|
107 |
district_data['arrival_date'] = pd.to_datetime(district_data['arrival_date'])
|
108 |
district_data['month'] = district_data['arrival_date'].dt.month
|
109 |
monthly_trends = district_data.groupby(['commodity', 'month'])['modal_price'].mean().round(2)
|
110 |
-
|
111 |
# Market competition analysis
|
112 |
market_competition = len(district_data['market'].unique())
|
113 |
-
|
114 |
# Prepare comprehensive market summary
|
115 |
market_summary = {
|
116 |
"high_value_crops": high_value_crops.index.tolist(),
|
@@ -168,69 +170,71 @@ def get_ai_insights(market_data, state, district):
|
|
168 |
"""
|
169 |
api_url = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-1B-Instruct/v1/chat/completions"
|
170 |
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
|
|
|
171 |
payload = {
|
172 |
"inputs": prompt
|
173 |
}
|
174 |
-
|
175 |
-
response = requests.post(api_url,headers=headers, json=payload)
|
176 |
if response.status_code == 200:
|
177 |
response_data = response.json()
|
178 |
-
if (response_data and
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
insights = response_data['choices'][0]['message']['content']
|
185 |
formatted_insights = format_ai_insights(insights)
|
186 |
return formatted_insights
|
187 |
-
|
188 |
return "AI insights temporarily unavailable"
|
189 |
|
190 |
except Exception as e:
|
191 |
print(f"Error generating insights: {str(e)}")
|
192 |
return f"Could not generate insights: {str(e)}"
|
193 |
|
|
|
194 |
def generate_plots(df, lang='en'):
|
195 |
"""Generate all plots with language support"""
|
196 |
if df.empty:
|
197 |
return {}, "No data available"
|
198 |
-
|
199 |
# Convert price columns to numeric
|
200 |
price_cols = ['min_price', 'max_price', 'modal_price']
|
201 |
for col in price_cols:
|
202 |
df[col] = pd.to_numeric(df[col], errors='coerce')
|
203 |
-
|
204 |
# Color scheme
|
205 |
colors = ["#4CAF50", "#8BC34A", "#CDDC39", "#FFC107", "#FF5722"]
|
206 |
-
|
207 |
# 1. Bar Chart
|
208 |
df_bar = df.groupby('commodity')['modal_price'].mean().reset_index()
|
209 |
-
fig_bar = px.bar(df_bar,
|
210 |
-
x='commodity',
|
211 |
y='modal_price',
|
212 |
-
title=translate_to_marathi(
|
|
|
213 |
color_discrete_sequence=colors)
|
214 |
-
|
215 |
# 2. Line Chart (if commodity selected)
|
216 |
fig_line = None
|
217 |
if 'commodity' in df.columns and len(df['commodity'].unique()) == 1:
|
218 |
df['arrival_date'] = pd.to_datetime(df['arrival_date'])
|
219 |
df_line = df.sort_values('arrival_date')
|
220 |
-
fig_line = px.line(df_line,
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
# 3. Box Plot
|
227 |
-
fig_box = px.box(df,
|
228 |
-
x='commodity',
|
229 |
y='modal_price',
|
230 |
title=translate_to_marathi("Price Distribution") if lang == 'mr' else "Price Distribution",
|
231 |
color='commodity',
|
232 |
color_discrete_sequence=colors)
|
233 |
-
|
234 |
# Convert to HTML
|
235 |
plots = {
|
236 |
'bar': pio.to_html(fig_bar, full_html=False),
|
@@ -238,17 +242,19 @@ def generate_plots(df, lang='en'):
|
|
238 |
}
|
239 |
if fig_line:
|
240 |
plots['line'] = pio.to_html(fig_line, full_html=False)
|
241 |
-
|
242 |
return plots
|
243 |
|
|
|
244 |
@app.route('/')
|
245 |
def index():
|
246 |
"""Render main page"""
|
247 |
initial_data = fetch_market_data()
|
248 |
states = sorted(initial_data['state'].dropna().unique())
|
249 |
-
return render_template('index.html',
|
250 |
-
|
251 |
-
|
|
|
252 |
|
253 |
@app.route('/filter_data', methods=['POST'])
|
254 |
def filter_data():
|
@@ -258,11 +264,11 @@ def filter_data():
|
|
258 |
market = request.form.get('market')
|
259 |
commodity = request.form.get('commodity')
|
260 |
lang = request.form.get('language', 'en')
|
261 |
-
|
262 |
df = fetch_market_data(state, district, market, commodity)
|
263 |
plots = generate_plots(df, lang)
|
264 |
insights = get_ai_insights(df, state, district) if state and district and not df.empty else ""
|
265 |
-
|
266 |
# Generate market data table HTML
|
267 |
market_table_html = """
|
268 |
<div class="table-responsive">
|
@@ -283,7 +289,7 @@ def filter_data():
|
|
283 |
</thead>
|
284 |
<tbody>
|
285 |
"""
|
286 |
-
|
287 |
for _, row in df.iterrows():
|
288 |
market_table_html += f"""
|
289 |
<tr>
|
@@ -315,7 +321,7 @@ def filter_data():
|
|
315 |
</thead>
|
316 |
<tbody>
|
317 |
"""
|
318 |
-
|
319 |
for _, row in cheapest_crops.iterrows():
|
320 |
cheapest_table_html += f"""
|
321 |
<tr>
|
@@ -340,7 +346,7 @@ def filter_data():
|
|
340 |
</thead>
|
341 |
<tbody>
|
342 |
"""
|
343 |
-
|
344 |
for _, row in costliest_crops.iterrows():
|
345 |
costliest_table_html += f"""
|
346 |
<tr>
|
@@ -370,9 +376,10 @@ def filter_data():
|
|
370 |
'costliest_html': costliest_table_html,
|
371 |
'market_stats': market_stats
|
372 |
}
|
373 |
-
|
374 |
return jsonify(response)
|
375 |
|
|
|
376 |
def format_ai_insights(insights_data, lang='en'):
|
377 |
"""Format AI insights into structured HTML with language support"""
|
378 |
# Translation dictionary for section headers and labels
|
@@ -419,7 +426,7 @@ def format_ai_insights(insights_data, lang='en'):
|
|
419 |
<h3 class="en">AI Market Insights</h3>
|
420 |
<h3 class="mr" style="display:none;">एआय बाजार विश्लेषण</h3>
|
421 |
</div>
|
422 |
-
|
423 |
<div class="insight-section">
|
424 |
<h4>Immediate Market Opportunities</h4>
|
425 |
<div class="insight-card">
|
@@ -429,7 +436,7 @@ def format_ai_insights(insights_data, lang='en'):
|
|
429 |
<li>Bottle gourd premium quality fetching <span class="price-highlight">₹150 per kg</span></li>
|
430 |
</ul>
|
431 |
</div>
|
432 |
-
|
433 |
<div class="insight-card">
|
434 |
<h5>Current Market Status</h5>
|
435 |
<ul class="insight-list">
|
@@ -438,7 +445,7 @@ def format_ai_insights(insights_data, lang='en'):
|
|
438 |
</ul>
|
439 |
</div>
|
440 |
</div>
|
441 |
-
|
442 |
<div class="insight-section">
|
443 |
<h4>Strategic Planning</h4>
|
444 |
<div class="insight-card">
|
@@ -448,7 +455,7 @@ def format_ai_insights(insights_data, lang='en'):
|
|
448 |
<li>Best planting time: Spring season for cauliflower and bottle gourd</li>
|
449 |
</ul>
|
450 |
</div>
|
451 |
-
|
452 |
<div class="insight-card">
|
453 |
<h5>Recommended Crop Combinations</h5>
|
454 |
<ul class="insight-list">
|
@@ -456,7 +463,7 @@ def format_ai_insights(insights_data, lang='en'):
|
|
456 |
</ul>
|
457 |
</div>
|
458 |
</div>
|
459 |
-
|
460 |
<div class="insight-section">
|
461 |
<h4>Risk Management & Market Strategy</h4>
|
462 |
<div class="insight-card">
|
@@ -467,7 +474,7 @@ def format_ai_insights(insights_data, lang='en'):
|
|
467 |
</ul>
|
468 |
</div>
|
469 |
</div>
|
470 |
-
|
471 |
<div class="action-box">
|
472 |
<h5>Recommended Actions</h5>
|
473 |
<ul class="action-list">
|
@@ -482,9 +489,10 @@ def format_ai_insights(insights_data, lang='en'):
|
|
482 |
html = translate_text(html)
|
483 |
# print(html
|
484 |
return html
|
485 |
-
|
486 |
return html
|
487 |
|
|
|
488 |
@app.route('/get_districts', methods=['POST'])
|
489 |
def get_districts():
|
490 |
"""Get districts for selected state"""
|
@@ -493,6 +501,7 @@ def get_districts():
|
|
493 |
districts = sorted(df['district'].dropna().unique())
|
494 |
return jsonify(districts)
|
495 |
|
|
|
496 |
@app.route('/get_markets', methods=['POST'])
|
497 |
def get_markets():
|
498 |
"""Get markets for selected district"""
|
@@ -501,6 +510,7 @@ def get_markets():
|
|
501 |
markets = sorted(df['market'].dropna().unique())
|
502 |
return jsonify(markets)
|
503 |
|
|
|
504 |
@app.route('/get_commodities', methods=['POST'])
|
505 |
def get_commodities():
|
506 |
"""Get commodities for selected market"""
|
@@ -509,5 +519,6 @@ def get_commodities():
|
|
509 |
commodities = sorted(df['commodity'].dropna().unique())
|
510 |
return jsonify(commodities)
|
511 |
|
512 |
-
|
513 |
-
|
|
|
|
8 |
from googletrans import Translator
|
9 |
import numpy as np
|
10 |
|
11 |
+
app = Flask(_name_)
|
12 |
|
13 |
# Initialize translator
|
14 |
translator = Translator()
|
|
|
34 |
'Top 5 Costliest Crops': 'सर्वात महाग 5 पिके'
|
35 |
}
|
36 |
|
37 |
+
|
38 |
def translate_to_marathi(text):
|
39 |
"""Translate text to Marathi"""
|
40 |
try:
|
|
|
45 |
except:
|
46 |
return text
|
47 |
|
48 |
+
|
49 |
def fetch_market_data(state=None, district=None, market=None, commodity=None):
|
50 |
"""Fetch data from the agricultural market API"""
|
51 |
+
api_key = os.getenv("data_api_key")
|
52 |
print(api_key)
|
53 |
base_url = "https://api.data.gov.in/resource/9ef84268-d588-465a-a308-a864a43d0070"
|
54 |
+
|
55 |
params = {
|
56 |
"api-key": api_key,
|
57 |
"format": "json",
|
58 |
+
"limit": 15000,
|
59 |
}
|
60 |
+
|
61 |
# Add filters if provided
|
62 |
if state:
|
63 |
params["filters[state]"] = state
|
|
|
91 |
try:
|
92 |
# Calculate additional market metrics
|
93 |
district_data = market_data[market_data['district'] == district]
|
94 |
+
|
95 |
# Price trends and volatility
|
96 |
price_trends = district_data.groupby('commodity').agg({
|
97 |
'modal_price': ['mean', 'min', 'max', 'std']
|
98 |
}).round(2)
|
99 |
+
|
100 |
# Calculate price stability (lower std/mean ratio indicates more stable prices)
|
101 |
+
price_trends['price_stability'] = (price_trends['modal_price']['std'] /
|
102 |
+
price_trends['modal_price']['mean']).round(2)
|
103 |
+
|
104 |
# Identify commodities with consistent high prices
|
105 |
+
high_value_crops = price_trends[price_trends['modal_price']['mean'] >
|
106 |
+
price_trends['modal_price']['mean'].median()]
|
107 |
+
|
108 |
# Get seasonal patterns
|
109 |
district_data['arrival_date'] = pd.to_datetime(district_data['arrival_date'])
|
110 |
district_data['month'] = district_data['arrival_date'].dt.month
|
111 |
monthly_trends = district_data.groupby(['commodity', 'month'])['modal_price'].mean().round(2)
|
112 |
+
|
113 |
# Market competition analysis
|
114 |
market_competition = len(district_data['market'].unique())
|
115 |
+
|
116 |
# Prepare comprehensive market summary
|
117 |
market_summary = {
|
118 |
"high_value_crops": high_value_crops.index.tolist(),
|
|
|
170 |
"""
|
171 |
api_url = "https://api-inference.huggingface.co/models/meta-llama/Llama-3.2-1B-Instruct/v1/chat/completions"
|
172 |
headers = {"Authorization": f"Bearer {os.getenv('HUGGINGFACE_API_KEY')}"}
|
173 |
+
|
174 |
payload = {
|
175 |
"inputs": prompt
|
176 |
}
|
177 |
+
|
178 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
179 |
if response.status_code == 200:
|
180 |
response_data = response.json()
|
181 |
+
if (response_data and
|
182 |
+
'choices' in response_data and
|
183 |
+
len(response_data['choices']) > 0 and
|
184 |
+
'message' in response_data['choices'][0] and
|
185 |
+
'content' in response_data['choices'][0]['message']):
|
|
|
186 |
insights = response_data['choices'][0]['message']['content']
|
187 |
formatted_insights = format_ai_insights(insights)
|
188 |
return formatted_insights
|
189 |
+
|
190 |
return "AI insights temporarily unavailable"
|
191 |
|
192 |
except Exception as e:
|
193 |
print(f"Error generating insights: {str(e)}")
|
194 |
return f"Could not generate insights: {str(e)}"
|
195 |
|
196 |
+
|
197 |
def generate_plots(df, lang='en'):
|
198 |
"""Generate all plots with language support"""
|
199 |
if df.empty:
|
200 |
return {}, "No data available"
|
201 |
+
|
202 |
# Convert price columns to numeric
|
203 |
price_cols = ['min_price', 'max_price', 'modal_price']
|
204 |
for col in price_cols:
|
205 |
df[col] = pd.to_numeric(df[col], errors='coerce')
|
206 |
+
|
207 |
# Color scheme
|
208 |
colors = ["#4CAF50", "#8BC34A", "#CDDC39", "#FFC107", "#FF5722"]
|
209 |
+
|
210 |
# 1. Bar Chart
|
211 |
df_bar = df.groupby('commodity')['modal_price'].mean().reset_index()
|
212 |
+
fig_bar = px.bar(df_bar,
|
213 |
+
x='commodity',
|
214 |
y='modal_price',
|
215 |
+
title=translate_to_marathi(
|
216 |
+
"Average Price by Commodity") if lang == 'mr' else "Average Price by Commodity",
|
217 |
color_discrete_sequence=colors)
|
218 |
+
|
219 |
# 2. Line Chart (if commodity selected)
|
220 |
fig_line = None
|
221 |
if 'commodity' in df.columns and len(df['commodity'].unique()) == 1:
|
222 |
df['arrival_date'] = pd.to_datetime(df['arrival_date'])
|
223 |
df_line = df.sort_values('arrival_date')
|
224 |
+
fig_line = px.line(df_line,
|
225 |
+
x='arrival_date',
|
226 |
+
y='modal_price',
|
227 |
+
title=translate_to_marathi("Price Trend") if lang == 'mr' else "Price Trend",
|
228 |
+
color_discrete_sequence=colors)
|
229 |
+
|
230 |
# 3. Box Plot
|
231 |
+
fig_box = px.box(df,
|
232 |
+
x='commodity',
|
233 |
y='modal_price',
|
234 |
title=translate_to_marathi("Price Distribution") if lang == 'mr' else "Price Distribution",
|
235 |
color='commodity',
|
236 |
color_discrete_sequence=colors)
|
237 |
+
|
238 |
# Convert to HTML
|
239 |
plots = {
|
240 |
'bar': pio.to_html(fig_bar, full_html=False),
|
|
|
242 |
}
|
243 |
if fig_line:
|
244 |
plots['line'] = pio.to_html(fig_line, full_html=False)
|
245 |
+
|
246 |
return plots
|
247 |
|
248 |
+
|
249 |
@app.route('/')
|
250 |
def index():
|
251 |
"""Render main page"""
|
252 |
initial_data = fetch_market_data()
|
253 |
states = sorted(initial_data['state'].dropna().unique())
|
254 |
+
return render_template('index.html',
|
255 |
+
states=states,
|
256 |
+
today=datetime.today().strftime('%Y-%m-%d'))
|
257 |
+
|
258 |
|
259 |
@app.route('/filter_data', methods=['POST'])
|
260 |
def filter_data():
|
|
|
264 |
market = request.form.get('market')
|
265 |
commodity = request.form.get('commodity')
|
266 |
lang = request.form.get('language', 'en')
|
267 |
+
|
268 |
df = fetch_market_data(state, district, market, commodity)
|
269 |
plots = generate_plots(df, lang)
|
270 |
insights = get_ai_insights(df, state, district) if state and district and not df.empty else ""
|
271 |
+
|
272 |
# Generate market data table HTML
|
273 |
market_table_html = """
|
274 |
<div class="table-responsive">
|
|
|
289 |
</thead>
|
290 |
<tbody>
|
291 |
"""
|
292 |
+
|
293 |
for _, row in df.iterrows():
|
294 |
market_table_html += f"""
|
295 |
<tr>
|
|
|
321 |
</thead>
|
322 |
<tbody>
|
323 |
"""
|
324 |
+
|
325 |
for _, row in cheapest_crops.iterrows():
|
326 |
cheapest_table_html += f"""
|
327 |
<tr>
|
|
|
346 |
</thead>
|
347 |
<tbody>
|
348 |
"""
|
349 |
+
|
350 |
for _, row in costliest_crops.iterrows():
|
351 |
costliest_table_html += f"""
|
352 |
<tr>
|
|
|
376 |
'costliest_html': costliest_table_html,
|
377 |
'market_stats': market_stats
|
378 |
}
|
379 |
+
|
380 |
return jsonify(response)
|
381 |
|
382 |
+
|
383 |
def format_ai_insights(insights_data, lang='en'):
|
384 |
"""Format AI insights into structured HTML with language support"""
|
385 |
# Translation dictionary for section headers and labels
|
|
|
426 |
<h3 class="en">AI Market Insights</h3>
|
427 |
<h3 class="mr" style="display:none;">एआय बाजार विश्लेषण</h3>
|
428 |
</div>
|
429 |
+
|
430 |
<div class="insight-section">
|
431 |
<h4>Immediate Market Opportunities</h4>
|
432 |
<div class="insight-card">
|
|
|
436 |
<li>Bottle gourd premium quality fetching <span class="price-highlight">₹150 per kg</span></li>
|
437 |
</ul>
|
438 |
</div>
|
439 |
+
|
440 |
<div class="insight-card">
|
441 |
<h5>Current Market Status</h5>
|
442 |
<ul class="insight-list">
|
|
|
445 |
</ul>
|
446 |
</div>
|
447 |
</div>
|
448 |
+
|
449 |
<div class="insight-section">
|
450 |
<h4>Strategic Planning</h4>
|
451 |
<div class="insight-card">
|
|
|
455 |
<li>Best planting time: Spring season for cauliflower and bottle gourd</li>
|
456 |
</ul>
|
457 |
</div>
|
458 |
+
|
459 |
<div class="insight-card">
|
460 |
<h5>Recommended Crop Combinations</h5>
|
461 |
<ul class="insight-list">
|
|
|
463 |
</ul>
|
464 |
</div>
|
465 |
</div>
|
466 |
+
|
467 |
<div class="insight-section">
|
468 |
<h4>Risk Management & Market Strategy</h4>
|
469 |
<div class="insight-card">
|
|
|
474 |
</ul>
|
475 |
</div>
|
476 |
</div>
|
477 |
+
|
478 |
<div class="action-box">
|
479 |
<h5>Recommended Actions</h5>
|
480 |
<ul class="action-list">
|
|
|
489 |
html = translate_text(html)
|
490 |
# print(html
|
491 |
return html
|
492 |
+
|
493 |
return html
|
494 |
|
495 |
+
|
496 |
@app.route('/get_districts', methods=['POST'])
|
497 |
def get_districts():
|
498 |
"""Get districts for selected state"""
|
|
|
501 |
districts = sorted(df['district'].dropna().unique())
|
502 |
return jsonify(districts)
|
503 |
|
504 |
+
|
505 |
@app.route('/get_markets', methods=['POST'])
|
506 |
def get_markets():
|
507 |
"""Get markets for selected district"""
|
|
|
510 |
markets = sorted(df['market'].dropna().unique())
|
511 |
return jsonify(markets)
|
512 |
|
513 |
+
|
514 |
@app.route('/get_commodities', methods=['POST'])
|
515 |
def get_commodities():
|
516 |
"""Get commodities for selected market"""
|
|
|
519 |
commodities = sorted(df['commodity'].dropna().unique())
|
520 |
return jsonify(commodities)
|
521 |
|
522 |
+
|
523 |
+
if _name_ == '_main_':
|
524 |
+
app.run(debug=True, host='0.0.0.0', port=7860)
|