File size: 1,038 Bytes
9142902
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
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
def format_response(result: dict, source: str):
    """Format the Q&A result for Streamlit display."""

    if not result or "error" in result:
        return f"❌ {result.get('error', 'No valid data found.')}"

    states = [s.title() for s in result.get("states", [])]
    crop = result.get("crop", "N/A").title()
    text = f"πŸ“Š Analysis for {', '.join(states)} β€” Crop: {crop}\n\n"

    # 🌧️ Rainfall Summary
    rainfall = result.get("rainfall_summary", {})
    if rainfall:
        text += "🌧️ Average Rainfall (mm):\n"
        for state, value in rainfall.items():
            text += f" β€’ {state.title()}: {round(value, 2)}\n"
        text += "\n"

    # 🌾 Production Summary
    production = result.get("production_summary", {})
    if production:
        text += "🌾 Total Production (tonnes):\n"
        for state, value in production.items():
            text += f" β€’ {state.title()}: {int(value):,}\n"
        text += "\n"

    # πŸ“š Citation
    text += f"πŸ“š Data Source: {source}"
    return text