Spaces:
Paused
Paused
NTEIGBANAM OKE commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,209 +1,299 @@
|
|
| 1 |
-
from pathlib import Path
|
| 2 |
-
|
| 3 |
-
app_code = r'''
|
| 4 |
import os
|
| 5 |
-
import csv
|
| 6 |
import math
|
| 7 |
import re
|
| 8 |
-
|
|
|
|
| 9 |
from pathlib import Path
|
| 10 |
|
| 11 |
import gradio as gr
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
#
|
| 24 |
-
#
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
3833, 4035, 4241, 4450, 4663, 4880, 5100, 5324, 5552, 5784, 6020,
|
| 58 |
-
6259, 6502, 6749, 6999, 7252, 7508, 7766, 8026, 8287, 8549
|
| 59 |
-
]
|
| 60 |
-
# Cobb's published table does not list cumulative feed for days 1-6.
|
| 61 |
-
# The calculator therefore uses the first published cumulative-feed
|
| 62 |
-
# checkpoint (day 7) for feed calculations in that early period.
|
| 63 |
-
|
| 64 |
-
# Arbor Acres Plus - As-Hatched, 2022.
|
| 65 |
-
# Daily values transcribed from the published objective table.
|
| 66 |
-
AA_WEIGHT_G = [
|
| 67 |
-
43, 60, 78, 99, 123, 149, 179, 209, 246, 287, 333, 382, 435, 492,
|
| 68 |
-
527, 596, 666, 736, 808, 881, 956, 1006, 1089, 1174, 1260, 1347,
|
| 69 |
-
1435, 1523, 1611, 1710, 1809, 1908, 2008, 2107, 2206, 2297, 2386,
|
| 70 |
-
2486, 2585, 2684, 2784, 2882, 2981, 3079, 3176, 3272, 3368, 3462,
|
| 71 |
-
3556, 3649, 3740, 3831, 3921, 4010, 4098, 4185, 4272
|
| 72 |
-
]
|
| 73 |
-
AA_CUM_FEED_G = [
|
| 74 |
-
0, 12, 28, 48, 71, 98, 130, 168, 210, 257, 308, 363, 422, 486,
|
| 75 |
-
538, 605, 675, 749, 827, 909, 994, 1163, 1253, 1350, 1453, 1561,
|
| 76 |
-
1673, 1789, 1909, 2033, 2161, 2293, 2429, 2569, 2713, 2862, 3015,
|
| 77 |
-
3173, 3336, 3503, 3673, 3849, 4029, 4214, 4405, 4601, 4801, 5006,
|
| 78 |
-
5216, 5429, 5648, 5869, 6093, 6320, 6550, 6783, 7019
|
| 79 |
-
]
|
| 80 |
-
|
| 81 |
-
BREEDS = {
|
| 82 |
-
"Ross 308 AP": (ROSS_WEIGHT_G, ROSS_CUM_FEED_G),
|
| 83 |
-
"Cobb 500": (COBB_WEIGHT_G, COBB_CUM_FEED_G),
|
| 84 |
-
"Arbor Acres Plus": (AA_WEIGHT_G, AA_CUM_FEED_G),
|
| 85 |
}
|
| 86 |
|
| 87 |
-
#
|
| 88 |
-
|
| 89 |
-
"
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
"
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
}
|
| 101 |
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
return "NG"
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
return False
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
def country_currency(request):
|
| 176 |
-
country = detect_country(request)
|
| 177 |
-
symbol, code, _ = CURRENCY_BY_COUNTRY.get(country, ("$", "USD", 0.00062))
|
| 178 |
-
return country, symbol, code
|
| 179 |
-
|
| 180 |
-
def update_currency(request):
|
| 181 |
-
country, symbol, code = country_currency(request)
|
| 182 |
-
default_price = 24000 if code == "NGN" else 0
|
| 183 |
-
return (
|
| 184 |
-
gr.update(value=f"{symbol} {code}"),
|
| 185 |
-
gr.update(value=default_price),
|
| 186 |
-
f"Detected country: **{country}** Β· Currency: **{symbol} {code}**"
|
| 187 |
-
)
|
| 188 |
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
)
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
try:
|
| 208 |
birds = int(birds)
|
| 209 |
weeks = int(weeks)
|
|
@@ -211,269 +301,438 @@ def calculate(
|
|
| 211 |
bag_size = float(bag_size)
|
| 212 |
price_per_bag = float(price_per_bag)
|
| 213 |
except Exception:
|
| 214 |
-
return
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
-
|
| 233 |
-
save_lead(email, country, code)
|
| 234 |
|
| 235 |
-
weight_values, feed_values = BREEDS[breed]
|
| 236 |
-
weight_g = interpolate(weight_values, total_days)
|
| 237 |
-
cumulative_g = feed_at_day(feed_values, total_days, breed)
|
| 238 |
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
total_g_per_bird = cumulative_g
|
| 243 |
-
finisher_g_per_bird = max(0.0, total_g_per_bird - starter_g_per_bird)
|
| 244 |
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
|
| 249 |
-
|
| 250 |
-
whole_bags = math.ceil(exact_bags)
|
| 251 |
|
| 252 |
-
|
| 253 |
-
finisher_exact_bags = finisher_kg / bag_size
|
| 254 |
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
finisher_buy = math.ceil(finisher_exact_bags) if finisher_kg > 0 else 0
|
| 259 |
-
purchase_bags = starter_buy + finisher_buy
|
| 260 |
-
purchase_cost = purchase_bags * price_per_bag
|
| 261 |
|
| 262 |
-
|
| 263 |
-
# purchasable whole-bag quantity is rounded up.
|
| 264 |
-
expected_flock_weight_kg = birds * weight_g / 1000
|
| 265 |
|
| 266 |
-
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
|
| 269 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 270 |
|
| 271 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
-
**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
-
|
| 276 |
|
| 277 |
-
**
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
|
| 279 |
-
|
| 280 |
|
| 281 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
-
|
| 284 |
|
| 285 |
-
|
| 286 |
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
|
| 293 |
-
|
| 294 |
|
| 295 |
-
|
| 296 |
|
| 297 |
-
|
|
|
|
| 298 |
|
| 299 |
-
|
| 300 |
|
| 301 |
-
### π Breed benchmark
|
| 302 |
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
- No artificial 20% allowance has been added.
|
| 307 |
-
- The bag quantity is rounded **up only because bags are purchased whole**.
|
| 308 |
|
| 309 |
-
|
| 310 |
-
|
|
|
|
|
|
|
| 311 |
|
| 312 |
-
return gr.update(value=result), gr.update(visible=True)
|
| 313 |
|
| 314 |
-
|
| 315 |
-
if not email or not re.fullmatch(r"[^@\s]+@[^@\s]+\.[^@\s]+", email.strip()):
|
| 316 |
-
return "Please enter your email above first so we can attach the question to your farmer record."
|
| 317 |
-
if save_question(email, question):
|
| 318 |
-
return "β
Question saved. We will use farmer questions to guide future upgrades."
|
| 319 |
-
return "Please type a question first."
|
| 320 |
|
| 321 |
-
|
| 322 |
-
#
|
| 323 |
-
#
|
|
|
|
| 324 |
|
| 325 |
CSS = """
|
| 326 |
-
body { background: #
|
| 327 |
-
.gradio-container { max-width:
|
| 328 |
-
.
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
}
|
| 332 |
-
.
|
| 333 |
-
|
| 334 |
-
border: 1px solid #e6e6e6;
|
| 335 |
-
}
|
| 336 |
"""
|
| 337 |
|
| 338 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 339 |
gr.HTML("""
|
| 340 |
-
<div class="
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
<p>Select the breed, number of birds, age, bag size and price.
|
| 344 |
-
<b>No calculations required.</b></p>
|
| 345 |
</div>
|
| 346 |
""")
|
| 347 |
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
label="Email address *",
|
| 354 |
-
placeholder="farmer@example.com",
|
| 355 |
-
type="email",
|
| 356 |
-
)
|
| 357 |
|
| 358 |
-
|
| 359 |
-
currency_display = gr.Textbox(label="Detected currency", interactive=False)
|
| 360 |
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
)
|
| 373 |
-
with gr.Row():
|
| 374 |
-
weeks = gr.Dropdown(
|
| 375 |
-
choices=list(range(0, 9)),
|
| 376 |
-
value=6,
|
| 377 |
-
label="Age β Weeks",
|
| 378 |
)
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
|
|
|
|
|
|
| 383 |
)
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
)
|
| 394 |
|
| 395 |
calculate_btn = gr.Button(
|
| 396 |
-
"π Calculate
|
| 397 |
variant="primary",
|
| 398 |
size="lg",
|
| 399 |
)
|
| 400 |
|
| 401 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
gr.Markdown("## π― Quick Answer")
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
)
|
| 407 |
|
|
|
|
| 408 |
gr.Markdown("---")
|
| 409 |
-
gr.Markdown("## β Ask a question")
|
| 410 |
-
gr.Markdown("**What would you like to know about raising your birds?**")
|
| 411 |
-
gr.Markdown("Type your question. Questions are saved to guide future app upgrades and answers.")
|
| 412 |
|
| 413 |
with gr.Row():
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
|
| 422 |
-
|
|
|
|
| 423 |
|
|
|
|
| 424 |
gr.Markdown("---")
|
| 425 |
-
|
| 426 |
gr.Markdown("""
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
|
| 430 |
-
|
|
| 431 |
-
| **
|
|
|
|
|
|
|
| 432 |
|
| 433 |
-
**Tip:**
|
| 434 |
-
""")
|
| 435 |
|
|
|
|
| 436 |
gr.Markdown("---")
|
| 437 |
-
gr.Markdown("### π Benchmark note")
|
| 438 |
-
gr.Markdown(
|
| 439 |
-
"The calculator uses manufacturer **As-Hatched** performance objectives. "
|
| 440 |
-
"As-Hatched means the benchmark represents the mixed-sex flock objective, "
|
| 441 |
-
"rather than male-only or female-only performance."
|
| 442 |
-
)
|
| 443 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 444 |
calculate_btn.click(
|
| 445 |
-
|
| 446 |
-
inputs=[
|
| 447 |
-
outputs=
|
| 448 |
)
|
| 449 |
|
| 450 |
-
# Save question automatically when the farmer asks/saves it.
|
| 451 |
ask_btn.click(
|
| 452 |
-
|
| 453 |
-
inputs=[email,
|
| 454 |
-
outputs=
|
| 455 |
)
|
| 456 |
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
outputs=[currency_display, price, country_status],
|
| 462 |
-
)
|
| 463 |
|
| 464 |
if __name__ == "__main__":
|
| 465 |
port = int(os.environ.get("PORT", "7860"))
|
| 466 |
-
demo.launch(
|
| 467 |
-
server_name="0.0.0.0",
|
| 468 |
-
server_port=port,
|
| 469 |
-
show_error=True,
|
| 470 |
-
)
|
| 471 |
-
'''
|
| 472 |
-
|
| 473 |
-
path = Path("/mnt/data/app.py")
|
| 474 |
-
path.write_text(app_code, encoding="utf-8")
|
| 475 |
-
compile(app_code, str(path), "exec")
|
| 476 |
-
|
| 477 |
-
print(f"Created: {path}")
|
| 478 |
-
print("Syntax check: OK")
|
| 479 |
-
print("Important: replace the existing app.py with this file; do not keep the old patching script.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import math
|
| 3 |
import re
|
| 4 |
+
import sqlite3
|
| 5 |
+
from datetime import datetime
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
+
|
| 11 |
+
# ============================================================
|
| 12 |
+
# APP CONFIG
|
| 13 |
+
# ============================================================
|
| 14 |
+
|
| 15 |
+
APP_NAME = "Poultry Feed Calculator"
|
| 16 |
+
DB_PATH = os.environ.get("DATABASE_PATH", "poultry_calculator.db")
|
| 17 |
+
|
| 18 |
+
STARTER_END_DAY = 21
|
| 19 |
+
|
| 20 |
+
# ============================================================
|
| 21 |
+
# VERIFIED BREED DATA - From Official PDFs
|
| 22 |
+
# ============================================================
|
| 23 |
+
|
| 24 |
+
# Body Weight (kg) at key days - As-Hatched Performance
|
| 25 |
+
BREED_WEIGHT_DATA = {
|
| 26 |
+
"Ross 308": {
|
| 27 |
+
0: 0.044, 7: 0.213, 14: 0.533, 21: 1.012,
|
| 28 |
+
28: 1.616, 35: 2.296, 42: 2.998, 49: 3.681, 56: 4.318,
|
| 29 |
+
},
|
| 30 |
+
"Cobb 500": {
|
| 31 |
+
0: 0.042, 7: 0.177, 14: 0.459, 21: 0.891,
|
| 32 |
+
28: 1.436, 35: 2.067, 42: 2.732, 49: 3.369, 56: 3.958,
|
| 33 |
+
},
|
| 34 |
+
"Arbor Acres": {
|
| 35 |
+
0: 0.044, 7: 0.209, 14: 0.527, 21: 1.006,
|
| 36 |
+
28: 1.611, 35: 2.287, 42: 2.964, 49: 3.662, 56: 4.318,
|
| 37 |
+
}
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
# Cumulative Feed (kg) at key days - As-Hatched Performance
|
| 41 |
+
BREED_FEED_DATA = {
|
| 42 |
+
"Ross 308": {
|
| 43 |
+
0: 0.000, 7: 0.166, 14: 0.535, 21: 1.155,
|
| 44 |
+
28: 2.051, 35: 3.211, 42: 4.586, 49: 6.115, 56: 7.733,
|
| 45 |
+
},
|
| 46 |
+
"Cobb 500": {
|
| 47 |
+
0: 0.000, 7: 0.150, 14: 0.465, 21: 1.053,
|
| 48 |
+
28: 1.963, 35: 3.216, 42: 4.659, 49: 6.185, 56: 7.772,
|
| 49 |
+
},
|
| 50 |
+
"Arbor Acres": {
|
| 51 |
+
0: 0.000, 7: 0.207, 14: 0.638, 21: 1.163,
|
| 52 |
+
28: 2.069, 35: 3.237, 42: 4.625, 49: 6.111, 56: 7.647,
|
| 53 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
}
|
| 55 |
|
| 56 |
+
# Breed descriptions and availability
|
| 57 |
+
BREED_INFO = {
|
| 58 |
+
"Ross 308": {
|
| 59 |
+
"description": "Big chest, thick legs, very fast growth. Excellent for meat production.",
|
| 60 |
+
"available_at": "Agrited, Sayed, Valentine",
|
| 61 |
+
"color": "#16a34a"
|
| 62 |
+
},
|
| 63 |
+
"Cobb 500": {
|
| 64 |
+
"description": "Good early growth, strong legs, solid feed conversion.",
|
| 65 |
+
"available_at": "Chikun, Zartech",
|
| 66 |
+
"color": "#2563eb"
|
| 67 |
+
},
|
| 68 |
+
"Arbor Acres": {
|
| 69 |
+
"description": "Balanced growth, hardy and adaptable. Great survival rate.",
|
| 70 |
+
"available_at": "Supreme, CHI, GS, Vertex, Fidan, Cascada, Orobor, Sunchi, Farm Support, Ajila",
|
| 71 |
+
"color": "#d97706"
|
| 72 |
+
}
|
| 73 |
}
|
| 74 |
|
| 75 |
+
|
| 76 |
+
def get_breed_weight_data(breed):
|
| 77 |
+
return BREED_WEIGHT_DATA.get(breed, BREED_WEIGHT_DATA["Ross 308"])
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
def get_breed_feed_data(breed):
|
| 81 |
+
return BREED_FEED_DATA.get(breed, BREED_FEED_DATA["Ross 308"])
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def interpolate_weight(day, breed):
|
| 85 |
+
"""Interpolate body weight between known checkpoints."""
|
| 86 |
+
weight_data = get_breed_weight_data(breed)
|
| 87 |
+
|
| 88 |
+
if day <= 0:
|
| 89 |
+
return weight_data[0]
|
| 90 |
+
if day >= 56:
|
| 91 |
+
return weight_data[56]
|
| 92 |
+
|
| 93 |
+
keys = sorted(weight_data.keys())
|
| 94 |
+
for i in range(len(keys) - 1):
|
| 95 |
+
if keys[i] <= day <= keys[i + 1]:
|
| 96 |
+
ratio = (day - keys[i]) / (keys[i + 1] - keys[i])
|
| 97 |
+
return weight_data[keys[i]] + (weight_data[keys[i + 1]] - weight_data[keys[i]]) * ratio
|
| 98 |
+
|
| 99 |
+
return weight_data[56]
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
def interpolate_feed(day, breed):
|
| 103 |
+
"""Interpolate cumulative feed between known checkpoints."""
|
| 104 |
+
feed_data = get_breed_feed_data(breed)
|
| 105 |
+
|
| 106 |
+
if day <= 0:
|
| 107 |
+
return 0.0
|
| 108 |
+
if day >= 56:
|
| 109 |
+
return feed_data[56]
|
| 110 |
+
|
| 111 |
+
keys = sorted(feed_data.keys())
|
| 112 |
+
for i in range(len(keys) - 1):
|
| 113 |
+
if keys[i] <= day <= keys[i + 1]:
|
| 114 |
+
ratio = (day - keys[i]) / (keys[i + 1] - keys[i])
|
| 115 |
+
return feed_data[keys[i]] + (feed_data[keys[i + 1]] - feed_data[keys[i]]) * ratio
|
| 116 |
+
|
| 117 |
+
return feed_data[56]
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
# ============================================================
|
| 121 |
+
# CURRENCY
|
| 122 |
+
# ============================================================
|
| 123 |
+
|
| 124 |
+
CURRENCIES = {
|
| 125 |
+
"NG": ("Nigeria", "β¦", "NGN"),
|
| 126 |
+
"GH": ("Ghana", "β΅", "GHS"),
|
| 127 |
+
"KE": ("Kenya", "KSh", "KES"),
|
| 128 |
+
"UG": ("Uganda", "USh", "UGX"),
|
| 129 |
+
"TZ": ("Tanzania", "TSh", "TZS"),
|
| 130 |
+
"ZA": ("South Africa", "R", "ZAR"),
|
| 131 |
+
"ZM": ("Zambia", "ZK", "ZMW"),
|
| 132 |
+
"ZW": ("Zimbabwe", "ZiG", "ZWG"),
|
| 133 |
+
"CM": ("Cameroon", "FCFA", "XAF"),
|
| 134 |
+
"CI": ("CΓ΄te d'Ivoire", "CFA", "XOF"),
|
| 135 |
+
"SN": ("Senegal", "CFA", "XOF"),
|
| 136 |
+
"US": ("United States", "$", "USD"),
|
| 137 |
+
"GB": ("United Kingdom", "Β£", "GBP"),
|
| 138 |
+
"CA": ("Canada", "C$", "CAD"),
|
| 139 |
+
"AU": ("Australia", "A$", "AUD"),
|
| 140 |
+
}
|
| 141 |
+
|
| 142 |
+
|
| 143 |
+
def detect_country():
|
| 144 |
+
candidates = [
|
| 145 |
+
os.environ.get("COUNTRY_CODE"),
|
| 146 |
+
os.environ.get("CF-IPCOUNTRY"),
|
| 147 |
+
os.environ.get("GEOIP_COUNTRY"),
|
| 148 |
+
]
|
| 149 |
+
|
| 150 |
+
for value in candidates:
|
| 151 |
+
if value:
|
| 152 |
+
value = value.strip().upper()
|
| 153 |
+
if value in CURRENCIES:
|
| 154 |
+
return value
|
| 155 |
+
|
| 156 |
return "NG"
|
| 157 |
|
| 158 |
+
|
| 159 |
+
def currency_for_country(country_code):
|
| 160 |
+
data = CURRENCIES.get(country_code, CURRENCIES["NG"])
|
| 161 |
+
return data[1]
|
| 162 |
+
|
| 163 |
+
|
| 164 |
+
# ============================================================
|
| 165 |
+
# DATABASE
|
| 166 |
+
# ============================================================
|
| 167 |
+
|
| 168 |
+
def init_db():
|
| 169 |
+
conn = sqlite3.connect(DB_PATH)
|
| 170 |
+
cur = conn.cursor()
|
| 171 |
+
|
| 172 |
+
cur.execute("""
|
| 173 |
+
CREATE TABLE IF NOT EXISTS leads (
|
| 174 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 175 |
+
email TEXT UNIQUE,
|
| 176 |
+
country TEXT,
|
| 177 |
+
currency TEXT,
|
| 178 |
+
breed TEXT,
|
| 179 |
+
created_at TEXT
|
| 180 |
+
)
|
| 181 |
+
""")
|
| 182 |
+
|
| 183 |
+
cur.execute("""
|
| 184 |
+
CREATE TABLE IF NOT EXISTS farmer_questions (
|
| 185 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 186 |
+
email TEXT,
|
| 187 |
+
question TEXT,
|
| 188 |
+
breed TEXT,
|
| 189 |
+
created_at TEXT
|
| 190 |
+
)
|
| 191 |
+
""")
|
| 192 |
+
|
| 193 |
+
cur.execute("""
|
| 194 |
+
CREATE TABLE IF NOT EXISTS calculations (
|
| 195 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 196 |
+
email TEXT,
|
| 197 |
+
country TEXT,
|
| 198 |
+
currency TEXT,
|
| 199 |
+
breed TEXT,
|
| 200 |
+
birds INTEGER,
|
| 201 |
+
weeks INTEGER,
|
| 202 |
+
days INTEGER,
|
| 203 |
+
total_days INTEGER,
|
| 204 |
+
bag_size REAL,
|
| 205 |
+
price_per_bag REAL,
|
| 206 |
+
total_feed_kg REAL,
|
| 207 |
+
starter_kg REAL,
|
| 208 |
+
finisher_kg REAL,
|
| 209 |
+
starter_bags REAL,
|
| 210 |
+
finisher_bags REAL,
|
| 211 |
+
total_bags REAL,
|
| 212 |
+
rounded_bags INTEGER,
|
| 213 |
+
total_cost REAL,
|
| 214 |
+
created_at TEXT
|
| 215 |
+
)
|
| 216 |
+
""")
|
| 217 |
+
|
| 218 |
+
conn.commit()
|
| 219 |
+
conn.close()
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
init_db()
|
| 223 |
+
|
| 224 |
+
|
| 225 |
+
# ============================================================
|
| 226 |
+
# DATABASE HELPERS
|
| 227 |
+
# ============================================================
|
| 228 |
+
|
| 229 |
+
def valid_email(email):
|
| 230 |
+
if not email:
|
| 231 |
return False
|
| 232 |
+
email = email.strip()
|
| 233 |
+
pattern = r"^[^@\s]+@[^@\s]+\.[^@\s]+$"
|
| 234 |
+
return bool(re.match(pattern, email))
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
def save_lead(email, country, currency, breed):
|
| 238 |
+
if not valid_email(email):
|
| 239 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
|
| 241 |
+
conn = sqlite3.connect(DB_PATH)
|
| 242 |
+
cur = conn.cursor()
|
| 243 |
+
|
| 244 |
+
cur.execute("""
|
| 245 |
+
INSERT OR IGNORE INTO leads (email, country, currency, breed, created_at)
|
| 246 |
+
VALUES (?, ?, ?, ?, ?)
|
| 247 |
+
""", (email.strip().lower(), country, currency, breed, datetime.utcnow().isoformat()))
|
| 248 |
+
|
| 249 |
+
conn.commit()
|
| 250 |
+
conn.close()
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
def save_question(email, question, breed):
|
| 254 |
+
if not question or not question.strip():
|
| 255 |
+
return
|
| 256 |
+
|
| 257 |
+
conn = sqlite3.connect(DB_PATH)
|
| 258 |
+
cur = conn.cursor()
|
| 259 |
+
|
| 260 |
+
cur.execute("""
|
| 261 |
+
INSERT INTO farmer_questions (email, question, breed, created_at)
|
| 262 |
+
VALUES (?, ?, ?, ?)
|
| 263 |
+
""", (email.strip().lower() if email else "", question.strip(), breed, datetime.utcnow().isoformat()))
|
| 264 |
+
|
| 265 |
+
conn.commit()
|
| 266 |
+
conn.close()
|
| 267 |
+
|
| 268 |
+
|
| 269 |
+
def save_calculation(email, country, currency, breed, birds, weeks, days, total_days,
|
| 270 |
+
bag_size, price_per_bag, total_feed_kg, starter_kg, finisher_kg,
|
| 271 |
+
starter_bags, finisher_bags, total_bags, rounded_bags, total_cost):
|
| 272 |
+
conn = sqlite3.connect(DB_PATH)
|
| 273 |
+
cur = conn.cursor()
|
| 274 |
+
|
| 275 |
+
cur.execute("""
|
| 276 |
+
INSERT INTO calculations (
|
| 277 |
+
email, country, currency, breed, birds, weeks, days, total_days,
|
| 278 |
+
bag_size, price_per_bag, total_feed_kg, starter_kg, finisher_kg,
|
| 279 |
+
starter_bags, finisher_bags, total_bags, rounded_bags, total_cost, created_at
|
| 280 |
)
|
| 281 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
| 282 |
+
""", (email.strip().lower() if email else "", country, currency, breed, birds, weeks, days, total_days,
|
| 283 |
+
bag_size, price_per_bag, total_feed_kg, starter_kg, finisher_kg,
|
| 284 |
+
starter_bags, finisher_bags, total_bags, rounded_bags, total_cost,
|
| 285 |
+
datetime.utcnow().isoformat()))
|
| 286 |
+
|
| 287 |
+
conn.commit()
|
| 288 |
+
conn.close()
|
| 289 |
+
|
| 290 |
|
| 291 |
+
# ============================================================
|
| 292 |
+
# FEED CALCULATION
|
| 293 |
+
# ============================================================
|
| 294 |
+
|
| 295 |
+
def calculate_feed(birds, weeks, days, bag_size, price_per_bag, email, country_code, breed):
|
| 296 |
+
# Validation
|
| 297 |
try:
|
| 298 |
birds = int(birds)
|
| 299 |
weeks = int(weeks)
|
|
|
|
| 301 |
bag_size = float(bag_size)
|
| 302 |
price_per_bag = float(price_per_bag)
|
| 303 |
except Exception:
|
| 304 |
+
return "β οΈ Please enter valid numbers."
|
| 305 |
+
|
| 306 |
+
if birds <= 0:
|
| 307 |
+
return "β οΈ Number of birds must be greater than zero."
|
| 308 |
+
if weeks < 0:
|
| 309 |
+
return "β οΈ Weeks cannot be negative."
|
| 310 |
+
if days < 0 or days > 6:
|
| 311 |
+
return "β οΈ Days must be between 0 and 6."
|
| 312 |
+
if bag_size <= 0:
|
| 313 |
+
return "β οΈ Feed bag size must be greater than zero."
|
| 314 |
+
if price_per_bag < 0:
|
| 315 |
+
return "β οΈ Price per bag cannot be negative."
|
| 316 |
+
|
| 317 |
+
if not valid_email(email):
|
| 318 |
+
return "π§ **Email required**\n\nPlease enter your email address before calculating."
|
| 319 |
+
|
| 320 |
+
# Country / Currency
|
| 321 |
+
country_code = country_code or "NG"
|
| 322 |
+
if country_code not in CURRENCIES:
|
| 323 |
+
country_code = "NG"
|
| 324 |
+
currency = currency_for_country(country_code)
|
| 325 |
+
|
| 326 |
+
# Save lead with breed
|
| 327 |
+
save_lead(email, country_code, currency, breed)
|
| 328 |
+
|
| 329 |
+
# Total days
|
| 330 |
+
total_days = (weeks * 7) + days
|
| 331 |
+
|
| 332 |
+
# Total feed
|
| 333 |
+
total_feed_per_bird = interpolate_feed(total_days, breed)
|
| 334 |
+
total_feed_kg = birds * total_feed_per_bird
|
| 335 |
+
|
| 336 |
+
# Starter / Finisher
|
| 337 |
+
starter_days = min(total_days, STARTER_END_DAY)
|
| 338 |
+
starter_per_bird = interpolate_feed(starter_days, breed)
|
| 339 |
+
starter_kg = birds * starter_per_bird
|
| 340 |
+
finisher_kg = max(total_feed_kg - starter_kg, 0)
|
| 341 |
+
|
| 342 |
+
# Bags
|
| 343 |
+
starter_bags = starter_kg / bag_size
|
| 344 |
+
finisher_bags = finisher_kg / bag_size
|
| 345 |
+
total_bags = total_feed_kg / bag_size
|
| 346 |
+
rounded_bags = math.ceil(total_bags)
|
| 347 |
+
total_cost = rounded_bags * price_per_bag
|
| 348 |
+
|
| 349 |
+
# Expected performance
|
| 350 |
+
expected_weight = interpolate_weight(total_days, breed)
|
| 351 |
+
flock_weight = expected_weight * birds
|
| 352 |
+
|
| 353 |
+
# Save calculation
|
| 354 |
+
save_calculation(email, country_code, currency, breed, birds, weeks, days, total_days,
|
| 355 |
+
bag_size, price_per_bag, total_feed_kg, starter_kg, finisher_kg,
|
| 356 |
+
starter_bags, finisher_bags, total_bags, rounded_bags, total_cost)
|
| 357 |
+
|
| 358 |
+
# Breed info
|
| 359 |
+
breed_info = BREED_INFO.get(breed, BREED_INFO["Ross 308"])
|
| 360 |
+
breed_color = breed_info["color"]
|
| 361 |
+
|
| 362 |
+
# Output HTML
|
| 363 |
+
starter_bags_display = f"{starter_bags:.1f}"
|
| 364 |
+
finisher_bags_display = f"{finisher_bags:.1f}"
|
| 365 |
+
|
| 366 |
+
age_text = f"{weeks} week{'s' if weeks != 1 else ''}"
|
| 367 |
+
if days:
|
| 368 |
+
age_text += f" + {days} day{'s' if days != 1 else ''}"
|
| 369 |
+
|
| 370 |
+
html = f"""
|
| 371 |
+
<div style="font-size: 18px; line-height: 1.6;">
|
| 372 |
+
|
| 373 |
+
<div style="background: #f0fdf4; padding: 16px 20px; border-radius: 12px; border-left: 5px solid #22c55e; margin-bottom: 20px;">
|
| 374 |
+
<div style="font-size: 14px; color: #16a34a; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">π― Quick Answer</div>
|
| 375 |
+
<div style="font-size: 22px; font-weight: 700; color: #166534;">
|
| 376 |
+
{rounded_bags} bags of {bag_size:g} kg feed
|
| 377 |
+
</div>
|
| 378 |
+
<div style="color: #4b5563; margin-top: 4px;">
|
| 379 |
+
To raise <strong>{birds:,} birds</strong> for <strong>{age_text} ({total_days} days)</strong>
|
| 380 |
+
</div>
|
| 381 |
+
<div style="margin-top: 6px; font-size: 14px;">
|
| 382 |
+
<span style="background: {breed_color}; color: white; padding: 2px 10px; border-radius: 12px; font-weight: 600;">{breed}</span>
|
| 383 |
+
</div>
|
| 384 |
+
</div>
|
| 385 |
|
| 386 |
+
<div style="margin-bottom: 16px;">
|
| 387 |
+
<div style="font-size: 14px; color: #6b7280; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">πΎ Feed Breakdown</div>
|
| 388 |
+
<table style="width: 100%; border-collapse: collapse; margin-top: 6px;">
|
| 389 |
+
<tr style="border-bottom: 1px solid #e5e7eb;">
|
| 390 |
+
<td style="padding: 8px 4px;"><strong>π± Starter</strong></td>
|
| 391 |
+
<td style="padding: 8px 4px; text-align: right;">{starter_kg:,.1f} kg</td>
|
| 392 |
+
<td style="padding: 8px 4px; text-align: right; font-weight: 600;">{starter_bags_display} bags</td>
|
| 393 |
+
</tr>
|
| 394 |
+
<tr style="border-bottom: 1px solid #e5e7eb;">
|
| 395 |
+
<td style="padding: 8px 4px;"><strong>π½ Finisher</strong></td>
|
| 396 |
+
<td style="padding: 8px 4px; text-align: right;">{finisher_kg:,.1f} kg</td>
|
| 397 |
+
<td style="padding: 8px 4px; text-align: right; font-weight: 600;">{finisher_bags_display} bags</td>
|
| 398 |
+
</tr>
|
| 399 |
+
<tr>
|
| 400 |
+
<td style="padding: 8px 4px; font-weight: 700;">TOTAL</td>
|
| 401 |
+
<td style="padding: 8px 4px; text-align: right; font-weight: 700;">{total_feed_kg:,.1f} kg</td>
|
| 402 |
+
<td style="padding: 8px 4px; text-align: right; font-weight: 700;">{total_bags:.1f}</td>
|
| 403 |
+
</tr>
|
| 404 |
+
</table>
|
| 405 |
+
</div>
|
| 406 |
|
| 407 |
+
<div style="margin-bottom: 16px;">
|
| 408 |
+
<div style="font-size: 14px; color: #6b7280; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">π° Feed Cost</div>
|
| 409 |
+
<div style="font-size: 28px; font-weight: 800; color: #1e40af;">
|
| 410 |
+
{currency}{total_cost:,.0f}
|
| 411 |
+
</div>
|
| 412 |
+
<div style="color: #6b7280; font-size: 14px;">
|
| 413 |
+
{rounded_bags} bags Γ {currency}{price_per_bag:,.0f} per bag
|
| 414 |
+
</div>
|
| 415 |
+
</div>
|
| 416 |
+
|
| 417 |
+
<div style="border-top: 1px solid #e5e7eb; padding-top: 14px;">
|
| 418 |
+
<div style="font-size: 14px; color: #6b7280; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">π Expected Performance</div>
|
| 419 |
+
<table style="width: 100%; border-collapse: collapse; margin-top: 6px;">
|
| 420 |
+
<tr style="border-bottom: 1px solid #e5e7eb;">
|
| 421 |
+
<td style="padding: 6px 4px;">Body weight / bird</td>
|
| 422 |
+
<td style="padding: 6px 4px; text-align: right; font-weight: 600;">{expected_weight:.3f} kg</td>
|
| 423 |
+
</tr>
|
| 424 |
+
<tr>
|
| 425 |
+
<td style="padding: 6px 4px;">Flock live weight</td>
|
| 426 |
+
<td style="padding: 6px 4px; text-align: right; font-weight: 600;">{flock_weight:,.1f} kg</td>
|
| 427 |
+
</tr>
|
| 428 |
+
</table>
|
| 429 |
+
</div>
|
| 430 |
+
|
| 431 |
+
<div style="margin-top: 14px; font-size: 12px; color: #9ca3af; border-top: 1px solid #f3f4f6; padding-top: 10px;">
|
| 432 |
+
Estimate based on {breed} benchmark. Actual results may vary.
|
| 433 |
+
</div>
|
| 434 |
+
</div>
|
| 435 |
+
"""
|
| 436 |
|
| 437 |
+
return html
|
|
|
|
| 438 |
|
|
|
|
|
|
|
|
|
|
| 439 |
|
| 440 |
+
# ============================================================
|
| 441 |
+
# QUESTION / ANSWER
|
| 442 |
+
# ============================================================
|
|
|
|
|
|
|
| 443 |
|
| 444 |
+
def answer_farmer_question(question, email, breed):
|
| 445 |
+
if not question or not question.strip():
|
| 446 |
+
return "Please type or speak your question first."
|
| 447 |
|
| 448 |
+
save_question(email or "", question, breed)
|
|
|
|
| 449 |
|
| 450 |
+
q = question.lower().strip()
|
|
|
|
| 451 |
|
| 452 |
+
if "bag" in q and ("feed" in q or "birds" in q):
|
| 453 |
+
answer = """
|
| 454 |
+
### π Feed-bag questions
|
|
|
|
|
|
|
|
|
|
| 455 |
|
| 456 |
+
For an exact feed estimate, use the calculator above.
|
|
|
|
|
|
|
| 457 |
|
| 458 |
+
Enter:
|
| 459 |
+
1. Number of birds
|
| 460 |
+
2. Age in weeks + days
|
| 461 |
+
3. Feed bag size
|
| 462 |
+
4. Price per bag
|
| 463 |
+
5. Your email
|
| 464 |
+
6. Breed
|
| 465 |
|
| 466 |
+
The calculator will give you the estimated total bags, starter/finisher breakdown and feed cost.
|
| 467 |
+
"""
|
| 468 |
+
elif "breed" in q or "ross" in q or "cobb" in q or "arbor" in q:
|
| 469 |
+
answer = """
|
| 470 |
+
### π Common Broiler Breeds in Nigeria
|
| 471 |
|
| 472 |
+
| Breed | Characteristics | Available At |
|
| 473 |
+
|-------|-----------------|--------------|
|
| 474 |
+
| **Ross 308** | Big chest, thick legs, very fast growth | Agrited, Sayed, Valentine |
|
| 475 |
+
| **Cobb 500** | Good early growth, strong legs | Chikun, Zartech |
|
| 476 |
+
| **Arbor Acres** | Balanced growth, hardy, adaptable | Supreme, CHI, GS, Vertex, Fidan, Cascada, Orobor, Sunchi, Farm Support, Ajila |
|
| 477 |
|
| 478 |
+
π‘ **Tip:** Before buying chicks, ask about the breed and hatchery. Not all broilers are the same!
|
| 479 |
+
"""
|
| 480 |
+
elif "starter" in q:
|
| 481 |
+
answer = f"""
|
| 482 |
+
### πΎ Starter feed
|
| 483 |
|
| 484 |
+
Starter feed is used during the early growth period (Day 0β{STARTER_END_DAY}).
|
| 485 |
|
| 486 |
+
The calculator separates the estimated feed requirement into **Starter** and **Finisher** based on the selected age.
|
| 487 |
+
"""
|
| 488 |
+
elif "finisher" in q:
|
| 489 |
+
answer = f"""
|
| 490 |
+
### πΎ Finisher feed
|
| 491 |
|
| 492 |
+
Finisher feed is used after day {STARTER_END_DAY}.
|
| 493 |
|
| 494 |
+
Your calculator automatically estimates the finisher requirement from the selected number of birds and total age.
|
| 495 |
+
"""
|
| 496 |
+
elif "cost" in q or "price" in q:
|
| 497 |
+
answer = """
|
| 498 |
+
### π° Feed cost
|
| 499 |
|
| 500 |
+
Enter the current price of one feed bag in the calculator.
|
| 501 |
|
| 502 |
+
The app calculates: **Number of bags Γ price per bag = estimated feed cost**
|
| 503 |
|
| 504 |
+
The result is for **feed alone**.
|
| 505 |
+
"""
|
| 506 |
+
else:
|
| 507 |
+
answer = f"""
|
| 508 |
+
### π‘ Question received
|
| 509 |
|
| 510 |
+
**β{question.strip()}β**
|
| 511 |
|
| 512 |
+
Your question has been saved.
|
| 513 |
|
| 514 |
+
We can use the questions farmers submit to decide which answers, calculators and features should be added to the app next.
|
| 515 |
+
"""
|
| 516 |
|
| 517 |
+
return answer
|
| 518 |
|
|
|
|
| 519 |
|
| 520 |
+
# ============================================================
|
| 521 |
+
# COUNTRY / CURRENCY UI
|
| 522 |
+
# ============================================================
|
|
|
|
|
|
|
| 523 |
|
| 524 |
+
def get_country_info():
|
| 525 |
+
country = detect_country()
|
| 526 |
+
currency = currency_for_country(country)
|
| 527 |
+
return country, currency
|
| 528 |
|
|
|
|
| 529 |
|
| 530 |
+
DEFAULT_COUNTRY, DEFAULT_CURRENCY = get_country_info()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 531 |
|
| 532 |
+
|
| 533 |
+
# ============================================================
|
| 534 |
+
# CSS
|
| 535 |
+
# ============================================================
|
| 536 |
|
| 537 |
CSS = """
|
| 538 |
+
body { background: #f6f8f4; }
|
| 539 |
+
.gradio-container { max-width: 1200px !important; margin: auto !important; }
|
| 540 |
+
.main-title { text-align: center; padding: 15px 10px 5px 10px; }
|
| 541 |
+
.main-title h1 { font-size: 34px; margin-bottom: 4px; }
|
| 542 |
+
.main-title p { font-size: 17px; color: #555; }
|
| 543 |
+
.answer-box { background: white; border-radius: 16px; padding: 16px 20px; border: 1px solid #e5e7eb; box-shadow: 0 1px 3px rgba(0,0,0,0.05); min-height: 400px; }
|
| 544 |
+
.email-box { border: 2px solid #d9e7d2; border-radius: 12px; padding: 10px; background: #fbfdf9; }
|
| 545 |
+
button { min-height: 48px !important; }
|
|
|
|
|
|
|
| 546 |
"""
|
| 547 |
|
| 548 |
+
|
| 549 |
+
# ============================================================
|
| 550 |
+
# UI
|
| 551 |
+
# ============================================================
|
| 552 |
+
|
| 553 |
+
with gr.Blocks(title=APP_NAME, css=CSS) as demo:
|
| 554 |
+
|
| 555 |
gr.HTML("""
|
| 556 |
+
<div class="main-title">
|
| 557 |
+
<h1>π Poultry Feed Calculator</h1>
|
| 558 |
+
<p>Know exactly how much feed you need before buying.</p>
|
|
|
|
|
|
|
| 559 |
</div>
|
| 560 |
""")
|
| 561 |
|
| 562 |
+
# Breed Info Banner
|
| 563 |
+
gr.Markdown("""
|
| 564 |
+
### π Select your breed
|
| 565 |
+
**Ross 308** β Fastest growth, big chest | **Cobb 500** β Good early growth | **Arbor Acres** β Hardy & adaptable
|
| 566 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 567 |
|
| 568 |
+
with gr.Row(equal_height=True):
|
|
|
|
| 569 |
|
| 570 |
+
# LEFT COLUMN: INPUTS
|
| 571 |
+
with gr.Column(scale=1, min_width=350):
|
| 572 |
+
|
| 573 |
+
with gr.Group():
|
| 574 |
+
gr.Markdown("## π 1. Breed & Number of birds")
|
| 575 |
+
|
| 576 |
+
breed = gr.Dropdown(
|
| 577 |
+
choices=["Ross 308", "Cobb 500", "Arbor Acres"],
|
| 578 |
+
value="Ross 308",
|
| 579 |
+
label="Breed",
|
| 580 |
+
info="Select your broiler breed for accurate estimates",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 581 |
)
|
| 582 |
+
|
| 583 |
+
birds = gr.Dropdown(
|
| 584 |
+
choices=[25, 50, 75, 100, 150, 200, 500, 1000],
|
| 585 |
+
value=50,
|
| 586 |
+
label="Number of birds",
|
| 587 |
+
allow_custom_value=True,
|
| 588 |
)
|
| 589 |
+
|
| 590 |
+
gr.Markdown("## π
2. Age")
|
| 591 |
+
|
| 592 |
+
with gr.Row():
|
| 593 |
+
weeks = gr.Dropdown(
|
| 594 |
+
choices=list(range(0, 21)),
|
| 595 |
+
value=6,
|
| 596 |
+
label="Weeks",
|
| 597 |
+
)
|
| 598 |
+
days = gr.Dropdown(
|
| 599 |
+
choices=list(range(0, 7)),
|
| 600 |
+
value=0,
|
| 601 |
+
label="+ Days",
|
| 602 |
+
)
|
| 603 |
+
|
| 604 |
+
gr.Markdown("## πΎ 3. Feed")
|
| 605 |
+
|
| 606 |
+
with gr.Row():
|
| 607 |
+
bag_size = gr.Dropdown(
|
| 608 |
+
choices=[10, 20, 25, 50],
|
| 609 |
+
value=25,
|
| 610 |
+
label="Bag size (kg)",
|
| 611 |
+
)
|
| 612 |
+
price = gr.Number(
|
| 613 |
+
value=24000,
|
| 614 |
+
label=f"Price per bag ({DEFAULT_CURRENCY})",
|
| 615 |
+
minimum=0,
|
| 616 |
+
)
|
| 617 |
+
|
| 618 |
+
gr.Markdown("## π§ 4. Your email")
|
| 619 |
+
|
| 620 |
+
email = gr.Textbox(
|
| 621 |
+
label="",
|
| 622 |
+
placeholder="farmer@example.com",
|
| 623 |
+
type="email",
|
| 624 |
+
elem_classes=["email-box"],
|
| 625 |
)
|
| 626 |
|
| 627 |
calculate_btn = gr.Button(
|
| 628 |
+
"π Calculate",
|
| 629 |
variant="primary",
|
| 630 |
size="lg",
|
| 631 |
)
|
| 632 |
|
| 633 |
+
country_state = gr.State(DEFAULT_COUNTRY)
|
| 634 |
+
|
| 635 |
+
# RIGHT COLUMN: RESULTS
|
| 636 |
+
with gr.Column(scale=1, min_width=350):
|
| 637 |
+
|
| 638 |
gr.Markdown("## π― Quick Answer")
|
| 639 |
+
|
| 640 |
+
result = gr.HTML(
|
| 641 |
+
value="""
|
| 642 |
+
<div style="font-size: 16px; color: #9ca3af; text-align: center; padding: 40px 20px;">
|
| 643 |
+
<div style="font-size: 48px; margin-bottom: 12px;">π</div>
|
| 644 |
+
<div>Select your breed, enter details and click <strong>Calculate</strong></div>
|
| 645 |
+
<div style="font-size: 14px; margin-top: 8px;">Your feed estimate will appear here</div>
|
| 646 |
+
</div>
|
| 647 |
+
""",
|
| 648 |
+
elem_classes=["answer-box"],
|
| 649 |
)
|
| 650 |
|
| 651 |
+
# QUESTION SECTION
|
| 652 |
gr.Markdown("---")
|
|
|
|
|
|
|
|
|
|
| 653 |
|
| 654 |
with gr.Row():
|
| 655 |
+
with gr.Column(scale=2):
|
| 656 |
+
gr.Markdown("""
|
| 657 |
+
# β Ask a question
|
| 658 |
+
### What would you like to know about raising your birds?
|
| 659 |
+
""")
|
| 660 |
+
|
| 661 |
+
question = gr.Textbox(
|
| 662 |
+
label="",
|
| 663 |
+
placeholder="Type or speak your question...",
|
| 664 |
+
lines=3,
|
| 665 |
+
)
|
| 666 |
|
| 667 |
+
ask_btn = gr.Button("π¬ Get Answer", variant="secondary", size="lg")
|
| 668 |
+
question_answer = gr.Markdown("")
|
| 669 |
|
| 670 |
+
# BREED REFERENCE
|
| 671 |
gr.Markdown("---")
|
| 672 |
+
|
| 673 |
gr.Markdown("""
|
| 674 |
+
### π Common Broiler Breeds in Nigeria
|
| 675 |
+
|
| 676 |
+
| Breed | Characteristics | Available At |
|
| 677 |
+
|-------|-----------------|--------------|
|
| 678 |
+
| **Ross 308** | Big chest, thick legs, very fast growth | Agrited, Sayed, Valentine |
|
| 679 |
+
| **Cobb 500** | Good early growth, strong legs | Chikun, Zartech |
|
| 680 |
+
| **Arbor Acres** | Balanced growth, hardy, adaptable | Supreme, CHI, GS, Vertex, Fidan, Cascada, Orobor, Sunchi, Farm Support, Ajila |
|
| 681 |
|
| 682 |
+
π‘ **Tip:** Before buying chicks, ask about the breed and hatchery. Not all broilers are the same!
|
| 683 |
+
""")
|
| 684 |
|
| 685 |
+
# BENCHMARK REFERENCE
|
| 686 |
gr.Markdown("---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 687 |
|
| 688 |
+
with gr.Row():
|
| 689 |
+
with gr.Column(scale=1):
|
| 690 |
+
gr.Markdown("""
|
| 691 |
+
### π Ross 308 Weekly Benchmark
|
| 692 |
+
|
| 693 |
+
| Days | Body Weight (kg) | Cum Feed (kg) |
|
| 694 |
+
|------|------------------|---------------|
|
| 695 |
+
| 7 | 0.213 | 0.166 |
|
| 696 |
+
| 14 | 0.533 | 0.535 |
|
| 697 |
+
| 21 | 1.012 | 1.155 |
|
| 698 |
+
| 28 | 1.616 | 2.051 |
|
| 699 |
+
| 35 | 2.296 | 3.211 |
|
| 700 |
+
| 42 | 2.998 | 4.586 |
|
| 701 |
+
| 49 | 3.681 | 6.115 |
|
| 702 |
+
| 56 | 4.318 | 7.733 |
|
| 703 |
+
""")
|
| 704 |
+
|
| 705 |
+
with gr.Column(scale=1):
|
| 706 |
+
gr.Markdown("""
|
| 707 |
+
### π How it works
|
| 708 |
+
|
| 709 |
+
- **Weeks + Days** β total days
|
| 710 |
+
- **Starter** β Day 0β21
|
| 711 |
+
- **Finisher** β Day 22+
|
| 712 |
+
- **Bags** β rounded up to next whole bag
|
| 713 |
+
- **Breed-specific benchmarks** used for estimates
|
| 714 |
+
|
| 715 |
+
β οΈ Actual results may vary with management, feed quality, and other conditions.
|
| 716 |
+
""")
|
| 717 |
+
|
| 718 |
+
# EVENTS
|
| 719 |
calculate_btn.click(
|
| 720 |
+
fn=calculate_feed,
|
| 721 |
+
inputs=[birds, weeks, days, bag_size, price, email, country_state, breed],
|
| 722 |
+
outputs=result,
|
| 723 |
)
|
| 724 |
|
|
|
|
| 725 |
ask_btn.click(
|
| 726 |
+
fn=answer_farmer_question,
|
| 727 |
+
inputs=[question, email, breed],
|
| 728 |
+
outputs=question_answer,
|
| 729 |
)
|
| 730 |
|
| 731 |
+
|
| 732 |
+
# ============================================================
|
| 733 |
+
# START APP
|
| 734 |
+
# ============================================================
|
|
|
|
|
|
|
| 735 |
|
| 736 |
if __name__ == "__main__":
|
| 737 |
port = int(os.environ.get("PORT", "7860"))
|
| 738 |
+
demo.launch(server_name="0.0.0.0", server_port=port, root_path="/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|