Spaces:
Sleeping
Sleeping
AdityaAdaki
commited on
Commit
·
f510322
1
Parent(s):
234a210
logs
Browse files
app.py
CHANGED
@@ -458,140 +458,62 @@ def generate_plots(df):
|
|
458 |
|
459 |
return plots
|
460 |
|
|
|
|
|
|
|
|
|
|
|
|
|
461 |
@app.route('/')
|
462 |
def index():
|
463 |
try:
|
|
|
464 |
initial_data = fetch_market_data()
|
465 |
states = sorted(initial_data['state'].dropna().unique()) if not initial_data.empty else []
|
466 |
except Exception as e:
|
467 |
-
|
468 |
states = []
|
469 |
|
470 |
try:
|
|
|
471 |
return render_template('index.html',
|
472 |
-
|
473 |
-
|
474 |
except Exception as e:
|
475 |
-
|
476 |
return f"Error loading application: {str(e)}", 500
|
477 |
|
478 |
@app.route('/filter_data', methods=['POST'])
|
479 |
def filter_data():
|
|
|
480 |
state = request.form.get('state')
|
481 |
district = request.form.get('district')
|
482 |
market = request.form.get('market')
|
483 |
commodity = request.form.get('commodity')
|
484 |
language = request.form.get('language', 'English') # Default to English
|
485 |
|
486 |
-
|
487 |
-
|
488 |
-
|
489 |
-
|
490 |
-
|
491 |
-
|
492 |
-
|
493 |
-
|
494 |
-
|
495 |
-
|
496 |
-
|
497 |
-
|
498 |
-
|
499 |
-
|
500 |
-
|
501 |
-
|
502 |
-
|
503 |
-
|
504 |
-
|
505 |
-
|
506 |
-
|
507 |
-
|
508 |
-
<tbody>
|
509 |
-
"""
|
510 |
-
|
511 |
-
for _, row in df.iterrows():
|
512 |
-
market_table_html += f"""
|
513 |
-
<tr>
|
514 |
-
<td>{row['state']}</td>
|
515 |
-
<td>{row['district']}</td>
|
516 |
-
<td>{row['market']}</td>
|
517 |
-
<td>{row['commodity']}</td>
|
518 |
-
<td>{row['variety']}</td>
|
519 |
-
<td>{row['grade']}</td>
|
520 |
-
<td>{row['arrival_date']}</td>
|
521 |
-
<td>₹{row['min_price']}</td>
|
522 |
-
<td>₹{row['max_price']}</td>
|
523 |
-
<td>₹{row['modal_price']}</td>
|
524 |
-
</tr>
|
525 |
-
"""
|
526 |
-
market_table_html += "</tbody></table></div>"
|
527 |
-
|
528 |
-
cheapest_crops = df.sort_values('modal_price', ascending=True).head(5)
|
529 |
-
cheapest_table_html = """
|
530 |
-
<div class="table-responsive">
|
531 |
-
<table class="table table-sm table-bordered">
|
532 |
-
<thead>
|
533 |
-
<tr>
|
534 |
-
<th>Commodity</th>
|
535 |
-
<th>Market</th>
|
536 |
-
<th>Modal Price</th>
|
537 |
-
</tr>
|
538 |
-
</thead>
|
539 |
-
<tbody>
|
540 |
-
"""
|
541 |
-
|
542 |
-
for _, row in cheapest_crops.iterrows():
|
543 |
-
cheapest_table_html += f"""
|
544 |
-
<tr>
|
545 |
-
<td>{row['commodity']}</td>
|
546 |
-
<td>{row['market']}</td>
|
547 |
-
<td>₹{row['modal_price']}</td>
|
548 |
-
</tr>
|
549 |
-
"""
|
550 |
-
cheapest_table_html += "</tbody></table></div>"
|
551 |
-
|
552 |
-
costliest_crops = df.sort_values('modal_price', ascending=False).head(5)
|
553 |
-
costliest_table_html = """
|
554 |
-
<div class="table-responsive">
|
555 |
-
<table class="table table-sm table-bordered">
|
556 |
-
<thead>
|
557 |
-
<tr>
|
558 |
-
<th>Commodity</th>
|
559 |
-
<th>Market</th>
|
560 |
-
<th>Modal Price</th>
|
561 |
-
</tr>
|
562 |
-
</thead>
|
563 |
-
<tbody>
|
564 |
-
"""
|
565 |
-
|
566 |
-
for _, row in costliest_crops.iterrows():
|
567 |
-
costliest_table_html += f"""
|
568 |
-
<tr>
|
569 |
-
<td>{row['commodity']}</td>
|
570 |
-
<td>{row['market']}</td>
|
571 |
-
<td>₹{row['modal_price']}</td>
|
572 |
-
</tr>
|
573 |
-
"""
|
574 |
-
costliest_table_html += "</tbody></table></div>"
|
575 |
-
|
576 |
-
market_stats = {
|
577 |
-
'total_commodities': len(df['commodity'].unique()),
|
578 |
-
'avg_modal_price': f"₹{df['modal_price'].mean():.2f}",
|
579 |
-
'price_range': f"₹{df['modal_price'].min():.2f} - ₹{df['modal_price'].max():.2f}",
|
580 |
-
'total_markets': len(df['market'].unique())
|
581 |
-
}
|
582 |
-
|
583 |
-
response = {
|
584 |
-
'plots': plots,
|
585 |
-
'insights': insights,
|
586 |
-
'success': not df.empty,
|
587 |
-
'hasStateDistrict': bool(state and district),
|
588 |
-
'market_html': market_table_html,
|
589 |
-
'cheapest_html': cheapest_table_html,
|
590 |
-
'costliest_html': costliest_table_html,
|
591 |
-
'market_stats': market_stats
|
592 |
-
}
|
593 |
-
|
594 |
-
return jsonify(response)
|
595 |
|
596 |
@app.route('/get_districts', methods=['POST'])
|
597 |
def get_districts():
|
|
|
458 |
|
459 |
return plots
|
460 |
|
461 |
+
# Configure logging
|
462 |
+
logging.basicConfig(level=logging.INFO)
|
463 |
+
handler = RotatingFileHandler('app.log', maxBytes=10000, backupCount=1)
|
464 |
+
handler.setLevel(logging.INFO)
|
465 |
+
app.logger.addHandler(handler)
|
466 |
+
|
467 |
@app.route('/')
|
468 |
def index():
|
469 |
try:
|
470 |
+
app.logger.info("Fetching initial market data")
|
471 |
initial_data = fetch_market_data()
|
472 |
states = sorted(initial_data['state'].dropna().unique()) if not initial_data.empty else []
|
473 |
except Exception as e:
|
474 |
+
app.logger.error(f"Error fetching initial data: {str(e)}")
|
475 |
states = []
|
476 |
|
477 |
try:
|
478 |
+
app.logger.info("Rendering index template")
|
479 |
return render_template('index.html',
|
480 |
+
states=states,
|
481 |
+
today=datetime.today().strftime('%Y-%m-%d'))
|
482 |
except Exception as e:
|
483 |
+
app.logger.error(f"Template rendering error: {str(e)}")
|
484 |
return f"Error loading application: {str(e)}", 500
|
485 |
|
486 |
@app.route('/filter_data', methods=['POST'])
|
487 |
def filter_data():
|
488 |
+
app.logger.info("Received filter_data request")
|
489 |
state = request.form.get('state')
|
490 |
district = request.form.get('district')
|
491 |
market = request.form.get('market')
|
492 |
commodity = request.form.get('commodity')
|
493 |
language = request.form.get('language', 'English') # Default to English
|
494 |
|
495 |
+
try:
|
496 |
+
df = fetch_market_data(state, district, market, commodity)
|
497 |
+
plots = generate_plots(df)
|
498 |
+
insights = get_ai_insights(df, state, district, market, commodity, language) if state and district and not df.empty else ""
|
499 |
+
|
500 |
+
app.logger.info("Successfully processed filter_data request")
|
501 |
+
|
502 |
+
response = {
|
503 |
+
'plots': plots,
|
504 |
+
'insights': insights,
|
505 |
+
'success': not df.empty,
|
506 |
+
'hasStateDistrict': bool(state and district),
|
507 |
+
'market_html': market_table_html,
|
508 |
+
'cheapest_html': cheapest_table_html,
|
509 |
+
'costliest_html': costliest_table_html,
|
510 |
+
'market_stats': market_stats
|
511 |
+
}
|
512 |
+
|
513 |
+
return jsonify(response)
|
514 |
+
except Exception as e:
|
515 |
+
app.logger.error(f"Error processing filter_data request: {str(e)}")
|
516 |
+
return jsonify({'success': False, 'error': str(e)}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
517 |
|
518 |
@app.route('/get_districts', methods=['POST'])
|
519 |
def get_districts():
|