Delete app.py
Browse files
app.py
DELETED
@@ -1,188 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import requests
|
3 |
-
from datetime import datetime
|
4 |
-
|
5 |
-
# Use your deployed server instead of localhost
|
6 |
-
MCP_SERVER_URL = "https://elanuk-mcp-hf.hf.space"
|
7 |
-
|
8 |
-
# Bihar districts list
|
9 |
-
BIHAR_DISTRICTS = [
|
10 |
-
"Patna", "Gaya", "Bhagalpur", "Muzaffarpur", "Darbhanga", "Siwan",
|
11 |
-
"Begusarai", "Katihar", "Nalanda", "Rohtas", "Saran", "Samastipur",
|
12 |
-
"Madhubani", "Purnia", "Araria", "Kishanganj", "Supaul", "Madhepura",
|
13 |
-
"Saharsa", "Khagaria", "Munger", "Lakhisarai", "Sheikhpura", "Nawada",
|
14 |
-
"Jamui", "Jehanabad", "Aurangabad", "Arwal", "Kaimur", "Buxar",
|
15 |
-
"Bhojpur", "Saran", "Siwan", "Gopalganj", "East Champaran", "West Champaran",
|
16 |
-
"Sitamarhi", "Sheohar", "Vaishali"
|
17 |
-
]
|
18 |
-
|
19 |
-
def format_workflow_output(raw_output):
|
20 |
-
"""Format the workflow output for display"""
|
21 |
-
if not raw_output:
|
22 |
-
return "β No output received"
|
23 |
-
|
24 |
-
lines = raw_output.split('\n')
|
25 |
-
formatted_lines = []
|
26 |
-
|
27 |
-
for line in lines:
|
28 |
-
line = line.strip()
|
29 |
-
if not line:
|
30 |
-
formatted_lines.append("")
|
31 |
-
continue
|
32 |
-
|
33 |
-
if line.startswith('πΎ') and 'Workflow' in line:
|
34 |
-
formatted_lines.append(f"## {line}")
|
35 |
-
elif line.startswith('=') or line.startswith('-'):
|
36 |
-
continue
|
37 |
-
elif line.startswith('π€οΈ') or line.startswith('β
Workflow'):
|
38 |
-
formatted_lines.append(f"### {line}")
|
39 |
-
elif line.startswith('π±') or line.startswith('π') or line.startswith('ποΈ') or line.startswith('π€'):
|
40 |
-
formatted_lines.append(f"#### {line}")
|
41 |
-
elif line.startswith('β
') or line.startswith('β'):
|
42 |
-
formatted_lines.append(f"- {line}")
|
43 |
-
else:
|
44 |
-
formatted_lines.append(line)
|
45 |
-
|
46 |
-
return '\n'.join(formatted_lines)
|
47 |
-
|
48 |
-
def format_alert_summary(raw_data):
|
49 |
-
"""Create a formatted summary of the alert data"""
|
50 |
-
if not raw_data or 'alert_data' not in raw_data:
|
51 |
-
return "No alert data available"
|
52 |
-
|
53 |
-
alert_data = raw_data['alert_data']
|
54 |
-
|
55 |
-
summary = f"""
|
56 |
-
## π¨ Alert Summary
|
57 |
-
|
58 |
-
**π Location:** {alert_data['location']['village']}, {alert_data['location']['district']}, {alert_data['location']['state']}
|
59 |
-
|
60 |
-
**πΎ Crop Information:**
|
61 |
-
- **Crop:** {alert_data['crop']['name'].title()}
|
62 |
-
- **Growth Stage:** {alert_data['crop']['stage']}
|
63 |
-
- **Season:** {alert_data['crop']['season'].title()}
|
64 |
-
|
65 |
-
**π€οΈ Weather Conditions:**
|
66 |
-
- **Temperature:** {alert_data['weather']['temperature']}
|
67 |
-
- **Expected Rainfall:** {alert_data['weather']['expected_rainfall']}
|
68 |
-
- **Wind Speed:** {alert_data['weather']['wind_speed']}
|
69 |
-
- **Rain Probability:** {alert_data['weather']['rain_probability']}%
|
70 |
-
|
71 |
-
**β οΈ Alert Details:**
|
72 |
-
- **Type:** {alert_data['alert']['type'].replace('_', ' ').title()}
|
73 |
-
- **Urgency:** {alert_data['alert']['urgency'].upper()}
|
74 |
-
- **AI Enhanced:** {'β
Yes' if alert_data['alert']['ai_generated'] else 'β No'}
|
75 |
-
|
76 |
-
**π¨ Alert Message:**
|
77 |
-
{alert_data['alert']['message']}
|
78 |
-
|
79 |
-
**π― Action Items:**
|
80 |
-
{chr(10).join([f"- {item.replace('_', ' ').title()}" for item in alert_data['alert']['action_items']])}
|
81 |
-
"""
|
82 |
-
return summary
|
83 |
-
|
84 |
-
def run_workflow(district):
|
85 |
-
"""Run the workflow and return results"""
|
86 |
-
if not district:
|
87 |
-
return "β Please select a district", "", ""
|
88 |
-
|
89 |
-
try:
|
90 |
-
payload = {
|
91 |
-
"state": "bihar",
|
92 |
-
"district": district.lower()
|
93 |
-
}
|
94 |
-
|
95 |
-
# Call your existing deployed server
|
96 |
-
response = requests.post(
|
97 |
-
f"{MCP_SERVER_URL}/api/run-workflow",
|
98 |
-
json=payload,
|
99 |
-
timeout=60
|
100 |
-
)
|
101 |
-
|
102 |
-
if response.status_code == 200:
|
103 |
-
result = response.json()
|
104 |
-
|
105 |
-
workflow_output = format_workflow_output(result.get('message', ''))
|
106 |
-
alert_summary = format_alert_summary(result.get('raw_data', {}))
|
107 |
-
csv_content = result.get('csv', '')
|
108 |
-
|
109 |
-
# Create CSV file if content exists
|
110 |
-
if csv_content:
|
111 |
-
filename = f"bihar_alert_{district.lower()}_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"
|
112 |
-
with open(filename, 'w', encoding='utf-8') as f:
|
113 |
-
f.write(csv_content)
|
114 |
-
return workflow_output, alert_summary, gr.File(value=filename, visible=True)
|
115 |
-
else:
|
116 |
-
return workflow_output, alert_summary, gr.File(visible=False)
|
117 |
-
|
118 |
-
else:
|
119 |
-
error_msg = f"β Server Error ({response.status_code})"
|
120 |
-
return error_msg, "", gr.File(visible=False)
|
121 |
-
|
122 |
-
except Exception as e:
|
123 |
-
error_msg = f"β Error: {str(e)}"
|
124 |
-
return error_msg, "", gr.File(visible=False)
|
125 |
-
|
126 |
-
# Create Gradio interface
|
127 |
-
with gr.Blocks(
|
128 |
-
title="BIHAR AgMCP - Agricultural Weather Alerts",
|
129 |
-
theme=gr.themes.Soft()
|
130 |
-
) as demo:
|
131 |
-
|
132 |
-
gr.Markdown("""
|
133 |
-
# πΎ BIHAR AgMCP - Agricultural Weather Alert System
|
134 |
-
|
135 |
-
**AI-Powered Weather Alerts for Bihar Farmers**
|
136 |
-
|
137 |
-
Generate personalized weather alerts for agricultural activities in Bihar districts.
|
138 |
-
|
139 |
-
## How to Use:
|
140 |
-
1. Select a Bihar district from the dropdown
|
141 |
-
2. Click "Generate Weather Alert"
|
142 |
-
3. View the formatted results and download CSV data
|
143 |
-
""")
|
144 |
-
|
145 |
-
with gr.Row():
|
146 |
-
with gr.Column(scale=1):
|
147 |
-
district_input = gr.Dropdown(
|
148 |
-
choices=BIHAR_DISTRICTS,
|
149 |
-
label="π Select Bihar District",
|
150 |
-
value="Patna"
|
151 |
-
)
|
152 |
-
|
153 |
-
run_btn = gr.Button(
|
154 |
-
"π Generate Weather Alert",
|
155 |
-
variant="primary",
|
156 |
-
size="lg"
|
157 |
-
)
|
158 |
-
|
159 |
-
with gr.Row():
|
160 |
-
with gr.Column(scale=2):
|
161 |
-
workflow_output = gr.Markdown(
|
162 |
-
label="π Workflow Output",
|
163 |
-
value="Select a district and click the button to generate alerts..."
|
164 |
-
)
|
165 |
-
|
166 |
-
with gr.Column(scale=1):
|
167 |
-
alert_summary = gr.Markdown(
|
168 |
-
label="π Alert Summary",
|
169 |
-
value="Alert details will appear here..."
|
170 |
-
)
|
171 |
-
|
172 |
-
csv_output = gr.File(
|
173 |
-
label="π Download CSV Data",
|
174 |
-
visible=False
|
175 |
-
)
|
176 |
-
|
177 |
-
# Connect the button
|
178 |
-
run_btn.click(
|
179 |
-
run_workflow,
|
180 |
-
inputs=[district_input],
|
181 |
-
outputs=[workflow_output, alert_summary, csv_output]
|
182 |
-
)
|
183 |
-
|
184 |
-
if __name__ == "__main__":
|
185 |
-
demo.launch(
|
186 |
-
server_name="0.0.0.0",
|
187 |
-
server_port=7860
|
188 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|