Spaces:
Paused
Paused
NTEIGBANAM OKE commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,844 +1,2310 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import math
|
| 3 |
import os
|
| 4 |
import re
|
|
|
|
|
|
|
| 5 |
from datetime import datetime
|
|
|
|
| 6 |
|
|
|
|
| 7 |
import gradio as gr
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
# ============================================================
|
| 11 |
-
#
|
| 12 |
# ============================================================
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
LEADS_FILE = "farmer_leads.csv"
|
| 17 |
-
QUESTIONS_FILE = "farmer_questions.csv"
|
| 18 |
|
|
|
|
| 19 |
|
| 20 |
-
#
|
| 21 |
-
#
|
| 22 |
-
# ============================================================
|
| 23 |
#
|
| 24 |
-
#
|
| 25 |
-
#
|
| 26 |
-
# weeks + days can be calculated as exact total days.
|
| 27 |
#
|
| 28 |
-
#
|
| 29 |
-
#
|
| 30 |
-
#
|
| 31 |
-
#
|
| 32 |
-
#
|
| 33 |
-
#
|
| 34 |
-
#
|
| 35 |
-
#
|
| 36 |
-
# ============================================================
|
| 37 |
|
| 38 |
-
|
| 39 |
-
0: {
|
| 40 |
-
"body_weight": 0.044,
|
| 41 |
-
"cumulative_feed": 0.000,
|
| 42 |
-
},
|
| 43 |
-
7: {
|
| 44 |
-
"body_weight": 0.213,
|
| 45 |
-
"cumulative_feed": 0.180,
|
| 46 |
-
},
|
| 47 |
-
14: {
|
| 48 |
-
"body_weight": 0.526,
|
| 49 |
-
"cumulative_feed": 0.690,
|
| 50 |
-
},
|
| 51 |
-
21: {
|
| 52 |
-
"body_weight": 1.087,
|
| 53 |
-
"cumulative_feed": 1.410,
|
| 54 |
-
},
|
| 55 |
-
28: {
|
| 56 |
-
"body_weight": 1.697,
|
| 57 |
-
"cumulative_feed": 2.147,
|
| 58 |
-
},
|
| 59 |
-
35: {
|
| 60 |
-
"body_weight": 2.440,
|
| 61 |
-
"cumulative_feed": 3.390,
|
| 62 |
-
},
|
| 63 |
-
42: {
|
| 64 |
-
"body_weight": 3.220,
|
| 65 |
-
"cumulative_feed": 4.890,
|
| 66 |
-
},
|
| 67 |
-
49: {
|
| 68 |
-
"body_weight": 4.020,
|
| 69 |
-
"cumulative_feed": 6.580,
|
| 70 |
-
},
|
| 71 |
-
56: {
|
| 72 |
-
"body_weight": 4.800,
|
| 73 |
-
"cumulative_feed": 8.330,
|
| 74 |
-
},
|
| 75 |
-
}
|
| 76 |
|
| 77 |
|
| 78 |
# ============================================================
|
| 79 |
-
#
|
| 80 |
# ============================================================
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
return ""
|
| 91 |
|
| 92 |
-
pattern = r"^[^@\s]+@[^@\s]+\.[^@\s]+$"
|
| 93 |
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
return email
|
| 98 |
|
|
|
|
| 99 |
|
| 100 |
-
|
| 101 |
-
"""Save farmer email to the local mailing-list CSV."""
|
| 102 |
-
email = clean_email(email)
|
| 103 |
|
| 104 |
-
|
| 105 |
-
return False
|
| 106 |
|
| 107 |
-
|
|
|
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 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 |
-
total_days=None,
|
| 148 |
-
bag_size=None,
|
| 149 |
-
price=None,
|
| 150 |
-
):
|
| 151 |
-
"""Save farmer question for future product development."""
|
| 152 |
|
| 153 |
-
|
|
|
|
| 154 |
|
| 155 |
-
|
| 156 |
-
|
| 157 |
|
| 158 |
-
|
|
|
|
| 159 |
|
| 160 |
-
|
|
|
|
| 161 |
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
QUESTIONS_FILE,
|
| 165 |
-
"a",
|
| 166 |
-
newline="",
|
| 167 |
-
encoding="utf-8",
|
| 168 |
-
) as f:
|
| 169 |
-
|
| 170 |
-
writer = csv.writer(f)
|
| 171 |
-
|
| 172 |
-
if not file_exists:
|
| 173 |
-
writer.writerow(
|
| 174 |
-
[
|
| 175 |
-
"date",
|
| 176 |
-
"email",
|
| 177 |
-
"question",
|
| 178 |
-
"birds",
|
| 179 |
-
"age_days",
|
| 180 |
-
"bag_size_kg",
|
| 181 |
-
"price_per_bag",
|
| 182 |
-
]
|
| 183 |
-
)
|
| 184 |
|
| 185 |
-
|
| 186 |
-
[
|
| 187 |
-
datetime.now().isoformat(timespec="seconds"),
|
| 188 |
-
email,
|
| 189 |
-
question,
|
| 190 |
-
birds if birds is not None else "",
|
| 191 |
-
total_days if total_days is not None else "",
|
| 192 |
-
bag_size if bag_size is not None else "",
|
| 193 |
-
price if price is not None else "",
|
| 194 |
-
]
|
| 195 |
)
|
|
|
|
|
|
|
| 196 |
|
| 197 |
-
|
|
|
|
| 198 |
|
| 199 |
-
except Exception as e:
|
| 200 |
-
print("Question save error:", e)
|
| 201 |
-
return False
|
| 202 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
-
def interpolate(day, key):
|
| 205 |
-
"""
|
| 206 |
-
Linearly interpolate a benchmark value between
|
| 207 |
-
the nearest weekly checkpoints.
|
| 208 |
-
"""
|
| 209 |
|
| 210 |
-
|
|
|
|
| 211 |
|
| 212 |
-
if day <= days[0]:
|
| 213 |
-
return ROSS_BENCHMARK[days[0]][key]
|
| 214 |
|
| 215 |
-
|
| 216 |
-
|
|
|
|
| 217 |
|
| 218 |
-
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
|
| 223 |
-
|
|
|
|
|
|
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
| 227 |
|
| 228 |
-
|
|
|
|
|
|
|
|
|
|
| 229 |
|
| 230 |
-
return v1 + (v2 - v1) * fraction
|
| 231 |
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
|
| 235 |
-
def
|
| 236 |
-
"""Format Nigerian Naira."""
|
| 237 |
-
return f"₦{amount:,.0f}"
|
| 238 |
|
|
|
|
| 239 |
|
| 240 |
-
|
| 241 |
-
|
|
|
|
|
|
|
| 242 |
|
| 243 |
|
| 244 |
# ============================================================
|
| 245 |
-
#
|
| 246 |
# ============================================================
|
| 247 |
|
| 248 |
-
def
|
| 249 |
-
birds,
|
| 250 |
-
weeks,
|
| 251 |
-
extra_days,
|
| 252 |
-
bag_size,
|
| 253 |
-
price_per_bag,
|
| 254 |
-
):
|
| 255 |
-
"""
|
| 256 |
-
Main feed calculator.
|
| 257 |
|
| 258 |
-
|
| 259 |
-
|
|
|
|
| 260 |
|
| 261 |
-
birds = int(birds)
|
| 262 |
-
weeks = int(weeks)
|
| 263 |
-
extra_days = int(extra_days)
|
| 264 |
-
bag_size = float(bag_size)
|
| 265 |
-
price_per_bag = float(price_per_bag)
|
| 266 |
|
| 267 |
-
|
| 268 |
|
| 269 |
-
|
| 270 |
-
raise ValueError("Age must be at least 1 day.")
|
| 271 |
|
| 272 |
-
if birds <= 0:
|
| 273 |
-
raise ValueError("Number of birds must be greater than zero.")
|
| 274 |
|
| 275 |
-
|
| 276 |
-
raise ValueError("Feed bag size must be greater than zero.")
|
| 277 |
|
| 278 |
-
|
| 279 |
-
raise ValueError("Price per bag cannot be negative.")
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
# --------------------------------------------------------
|
| 284 |
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
"
|
| 288 |
)
|
| 289 |
|
| 290 |
-
|
| 291 |
-
total_days,
|
| 292 |
-
"body_weight",
|
| 293 |
-
)
|
| 294 |
|
| 295 |
-
total_feed_kg = (
|
| 296 |
-
cumulative_feed_per_bird * birds
|
| 297 |
-
)
|
| 298 |
|
| 299 |
-
|
| 300 |
-
# STARTER / FINISHER SPLIT
|
| 301 |
-
# --------------------------------------------------------
|
| 302 |
|
| 303 |
-
|
| 304 |
-
|
| 305 |
|
| 306 |
-
|
| 307 |
-
starter_cutoff,
|
| 308 |
-
"cumulative_feed",
|
| 309 |
-
)
|
| 310 |
|
| 311 |
-
starter_total_kg = (
|
| 312 |
-
starter_feed_per_bird * birds
|
| 313 |
-
)
|
| 314 |
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
|
| 323 |
# --------------------------------------------------------
|
| 324 |
-
#
|
|
|
|
| 325 |
# --------------------------------------------------------
|
| 326 |
|
| 327 |
-
|
| 328 |
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
)
|
| 332 |
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
# and round THAT total upward.
|
| 342 |
-
#
|
| 343 |
-
# Example:
|
| 344 |
-
# 145 kg / 25 kg = 5.8 bags
|
| 345 |
-
# => 6 bags
|
| 346 |
-
#
|
| 347 |
-
whole_total_bags = math.ceil(
|
| 348 |
-
exact_total_bags
|
| 349 |
-
)
|
| 350 |
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
# Starter bags are rounded up, then finisher bags
|
| 354 |
-
# are adjusted so that the combined number of bags
|
| 355 |
-
# never exceeds the total number needed.
|
| 356 |
-
starter_bags_to_buy = math.ceil(
|
| 357 |
-
exact_starter_bags
|
| 358 |
-
)
|
| 359 |
|
| 360 |
-
|
| 361 |
-
0,
|
| 362 |
-
whole_total_bags - starter_bags_to_buy,
|
| 363 |
-
)
|
| 364 |
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
remaining_bags,
|
| 373 |
-
)
|
| 374 |
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
#
|
| 380 |
-
# starter + finisher <= total bags
|
| 381 |
-
#
|
| 382 |
-
total_split_bags = (
|
| 383 |
-
starter_bags_to_buy
|
| 384 |
-
+ finisher_bags_to_buy
|
| 385 |
-
)
|
| 386 |
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
|
|
|
|
|
|
| 390 |
)
|
|
|
|
|
|
|
| 391 |
|
| 392 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
|
| 394 |
# --------------------------------------------------------
|
| 395 |
-
#
|
| 396 |
# --------------------------------------------------------
|
| 397 |
|
| 398 |
-
|
| 399 |
-
|
| 400 |
)
|
| 401 |
|
| 402 |
-
|
| 403 |
-
|
| 404 |
)
|
| 405 |
|
| 406 |
-
#
|
|
|
|
| 407 |
#
|
| 408 |
-
#
|
| 409 |
-
# day and previous day.
|
| 410 |
# --------------------------------------------------------
|
| 411 |
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
total_days - 1,
|
| 415 |
)
|
| 416 |
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
"cumulative_feed",
|
| 420 |
)
|
| 421 |
|
| 422 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 423 |
0,
|
| 424 |
-
|
| 425 |
-
- previous_feed,
|
| 426 |
)
|
| 427 |
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
)
|
| 434 |
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
"bag_size": bag_size,
|
| 441 |
-
"price_per_bag": price_per_bag,
|
| 442 |
|
| 443 |
-
|
| 444 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 445 |
|
| 446 |
-
|
| 447 |
-
|
|
|
|
|
|
|
|
|
|
| 448 |
|
| 449 |
-
|
| 450 |
-
|
|
|
|
|
|
|
|
|
|
| 451 |
|
| 452 |
-
|
| 453 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 454 |
|
| 455 |
-
"finisher_total_kg":
|
| 456 |
-
finisher_total_kg,
|
| 457 |
|
| 458 |
-
|
| 459 |
-
|
|
|
|
| 460 |
|
| 461 |
-
|
| 462 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 463 |
|
| 464 |
-
|
| 465 |
-
exact_finisher_bags,
|
| 466 |
|
| 467 |
-
|
| 468 |
-
|
| 469 |
|
| 470 |
-
|
| 471 |
-
starter_bags_to_buy,
|
| 472 |
|
| 473 |
-
|
| 474 |
-
finisher_bags_to_buy,
|
| 475 |
|
| 476 |
-
|
| 477 |
-
estimated_cost,
|
| 478 |
|
| 479 |
-
|
| 480 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 481 |
|
| 482 |
-
|
| 483 |
-
|
| 484 |
|
| 485 |
-
|
| 486 |
-
benchmark_fcr,
|
| 487 |
-
}
|
| 488 |
|
| 489 |
|
| 490 |
# ============================================================
|
| 491 |
-
#
|
| 492 |
# ============================================================
|
| 493 |
|
| 494 |
-
def
|
| 495 |
-
"""Create the farmer-friendly result."""
|
| 496 |
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
extra_days = result["extra_days"]
|
| 500 |
-
total_days = result["total_days"]
|
| 501 |
|
| 502 |
-
|
| 503 |
|
| 504 |
-
|
| 505 |
|
| 506 |
-
|
| 507 |
-
|
|
|
|
|
|
|
| 508 |
|
| 509 |
-
|
|
|
|
| 510 |
|
| 511 |
-
|
|
|
|
|
|
|
|
|
|
| 512 |
|
| 513 |
-
|
| 514 |
-
finisher_bags = result["finisher_bags_to_buy"]
|
| 515 |
|
| 516 |
-
|
|
|
|
|
|
|
|
|
|
| 517 |
|
| 518 |
-
|
|
|
|
|
|
|
| 519 |
|
| 520 |
-
|
|
|
|
| 521 |
|
| 522 |
-
|
|
|
|
| 523 |
|
| 524 |
-
|
|
|
|
| 525 |
|
| 526 |
-
|
| 527 |
|
| 528 |
-
if weeks != 1:
|
| 529 |
-
age_text += "s"
|
| 530 |
|
| 531 |
-
|
| 532 |
-
|
|
|
|
| 533 |
|
| 534 |
-
|
| 535 |
-
|
| 536 |
|
| 537 |
-
|
| 538 |
-
|
| 539 |
-
# --------------------------------------------------------
|
| 540 |
|
| 541 |
-
|
| 542 |
-
|
|
|
|
| 543 |
|
| 544 |
-
#
|
|
|
|
| 545 |
|
| 546 |
-
|
| 547 |
|
| 548 |
-
|
| 549 |
|
| 550 |
-
|
| 551 |
-
|
| 552 |
-
|
| 553 |
-
|
| 554 |
-
| **TOTAL** | **{total_feed:,.1f} kg** | **{total_bags} bags** |
|
| 555 |
|
| 556 |
-
|
|
|
|
|
|
|
|
|
|
| 557 |
|
| 558 |
-
|
| 559 |
|
| 560 |
-
|
| 561 |
|
| 562 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 563 |
|
| 564 |
-
|
| 565 |
|
| 566 |
-
|
| 567 |
|
| 568 |
-
|
|
|
|
|
|
|
|
|
|
| 569 |
|
| 570 |
-
|
|
|
|
|
|
|
|
|
|
| 571 |
|
| 572 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 573 |
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
| 577 |
-
|
| 578 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 579 |
|
| 580 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 581 |
|
| 582 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 583 |
|
| 584 |
-
This is a planning estimate based on the Ross 308 benchmark.
|
| 585 |
|
| 586 |
-
|
|
|
|
|
|
|
| 587 |
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
-
|
| 592 |
-
- temperature
|
| 593 |
-
- housing
|
| 594 |
-
- management
|
| 595 |
-
- disease
|
| 596 |
-
- stocking density
|
| 597 |
-
- water availability
|
| 598 |
-
- other farm conditions
|
| 599 |
|
| 600 |
-
|
| 601 |
-
""
|
|
|
|
|
|
|
| 602 |
|
| 603 |
-
|
|
|
|
|
|
|
|
|
|
| 604 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 605 |
|
| 606 |
-
# ============================================================
|
| 607 |
-
# EMAIL GATE + CALCULATION
|
| 608 |
-
# ============================================================
|
| 609 |
|
| 610 |
-
|
| 611 |
-
|
| 612 |
-
|
| 613 |
-
|
| 614 |
-
extra_days,
|
| 615 |
-
bag_size,
|
| 616 |
-
price_per_bag,
|
| 617 |
-
question,
|
| 618 |
-
):
|
| 619 |
-
"""
|
| 620 |
-
The farmer must provide an email before getting
|
| 621 |
-
the calculator result.
|
| 622 |
|
| 623 |
-
The question is automatically saved when the farmer
|
| 624 |
-
clicks Calculate / Get Answer.
|
| 625 |
-
"""
|
| 626 |
|
| 627 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 628 |
|
| 629 |
-
if not email:
|
| 630 |
-
return (
|
| 631 |
-
"### 📧 Email required\n\n"
|
| 632 |
-
"Please enter a valid email address before "
|
| 633 |
-
"calculating your feed requirement.",
|
| 634 |
-
"",
|
| 635 |
-
)
|
| 636 |
|
| 637 |
-
|
| 638 |
-
result = calculate_feed(
|
| 639 |
-
birds,
|
| 640 |
-
weeks,
|
| 641 |
-
extra_days,
|
| 642 |
-
bag_size,
|
| 643 |
-
price_per_bag,
|
| 644 |
-
)
|
| 645 |
|
| 646 |
-
except Exception as e:
|
| 647 |
|
| 648 |
-
|
| 649 |
-
|
| 650 |
-
|
| 651 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 652 |
|
| 653 |
-
# --------------------------------------------------------
|
| 654 |
-
# SAVE EMAIL
|
| 655 |
-
# --------------------------------------------------------
|
| 656 |
|
| 657 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 658 |
|
| 659 |
# --------------------------------------------------------
|
| 660 |
-
#
|
| 661 |
-
#
|
| 662 |
-
# This happens when Get Answer is clicked.
|
| 663 |
-
# No separate Save Question button is necessary.
|
| 664 |
# --------------------------------------------------------
|
| 665 |
|
| 666 |
-
|
| 667 |
|
| 668 |
-
|
|
|
|
|
|
|
| 669 |
|
| 670 |
-
|
| 671 |
-
|
| 672 |
-
|
| 673 |
-
birds=result["birds"],
|
| 674 |
-
total_days=result["total_days"],
|
| 675 |
-
bag_size=result["bag_size"],
|
| 676 |
-
price=result["price_per_bag"],
|
| 677 |
-
)
|
| 678 |
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
|
| 682 |
-
|
| 683 |
|
| 684 |
-
|
|
|
|
|
|
|
|
|
|
| 685 |
|
| 686 |
-
|
| 687 |
-
"Your calculation has been completed."
|
| 688 |
-
)
|
| 689 |
|
| 690 |
-
|
|
|
|
|
|
|
|
|
|
| 691 |
|
| 692 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 693 |
|
| 694 |
|
| 695 |
# ============================================================
|
| 696 |
-
#
|
| 697 |
# ============================================================
|
| 698 |
|
| 699 |
-
|
| 700 |
-
|
| 701 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 702 |
):
|
| 703 |
-
"""
|
| 704 |
-
Optional standalone question saving.
|
| 705 |
-
The main calculator already saves questions automatically
|
| 706 |
-
when Get Answer is clicked.
|
| 707 |
-
"""
|
| 708 |
|
| 709 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 710 |
|
| 711 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 712 |
|
| 713 |
-
|
| 714 |
-
|
|
|
|
| 715 |
|
| 716 |
-
|
| 717 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 718 |
|
| 719 |
-
|
| 720 |
-
|
| 721 |
-
|
| 722 |
-
|
| 723 |
-
|
| 724 |
-
|
| 725 |
-
|
| 726 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 727 |
|
| 728 |
-
|
|
|
|
| 729 |
|
| 730 |
|
| 731 |
# ============================================================
|
| 732 |
-
#
|
| 733 |
# ============================================================
|
| 734 |
|
| 735 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 736 |
"""
|
| 737 |
-
|
| 738 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 739 |
"""
|
| 740 |
|
| 741 |
-
if value == "Custom":
|
| 742 |
-
return gr.update(
|
| 743 |
-
visible=True,
|
| 744 |
-
value=100,
|
| 745 |
-
)
|
| 746 |
|
| 747 |
-
|
| 748 |
-
|
| 749 |
-
|
| 750 |
-
)
|
| 751 |
|
|
|
|
|
|
|
| 752 |
|
| 753 |
-
|
| 754 |
-
""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 755 |
|
| 756 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 757 |
|
| 758 |
-
|
| 759 |
-
raise ValueError(
|
| 760 |
-
"Please enter your number of birds."
|
| 761 |
-
)
|
| 762 |
|
| 763 |
-
|
| 764 |
|
| 765 |
-
|
| 766 |
-
raise ValueError(
|
| 767 |
-
"Number of birds must be greater than zero."
|
| 768 |
-
)
|
| 769 |
|
| 770 |
-
|
|
|
|
|
|
|
| 771 |
|
| 772 |
-
|
|
|
|
| 773 |
|
|
|
|
| 774 |
|
| 775 |
-
|
| 776 |
-
|
| 777 |
-
bird_selection,
|
| 778 |
-
custom_birds,
|
| 779 |
-
weeks,
|
| 780 |
-
extra_days,
|
| 781 |
-
bag_size,
|
| 782 |
-
price_per_bag,
|
| 783 |
-
question,
|
| 784 |
-
):
|
| 785 |
-
"""Wrapper around the main calculator."""
|
| 786 |
|
| 787 |
-
|
| 788 |
-
|
| 789 |
-
bird_selection,
|
| 790 |
-
custom_birds,
|
| 791 |
-
)
|
| 792 |
|
| 793 |
-
|
|
|
|
| 794 |
|
| 795 |
-
|
| 796 |
-
|
| 797 |
-
|
| 798 |
-
)
|
| 799 |
|
| 800 |
-
|
| 801 |
-
|
| 802 |
-
birds,
|
| 803 |
-
weeks,
|
| 804 |
-
extra_days,
|
| 805 |
-
bag_size,
|
| 806 |
-
price_per_bag,
|
| 807 |
-
question,
|
| 808 |
-
)
|
| 809 |
|
|
|
|
| 810 |
|
| 811 |
-
|
| 812 |
-
|
| 813 |
-
# ============================================================
|
| 814 |
|
| 815 |
-
|
| 816 |
-
.
|
| 817 |
-
|
| 818 |
-
|
| 819 |
-
}
|
| 820 |
|
| 821 |
-
|
| 822 |
-
font-size: 1.08rem;
|
| 823 |
-
}
|
| 824 |
|
| 825 |
-
|
| 826 |
-
border: 2px solid #f97316;
|
| 827 |
-
border-radius: 12px;
|
| 828 |
-
padding: 12px;
|
| 829 |
-
}
|
| 830 |
|
| 831 |
-
.
|
| 832 |
-
|
| 833 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 834 |
|
| 835 |
-
|
| 836 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 837 |
}
|
|
|
|
|
|
|
| 838 |
"""
|
| 839 |
|
| 840 |
|
| 841 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 842 |
|
| 843 |
gr.Markdown(
|
| 844 |
"""
|
|
@@ -846,108 +2312,166 @@ with gr.Blocks(css=CSS) as demo:
|
|
| 846 |
|
| 847 |
### Simple broiler feed calculator for farmers
|
| 848 |
|
| 849 |
-
Choose
|
| 850 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 851 |
|
| 852 |
-
|
| 853 |
-
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 854 |
)
|
| 855 |
|
| 856 |
-
#
|
| 857 |
-
#
|
| 858 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 859 |
|
| 860 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 861 |
|
| 862 |
gr.Markdown(
|
| 863 |
"""
|
| 864 |
-
##
|
| 865 |
|
| 866 |
-
Enter your email
|
| 867 |
-
updates and improvements.
|
| 868 |
|
| 869 |
-
|
|
|
|
|
|
|
|
|
|
| 870 |
"""
|
| 871 |
)
|
| 872 |
|
| 873 |
email = gr.Textbox(
|
| 874 |
-
label="Email address",
|
| 875 |
placeholder="farmer@example.com",
|
| 876 |
type="email",
|
| 877 |
)
|
| 878 |
|
| 879 |
-
|
| 880 |
-
|
| 881 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 882 |
|
| 883 |
with gr.Row():
|
| 884 |
|
| 885 |
-
#
|
| 886 |
# INPUTS
|
| 887 |
-
#
|
| 888 |
|
| 889 |
-
with gr.Column(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 890 |
|
| 891 |
gr.Markdown(
|
| 892 |
"## 🧮 Farm inputs"
|
| 893 |
)
|
| 894 |
|
| 895 |
-
|
| 896 |
-
label="🐔 Number of birds",
|
| 897 |
choices=[
|
| 898 |
-
|
| 899 |
-
|
| 900 |
-
|
| 901 |
-
|
| 902 |
-
|
| 903 |
-
|
| 904 |
-
|
| 905 |
-
|
| 906 |
-
"Custom",
|
| 907 |
],
|
| 908 |
-
value=
|
| 909 |
-
|
| 910 |
-
|
| 911 |
-
custom_birds = gr.Number(
|
| 912 |
-
label="Custom number of birds",
|
| 913 |
-
value=100,
|
| 914 |
-
minimum=1,
|
| 915 |
-
precision=0,
|
| 916 |
-
visible=False,
|
| 917 |
)
|
| 918 |
|
| 919 |
weeks = gr.Dropdown(
|
| 920 |
-
|
| 921 |
-
|
|
|
|
| 922 |
value=5,
|
|
|
|
| 923 |
)
|
| 924 |
|
| 925 |
-
|
| 926 |
-
|
| 927 |
-
|
|
|
|
| 928 |
value=0,
|
|
|
|
| 929 |
)
|
| 930 |
|
| 931 |
bag_size = gr.Dropdown(
|
| 932 |
-
label="🌾 Feed bag size",
|
| 933 |
choices=[
|
| 934 |
10,
|
|
|
|
|
|
|
| 935 |
25,
|
| 936 |
50,
|
| 937 |
],
|
| 938 |
value=25,
|
|
|
|
| 939 |
)
|
| 940 |
|
| 941 |
-
|
| 942 |
-
label="💰 Price per bag (₦)",
|
| 943 |
value=24000,
|
| 944 |
-
minimum=0,
|
| 945 |
precision=0,
|
|
|
|
| 946 |
)
|
| 947 |
|
| 948 |
-
|
| 949 |
-
|
| 950 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 951 |
|
| 952 |
with gr.Column(
|
| 953 |
scale=1,
|
|
@@ -959,16 +2483,21 @@ updates and improvements.
|
|
| 959 |
|
| 960 |
result = gr.Markdown(
|
| 961 |
"""
|
| 962 |
-
#
|
| 963 |
|
| 964 |
-
|
| 965 |
-
|
| 966 |
-
|
|
|
|
| 967 |
)
|
| 968 |
|
| 969 |
-
|
| 970 |
-
|
| 971 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 972 |
|
| 973 |
gr.Markdown("---")
|
| 974 |
|
|
@@ -978,9 +2507,11 @@ Your answer will appear here.
|
|
| 978 |
|
| 979 |
### What would you like to know about raising your birds?
|
| 980 |
|
| 981 |
-
You can type
|
| 982 |
|
| 983 |
-
|
|
|
|
|
|
|
| 984 |
"""
|
| 985 |
)
|
| 986 |
|
|
@@ -988,155 +2519,213 @@ You can type a question or use your device's microphone.
|
|
| 988 |
label="Your question",
|
| 989 |
placeholder=(
|
| 990 |
"Example: How many bags of feed do I need "
|
| 991 |
-
"for
|
| 992 |
),
|
| 993 |
lines=4,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 994 |
)
|
| 995 |
|
| 996 |
gr.Markdown(
|
| 997 |
"""
|
| 998 |
-
|
| 999 |
|
| 1000 |
-
- How many bags for 50 birds for 6 weeks?
|
| 1001 |
-
- How
|
|
|
|
| 1002 |
- Is 3 bags enough for 51 birds at 3 weeks?
|
| 1003 |
-
- How much
|
| 1004 |
-
- How much
|
| 1005 |
-
- How
|
| 1006 |
-
|
| 1007 |
-
|
| 1008 |
-
-
|
| 1009 |
-
|
| 1010 |
-
"""
|
| 1011 |
)
|
| 1012 |
|
| 1013 |
-
|
| 1014 |
-
|
| 1015 |
-
# ========================================================
|
| 1016 |
-
|
| 1017 |
-
calculate_button = gr.Button(
|
| 1018 |
-
"🐔 Get Answer / Calculate Feed Requirement",
|
| 1019 |
variant="primary",
|
| 1020 |
-
size="lg",
|
| 1021 |
)
|
| 1022 |
|
| 1023 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1024 |
|
| 1025 |
-
|
| 1026 |
-
# The main Calculate button automatically saves it,
|
| 1027 |
-
# but this allows farmers to submit questions even
|
| 1028 |
-
# without running a calculation.
|
| 1029 |
|
| 1030 |
-
|
| 1031 |
-
|
| 1032 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1033 |
)
|
| 1034 |
|
| 1035 |
# ========================================================
|
| 1036 |
# EVENTS
|
| 1037 |
# ========================================================
|
| 1038 |
|
| 1039 |
-
|
| 1040 |
-
|
| 1041 |
-
|
| 1042 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1043 |
)
|
| 1044 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1045 |
calculate_button.click(
|
| 1046 |
-
fn=
|
| 1047 |
inputs=[
|
| 1048 |
-
|
| 1049 |
-
bird_selection,
|
| 1050 |
-
custom_birds,
|
| 1051 |
weeks,
|
| 1052 |
-
|
| 1053 |
bag_size,
|
| 1054 |
-
|
| 1055 |
-
|
|
|
|
|
|
|
|
|
|
| 1056 |
],
|
| 1057 |
outputs=[
|
| 1058 |
result,
|
| 1059 |
-
|
| 1060 |
],
|
|
|
|
|
|
|
| 1061 |
)
|
| 1062 |
|
| 1063 |
-
|
| 1064 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1065 |
inputs=[
|
| 1066 |
-
email,
|
| 1067 |
question,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1068 |
],
|
| 1069 |
outputs=[
|
|
|
|
| 1070 |
question_status,
|
| 1071 |
],
|
|
|
|
|
|
|
| 1072 |
)
|
| 1073 |
|
| 1074 |
-
# ========================================================
|
| 1075 |
-
# HOW IT WORKS
|
| 1076 |
-
# ========================================================
|
| 1077 |
-
|
| 1078 |
-
gr.Markdown(
|
| 1079 |
-
"""
|
| 1080 |
-
---
|
| 1081 |
-
|
| 1082 |
-
## 📌 How the estimate works
|
| 1083 |
|
| 1084 |
-
|
| 1085 |
-
|
| 1086 |
-
|
| 1087 |
-
|
| 1088 |
-
|
| 1089 |
-
|
| 1090 |
-
|
| 1091 |
-
|
| 1092 |
-
|
| 1093 |
-
|
| 1094 |
-
#
|
| 1095 |
-
|
| 1096 |
-
|
| 1097 |
-
|
| 1098 |
-
**145 kg ÷ 25 kg = 5.8 bags**
|
| 1099 |
-
|
| 1100 |
-
the app will tell the farmer to plan for:
|
| 1101 |
-
|
| 1102 |
-
## **6 bags**
|
| 1103 |
-
|
| 1104 |
-
rather than incorrectly displaying only 5 bags.
|
| 1105 |
-
|
| 1106 |
-
---
|
| 1107 |
-
|
| 1108 |
-
## ⚠️ Important
|
| 1109 |
-
|
| 1110 |
-
Actual farm consumption can vary with genetics, sex, mortality,
|
| 1111 |
-
feed quality, temperature, management, disease, stocking density,
|
| 1112 |
-
water availability and other farm conditions.
|
| 1113 |
-
|
| 1114 |
-
This calculator provides a **planning estimate**, not veterinary
|
| 1115 |
-
or farm-specific advice.
|
| 1116 |
-
|
| 1117 |
-
---
|
| 1118 |
-
|
| 1119 |
-
## 💡 Help us build better tools
|
| 1120 |
|
| 1121 |
-
|
| 1122 |
-
|
| 1123 |
-
|
| 1124 |
-
""
|
| 1125 |
-
|
| 1126 |
|
| 1127 |
|
| 1128 |
# ============================================================
|
| 1129 |
-
#
|
| 1130 |
-
# ============================================================
|
| 1131 |
-
#
|
| 1132 |
-
# DO NOT specify port 7860 manually.
|
| 1133 |
-
# DO NOT start uvicorn separately.
|
| 1134 |
-
# DO NOT call demo.launch() more than once.
|
| 1135 |
-
#
|
| 1136 |
-
# Hugging Face Spaces manages the server/port.
|
| 1137 |
# ============================================================
|
| 1138 |
|
| 1139 |
if __name__ == "__main__":
|
| 1140 |
-
|
| 1141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1142 |
)
|
|
|
|
| 1 |
+
# ============================================================
|
| 2 |
+
# PoultryFeedCalc
|
| 3 |
+
# FINAL FARMER-FRIENDLY VERSION
|
| 4 |
+
#
|
| 5 |
+
# Features:
|
| 6 |
+
# - Simple bird selection
|
| 7 |
+
# - Age = weeks + days
|
| 8 |
+
# - Converts age to total days
|
| 9 |
+
# - Starter / Finisher breakdown
|
| 10 |
+
# - 20% practical purchase allowance
|
| 11 |
+
# - Whole bags rounded UP
|
| 12 |
+
# - Farmer-friendly quick answer
|
| 13 |
+
# - Feed cost
|
| 14 |
+
# - Email required before calculation
|
| 15 |
+
# - Optional Google Sign-In / automatic email collection
|
| 16 |
+
# - Automatic country + currency detection
|
| 17 |
+
# - Ask a Question
|
| 18 |
+
# - Questions automatically saved when Get Answer is clicked
|
| 19 |
+
# - Common farmer feed questions answered automatically
|
| 20 |
+
# - SQLite database
|
| 21 |
+
# - FastAPI + Gradio 6
|
| 22 |
+
#
|
| 23 |
+
# IMPORTANT:
|
| 24 |
+
# This file has ONE server startup only.
|
| 25 |
+
# Do NOT add demo.launch() anywhere else.
|
| 26 |
+
# ============================================================
|
| 27 |
+
|
| 28 |
+
from __future__ import annotations
|
| 29 |
+
|
| 30 |
import math
|
| 31 |
import os
|
| 32 |
import re
|
| 33 |
+
import sqlite3
|
| 34 |
+
import threading
|
| 35 |
from datetime import datetime
|
| 36 |
+
from typing import Optional
|
| 37 |
|
| 38 |
+
import requests
|
| 39 |
import gradio as gr
|
| 40 |
|
| 41 |
+
from fastapi import FastAPI, Request
|
| 42 |
+
from fastapi.responses import RedirectResponse, HTMLResponse
|
| 43 |
+
from starlette.middleware.sessions import SessionMiddleware
|
| 44 |
+
|
| 45 |
+
from authlib.integrations.starlette_client import OAuth, OAuthError
|
| 46 |
+
from itsdangerous import URLSafeTimedSerializer, BadSignature, SignatureExpired
|
| 47 |
+
|
| 48 |
|
| 49 |
# ============================================================
|
| 50 |
+
# APPLICATION SETTINGS
|
| 51 |
# ============================================================
|
| 52 |
|
| 53 |
+
APP_NAME = "PoultryFeedCalc"
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
PORT = int(os.getenv("PORT", "7860"))
|
| 56 |
|
| 57 |
+
# ------------------------------------------------------------
|
| 58 |
+
# Practical purchase allowance
|
|
|
|
| 59 |
#
|
| 60 |
+
# This is the adjustment requested for the farmer-facing
|
| 61 |
+
# purchase recommendation.
|
|
|
|
| 62 |
#
|
| 63 |
+
# Example:
|
| 64 |
+
# 122.2 kg benchmark
|
| 65 |
+
# × 1.20
|
| 66 |
+
# = 146.64 kg
|
| 67 |
+
# ÷ 25 kg
|
| 68 |
+
# = 5.8656 bags
|
| 69 |
+
# → 6 whole bags
|
| 70 |
+
# ------------------------------------------------------------
|
|
|
|
| 71 |
|
| 72 |
+
PURCHASE_ALLOWANCE = 0.20
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
|
| 75 |
# ============================================================
|
| 76 |
+
# DATABASE
|
| 77 |
# ============================================================
|
| 78 |
|
| 79 |
+
# If a persistent HF storage volume is mounted at /data,
|
| 80 |
+
# use it automatically.
|
| 81 |
+
#
|
| 82 |
+
# Otherwise use the local Space directory.
|
| 83 |
|
| 84 |
+
if os.path.isdir("/data"):
|
| 85 |
+
DB_PATH = "/data/poultryfeedcalc.db"
|
| 86 |
+
else:
|
| 87 |
+
DB_PATH = "poultryfeedcalc.db"
|
| 88 |
|
| 89 |
+
DB_LOCK = threading.Lock()
|
|
|
|
| 90 |
|
|
|
|
| 91 |
|
| 92 |
+
def get_db():
|
| 93 |
+
conn = sqlite3.connect(
|
| 94 |
+
DB_PATH,
|
| 95 |
+
check_same_thread=False,
|
| 96 |
+
)
|
| 97 |
+
conn.row_factory = sqlite3.Row
|
| 98 |
+
return conn
|
| 99 |
|
|
|
|
| 100 |
|
| 101 |
+
def init_database():
|
| 102 |
|
| 103 |
+
with DB_LOCK:
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
conn = get_db()
|
|
|
|
| 106 |
|
| 107 |
+
# ----------------------------------------------------
|
| 108 |
+
# Mailing list
|
| 109 |
+
# ----------------------------------------------------
|
| 110 |
|
| 111 |
+
conn.execute(
|
| 112 |
+
"""
|
| 113 |
+
CREATE TABLE IF NOT EXISTS subscribers (
|
| 114 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 115 |
+
email TEXT UNIQUE NOT NULL,
|
| 116 |
+
country TEXT,
|
| 117 |
+
currency TEXT,
|
| 118 |
+
source TEXT,
|
| 119 |
+
created_at TEXT NOT NULL,
|
| 120 |
+
last_seen_at TEXT NOT NULL
|
| 121 |
+
)
|
| 122 |
+
"""
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
# ----------------------------------------------------
|
| 126 |
+
# Farmer questions
|
| 127 |
+
# ----------------------------------------------------
|
|
|
|
| 128 |
|
| 129 |
+
conn.execute(
|
| 130 |
+
"""
|
| 131 |
+
CREATE TABLE IF NOT EXISTS farmer_questions (
|
| 132 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 133 |
+
email TEXT,
|
| 134 |
+
question TEXT NOT NULL,
|
| 135 |
+
country TEXT,
|
| 136 |
+
created_at TEXT NOT NULL
|
| 137 |
)
|
| 138 |
+
"""
|
| 139 |
+
)
|
| 140 |
|
| 141 |
+
# ----------------------------------------------------
|
| 142 |
+
# Calculator usage
|
| 143 |
+
# ----------------------------------------------------
|
| 144 |
|
| 145 |
+
conn.execute(
|
| 146 |
+
"""
|
| 147 |
+
CREATE TABLE IF NOT EXISTS calculations (
|
| 148 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 149 |
|
| 150 |
+
email TEXT,
|
| 151 |
+
country TEXT,
|
| 152 |
+
currency TEXT,
|
| 153 |
|
| 154 |
+
birds INTEGER,
|
| 155 |
+
weeks INTEGER,
|
| 156 |
+
days INTEGER,
|
| 157 |
+
total_days INTEGER,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
+
bag_size REAL,
|
| 160 |
+
price_per_bag REAL,
|
| 161 |
|
| 162 |
+
benchmark_feed_kg REAL,
|
| 163 |
+
purchase_feed_kg REAL,
|
| 164 |
|
| 165 |
+
exact_bags REAL,
|
| 166 |
+
whole_bags INTEGER,
|
| 167 |
|
| 168 |
+
starter_kg REAL,
|
| 169 |
+
finisher_kg REAL,
|
| 170 |
|
| 171 |
+
starter_bags INTEGER,
|
| 172 |
+
finisher_bags INTEGER,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
|
| 174 |
+
created_at TEXT NOT NULL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
)
|
| 176 |
+
"""
|
| 177 |
+
)
|
| 178 |
|
| 179 |
+
conn.commit()
|
| 180 |
+
conn.close()
|
| 181 |
|
|
|
|
|
|
|
|
|
|
| 182 |
|
| 183 |
+
init_database()
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
# ============================================================
|
| 187 |
+
# ROSS-STYLE FEED BENCHMARK
|
| 188 |
+
# ============================================================
|
| 189 |
+
|
| 190 |
+
# Cumulative feed kg per bird.
|
| 191 |
+
#
|
| 192 |
+
# These are the benchmark values used by the current
|
| 193 |
+
# PoultryFeedCalc MVP.
|
| 194 |
+
#
|
| 195 |
+
# The calculator interpolates between weekly checkpoints
|
| 196 |
+
# when the farmer enters additional days.
|
| 197 |
+
|
| 198 |
+
ROSS_FEED = {
|
| 199 |
+
0: 0.000,
|
| 200 |
+
1: 0.160,
|
| 201 |
+
2: 0.480,
|
| 202 |
+
3: 1.050,
|
| 203 |
+
4: 2.147,
|
| 204 |
+
5: 3.392,
|
| 205 |
+
6: 4.888,
|
| 206 |
+
7: 6.420,
|
| 207 |
+
8: 7.950,
|
| 208 |
+
}
|
| 209 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
+
# Starter ends at day 21.
|
| 212 |
+
STARTER_END_DAY = 21
|
| 213 |
|
|
|
|
|
|
|
| 214 |
|
| 215 |
+
# ============================================================
|
| 216 |
+
# CURRENCY
|
| 217 |
+
# ============================================================
|
| 218 |
|
| 219 |
+
CURRENCY_SYMBOLS = {
|
| 220 |
|
| 221 |
+
"NGN": "₦",
|
| 222 |
+
"GHS": "GH₵",
|
| 223 |
+
"KES": "KSh",
|
| 224 |
+
"TZS": "TSh",
|
| 225 |
+
"UGX": "USh",
|
| 226 |
+
"ZAR": "R",
|
| 227 |
+
"ZMW": "ZK",
|
| 228 |
+
"RWF": "FRw",
|
| 229 |
+
"ETB": "Br",
|
| 230 |
|
| 231 |
+
"USD": "$",
|
| 232 |
+
"CAD": "C$",
|
| 233 |
+
"AUD": "A$",
|
| 234 |
|
| 235 |
+
"GBP": "£",
|
| 236 |
+
"EUR": "€",
|
| 237 |
|
| 238 |
+
"INR": "₹",
|
| 239 |
+
"AED": "د.إ",
|
| 240 |
+
"SAR": "﷼",
|
| 241 |
+
}
|
| 242 |
|
|
|
|
| 243 |
|
| 244 |
+
DEFAULT_CURRENCY = {
|
| 245 |
+
"country": "Nigeria",
|
| 246 |
+
"currency": "NGN",
|
| 247 |
+
"symbol": "₦",
|
| 248 |
+
"price": 24000,
|
| 249 |
+
}
|
| 250 |
|
| 251 |
|
| 252 |
+
def currency_symbol(currency_code: str) -> str:
|
|
|
|
|
|
|
| 253 |
|
| 254 |
+
currency_code = (currency_code or "").upper()
|
| 255 |
|
| 256 |
+
return CURRENCY_SYMBOLS.get(
|
| 257 |
+
currency_code,
|
| 258 |
+
currency_code,
|
| 259 |
+
)
|
| 260 |
|
| 261 |
|
| 262 |
# ============================================================
|
| 263 |
+
# GENERAL HELPERS
|
| 264 |
# ============================================================
|
| 265 |
|
| 266 |
+
def now_iso():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
|
| 268 |
+
return datetime.utcnow().isoformat(
|
| 269 |
+
timespec="seconds"
|
| 270 |
+
)
|
| 271 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
+
def clean_email(email):
|
| 274 |
|
| 275 |
+
return (email or "").strip().lower()
|
|
|
|
| 276 |
|
|
|
|
|
|
|
| 277 |
|
| 278 |
+
def valid_email(email):
|
|
|
|
| 279 |
|
| 280 |
+
email = clean_email(email)
|
|
|
|
| 281 |
|
| 282 |
+
if not email:
|
| 283 |
+
return False
|
|
|
|
| 284 |
|
| 285 |
+
pattern = (
|
| 286 |
+
r"^[A-Za-z0-9.!#$%&'*+/=?^_`{|}~-]+@"
|
| 287 |
+
r"[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+$"
|
| 288 |
)
|
| 289 |
|
| 290 |
+
return bool(re.match(pattern, email))
|
|
|
|
|
|
|
|
|
|
| 291 |
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
+
def format_number(value, decimals=1):
|
|
|
|
|
|
|
| 294 |
|
| 295 |
+
if decimals == 0:
|
| 296 |
+
return f"{value:,.0f}"
|
| 297 |
|
| 298 |
+
return f"{value:,.{decimals}f}"
|
|
|
|
|
|
|
|
|
|
| 299 |
|
|
|
|
|
|
|
|
|
|
| 300 |
|
| 301 |
+
def format_money(amount, symbol):
|
| 302 |
+
|
| 303 |
+
if symbol == "₦":
|
| 304 |
+
return f"₦{amount:,.0f}"
|
| 305 |
+
|
| 306 |
+
return f"{symbol}{amount:,.0f}"
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
def safe_int(value):
|
| 310 |
+
|
| 311 |
+
try:
|
| 312 |
+
return int(float(value))
|
| 313 |
+
except Exception:
|
| 314 |
+
return 0
|
| 315 |
+
|
| 316 |
+
|
| 317 |
+
def safe_float(value):
|
| 318 |
+
|
| 319 |
+
try:
|
| 320 |
+
return float(value)
|
| 321 |
+
except Exception:
|
| 322 |
+
return 0.0
|
| 323 |
+
|
| 324 |
+
|
| 325 |
+
# ============================================================
|
| 326 |
+
# FEED CALCULATION
|
| 327 |
+
# ============================================================
|
| 328 |
+
|
| 329 |
+
def cumulative_feed_per_bird(total_days: int):
|
| 330 |
+
|
| 331 |
+
total_days = max(0, int(total_days))
|
| 332 |
|
| 333 |
# --------------------------------------------------------
|
| 334 |
+
# Beyond week 8:
|
| 335 |
+
# continue using the last weekly increase.
|
| 336 |
# --------------------------------------------------------
|
| 337 |
|
| 338 |
+
if total_days >= 56:
|
| 339 |
|
| 340 |
+
last_week_value = ROSS_FEED[8]
|
| 341 |
+
previous_week_value = ROSS_FEED[7]
|
|
|
|
| 342 |
|
| 343 |
+
daily_increment = (
|
| 344 |
+
last_week_value - previous_week_value
|
| 345 |
+
) / 7
|
| 346 |
|
| 347 |
+
return (
|
| 348 |
+
last_week_value
|
| 349 |
+
+ ((total_days - 56) * daily_increment)
|
| 350 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 351 |
|
| 352 |
+
week = total_days // 7
|
| 353 |
+
remaining_days = total_days % 7
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 354 |
|
| 355 |
+
if remaining_days == 0:
|
|
|
|
|
|
|
|
|
|
| 356 |
|
| 357 |
+
return ROSS_FEED.get(
|
| 358 |
+
week,
|
| 359 |
+
ROSS_FEED[8],
|
| 360 |
+
)
|
| 361 |
|
| 362 |
+
current_week = min(week, 8)
|
| 363 |
+
next_week = min(week + 1, 8)
|
|
|
|
|
|
|
| 364 |
|
| 365 |
+
current_value = ROSS_FEED[current_week]
|
| 366 |
+
next_value = ROSS_FEED[next_week]
|
| 367 |
+
|
| 368 |
+
fraction = remaining_days / 7
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 369 |
|
| 370 |
+
return (
|
| 371 |
+
current_value
|
| 372 |
+
+ (
|
| 373 |
+
(next_value - current_value)
|
| 374 |
+
* fraction
|
| 375 |
)
|
| 376 |
+
)
|
| 377 |
+
|
| 378 |
|
| 379 |
+
def calculate_feed_numbers(
|
| 380 |
+
birds: int,
|
| 381 |
+
total_days: int,
|
| 382 |
+
bag_size: float,
|
| 383 |
+
):
|
| 384 |
|
| 385 |
# --------------------------------------------------------
|
| 386 |
+
# Benchmark feed
|
| 387 |
# --------------------------------------------------------
|
| 388 |
|
| 389 |
+
feed_per_bird = cumulative_feed_per_bird(
|
| 390 |
+
total_days
|
| 391 |
)
|
| 392 |
|
| 393 |
+
benchmark_total_kg = (
|
| 394 |
+
feed_per_bird * birds
|
| 395 |
)
|
| 396 |
|
| 397 |
+
# --------------------------------------------------------
|
| 398 |
+
# Starter
|
| 399 |
#
|
| 400 |
+
# Starter is the benchmark consumption through day 21.
|
|
|
|
| 401 |
# --------------------------------------------------------
|
| 402 |
|
| 403 |
+
starter_feed_per_bird = cumulative_feed_per_bird(
|
| 404 |
+
min(total_days, STARTER_END_DAY)
|
|
|
|
| 405 |
)
|
| 406 |
|
| 407 |
+
starter_kg = (
|
| 408 |
+
starter_feed_per_bird * birds
|
|
|
|
| 409 |
)
|
| 410 |
|
| 411 |
+
# --------------------------------------------------------
|
| 412 |
+
# Finisher
|
| 413 |
+
# --------------------------------------------------------
|
| 414 |
+
|
| 415 |
+
finisher_kg = max(
|
| 416 |
0,
|
| 417 |
+
benchmark_total_kg - starter_kg,
|
|
|
|
| 418 |
)
|
| 419 |
|
| 420 |
+
# --------------------------------------------------------
|
| 421 |
+
# Practical purchase allowance
|
| 422 |
+
#
|
| 423 |
+
# This is applied to TOTAL feed only.
|
| 424 |
+
#
|
| 425 |
+
# This is important because the farmer's final bag
|
| 426 |
+
# recommendation must be based on the total estimated
|
| 427 |
+
# requirement rather than adding independently rounded
|
| 428 |
+
# Starter + Finisher bags.
|
| 429 |
+
# --------------------------------------------------------
|
| 430 |
+
|
| 431 |
+
purchase_feed_kg = (
|
| 432 |
+
benchmark_total_kg
|
| 433 |
+
* (1 + PURCHASE_ALLOWANCE)
|
| 434 |
)
|
| 435 |
|
| 436 |
+
exact_bags = (
|
| 437 |
+
purchase_feed_kg / bag_size
|
| 438 |
+
)
|
| 439 |
+
|
| 440 |
+
whole_bags = math.ceil(exact_bags)
|
|
|
|
|
|
|
| 441 |
|
| 442 |
+
# --------------------------------------------------------
|
| 443 |
+
# Stage bag estimates
|
| 444 |
+
#
|
| 445 |
+
# These remain based on benchmark quantities so that:
|
| 446 |
+
#
|
| 447 |
+
# 5 weeks:
|
| 448 |
+
# 2 Starter + 3 Finisher
|
| 449 |
+
#
|
| 450 |
+
# 6 weeks:
|
| 451 |
+
# 2 Starter + 4 Finisher
|
| 452 |
+
#
|
| 453 |
+
# while TOTAL uses the practical allowance.
|
| 454 |
+
# --------------------------------------------------------
|
| 455 |
|
| 456 |
+
starter_bags = (
|
| 457 |
+
math.ceil(starter_kg / bag_size)
|
| 458 |
+
if starter_kg > 0
|
| 459 |
+
else 0
|
| 460 |
+
)
|
| 461 |
|
| 462 |
+
finisher_bags = (
|
| 463 |
+
math.ceil(finisher_kg / bag_size)
|
| 464 |
+
if finisher_kg > 0
|
| 465 |
+
else 0
|
| 466 |
+
)
|
| 467 |
|
| 468 |
+
return {
|
| 469 |
+
"feed_per_bird": feed_per_bird,
|
| 470 |
+
"benchmark_total_kg": benchmark_total_kg,
|
| 471 |
+
"purchase_feed_kg": purchase_feed_kg,
|
| 472 |
+
"exact_bags": exact_bags,
|
| 473 |
+
"whole_bags": whole_bags,
|
| 474 |
+
"starter_kg": starter_kg,
|
| 475 |
+
"finisher_kg": finisher_kg,
|
| 476 |
+
"starter_bags": starter_bags,
|
| 477 |
+
"finisher_bags": finisher_bags,
|
| 478 |
+
}
|
| 479 |
|
|
|
|
|
|
|
| 480 |
|
| 481 |
+
# ============================================================
|
| 482 |
+
# EMAIL DATABASE
|
| 483 |
+
# ============================================================
|
| 484 |
|
| 485 |
+
def save_subscriber(
|
| 486 |
+
email,
|
| 487 |
+
country,
|
| 488 |
+
currency,
|
| 489 |
+
source="calculator",
|
| 490 |
+
):
|
| 491 |
|
| 492 |
+
email = clean_email(email)
|
|
|
|
| 493 |
|
| 494 |
+
if not valid_email(email):
|
| 495 |
+
return False
|
| 496 |
|
| 497 |
+
timestamp = now_iso()
|
|
|
|
| 498 |
|
| 499 |
+
with DB_LOCK:
|
|
|
|
| 500 |
|
| 501 |
+
conn = get_db()
|
|
|
|
| 502 |
|
| 503 |
+
conn.execute(
|
| 504 |
+
"""
|
| 505 |
+
INSERT INTO subscribers (
|
| 506 |
+
email,
|
| 507 |
+
country,
|
| 508 |
+
currency,
|
| 509 |
+
source,
|
| 510 |
+
created_at,
|
| 511 |
+
last_seen_at
|
| 512 |
+
)
|
| 513 |
+
VALUES (?, ?, ?, ?, ?, ?)
|
| 514 |
+
|
| 515 |
+
ON CONFLICT(email)
|
| 516 |
+
DO UPDATE SET
|
| 517 |
+
country = excluded.country,
|
| 518 |
+
currency = excluded.currency,
|
| 519 |
+
source = excluded.source,
|
| 520 |
+
last_seen_at = excluded.last_seen_at
|
| 521 |
+
""",
|
| 522 |
+
(
|
| 523 |
+
email,
|
| 524 |
+
country,
|
| 525 |
+
currency,
|
| 526 |
+
source,
|
| 527 |
+
timestamp,
|
| 528 |
+
timestamp,
|
| 529 |
+
),
|
| 530 |
+
)
|
| 531 |
|
| 532 |
+
conn.commit()
|
| 533 |
+
conn.close()
|
| 534 |
|
| 535 |
+
return True
|
|
|
|
|
|
|
| 536 |
|
| 537 |
|
| 538 |
# ============================================================
|
| 539 |
+
# COUNTRY DETECTION
|
| 540 |
# ============================================================
|
| 541 |
|
| 542 |
+
def get_client_ip(request: Optional[gr.Request]):
|
|
|
|
| 543 |
|
| 544 |
+
if not request:
|
| 545 |
+
return None
|
|
|
|
|
|
|
| 546 |
|
| 547 |
+
try:
|
| 548 |
|
| 549 |
+
headers = dict(request.headers)
|
| 550 |
|
| 551 |
+
# Cloudflare
|
| 552 |
+
ip = headers.get(
|
| 553 |
+
"cf-connecting-ip"
|
| 554 |
+
)
|
| 555 |
|
| 556 |
+
if ip:
|
| 557 |
+
return ip.strip()
|
| 558 |
|
| 559 |
+
# Reverse proxy
|
| 560 |
+
forwarded = headers.get(
|
| 561 |
+
"x-forwarded-for"
|
| 562 |
+
)
|
| 563 |
|
| 564 |
+
if forwarded:
|
|
|
|
| 565 |
|
| 566 |
+
return (
|
| 567 |
+
forwarded.split(",")[0]
|
| 568 |
+
.strip()
|
| 569 |
+
)
|
| 570 |
|
| 571 |
+
real_ip = headers.get(
|
| 572 |
+
"x-real-ip"
|
| 573 |
+
)
|
| 574 |
|
| 575 |
+
if real_ip:
|
| 576 |
+
return real_ip.strip()
|
| 577 |
|
| 578 |
+
if request.client:
|
| 579 |
+
return request.client.host
|
| 580 |
|
| 581 |
+
except Exception:
|
| 582 |
+
pass
|
| 583 |
|
| 584 |
+
return None
|
| 585 |
|
|
|
|
|
|
|
| 586 |
|
| 587 |
+
def detect_country_from_request(
|
| 588 |
+
request: Optional[gr.Request] = None,
|
| 589 |
+
):
|
| 590 |
|
| 591 |
+
"""
|
| 592 |
+
Detect country using the visitor's IP.
|
| 593 |
|
| 594 |
+
If detection fails, Nigeria is used as the MVP default.
|
| 595 |
+
"""
|
|
|
|
| 596 |
|
| 597 |
+
country = DEFAULT_CURRENCY["country"]
|
| 598 |
+
currency = DEFAULT_CURRENCY["currency"]
|
| 599 |
+
symbol = DEFAULT_CURRENCY["symbol"]
|
| 600 |
|
| 601 |
+
# Keep Nigeria's requested default price.
|
| 602 |
+
price = DEFAULT_CURRENCY["price"]
|
| 603 |
|
| 604 |
+
try:
|
| 605 |
|
| 606 |
+
ip = get_client_ip(request)
|
| 607 |
|
| 608 |
+
if ip and ip not in (
|
| 609 |
+
"127.0.0.1",
|
| 610 |
+
"::1",
|
| 611 |
+
):
|
|
|
|
| 612 |
|
| 613 |
+
url = (
|
| 614 |
+
f"https://ipapi.co/"
|
| 615 |
+
f"{ip}/json/"
|
| 616 |
+
)
|
| 617 |
|
| 618 |
+
else:
|
| 619 |
|
| 620 |
+
url = "https://ipapi.co/json/"
|
| 621 |
|
| 622 |
+
response = requests.get(
|
| 623 |
+
url,
|
| 624 |
+
timeout=3,
|
| 625 |
+
headers={
|
| 626 |
+
"User-Agent":
|
| 627 |
+
"PoultryFeedCalc/1.0"
|
| 628 |
+
},
|
| 629 |
+
)
|
| 630 |
|
| 631 |
+
if response.ok:
|
| 632 |
|
| 633 |
+
data = response.json()
|
| 634 |
|
| 635 |
+
detected_country = (
|
| 636 |
+
data.get("country_name")
|
| 637 |
+
or country
|
| 638 |
+
)
|
| 639 |
|
| 640 |
+
detected_currency = (
|
| 641 |
+
data.get("currency")
|
| 642 |
+
or currency
|
| 643 |
+
).upper()
|
| 644 |
|
| 645 |
+
country = detected_country
|
| 646 |
+
currency = detected_currency
|
| 647 |
+
symbol = currency_symbol(
|
| 648 |
+
detected_currency
|
| 649 |
+
)
|
| 650 |
|
| 651 |
+
# ------------------------------------------------
|
| 652 |
+
# We do NOT pretend that foreign feed prices can
|
| 653 |
+
# be converted accurately using FX rates.
|
| 654 |
+
#
|
| 655 |
+
# Nigerian MVP keeps ₦24,000.
|
| 656 |
+
# Other countries ask the farmer to enter their
|
| 657 |
+
# local bag price.
|
| 658 |
+
# ------------------------------------------------
|
| 659 |
+
|
| 660 |
+
if currency != "NGN":
|
| 661 |
+
price = None
|
| 662 |
+
|
| 663 |
+
except Exception:
|
| 664 |
+
pass
|
| 665 |
+
|
| 666 |
+
display = (
|
| 667 |
+
f"🌍 **Detected country:** {country}\n\n"
|
| 668 |
+
f"💱 **Currency:** {currency} "
|
| 669 |
+
f"({symbol})"
|
| 670 |
+
)
|
| 671 |
|
| 672 |
+
price_component = gr.Number(
|
| 673 |
+
label=f"💰 Price per bag ({symbol})",
|
| 674 |
+
value=price,
|
| 675 |
+
precision=0,
|
| 676 |
+
)
|
| 677 |
|
| 678 |
+
return (
|
| 679 |
+
country,
|
| 680 |
+
currency,
|
| 681 |
+
symbol,
|
| 682 |
+
display,
|
| 683 |
+
price_component,
|
| 684 |
+
)
|
| 685 |
|
|
|
|
| 686 |
|
| 687 |
+
# ============================================================
|
| 688 |
+
# GOOGLE OAUTH
|
| 689 |
+
# ============================================================
|
| 690 |
|
| 691 |
+
GOOGLE_CLIENT_ID = os.getenv(
|
| 692 |
+
"GOOGLE_CLIENT_ID",
|
| 693 |
+
"",
|
| 694 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
|
| 696 |
+
GOOGLE_CLIENT_SECRET = os.getenv(
|
| 697 |
+
"GOOGLE_CLIENT_SECRET",
|
| 698 |
+
"",
|
| 699 |
+
)
|
| 700 |
|
| 701 |
+
GOOGLE_REDIRECT_URI = os.getenv(
|
| 702 |
+
"GOOGLE_REDIRECT_URI",
|
| 703 |
+
"",
|
| 704 |
+
)
|
| 705 |
|
| 706 |
+
PFC_SECRET_KEY = os.getenv(
|
| 707 |
+
"PFC_SECRET_KEY",
|
| 708 |
+
"change-this-secret-key-in-production",
|
| 709 |
+
)
|
| 710 |
|
|
|
|
|
|
|
|
|
|
| 711 |
|
| 712 |
+
GOOGLE_ENABLED = bool(
|
| 713 |
+
GOOGLE_CLIENT_ID
|
| 714 |
+
and GOOGLE_CLIENT_SECRET
|
| 715 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 716 |
|
|
|
|
|
|
|
|
|
|
| 717 |
|
| 718 |
+
# Signed cookie for the email obtained from Google.
|
| 719 |
+
EMAIL_SIGNER = URLSafeTimedSerializer(
|
| 720 |
+
PFC_SECRET_KEY,
|
| 721 |
+
salt="poultryfeedcalc-google-email",
|
| 722 |
+
)
|
| 723 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 724 |
|
| 725 |
+
oauth = OAuth()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 726 |
|
|
|
|
| 727 |
|
| 728 |
+
if GOOGLE_ENABLED:
|
| 729 |
+
|
| 730 |
+
oauth.register(
|
| 731 |
+
name="google",
|
| 732 |
+
|
| 733 |
+
server_metadata_url=(
|
| 734 |
+
"https://accounts.google.com/"
|
| 735 |
+
".well-known/"
|
| 736 |
+
"openid-configuration"
|
| 737 |
+
),
|
| 738 |
+
|
| 739 |
+
client_kwargs={
|
| 740 |
+
"scope": (
|
| 741 |
+
"openid "
|
| 742 |
+
"email "
|
| 743 |
+
"profile"
|
| 744 |
+
)
|
| 745 |
+
},
|
| 746 |
+
)
|
| 747 |
|
|
|
|
|
|
|
|
|
|
| 748 |
|
| 749 |
+
def google_redirect_uri(
|
| 750 |
+
request: Request,
|
| 751 |
+
):
|
| 752 |
+
|
| 753 |
+
if GOOGLE_REDIRECT_URI:
|
| 754 |
+
return GOOGLE_REDIRECT_URI
|
| 755 |
|
| 756 |
# --------------------------------------------------------
|
| 757 |
+
# Build public URL behind a proxy.
|
|
|
|
|
|
|
|
|
|
| 758 |
# --------------------------------------------------------
|
| 759 |
|
| 760 |
+
headers = request.headers
|
| 761 |
|
| 762 |
+
forwarded_proto = headers.get(
|
| 763 |
+
"x-forwarded-proto"
|
| 764 |
+
)
|
| 765 |
|
| 766 |
+
forwarded_host = headers.get(
|
| 767 |
+
"x-forwarded-host"
|
| 768 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 769 |
|
| 770 |
+
scheme = (
|
| 771 |
+
forwarded_proto
|
| 772 |
+
or request.url.scheme
|
| 773 |
+
)
|
| 774 |
|
| 775 |
+
host = (
|
| 776 |
+
forwarded_host
|
| 777 |
+
or headers.get("host")
|
| 778 |
+
)
|
| 779 |
|
| 780 |
+
if host:
|
|
|
|
|
|
|
| 781 |
|
| 782 |
+
return (
|
| 783 |
+
f"{scheme}://{host}"
|
| 784 |
+
"/auth/google/callback"
|
| 785 |
+
)
|
| 786 |
|
| 787 |
+
return str(
|
| 788 |
+
request.url_for(
|
| 789 |
+
"google_callback"
|
| 790 |
+
)
|
| 791 |
+
)
|
| 792 |
|
| 793 |
|
| 794 |
# ============================================================
|
| 795 |
+
# FASTAPI APP
|
| 796 |
# ============================================================
|
| 797 |
|
| 798 |
+
app = FastAPI()
|
| 799 |
+
|
| 800 |
+
app.add_middleware(
|
| 801 |
+
SessionMiddleware,
|
| 802 |
+
secret_key=PFC_SECRET_KEY,
|
| 803 |
+
)
|
| 804 |
+
|
| 805 |
+
|
| 806 |
+
@app.get(
|
| 807 |
+
"/auth/google",
|
| 808 |
+
name="google_login",
|
| 809 |
+
)
|
| 810 |
+
async def google_login(
|
| 811 |
+
request: Request,
|
| 812 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 813 |
|
| 814 |
+
if not GOOGLE_ENABLED:
|
| 815 |
+
|
| 816 |
+
return HTMLResponse(
|
| 817 |
+
"""
|
| 818 |
+
<html>
|
| 819 |
+
<body style="
|
| 820 |
+
font-family: Arial;
|
| 821 |
+
padding: 40px;
|
| 822 |
+
text-align: center;
|
| 823 |
+
">
|
| 824 |
+
<h2>Google sign-in is not configured yet.</h2>
|
| 825 |
+
|
| 826 |
+
<p>
|
| 827 |
+
Please return to PoultryFeedCalc and
|
| 828 |
+
enter your email address manually.
|
| 829 |
+
</p>
|
| 830 |
+
</body>
|
| 831 |
+
</html>
|
| 832 |
+
""",
|
| 833 |
+
status_code=503,
|
| 834 |
+
)
|
| 835 |
+
|
| 836 |
+
google = oauth.create_client(
|
| 837 |
+
"google"
|
| 838 |
+
)
|
| 839 |
+
|
| 840 |
+
redirect_uri = google_redirect_uri(
|
| 841 |
+
request
|
| 842 |
+
)
|
| 843 |
+
|
| 844 |
+
return await google.authorize_redirect(
|
| 845 |
+
request,
|
| 846 |
+
redirect_uri,
|
| 847 |
+
)
|
| 848 |
+
|
| 849 |
+
|
| 850 |
+
@app.get(
|
| 851 |
+
"/auth/google/callback",
|
| 852 |
+
name="google_callback",
|
| 853 |
+
)
|
| 854 |
+
async def google_callback(
|
| 855 |
+
request: Request,
|
| 856 |
+
):
|
| 857 |
+
|
| 858 |
+
if not GOOGLE_ENABLED:
|
| 859 |
+
|
| 860 |
+
return RedirectResponse(
|
| 861 |
+
url="/"
|
| 862 |
+
)
|
| 863 |
+
|
| 864 |
+
try:
|
| 865 |
+
|
| 866 |
+
google = oauth.create_client(
|
| 867 |
+
"google"
|
| 868 |
+
)
|
| 869 |
+
|
| 870 |
+
token = await (
|
| 871 |
+
google.authorize_access_token(
|
| 872 |
+
request
|
| 873 |
+
)
|
| 874 |
+
)
|
| 875 |
+
|
| 876 |
+
user = token.get(
|
| 877 |
+
"userinfo"
|
| 878 |
+
)
|
| 879 |
+
|
| 880 |
+
if not user:
|
| 881 |
+
|
| 882 |
+
try:
|
| 883 |
+
|
| 884 |
+
user = await google.userinfo(
|
| 885 |
+
token=token
|
| 886 |
+
)
|
| 887 |
+
|
| 888 |
+
except Exception:
|
| 889 |
+
user = {}
|
| 890 |
+
|
| 891 |
+
email = clean_email(
|
| 892 |
+
user.get("email", "")
|
| 893 |
+
)
|
| 894 |
+
|
| 895 |
+
if not valid_email(email):
|
| 896 |
+
|
| 897 |
+
return HTMLResponse(
|
| 898 |
+
"""
|
| 899 |
+
<html>
|
| 900 |
+
<body style="
|
| 901 |
+
font-family: Arial;
|
| 902 |
+
padding: 40px;
|
| 903 |
+
text-align: center;
|
| 904 |
+
">
|
| 905 |
+
<h2>
|
| 906 |
+
We could not retrieve your
|
| 907 |
+
Google email.
|
| 908 |
+
</h2>
|
| 909 |
+
|
| 910 |
+
<p>
|
| 911 |
+
Please return to PoultryFeedCalc
|
| 912 |
+
and enter your email manually.
|
| 913 |
+
</p>
|
| 914 |
+
</body>
|
| 915 |
+
</html>
|
| 916 |
+
""",
|
| 917 |
+
status_code=400,
|
| 918 |
+
)
|
| 919 |
+
|
| 920 |
+
# Save Google user immediately.
|
| 921 |
+
save_subscriber(
|
| 922 |
+
email=email,
|
| 923 |
+
country="",
|
| 924 |
+
currency="",
|
| 925 |
+
source="google",
|
| 926 |
+
)
|
| 927 |
+
|
| 928 |
+
# Sign email before storing it in the browser.
|
| 929 |
+
signed_email = EMAIL_SIGNER.dumps(
|
| 930 |
+
email
|
| 931 |
+
)
|
| 932 |
+
|
| 933 |
+
response = RedirectResponse(
|
| 934 |
+
url="/"
|
| 935 |
+
)
|
| 936 |
+
|
| 937 |
+
secure_cookie = (
|
| 938 |
+
request.url.scheme == "https"
|
| 939 |
+
)
|
| 940 |
+
|
| 941 |
+
response.set_cookie(
|
| 942 |
+
key="pfc_google_email",
|
| 943 |
+
value=signed_email,
|
| 944 |
+
max_age=60 * 60 * 24 * 30,
|
| 945 |
+
httponly=True,
|
| 946 |
+
secure=secure_cookie,
|
| 947 |
+
samesite="lax",
|
| 948 |
+
)
|
| 949 |
+
|
| 950 |
+
return response
|
| 951 |
+
|
| 952 |
+
except OAuthError:
|
| 953 |
+
|
| 954 |
+
return HTMLResponse(
|
| 955 |
+
"""
|
| 956 |
+
<html>
|
| 957 |
+
<body style="
|
| 958 |
+
font-family: Arial;
|
| 959 |
+
padding: 40px;
|
| 960 |
+
text-align: center;
|
| 961 |
+
">
|
| 962 |
+
<h2>Google sign-in was cancelled.</h2>
|
| 963 |
+
|
| 964 |
+
<p>
|
| 965 |
+
You can return to PoultryFeedCalc
|
| 966 |
+
and enter your email manually.
|
| 967 |
+
</p>
|
| 968 |
+
</body>
|
| 969 |
+
</html>
|
| 970 |
+
""",
|
| 971 |
+
status_code=400,
|
| 972 |
+
)
|
| 973 |
+
|
| 974 |
+
except Exception as exc:
|
| 975 |
+
|
| 976 |
+
print(
|
| 977 |
+
"Google OAuth error:",
|
| 978 |
+
repr(exc),
|
| 979 |
+
)
|
| 980 |
+
|
| 981 |
+
return HTMLResponse(
|
| 982 |
+
"""
|
| 983 |
+
<html>
|
| 984 |
+
<body style="
|
| 985 |
+
font-family: Arial;
|
| 986 |
+
padding: 40px;
|
| 987 |
+
text-align: center;
|
| 988 |
+
">
|
| 989 |
+
<h2>
|
| 990 |
+
Google sign-in could not be completed.
|
| 991 |
+
</h2>
|
| 992 |
+
|
| 993 |
+
<p>
|
| 994 |
+
Please return to PoultryFeedCalc
|
| 995 |
+
and enter your email manually.
|
| 996 |
+
</p>
|
| 997 |
+
</body>
|
| 998 |
+
</html>
|
| 999 |
+
""",
|
| 1000 |
+
status_code=500,
|
| 1001 |
+
)
|
| 1002 |
+
|
| 1003 |
+
|
| 1004 |
+
# ============================================================
|
| 1005 |
+
# GET GOOGLE EMAIL FROM COOKIE
|
| 1006 |
+
# ============================================================
|
| 1007 |
+
|
| 1008 |
+
def get_google_email(
|
| 1009 |
+
request: Optional[gr.Request] = None,
|
| 1010 |
+
):
|
| 1011 |
+
|
| 1012 |
+
if not request:
|
| 1013 |
+
return gr.skip()
|
| 1014 |
+
|
| 1015 |
+
try:
|
| 1016 |
+
|
| 1017 |
+
cookies = dict(
|
| 1018 |
+
request.cookies
|
| 1019 |
+
)
|
| 1020 |
+
|
| 1021 |
+
signed_email = cookies.get(
|
| 1022 |
+
"pfc_google_email"
|
| 1023 |
+
)
|
| 1024 |
+
|
| 1025 |
+
if not signed_email:
|
| 1026 |
+
return gr.skip()
|
| 1027 |
+
|
| 1028 |
+
email = EMAIL_SIGNER.loads(
|
| 1029 |
+
signed_email,
|
| 1030 |
+
max_age=60 * 60 * 24 * 30,
|
| 1031 |
+
)
|
| 1032 |
+
|
| 1033 |
+
if valid_email(email):
|
| 1034 |
+
|
| 1035 |
+
return email
|
| 1036 |
+
|
| 1037 |
+
except (
|
| 1038 |
+
BadSignature,
|
| 1039 |
+
SignatureExpired,
|
| 1040 |
+
Exception,
|
| 1041 |
+
):
|
| 1042 |
+
pass
|
| 1043 |
+
|
| 1044 |
+
return gr.skip()
|
| 1045 |
+
|
| 1046 |
+
|
| 1047 |
+
# ============================================================
|
| 1048 |
+
# MAIN CALCULATOR
|
| 1049 |
+
# ============================================================
|
| 1050 |
+
|
| 1051 |
+
def calculate_feed(
|
| 1052 |
+
birds,
|
| 1053 |
+
weeks,
|
| 1054 |
+
days,
|
| 1055 |
+
bag_size,
|
| 1056 |
+
price,
|
| 1057 |
+
email,
|
| 1058 |
+
country,
|
| 1059 |
+
currency,
|
| 1060 |
+
symbol,
|
| 1061 |
+
):
|
| 1062 |
+
|
| 1063 |
+
email = clean_email(email)
|
| 1064 |
+
|
| 1065 |
+
# --------------------------------------------------------
|
| 1066 |
+
# EMAIL REQUIRED BEFORE CALCULATION
|
| 1067 |
+
# --------------------------------------------------------
|
| 1068 |
+
|
| 1069 |
+
if not email:
|
| 1070 |
+
|
| 1071 |
+
return (
|
| 1072 |
+
"""
|
| 1073 |
+
# 📧 Email required
|
| 1074 |
+
|
| 1075 |
+
Please enter your email address **before calculating**.
|
| 1076 |
+
|
| 1077 |
+
You can also use **Sign in with Google** above to
|
| 1078 |
+
automatically provide your email.
|
| 1079 |
+
""",
|
| 1080 |
+
"⚠️ Please enter your email before calculating.",
|
| 1081 |
+
)
|
| 1082 |
+
|
| 1083 |
+
if not valid_email(email):
|
| 1084 |
+
|
| 1085 |
+
return (
|
| 1086 |
+
"""
|
| 1087 |
+
# ⚠️ Invalid email
|
| 1088 |
+
|
| 1089 |
+
Please enter a valid email address.
|
| 1090 |
+
|
| 1091 |
+
Example:
|
| 1092 |
+
|
| 1093 |
+
**farmer@example.com**
|
| 1094 |
+
""",
|
| 1095 |
+
"⚠️ Please enter a valid email address.",
|
| 1096 |
+
)
|
| 1097 |
+
|
| 1098 |
+
# --------------------------------------------------------
|
| 1099 |
+
# Convert inputs
|
| 1100 |
+
# --------------------------------------------------------
|
| 1101 |
+
|
| 1102 |
+
birds = safe_int(birds)
|
| 1103 |
+
weeks = safe_int(weeks)
|
| 1104 |
+
days = safe_int(days)
|
| 1105 |
+
bag_size = safe_float(bag_size)
|
| 1106 |
+
price = safe_float(price)
|
| 1107 |
+
|
| 1108 |
+
# --------------------------------------------------------
|
| 1109 |
+
# Validate
|
| 1110 |
+
# --------------------------------------------------------
|
| 1111 |
+
|
| 1112 |
+
if birds <= 0:
|
| 1113 |
+
|
| 1114 |
+
return (
|
| 1115 |
+
"### ⚠️ Please enter a valid number of birds.",
|
| 1116 |
+
"Number of birds must be greater than zero.",
|
| 1117 |
+
)
|
| 1118 |
+
|
| 1119 |
+
if weeks < 0 or days < 0:
|
| 1120 |
+
|
| 1121 |
+
return (
|
| 1122 |
+
"### ⚠️ Please check the bird age.",
|
| 1123 |
+
"Age cannot be negative.",
|
| 1124 |
+
)
|
| 1125 |
+
|
| 1126 |
+
if days > 6:
|
| 1127 |
+
|
| 1128 |
+
return (
|
| 1129 |
+
"### ⚠️ Days must be between 0 and 6.",
|
| 1130 |
+
"Please select a valid number of days.",
|
| 1131 |
+
)
|
| 1132 |
+
|
| 1133 |
+
if bag_size <= 0:
|
| 1134 |
+
|
| 1135 |
+
return (
|
| 1136 |
+
"### ⚠️ Please select a valid feed bag size.",
|
| 1137 |
+
"Feed bag size must be greater than zero.",
|
| 1138 |
+
)
|
| 1139 |
+
|
| 1140 |
+
if price <= 0:
|
| 1141 |
+
|
| 1142 |
+
return (
|
| 1143 |
+
"### ⚠️ Please enter the local feed price.",
|
| 1144 |
+
"Price per bag must be greater than zero.",
|
| 1145 |
+
)
|
| 1146 |
+
|
| 1147 |
+
# --------------------------------------------------------
|
| 1148 |
+
# Total days
|
| 1149 |
+
# --------------------------------------------------------
|
| 1150 |
+
|
| 1151 |
+
total_days = (
|
| 1152 |
+
(weeks * 7)
|
| 1153 |
+
+ days
|
| 1154 |
+
)
|
| 1155 |
+
|
| 1156 |
+
if total_days <= 0:
|
| 1157 |
+
|
| 1158 |
+
return (
|
| 1159 |
+
"### ⚠️ Bird age must be greater than zero.",
|
| 1160 |
+
"Please select an age.",
|
| 1161 |
+
)
|
| 1162 |
+
|
| 1163 |
+
# --------------------------------------------------------
|
| 1164 |
+
# Feed calculation
|
| 1165 |
+
# --------------------------------------------------------
|
| 1166 |
+
|
| 1167 |
+
calc = calculate_feed_numbers(
|
| 1168 |
+
birds=birds,
|
| 1169 |
+
total_days=total_days,
|
| 1170 |
+
bag_size=bag_size,
|
| 1171 |
+
)
|
| 1172 |
+
|
| 1173 |
+
# --------------------------------------------------------
|
| 1174 |
+
# Extract
|
| 1175 |
+
# --------------------------------------------------------
|
| 1176 |
+
|
| 1177 |
+
benchmark_total_kg = calc[
|
| 1178 |
+
"benchmark_total_kg"
|
| 1179 |
+
]
|
| 1180 |
+
|
| 1181 |
+
purchase_feed_kg = calc[
|
| 1182 |
+
"purchase_feed_kg"
|
| 1183 |
+
]
|
| 1184 |
+
|
| 1185 |
+
exact_bags = calc[
|
| 1186 |
+
"exact_bags"
|
| 1187 |
+
]
|
| 1188 |
+
|
| 1189 |
+
whole_bags = calc[
|
| 1190 |
+
"whole_bags"
|
| 1191 |
+
]
|
| 1192 |
+
|
| 1193 |
+
starter_kg = calc[
|
| 1194 |
+
"starter_kg"
|
| 1195 |
+
]
|
| 1196 |
+
|
| 1197 |
+
finisher_kg = calc[
|
| 1198 |
+
"finisher_kg"
|
| 1199 |
+
]
|
| 1200 |
+
|
| 1201 |
+
starter_bags = calc[
|
| 1202 |
+
"starter_bags"
|
| 1203 |
+
]
|
| 1204 |
+
|
| 1205 |
+
finisher_bags = calc[
|
| 1206 |
+
"finisher_bags"
|
| 1207 |
+
]
|
| 1208 |
+
|
| 1209 |
+
# --------------------------------------------------------
|
| 1210 |
+
# Cost
|
| 1211 |
+
# --------------------------------------------------------
|
| 1212 |
+
|
| 1213 |
+
total_cost = (
|
| 1214 |
+
whole_bags * price
|
| 1215 |
+
)
|
| 1216 |
+
|
| 1217 |
+
# --------------------------------------------------------
|
| 1218 |
+
# Save subscriber
|
| 1219 |
+
# --------------------------------------------------------
|
| 1220 |
+
|
| 1221 |
+
save_subscriber(
|
| 1222 |
+
email=email,
|
| 1223 |
+
country=country,
|
| 1224 |
+
currency=currency,
|
| 1225 |
+
source="calculator",
|
| 1226 |
+
)
|
| 1227 |
+
|
| 1228 |
+
# --------------------------------------------------------
|
| 1229 |
+
# Save calculation
|
| 1230 |
+
# --------------------------------------------------------
|
| 1231 |
+
|
| 1232 |
+
timestamp = now_iso()
|
| 1233 |
+
|
| 1234 |
+
with DB_LOCK:
|
| 1235 |
+
|
| 1236 |
+
conn = get_db()
|
| 1237 |
+
|
| 1238 |
+
conn.execute(
|
| 1239 |
+
"""
|
| 1240 |
+
INSERT INTO calculations (
|
| 1241 |
+
email,
|
| 1242 |
+
country,
|
| 1243 |
+
currency,
|
| 1244 |
+
birds,
|
| 1245 |
+
weeks,
|
| 1246 |
+
days,
|
| 1247 |
+
total_days,
|
| 1248 |
+
bag_size,
|
| 1249 |
+
price_per_bag,
|
| 1250 |
+
benchmark_feed_kg,
|
| 1251 |
+
purchase_feed_kg,
|
| 1252 |
+
exact_bags,
|
| 1253 |
+
whole_bags,
|
| 1254 |
+
starter_kg,
|
| 1255 |
+
finisher_kg,
|
| 1256 |
+
starter_bags,
|
| 1257 |
+
finisher_bags,
|
| 1258 |
+
created_at
|
| 1259 |
+
)
|
| 1260 |
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
| 1261 |
+
""",
|
| 1262 |
+
(
|
| 1263 |
+
email,
|
| 1264 |
+
country,
|
| 1265 |
+
currency,
|
| 1266 |
+
birds,
|
| 1267 |
+
weeks,
|
| 1268 |
+
days,
|
| 1269 |
+
total_days,
|
| 1270 |
+
bag_size,
|
| 1271 |
+
price,
|
| 1272 |
+
benchmark_total_kg,
|
| 1273 |
+
purchase_feed_kg,
|
| 1274 |
+
exact_bags,
|
| 1275 |
+
whole_bags,
|
| 1276 |
+
starter_kg,
|
| 1277 |
+
finisher_kg,
|
| 1278 |
+
starter_bags,
|
| 1279 |
+
finisher_bags,
|
| 1280 |
+
timestamp,
|
| 1281 |
+
),
|
| 1282 |
+
)
|
| 1283 |
+
|
| 1284 |
+
conn.commit()
|
| 1285 |
+
conn.close()
|
| 1286 |
+
|
| 1287 |
+
# --------------------------------------------------------
|
| 1288 |
+
# Farmer-friendly wording
|
| 1289 |
+
# --------------------------------------------------------
|
| 1290 |
+
|
| 1291 |
+
stage_summary = (
|
| 1292 |
+
f"{starter_bags} Starter + "
|
| 1293 |
+
f"{finisher_bags} Finisher"
|
| 1294 |
+
)
|
| 1295 |
+
|
| 1296 |
+
answer = f"""
|
| 1297 |
+
# 🐔 YOUR FEED REQUIREMENT
|
| 1298 |
+
|
| 1299 |
+
<div class="big-answer">
|
| 1300 |
+
|
| 1301 |
+
You will require approximately
|
| 1302 |
+
|
| 1303 |
+
# **{whole_bags} bags of {format_number(bag_size, 0)} kg feed**
|
| 1304 |
+
|
| 1305 |
+
</div>
|
| 1306 |
+
|
| 1307 |
+
### To raise **{birds:,} birds** to **{weeks} weeks + {days} days ({total_days} days)**.
|
| 1308 |
+
|
| 1309 |
+
---
|
| 1310 |
+
|
| 1311 |
+
# 🌾 Feed breakdown
|
| 1312 |
+
|
| 1313 |
+
| Feed | Estimated quantity | Bags to buy |
|
| 1314 |
+
|---|---:|---:|
|
| 1315 |
+
| 🌱 **Starter** | **{starter_kg:,.1f} kg** | **{starter_bags} bags** |
|
| 1316 |
+
| 🌽 **Finisher** | **{finisher_kg:,.1f} kg** | **{finisher_bags} bags** |
|
| 1317 |
+
| **TOTAL PURCHASE** | **{purchase_feed_kg:,.1f} kg allowance** | **{whole_bags} bags** |
|
| 1318 |
+
|
| 1319 |
+
---
|
| 1320 |
+
|
| 1321 |
+
# 💰 Estimated feed cost
|
| 1322 |
+
|
| 1323 |
+
<div class="cost-answer">
|
| 1324 |
+
|
| 1325 |
+
# **{format_money(total_cost, symbol)}**
|
| 1326 |
+
|
| 1327 |
+
</div>
|
| 1328 |
+
|
| 1329 |
+
Based on approximately **{format_money(price, symbol)} per {format_number(bag_size, 0)}-kg bag**.
|
| 1330 |
+
|
| 1331 |
+
---
|
| 1332 |
+
|
| 1333 |
+
# 🎯 QUICK ANSWER
|
| 1334 |
+
|
| 1335 |
+
<div class="quick-answer">
|
| 1336 |
+
|
| 1337 |
+
**To raise {birds:,} birds for {total_days} days ({weeks} weeks + {days} days), you will need an estimate → {whole_bags} bags of feed ({stage_summary}), costing approximately {format_money(total_cost, symbol)} on feeds alone.**
|
| 1338 |
+
|
| 1339 |
+
</div>
|
| 1340 |
+
|
| 1341 |
+
---
|
| 1342 |
+
|
| 1343 |
+
## 📊 How we calculated it
|
| 1344 |
+
|
| 1345 |
+
**Benchmark feed requirement:** {benchmark_total_kg:,.1f} kg
|
| 1346 |
+
|
| 1347 |
+
**Practical purchase allowance:** {PURCHASE_ALLOWANCE * 100:.0f}%
|
| 1348 |
+
|
| 1349 |
+
**Recommended purchase quantity:** {purchase_feed_kg:,.1f} kg
|
| 1350 |
+
|
| 1351 |
+
**Exact bag estimate:** {exact_bags:.2f} bags
|
| 1352 |
+
|
| 1353 |
+
**Whole bags to buy:** **{whole_bags} bags**
|
| 1354 |
+
|
| 1355 |
+
The final number is always rounded **up** so you do not run short.
|
| 1356 |
+
|
| 1357 |
+
---
|
| 1358 |
+
|
| 1359 |
+
### ⚠️ Important
|
| 1360 |
+
|
| 1361 |
+
This is an estimate based on the app's broiler feed benchmark.
|
| 1362 |
+
|
| 1363 |
+
Actual consumption can vary because of genetics, sex, mortality,
|
| 1364 |
+
feed quality, temperature, management and other farm conditions.
|
| 1365 |
+
"""
|
| 1366 |
+
|
| 1367 |
+
status = (
|
| 1368 |
+
f"✅ Calculation complete. "
|
| 1369 |
+
f"Your email **{email}** has been saved for "
|
| 1370 |
+
f"PoultryFeedCalc updates."
|
| 1371 |
+
)
|
| 1372 |
+
|
| 1373 |
+
return (
|
| 1374 |
+
answer,
|
| 1375 |
+
status,
|
| 1376 |
+
)
|
| 1377 |
+
|
| 1378 |
+
|
| 1379 |
+
# ============================================================
|
| 1380 |
+
# QUESTION PARSER
|
| 1381 |
+
# ============================================================
|
| 1382 |
+
|
| 1383 |
+
def extract_birds(question):
|
| 1384 |
+
|
| 1385 |
+
patterns = [
|
| 1386 |
+
|
| 1387 |
+
r"(\d[\d,]*)\s*"
|
| 1388 |
+
r"(?:birds?|broilers?|chickens?)",
|
| 1389 |
+
|
| 1390 |
+
r"(?:for|raise|raising)\s+"
|
| 1391 |
+
r"(\d[\d,]*)\s*"
|
| 1392 |
+
r"(?:birds?|broilers?|chickens?)",
|
| 1393 |
+
]
|
| 1394 |
+
|
| 1395 |
+
for pattern in patterns:
|
| 1396 |
+
|
| 1397 |
+
match = re.search(
|
| 1398 |
+
pattern,
|
| 1399 |
+
question,
|
| 1400 |
+
flags=re.IGNORECASE,
|
| 1401 |
+
)
|
| 1402 |
+
|
| 1403 |
+
if match:
|
| 1404 |
+
|
| 1405 |
+
return safe_int(
|
| 1406 |
+
match.group(1)
|
| 1407 |
+
.replace(",", "")
|
| 1408 |
+
)
|
| 1409 |
+
|
| 1410 |
+
return None
|
| 1411 |
+
|
| 1412 |
+
|
| 1413 |
+
def extract_weeks(question):
|
| 1414 |
+
|
| 1415 |
+
match = re.search(
|
| 1416 |
+
r"(\d+)\s*"
|
| 1417 |
+
r"(?:weeks?|wks?)",
|
| 1418 |
+
question,
|
| 1419 |
+
flags=re.IGNORECASE,
|
| 1420 |
+
)
|
| 1421 |
+
|
| 1422 |
+
if match:
|
| 1423 |
+
return safe_int(
|
| 1424 |
+
match.group(1)
|
| 1425 |
+
)
|
| 1426 |
+
|
| 1427 |
+
return None
|
| 1428 |
+
|
| 1429 |
+
|
| 1430 |
+
def extract_days(question):
|
| 1431 |
+
|
| 1432 |
+
match = re.search(
|
| 1433 |
+
r"(\d+)\s*"
|
| 1434 |
+
r"(?:days?|dys?)",
|
| 1435 |
+
question,
|
| 1436 |
+
flags=re.IGNORECASE,
|
| 1437 |
+
)
|
| 1438 |
+
|
| 1439 |
+
if match:
|
| 1440 |
+
return safe_int(
|
| 1441 |
+
match.group(1)
|
| 1442 |
+
)
|
| 1443 |
+
|
| 1444 |
+
return None
|
| 1445 |
+
|
| 1446 |
+
|
| 1447 |
+
def extract_bag_size(question):
|
| 1448 |
+
|
| 1449 |
+
match = re.search(
|
| 1450 |
+
r"(\d+(?:\.\d+)?)\s*"
|
| 1451 |
+
r"(?:kg|kilograms?)"
|
| 1452 |
+
r"(?:\s*bag)?",
|
| 1453 |
+
question,
|
| 1454 |
+
flags=re.IGNORECASE,
|
| 1455 |
+
)
|
| 1456 |
+
|
| 1457 |
+
if match:
|
| 1458 |
+
|
| 1459 |
+
return safe_float(
|
| 1460 |
+
match.group(1)
|
| 1461 |
+
)
|
| 1462 |
+
|
| 1463 |
+
return None
|
| 1464 |
+
|
| 1465 |
+
|
| 1466 |
+
def extract_number_of_bags(question):
|
| 1467 |
+
|
| 1468 |
+
match = re.search(
|
| 1469 |
+
r"(\d+(?:\.\d+)?)\s*"
|
| 1470 |
+
r"bags?",
|
| 1471 |
+
question,
|
| 1472 |
+
flags=re.IGNORECASE,
|
| 1473 |
+
)
|
| 1474 |
+
|
| 1475 |
+
if match:
|
| 1476 |
+
|
| 1477 |
+
return safe_float(
|
| 1478 |
+
match.group(1)
|
| 1479 |
+
)
|
| 1480 |
+
|
| 1481 |
+
return None
|
| 1482 |
+
|
| 1483 |
+
|
| 1484 |
+
# ============================================================
|
| 1485 |
+
# QUESTION ANSWER ENGINE
|
| 1486 |
+
# ============================================================
|
| 1487 |
+
|
| 1488 |
+
def answer_question(
|
| 1489 |
+
question,
|
| 1490 |
+
email,
|
| 1491 |
+
country,
|
| 1492 |
+
currency,
|
| 1493 |
+
symbol,
|
| 1494 |
+
selected_birds,
|
| 1495 |
+
selected_weeks,
|
| 1496 |
+
selected_days,
|
| 1497 |
+
selected_bag_size,
|
| 1498 |
+
selected_price,
|
| 1499 |
+
):
|
| 1500 |
+
|
| 1501 |
+
question = (
|
| 1502 |
+
question or ""
|
| 1503 |
+
).strip()
|
| 1504 |
+
|
| 1505 |
+
email = clean_email(
|
| 1506 |
+
email
|
| 1507 |
+
)
|
| 1508 |
+
|
| 1509 |
+
# --------------------------------------------------------
|
| 1510 |
+
# Question must have email so it can be saved.
|
| 1511 |
+
# --------------------------------------------------------
|
| 1512 |
+
|
| 1513 |
+
if not email:
|
| 1514 |
+
|
| 1515 |
+
return (
|
| 1516 |
+
"""
|
| 1517 |
+
# 📧 Email required
|
| 1518 |
+
|
| 1519 |
+
Please enter your email above first.
|
| 1520 |
+
|
| 1521 |
+
Your question will then be saved automatically
|
| 1522 |
+
when you click **Get Answer**.
|
| 1523 |
+
""",
|
| 1524 |
+
"⚠️ Email required.",
|
| 1525 |
+
)
|
| 1526 |
+
|
| 1527 |
+
if not valid_email(email):
|
| 1528 |
+
|
| 1529 |
+
return (
|
| 1530 |
+
"### ⚠️ Please enter a valid email address.",
|
| 1531 |
+
"Invalid email.",
|
| 1532 |
+
)
|
| 1533 |
+
|
| 1534 |
+
if not question:
|
| 1535 |
+
|
| 1536 |
+
return (
|
| 1537 |
+
"""
|
| 1538 |
+
# ❓ Please enter your question
|
| 1539 |
+
|
| 1540 |
+
Example:
|
| 1541 |
+
|
| 1542 |
+
**How many bags of feed do I need for 50 birds for 6 weeks?**
|
| 1543 |
+
""",
|
| 1544 |
+
"No question entered.",
|
| 1545 |
+
)
|
| 1546 |
+
|
| 1547 |
+
# --------------------------------------------------------
|
| 1548 |
+
# SAVE QUESTION FIRST
|
| 1549 |
+
#
|
| 1550 |
+
# This is deliberate.
|
| 1551 |
+
#
|
| 1552 |
+
# Clicking Get Answer automatically saves the question.
|
| 1553 |
+
# There is NO separate Save Question button.
|
| 1554 |
+
# --------------------------------------------------------
|
| 1555 |
+
|
| 1556 |
+
timestamp = now_iso()
|
| 1557 |
+
|
| 1558 |
+
with DB_LOCK:
|
| 1559 |
+
|
| 1560 |
+
conn = get_db()
|
| 1561 |
+
|
| 1562 |
+
conn.execute(
|
| 1563 |
+
"""
|
| 1564 |
+
INSERT INTO farmer_questions (
|
| 1565 |
+
email,
|
| 1566 |
+
question,
|
| 1567 |
+
country,
|
| 1568 |
+
created_at
|
| 1569 |
+
)
|
| 1570 |
+
VALUES (?, ?, ?, ?)
|
| 1571 |
+
""",
|
| 1572 |
+
(
|
| 1573 |
+
email,
|
| 1574 |
+
question,
|
| 1575 |
+
country,
|
| 1576 |
+
timestamp,
|
| 1577 |
+
),
|
| 1578 |
+
)
|
| 1579 |
+
|
| 1580 |
+
conn.commit()
|
| 1581 |
+
conn.close()
|
| 1582 |
+
|
| 1583 |
+
# Also ensure subscriber exists.
|
| 1584 |
+
save_subscriber(
|
| 1585 |
+
email=email,
|
| 1586 |
+
country=country,
|
| 1587 |
+
currency=currency,
|
| 1588 |
+
source="question",
|
| 1589 |
+
)
|
| 1590 |
+
|
| 1591 |
+
# --------------------------------------------------------
|
| 1592 |
+
# Parse question
|
| 1593 |
+
# --------------------------------------------------------
|
| 1594 |
+
|
| 1595 |
+
q = question.lower()
|
| 1596 |
+
|
| 1597 |
+
birds = extract_birds(q)
|
| 1598 |
+
|
| 1599 |
+
weeks = extract_weeks(q)
|
| 1600 |
+
|
| 1601 |
+
extra_days = extract_days(q)
|
| 1602 |
+
|
| 1603 |
+
bag_size = extract_bag_size(q)
|
| 1604 |
+
|
| 1605 |
+
asked_bags = extract_number_of_bags(q)
|
| 1606 |
+
|
| 1607 |
+
# --------------------------------------------------------
|
| 1608 |
+
# If question did not contain an age,
|
| 1609 |
+
# use the calculator's selected age.
|
| 1610 |
+
# --------------------------------------------------------
|
| 1611 |
+
|
| 1612 |
+
if weeks is None:
|
| 1613 |
+
|
| 1614 |
+
weeks = safe_int(
|
| 1615 |
+
selected_weeks
|
| 1616 |
+
)
|
| 1617 |
+
|
| 1618 |
+
if extra_days is None:
|
| 1619 |
+
|
| 1620 |
+
extra_days = safe_int(
|
| 1621 |
+
selected_days
|
| 1622 |
+
)
|
| 1623 |
+
|
| 1624 |
+
# --------------------------------------------------------
|
| 1625 |
+
# If question did not contain bird count,
|
| 1626 |
+
# use selected calculator bird count.
|
| 1627 |
+
# --------------------------------------------------------
|
| 1628 |
+
|
| 1629 |
+
if birds is None:
|
| 1630 |
+
|
| 1631 |
+
birds = safe_int(
|
| 1632 |
+
selected_birds
|
| 1633 |
+
)
|
| 1634 |
+
|
| 1635 |
+
# --------------------------------------------------------
|
| 1636 |
+
# If question did not specify bag size,
|
| 1637 |
+
# use selected calculator bag size.
|
| 1638 |
+
# --------------------------------------------------------
|
| 1639 |
+
|
| 1640 |
+
if bag_size is None:
|
| 1641 |
+
|
| 1642 |
+
bag_size = safe_float(
|
| 1643 |
+
selected_bag_size
|
| 1644 |
+
)
|
| 1645 |
+
|
| 1646 |
+
price = safe_float(
|
| 1647 |
+
selected_price
|
| 1648 |
+
)
|
| 1649 |
+
|
| 1650 |
+
# --------------------------------------------------------
|
| 1651 |
+
# FEED QUESTIONS
|
| 1652 |
+
# --------------------------------------------------------
|
| 1653 |
+
|
| 1654 |
+
is_feed_question = any(
|
| 1655 |
+
keyword in q
|
| 1656 |
+
for keyword in [
|
| 1657 |
+
"feed",
|
| 1658 |
+
"bag",
|
| 1659 |
+
"bags",
|
| 1660 |
+
"broiler",
|
| 1661 |
+
"broilers",
|
| 1662 |
+
]
|
| 1663 |
+
)
|
| 1664 |
+
|
| 1665 |
+
if is_feed_question:
|
| 1666 |
+
|
| 1667 |
+
if birds <= 0:
|
| 1668 |
+
|
| 1669 |
+
return (
|
| 1670 |
+
"""
|
| 1671 |
+
# 🐔 I need the number of birds
|
| 1672 |
+
|
| 1673 |
+
Please tell me something like:
|
| 1674 |
+
|
| 1675 |
+
**How many bags for 100 birds for 6 weeks?**
|
| 1676 |
+
""",
|
| 1677 |
+
"Question saved.",
|
| 1678 |
+
)
|
| 1679 |
+
|
| 1680 |
+
if weeks < 0 or extra_days < 0:
|
| 1681 |
+
|
| 1682 |
+
return (
|
| 1683 |
+
"### ⚠️ Please provide a valid bird age.",
|
| 1684 |
+
"Question saved.",
|
| 1685 |
+
)
|
| 1686 |
+
|
| 1687 |
+
total_days = (
|
| 1688 |
+
(weeks * 7)
|
| 1689 |
+
+ extra_days
|
| 1690 |
+
)
|
| 1691 |
+
|
| 1692 |
+
if total_days <= 0:
|
| 1693 |
+
|
| 1694 |
+
return (
|
| 1695 |
+
"""
|
| 1696 |
+
# 📅 I need the bird age
|
| 1697 |
+
|
| 1698 |
+
Please tell me the age, for example:
|
| 1699 |
+
|
| 1700 |
+
**How many bags for 100 birds for 6 weeks?**
|
| 1701 |
+
""",
|
| 1702 |
+
"Question saved.",
|
| 1703 |
+
)
|
| 1704 |
+
|
| 1705 |
+
if bag_size <= 0:
|
| 1706 |
+
|
| 1707 |
+
return (
|
| 1708 |
+
"### ⚠️ Please provide a feed bag size.",
|
| 1709 |
+
"Question saved.",
|
| 1710 |
+
)
|
| 1711 |
+
|
| 1712 |
+
calc = calculate_feed_numbers(
|
| 1713 |
+
birds=birds,
|
| 1714 |
+
total_days=total_days,
|
| 1715 |
+
bag_size=bag_size,
|
| 1716 |
+
)
|
| 1717 |
+
|
| 1718 |
+
whole_bags = calc[
|
| 1719 |
+
"whole_bags"
|
| 1720 |
+
]
|
| 1721 |
+
|
| 1722 |
+
exact_bags = calc[
|
| 1723 |
+
"exact_bags"
|
| 1724 |
+
]
|
| 1725 |
+
|
| 1726 |
+
benchmark_total_kg = calc[
|
| 1727 |
+
"benchmark_total_kg"
|
| 1728 |
+
]
|
| 1729 |
+
|
| 1730 |
+
purchase_feed_kg = calc[
|
| 1731 |
+
"purchase_feed_kg"
|
| 1732 |
+
]
|
| 1733 |
+
|
| 1734 |
+
starter_bags = calc[
|
| 1735 |
+
"starter_bags"
|
| 1736 |
+
]
|
| 1737 |
+
|
| 1738 |
+
finisher_bags = calc[
|
| 1739 |
+
"finisher_bags"
|
| 1740 |
+
]
|
| 1741 |
+
|
| 1742 |
+
starter_kg = calc[
|
| 1743 |
+
"starter_kg"
|
| 1744 |
+
]
|
| 1745 |
+
|
| 1746 |
+
finisher_kg = calc[
|
| 1747 |
+
"finisher_kg"
|
| 1748 |
+
]
|
| 1749 |
+
|
| 1750 |
+
total_cost = (
|
| 1751 |
+
whole_bags * price
|
| 1752 |
+
if price > 0
|
| 1753 |
+
else None
|
| 1754 |
+
)
|
| 1755 |
+
|
| 1756 |
+
# ----------------------------------------------------
|
| 1757 |
+
# "Are X bags enough?"
|
| 1758 |
+
# ----------------------------------------------------
|
| 1759 |
+
|
| 1760 |
+
asks_if_enough = (
|
| 1761 |
+
asked_bags is not None
|
| 1762 |
+
and (
|
| 1763 |
+
"enough" in q
|
| 1764 |
+
or "sufficient" in q
|
| 1765 |
+
or "enough feed" in q
|
| 1766 |
+
)
|
| 1767 |
+
)
|
| 1768 |
+
|
| 1769 |
+
if asks_if_enough:
|
| 1770 |
+
|
| 1771 |
+
available_kg = (
|
| 1772 |
+
asked_bags * bag_size
|
| 1773 |
+
)
|
| 1774 |
+
|
| 1775 |
+
enough = (
|
| 1776 |
+
available_kg
|
| 1777 |
+
>= purchase_feed_kg
|
| 1778 |
+
)
|
| 1779 |
+
|
| 1780 |
+
if enough:
|
| 1781 |
+
|
| 1782 |
+
surplus = (
|
| 1783 |
+
available_kg
|
| 1784 |
+
- purchase_feed_kg
|
| 1785 |
+
)
|
| 1786 |
+
|
| 1787 |
+
answer = f"""
|
| 1788 |
+
# 🐔 Yes — approximately {asked_bags:g} bags should be enough
|
| 1789 |
+
|
| 1790 |
+
For **{birds:,} birds** at **{weeks} weeks + {extra_days} days ({total_days} days)**:
|
| 1791 |
+
|
| 1792 |
+
- Estimated purchase requirement: **{purchase_feed_kg:,.1f} kg**
|
| 1793 |
+
- {asked_bags:g} bags × {bag_size:g} kg = **{available_kg:,.1f} kg**
|
| 1794 |
+
- Estimated surplus: **{surplus:,.1f} kg**
|
| 1795 |
+
|
| 1796 |
+
### 🌾 Recommended purchase
|
| 1797 |
+
|
| 1798 |
+
**Approximately {whole_bags} bags of {bag_size:g} kg feed**
|
| 1799 |
+
|
| 1800 |
+
The question has also been **saved automatically** for future PoultryFeedCalc improvements.
|
| 1801 |
+
"""
|
| 1802 |
+
|
| 1803 |
+
else:
|
| 1804 |
+
|
| 1805 |
+
shortage = (
|
| 1806 |
+
purchase_feed_kg
|
| 1807 |
+
- available_kg
|
| 1808 |
+
)
|
| 1809 |
+
|
| 1810 |
+
answer = f"""
|
| 1811 |
+
# ⚠️ No — {asked_bags:g} bags may not be enough
|
| 1812 |
+
|
| 1813 |
+
For **{birds:,} birds** at **{weeks} weeks + {extra_days} days ({total_days} days)**:
|
| 1814 |
+
|
| 1815 |
+
- Estimated purchase requirement: **{purchase_feed_kg:,.1f} kg**
|
| 1816 |
+
- {asked_bags:g} bags × {bag_size:g} kg = **{available_kg:,.1f} kg**
|
| 1817 |
+
- Estimated shortage: **{shortage:,.1f} kg**
|
| 1818 |
+
|
| 1819 |
+
### 🎯 Recommended
|
| 1820 |
+
|
| 1821 |
+
**Approximately {whole_bags} bags of {bag_size:g} kg feed**
|
| 1822 |
+
|
| 1823 |
+
The question has been **saved automatically** for future PoultryFeedCalc improvements.
|
| 1824 |
+
"""
|
| 1825 |
+
|
| 1826 |
+
return (
|
| 1827 |
+
answer,
|
| 1828 |
+
"✅ Question answered and saved automatically.",
|
| 1829 |
+
)
|
| 1830 |
+
|
| 1831 |
+
# ----------------------------------------------------
|
| 1832 |
+
# Normal feed question
|
| 1833 |
+
# ----------------------------------------------------
|
| 1834 |
+
|
| 1835 |
+
cost_text = ""
|
| 1836 |
+
|
| 1837 |
+
if total_cost is not None:
|
| 1838 |
+
|
| 1839 |
+
cost_text = (
|
| 1840 |
+
f"\n\n### 💰 Estimated feed cost\n\n"
|
| 1841 |
+
f"# {format_money(total_cost, symbol)}\n\n"
|
| 1842 |
+
f"Based on {format_money(price, symbol)} "
|
| 1843 |
+
f"per {bag_size:g}-kg bag."
|
| 1844 |
+
)
|
| 1845 |
+
|
| 1846 |
+
answer = f"""
|
| 1847 |
+
# 🐔 Your quick feed answer
|
| 1848 |
+
|
| 1849 |
+
## You will require approximately **{whole_bags} bags of {bag_size:g} kg feed**
|
| 1850 |
+
|
| 1851 |
+
To raise **{birds:,} birds** to **{weeks} weeks + {extra_days} days ({total_days} days)**.
|
| 1852 |
+
|
| 1853 |
+
### 🌾 Feed breakdown
|
| 1854 |
+
|
| 1855 |
+
| Feed | Quantity | Bags |
|
| 1856 |
+
|---|---:|---:|
|
| 1857 |
+
| 🌱 Starter | **{starter_kg:,.1f} kg** | **{starter_bags} bags** |
|
| 1858 |
+
| 🌽 Finisher | **{finisher_kg:,.1f} kg** | **{finisher_bags} bags** |
|
| 1859 |
+
| **Recommended total** | **{purchase_feed_kg:,.1f} kg** | **{whole_bags} bags** |
|
| 1860 |
+
|
| 1861 |
+
### 🎯 Quick answer
|
| 1862 |
+
|
| 1863 |
+
**{birds:,} birds × {total_days} days → approximately {whole_bags} bags ({starter_bags} Starter + {finisher_bags} Finisher).**
|
| 1864 |
+
|
| 1865 |
+
Benchmark feed before purchase allowance: **{benchmark_total_kg:,.1f} kg**
|
| 1866 |
+
|
| 1867 |
+
Practical purchase estimate: **{purchase_feed_kg:,.1f} kg**
|
| 1868 |
+
|
| 1869 |
+
Exact purchase estimate: **{exact_bags:.2f} bags**
|
| 1870 |
+
|
| 1871 |
+
The question has been **saved automatically**.
|
| 1872 |
+
{cost_text}
|
| 1873 |
+
"""
|
| 1874 |
+
|
| 1875 |
+
return (
|
| 1876 |
+
answer,
|
| 1877 |
+
"✅ Question answered and saved automatically.",
|
| 1878 |
+
)
|
| 1879 |
+
|
| 1880 |
+
# ========================================================
|
| 1881 |
+
# PROFIT QUESTIONS
|
| 1882 |
+
# ========================================================
|
| 1883 |
+
|
| 1884 |
+
if (
|
| 1885 |
+
"profit" in q
|
| 1886 |
+
or "profitability" in q
|
| 1887 |
+
or "make money" in q
|
| 1888 |
+
):
|
| 1889 |
+
|
| 1890 |
+
return (
|
| 1891 |
+
"""
|
| 1892 |
+
# 💰 Profit calculation
|
| 1893 |
+
|
| 1894 |
+
I can help build this into PoultryFeedCalc.
|
| 1895 |
+
|
| 1896 |
+
A proper broiler profit calculation needs:
|
| 1897 |
+
|
| 1898 |
+
- Number of chicks
|
| 1899 |
+
- Chick purchase price
|
| 1900 |
+
- Feed cost
|
| 1901 |
+
- Medication/vaccination
|
| 1902 |
+
- Labour
|
| 1903 |
+
- Electricity
|
| 1904 |
+
- Mortality
|
| 1905 |
+
- Other farm expenses
|
| 1906 |
+
- Expected selling weight
|
| 1907 |
+
- Selling price per bird or kg
|
| 1908 |
+
|
| 1909 |
+
Your question has been **saved automatically**.
|
| 1910 |
+
|
| 1911 |
+
We are collecting questions like this so future versions
|
| 1912 |
+
can include a full **Broiler Profit Calculator**.
|
| 1913 |
+
""",
|
| 1914 |
+
"✅ Question saved for future product development.",
|
| 1915 |
+
)
|
| 1916 |
+
|
| 1917 |
+
# ========================================================
|
| 1918 |
+
# WEIGHT QUESTIONS
|
| 1919 |
+
# ========================================================
|
| 1920 |
+
|
| 1921 |
+
if (
|
| 1922 |
+
"weight" in q
|
| 1923 |
+
or "weigh" in q
|
| 1924 |
+
or "kg" in q
|
| 1925 |
+
):
|
| 1926 |
+
|
| 1927 |
+
return (
|
| 1928 |
+
"""
|
| 1929 |
+
# ⚖️ Bird weight
|
| 1930 |
+
|
| 1931 |
+
Expected broiler weight depends on:
|
| 1932 |
+
|
| 1933 |
+
- Genetics
|
| 1934 |
+
- Sex
|
| 1935 |
+
- Feed quality
|
| 1936 |
+
- Feed intake
|
| 1937 |
+
- Temperature
|
| 1938 |
+
- Health
|
| 1939 |
+
- Management
|
| 1940 |
+
|
| 1941 |
+
Your question has been **saved automatically** for future
|
| 1942 |
+
PoultryFeedCalc improvements.
|
| 1943 |
+
|
| 1944 |
+
A future version can include a dedicated **Expected Weight
|
| 1945 |
+
Calculator**.
|
| 1946 |
+
""",
|
| 1947 |
+
"✅ Question saved automatically.",
|
| 1948 |
+
)
|
| 1949 |
+
|
| 1950 |
+
# ========================================================
|
| 1951 |
+
# WATER QUESTIONS
|
| 1952 |
+
# ========================================================
|
| 1953 |
+
|
| 1954 |
+
if "water" in q:
|
| 1955 |
+
|
| 1956 |
+
return (
|
| 1957 |
+
"""
|
| 1958 |
+
# 💧 Water requirement
|
| 1959 |
+
|
| 1960 |
+
Water consumption changes with:
|
| 1961 |
+
|
| 1962 |
+
- Bird age
|
| 1963 |
+
- Temperature
|
| 1964 |
+
- Feed intake
|
| 1965 |
+
- Housing
|
| 1966 |
+
- Health
|
| 1967 |
+
- Management
|
| 1968 |
+
|
| 1969 |
+
Your question has been **saved automatically**.
|
| 1970 |
+
|
| 1971 |
+
This is also a good candidate for a future
|
| 1972 |
+
**Poultry Water Calculator**.
|
| 1973 |
+
""",
|
| 1974 |
+
"✅ Question saved automatically.",
|
| 1975 |
+
)
|
| 1976 |
+
|
| 1977 |
+
# ========================================================
|
| 1978 |
+
# MEDICATION / HEALTH QUESTIONS
|
| 1979 |
+
# ========================================================
|
| 1980 |
+
|
| 1981 |
+
if any(
|
| 1982 |
+
keyword in q
|
| 1983 |
+
for keyword in [
|
| 1984 |
+
"medicine",
|
| 1985 |
+
"medication",
|
| 1986 |
+
"drug",
|
| 1987 |
+
"sick",
|
| 1988 |
+
"disease",
|
| 1989 |
+
"vaccine",
|
| 1990 |
+
"vaccination",
|
| 1991 |
+
]
|
| 1992 |
+
):
|
| 1993 |
+
|
| 1994 |
+
return (
|
| 1995 |
+
"""
|
| 1996 |
+
# 🐔 Farm health question received
|
| 1997 |
+
|
| 1998 |
+
Your question has been **saved automatically**.
|
| 1999 |
+
|
| 2000 |
+
Health and medication advice can depend on the actual
|
| 2001 |
+
symptoms, diagnosis, farm conditions and veterinary guidance.
|
| 2002 |
+
|
| 2003 |
+
We are collecting these questions to identify the most
|
| 2004 |
+
important health topics farmers want PoultryFeedCalc
|
| 2005 |
+
to address in future.
|
| 2006 |
+
""",
|
| 2007 |
+
"✅ Question saved automatically.",
|
| 2008 |
+
)
|
| 2009 |
+
|
| 2010 |
+
# ========================================================
|
| 2011 |
+
# GENERAL FARM QUESTION
|
| 2012 |
+
# ========================================================
|
| 2013 |
+
|
| 2014 |
+
return (
|
| 2015 |
+
f"""
|
| 2016 |
+
# 🤖 Question received
|
| 2017 |
+
|
| 2018 |
+
> **{question}**
|
| 2019 |
+
|
| 2020 |
+
Your question has been **saved automatically**.
|
| 2021 |
+
|
| 2022 |
+
We are collecting the real questions farmers ask so
|
| 2023 |
+
PoultryFeedCalc can become more useful and answer more
|
| 2024 |
+
farming questions in future.
|
| 2025 |
+
|
| 2026 |
+
### Questions we plan to support
|
| 2027 |
+
|
| 2028 |
+
- Feed requirements
|
| 2029 |
+
- Starter / Finisher quantities
|
| 2030 |
+
- Feed cost
|
| 2031 |
+
- Bird weight
|
| 2032 |
+
- Water requirements
|
| 2033 |
+
- Mortality
|
| 2034 |
+
- Profitability
|
| 2035 |
+
- Selling price
|
| 2036 |
+
- Farm expenses
|
| 2037 |
+
- Vaccination
|
| 2038 |
+
- General broiler management
|
| 2039 |
+
""",
|
| 2040 |
+
"✅ Question saved automatically for future improvements.",
|
| 2041 |
+
)
|
| 2042 |
+
|
| 2043 |
+
|
| 2044 |
+
# ============================================================
|
| 2045 |
+
# UI CSS
|
| 2046 |
+
# ============================================================
|
| 2047 |
+
|
| 2048 |
+
CSS = """
|
| 2049 |
+
<style>
|
| 2050 |
+
|
| 2051 |
+
body {
|
| 2052 |
+
background: #ffffff;
|
| 2053 |
+
}
|
| 2054 |
+
|
| 2055 |
+
.gradio-container {
|
| 2056 |
+
max-width: 1250px !important;
|
| 2057 |
+
margin: auto !important;
|
| 2058 |
+
}
|
| 2059 |
+
|
| 2060 |
+
.app-header {
|
| 2061 |
+
text-align: center;
|
| 2062 |
+
}
|
| 2063 |
+
|
| 2064 |
+
.app-subtitle {
|
| 2065 |
+
text-align: center;
|
| 2066 |
+
color: #555;
|
| 2067 |
+
margin-bottom: 25px;
|
| 2068 |
+
}
|
| 2069 |
|
| 2070 |
+
.email-box {
|
| 2071 |
+
border: 2px solid #f97316;
|
| 2072 |
+
border-radius: 14px;
|
| 2073 |
+
padding: 20px;
|
| 2074 |
+
background: #fffaf4;
|
| 2075 |
+
}
|
| 2076 |
|
| 2077 |
+
.input-card {
|
| 2078 |
+
border-radius: 14px;
|
| 2079 |
+
}
|
| 2080 |
|
| 2081 |
+
.big-answer {
|
| 2082 |
+
font-size: 28px;
|
| 2083 |
+
line-height: 1.15;
|
| 2084 |
+
padding: 18px;
|
| 2085 |
+
border-left: 8px solid #f97316;
|
| 2086 |
+
background: #fff7ed;
|
| 2087 |
+
border-radius: 12px;
|
| 2088 |
+
}
|
| 2089 |
|
| 2090 |
+
.cost-answer {
|
| 2091 |
+
font-size: 24px;
|
| 2092 |
+
}
|
| 2093 |
+
|
| 2094 |
+
.quick-answer {
|
| 2095 |
+
font-size: 21px;
|
| 2096 |
+
line-height: 1.55;
|
| 2097 |
+
padding: 18px;
|
| 2098 |
+
background: #f7f7f7;
|
| 2099 |
+
border-radius: 12px;
|
| 2100 |
+
}
|
| 2101 |
+
|
| 2102 |
+
.orange-button button {
|
| 2103 |
+
background: #f97316 !important;
|
| 2104 |
+
color: white !important;
|
| 2105 |
+
font-weight: 800 !important;
|
| 2106 |
+
font-size: 17px !important;
|
| 2107 |
+
}
|
| 2108 |
+
|
| 2109 |
+
.google-button {
|
| 2110 |
+
display: inline-block;
|
| 2111 |
+
padding: 10px 18px;
|
| 2112 |
+
border-radius: 8px;
|
| 2113 |
+
background: white;
|
| 2114 |
+
border: 1px solid #ccc;
|
| 2115 |
+
color: #222;
|
| 2116 |
+
text-decoration: none;
|
| 2117 |
+
font-weight: 700;
|
| 2118 |
+
}
|
| 2119 |
+
|
| 2120 |
+
.google-button:hover {
|
| 2121 |
+
background: #f5f5f5;
|
| 2122 |
+
}
|
| 2123 |
+
|
| 2124 |
+
.speak-button {
|
| 2125 |
+
display: inline-block;
|
| 2126 |
+
padding: 10px 18px;
|
| 2127 |
+
border-radius: 8px;
|
| 2128 |
+
background: #f3f4f6;
|
| 2129 |
+
border: 1px solid #ccc;
|
| 2130 |
+
cursor: pointer;
|
| 2131 |
+
font-weight: 700;
|
| 2132 |
+
}
|
| 2133 |
+
|
| 2134 |
+
.question-help {
|
| 2135 |
+
color: #555;
|
| 2136 |
+
font-size: 14px;
|
| 2137 |
+
}
|
| 2138 |
|
| 2139 |
+
</style>
|
| 2140 |
+
"""
|
| 2141 |
|
| 2142 |
|
| 2143 |
# ============================================================
|
| 2144 |
+
# GOOGLE LOGIN UI
|
| 2145 |
# ============================================================
|
| 2146 |
|
| 2147 |
+
if GOOGLE_ENABLED:
|
| 2148 |
+
|
| 2149 |
+
GOOGLE_UI = """
|
| 2150 |
+
<div style="margin-top:10px;">
|
| 2151 |
+
<a
|
| 2152 |
+
class="google-button"
|
| 2153 |
+
href="/auth/google"
|
| 2154 |
+
>
|
| 2155 |
+
🔐 Continue with Google
|
| 2156 |
+
</a>
|
| 2157 |
+
</div>
|
| 2158 |
+
|
| 2159 |
+
<div style="
|
| 2160 |
+
margin-top:8px;
|
| 2161 |
+
color:#666;
|
| 2162 |
+
font-size:13px;
|
| 2163 |
+
">
|
| 2164 |
+
Google sign-in can automatically provide your email.
|
| 2165 |
+
You can also enter your email manually.
|
| 2166 |
+
</div>
|
| 2167 |
"""
|
| 2168 |
+
|
| 2169 |
+
else:
|
| 2170 |
+
|
| 2171 |
+
GOOGLE_UI = """
|
| 2172 |
+
<div style="
|
| 2173 |
+
color:#666;
|
| 2174 |
+
font-size:13px;
|
| 2175 |
+
margin-top:8px;
|
| 2176 |
+
">
|
| 2177 |
+
Enter your email manually.
|
| 2178 |
+
Google sign-in can be enabled when OAuth credentials
|
| 2179 |
+
are added to the Space.
|
| 2180 |
+
</div>
|
| 2181 |
"""
|
| 2182 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2183 |
|
| 2184 |
+
# ============================================================
|
| 2185 |
+
# VOICE INPUT
|
| 2186 |
+
# ============================================================
|
|
|
|
| 2187 |
|
| 2188 |
+
VOICE_UI = """
|
| 2189 |
+
<div style="margin-top:8px;">
|
| 2190 |
|
| 2191 |
+
<button
|
| 2192 |
+
class="speak-button"
|
| 2193 |
+
type="button"
|
| 2194 |
+
onclick="startPoultrySpeech()"
|
| 2195 |
+
>
|
| 2196 |
+
🎤 Speak your question
|
| 2197 |
+
</button>
|
| 2198 |
|
| 2199 |
+
<span
|
| 2200 |
+
id="speech-status"
|
| 2201 |
+
style="
|
| 2202 |
+
margin-left:10px;
|
| 2203 |
+
color:#666;
|
| 2204 |
+
font-size:13px;
|
| 2205 |
+
"
|
| 2206 |
+
></span>
|
| 2207 |
|
| 2208 |
+
</div>
|
|
|
|
|
|
|
|
|
|
| 2209 |
|
| 2210 |
+
<script>
|
| 2211 |
|
| 2212 |
+
function startPoultrySpeech() {
|
|
|
|
|
|
|
|
|
|
| 2213 |
|
| 2214 |
+
const SpeechRecognition =
|
| 2215 |
+
window.SpeechRecognition ||
|
| 2216 |
+
window.webkitSpeechRecognition;
|
| 2217 |
|
| 2218 |
+
const status =
|
| 2219 |
+
document.getElementById("speech-status");
|
| 2220 |
|
| 2221 |
+
if (!SpeechRecognition) {
|
| 2222 |
|
| 2223 |
+
status.innerText =
|
| 2224 |
+
"Voice input is not supported by this browser.";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2225 |
|
| 2226 |
+
return;
|
| 2227 |
+
}
|
|
|
|
|
|
|
|
|
|
| 2228 |
|
| 2229 |
+
const recognition =
|
| 2230 |
+
new SpeechRecognition();
|
| 2231 |
|
| 2232 |
+
recognition.lang = "en-NG";
|
| 2233 |
+
recognition.interimResults = false;
|
| 2234 |
+
recognition.continuous = false;
|
|
|
|
| 2235 |
|
| 2236 |
+
status.innerText =
|
| 2237 |
+
"🎤 Listening...";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2238 |
|
| 2239 |
+
recognition.onresult = function(event) {
|
| 2240 |
|
| 2241 |
+
const transcript =
|
| 2242 |
+
event.results[0][0].transcript;
|
|
|
|
| 2243 |
|
| 2244 |
+
const box =
|
| 2245 |
+
document.querySelector(
|
| 2246 |
+
"#question_box textarea"
|
| 2247 |
+
);
|
|
|
|
| 2248 |
|
| 2249 |
+
if (box) {
|
|
|
|
|
|
|
| 2250 |
|
| 2251 |
+
box.value = transcript;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2252 |
|
| 2253 |
+
box.dispatchEvent(
|
| 2254 |
+
new Event(
|
| 2255 |
+
"input",
|
| 2256 |
+
{
|
| 2257 |
+
bubbles: true
|
| 2258 |
+
}
|
| 2259 |
+
)
|
| 2260 |
+
);
|
| 2261 |
+
}
|
| 2262 |
+
|
| 2263 |
+
status.innerText =
|
| 2264 |
+
"✅ Question captured.";
|
| 2265 |
+
};
|
| 2266 |
+
|
| 2267 |
+
recognition.onerror = function() {
|
| 2268 |
+
|
| 2269 |
+
status.innerText =
|
| 2270 |
+
"Could not capture your voice. Please try again.";
|
| 2271 |
+
};
|
| 2272 |
+
|
| 2273 |
+
recognition.onend = function() {
|
| 2274 |
|
| 2275 |
+
if (
|
| 2276 |
+
status.innerText ===
|
| 2277 |
+
"🎤 Listening..."
|
| 2278 |
+
) {
|
| 2279 |
+
|
| 2280 |
+
status.innerText = "";
|
| 2281 |
+
}
|
| 2282 |
+
};
|
| 2283 |
+
|
| 2284 |
+
recognition.start();
|
| 2285 |
}
|
| 2286 |
+
|
| 2287 |
+
</script>
|
| 2288 |
"""
|
| 2289 |
|
| 2290 |
|
| 2291 |
+
# ============================================================
|
| 2292 |
+
# BUILD GRADIO APP
|
| 2293 |
+
# ============================================================
|
| 2294 |
+
|
| 2295 |
+
with gr.Blocks() as demo:
|
| 2296 |
+
|
| 2297 |
+
# --------------------------------------------------------
|
| 2298 |
+
# CSS
|
| 2299 |
+
# --------------------------------------------------------
|
| 2300 |
+
|
| 2301 |
+
gr.HTML(
|
| 2302 |
+
CSS
|
| 2303 |
+
)
|
| 2304 |
+
|
| 2305 |
+
# --------------------------------------------------------
|
| 2306 |
+
# HEADER
|
| 2307 |
+
# --------------------------------------------------------
|
| 2308 |
|
| 2309 |
gr.Markdown(
|
| 2310 |
"""
|
|
|
|
| 2312 |
|
| 2313 |
### Simple broiler feed calculator for farmers
|
| 2314 |
|
| 2315 |
+
**Choose your birds, age, feed bag size and price — no calculations required.**
|
| 2316 |
+
""",
|
| 2317 |
+
elem_classes=[
|
| 2318 |
+
"app-header"
|
| 2319 |
+
],
|
| 2320 |
+
)
|
| 2321 |
|
| 2322 |
+
gr.Markdown(
|
| 2323 |
+
"""
|
| 2324 |
+
The calculator converts **Weeks + Days into total days** and
|
| 2325 |
+
uses the feed benchmark to estimate Starter and Finisher requirements.
|
| 2326 |
+
""",
|
| 2327 |
+
elem_classes=[
|
| 2328 |
+
"app-subtitle"
|
| 2329 |
+
],
|
| 2330 |
)
|
| 2331 |
|
| 2332 |
+
# --------------------------------------------------------
|
| 2333 |
+
# STATES
|
| 2334 |
+
# --------------------------------------------------------
|
| 2335 |
+
|
| 2336 |
+
country_state = gr.State(
|
| 2337 |
+
"Nigeria"
|
| 2338 |
+
)
|
| 2339 |
+
|
| 2340 |
+
currency_state = gr.State(
|
| 2341 |
+
"NGN"
|
| 2342 |
+
)
|
| 2343 |
+
|
| 2344 |
+
symbol_state = gr.State(
|
| 2345 |
+
"₦"
|
| 2346 |
+
)
|
| 2347 |
|
| 2348 |
+
# --------------------------------------------------------
|
| 2349 |
+
# EMAIL
|
| 2350 |
+
# --------------------------------------------------------
|
| 2351 |
+
|
| 2352 |
+
with gr.Group(
|
| 2353 |
+
elem_classes=[
|
| 2354 |
+
"email-box"
|
| 2355 |
+
]
|
| 2356 |
+
):
|
| 2357 |
|
| 2358 |
gr.Markdown(
|
| 2359 |
"""
|
| 2360 |
+
## 📧 Before you calculate
|
| 2361 |
|
| 2362 |
+
**Enter your email after the feed price and before calculating.**
|
|
|
|
| 2363 |
|
| 2364 |
+
Your email allows us to save your calculations and send
|
| 2365 |
+
future PoultryFeedCalc updates and improvements.
|
| 2366 |
+
|
| 2367 |
+
Your email is only used for **PoultryFeedCalc communications**.
|
| 2368 |
"""
|
| 2369 |
)
|
| 2370 |
|
| 2371 |
email = gr.Textbox(
|
| 2372 |
+
label="📧 Email address *",
|
| 2373 |
placeholder="farmer@example.com",
|
| 2374 |
type="email",
|
| 2375 |
)
|
| 2376 |
|
| 2377 |
+
gr.HTML(
|
| 2378 |
+
GOOGLE_UI
|
| 2379 |
+
)
|
| 2380 |
+
|
| 2381 |
+
email_status = gr.Markdown(
|
| 2382 |
+
""
|
| 2383 |
+
)
|
| 2384 |
+
|
| 2385 |
+
# --------------------------------------------------------
|
| 2386 |
+
# MAIN INPUT + RESULT
|
| 2387 |
+
# --------------------------------------------------------
|
| 2388 |
|
| 2389 |
with gr.Row():
|
| 2390 |
|
| 2391 |
+
# ====================================================
|
| 2392 |
# INPUTS
|
| 2393 |
+
# ====================================================
|
| 2394 |
|
| 2395 |
+
with gr.Column(
|
| 2396 |
+
scale=1,
|
| 2397 |
+
elem_classes=[
|
| 2398 |
+
"input-card"
|
| 2399 |
+
],
|
| 2400 |
+
):
|
| 2401 |
|
| 2402 |
gr.Markdown(
|
| 2403 |
"## 🧮 Farm inputs"
|
| 2404 |
)
|
| 2405 |
|
| 2406 |
+
birds = gr.Dropdown(
|
|
|
|
| 2407 |
choices=[
|
| 2408 |
+
25,
|
| 2409 |
+
50,
|
| 2410 |
+
75,
|
| 2411 |
+
100,
|
| 2412 |
+
150,
|
| 2413 |
+
200,
|
| 2414 |
+
500,
|
| 2415 |
+
1000,
|
|
|
|
| 2416 |
],
|
| 2417 |
+
value=25,
|
| 2418 |
+
allow_custom_value=True,
|
| 2419 |
+
label="🐔 Number of birds",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2420 |
)
|
| 2421 |
|
| 2422 |
weeks = gr.Dropdown(
|
| 2423 |
+
choices=list(
|
| 2424 |
+
range(0, 21)
|
| 2425 |
+
),
|
| 2426 |
value=5,
|
| 2427 |
+
label="📅 Age — Weeks",
|
| 2428 |
)
|
| 2429 |
|
| 2430 |
+
days = gr.Dropdown(
|
| 2431 |
+
choices=list(
|
| 2432 |
+
range(0, 7)
|
| 2433 |
+
),
|
| 2434 |
value=0,
|
| 2435 |
+
label="📅 Age — Days",
|
| 2436 |
)
|
| 2437 |
|
| 2438 |
bag_size = gr.Dropdown(
|
|
|
|
| 2439 |
choices=[
|
| 2440 |
10,
|
| 2441 |
+
15,
|
| 2442 |
+
20,
|
| 2443 |
25,
|
| 2444 |
50,
|
| 2445 |
],
|
| 2446 |
value=25,
|
| 2447 |
+
label="🌾 Feed bag size (kg)",
|
| 2448 |
)
|
| 2449 |
|
| 2450 |
+
price = gr.Number(
|
|
|
|
| 2451 |
value=24000,
|
|
|
|
| 2452 |
precision=0,
|
| 2453 |
+
label="💰 Price per bag (₦)",
|
| 2454 |
)
|
| 2455 |
|
| 2456 |
+
currency_display = gr.Markdown(
|
| 2457 |
+
"""
|
| 2458 |
+
🌍 **Detected country:** Nigeria
|
| 2459 |
+
|
| 2460 |
+
💱 **Currency:** NGN (₦)
|
| 2461 |
+
"""
|
| 2462 |
+
)
|
| 2463 |
+
|
| 2464 |
+
calculate_button = gr.Button(
|
| 2465 |
+
"🐔 Calculate Feed Requirement",
|
| 2466 |
+
variant="primary",
|
| 2467 |
+
elem_classes=[
|
| 2468 |
+
"orange-button"
|
| 2469 |
+
],
|
| 2470 |
+
)
|
| 2471 |
+
|
| 2472 |
+
# ====================================================
|
| 2473 |
+
# RESULTS
|
| 2474 |
+
# ====================================================
|
| 2475 |
|
| 2476 |
with gr.Column(
|
| 2477 |
scale=1,
|
|
|
|
| 2483 |
|
| 2484 |
result = gr.Markdown(
|
| 2485 |
"""
|
| 2486 |
+
# 👈 Start here
|
| 2487 |
|
| 2488 |
+
Choose your farm inputs, enter your email and click:
|
| 2489 |
+
|
| 2490 |
+
### **🐔 Calculate Feed Requirement**
|
| 2491 |
+
"""
|
| 2492 |
)
|
| 2493 |
|
| 2494 |
+
calculation_status = gr.Markdown(
|
| 2495 |
+
""
|
| 2496 |
+
)
|
| 2497 |
+
|
| 2498 |
+
# --------------------------------------------------------
|
| 2499 |
+
# ASK A QUESTION
|
| 2500 |
+
# --------------------------------------------------------
|
| 2501 |
|
| 2502 |
gr.Markdown("---")
|
| 2503 |
|
|
|
|
| 2507 |
|
| 2508 |
### What would you like to know about raising your birds?
|
| 2509 |
|
| 2510 |
+
You can **type or speak** your question.
|
| 2511 |
|
| 2512 |
+
Questions are saved automatically when you click
|
| 2513 |
+
**Get Answer** so we can use real farmer questions
|
| 2514 |
+
to improve future versions of PoultryFeedCalc.
|
| 2515 |
"""
|
| 2516 |
)
|
| 2517 |
|
|
|
|
| 2519 |
label="Your question",
|
| 2520 |
placeholder=(
|
| 2521 |
"Example: How many bags of feed do I need "
|
| 2522 |
+
"for 50 birds for 6 weeks?"
|
| 2523 |
),
|
| 2524 |
lines=4,
|
| 2525 |
+
elem_id="question_box",
|
| 2526 |
+
)
|
| 2527 |
+
|
| 2528 |
+
gr.HTML(
|
| 2529 |
+
VOICE_UI
|
| 2530 |
)
|
| 2531 |
|
| 2532 |
gr.Markdown(
|
| 2533 |
"""
|
| 2534 |
+
**Examples you can ask:**
|
| 2535 |
|
| 2536 |
+
- How many bags of feed for 50 birds for 6 weeks?
|
| 2537 |
+
- How much feed for 200 birds to 6 weeks?
|
| 2538 |
+
- How many bags for 500 broilers for 8 weeks?
|
| 2539 |
- Is 3 bags enough for 51 birds at 3 weeks?
|
| 2540 |
+
- How much will the feed cost?
|
| 2541 |
+
- How much water do my birds need?
|
| 2542 |
+
- How can I calculate my broiler profit?
|
| 2543 |
+
""",
|
| 2544 |
+
elem_classes=[
|
| 2545 |
+
"question-help"
|
| 2546 |
+
],
|
|
|
|
| 2547 |
)
|
| 2548 |
|
| 2549 |
+
ask_button = gr.Button(
|
| 2550 |
+
"🤖 Get Answer",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2551 |
variant="primary",
|
|
|
|
| 2552 |
)
|
| 2553 |
|
| 2554 |
+
question_answer = gr.Markdown(
|
| 2555 |
+
""
|
| 2556 |
+
)
|
| 2557 |
+
|
| 2558 |
+
question_status = gr.Markdown(
|
| 2559 |
+
""
|
| 2560 |
+
)
|
| 2561 |
+
|
| 2562 |
+
# --------------------------------------------------------
|
| 2563 |
+
# INFORMATION
|
| 2564 |
+
# --------------------------------------------------------
|
| 2565 |
+
|
| 2566 |
+
gr.Markdown("---")
|
| 2567 |
+
|
| 2568 |
+
gr.Markdown(
|
| 2569 |
+
f"""
|
| 2570 |
+
## 📊 How the calculator works
|
| 2571 |
+
|
| 2572 |
+
### 🌱 Starter
|
| 2573 |
+
Starter feed is calculated through **day 21**.
|
| 2574 |
+
|
| 2575 |
+
### 🌽 Finisher
|
| 2576 |
+
Finisher feed is calculated from **day 22 onward**.
|
| 2577 |
+
|
| 2578 |
+
### 🧮 Total bags
|
| 2579 |
+
The calculator:
|
| 2580 |
+
|
| 2581 |
+
1. Calculates benchmark feed in kilograms.
|
| 2582 |
+
2. Adds a **{PURCHASE_ALLOWANCE * 100:.0f}% practical purchase allowance**.
|
| 2583 |
+
3. Divides by your selected bag size.
|
| 2584 |
+
4. Rounds the result **up to the next whole bag**.
|
| 2585 |
+
|
| 2586 |
+
### Example
|
| 2587 |
+
|
| 2588 |
+
If the calculation gives:
|
| 2589 |
+
|
| 2590 |
+
**5.8 bags**
|
| 2591 |
+
|
| 2592 |
+
the farmer sees:
|
| 2593 |
|
| 2594 |
+
# **6 bags**
|
|
|
|
|
|
|
|
|
|
| 2595 |
|
| 2596 |
+
so the farmer does not run short.
|
| 2597 |
+
|
| 2598 |
+
### ⚠️ Important
|
| 2599 |
+
|
| 2600 |
+
Actual farm feed consumption can vary because of genetics,
|
| 2601 |
+
sex, mortality, feed quality, temperature, management and
|
| 2602 |
+
other farm conditions.
|
| 2603 |
+
|
| 2604 |
+
This calculator provides an **estimate**, not a guarantee.
|
| 2605 |
+
"""
|
| 2606 |
)
|
| 2607 |
|
| 2608 |
# ========================================================
|
| 2609 |
# EVENTS
|
| 2610 |
# ========================================================
|
| 2611 |
|
| 2612 |
+
# --------------------------------------------------------
|
| 2613 |
+
# Automatically populate email from Google sign-in.
|
| 2614 |
+
# --------------------------------------------------------
|
| 2615 |
+
|
| 2616 |
+
demo.load(
|
| 2617 |
+
fn=get_google_email,
|
| 2618 |
+
inputs=None,
|
| 2619 |
+
outputs=email,
|
| 2620 |
+
api_visibility="private",
|
| 2621 |
)
|
| 2622 |
|
| 2623 |
+
# --------------------------------------------------------
|
| 2624 |
+
# Automatically detect country and currency.
|
| 2625 |
+
# --------------------------------------------------------
|
| 2626 |
+
|
| 2627 |
+
demo.load(
|
| 2628 |
+
fn=detect_country_from_request,
|
| 2629 |
+
inputs=None,
|
| 2630 |
+
outputs=[
|
| 2631 |
+
country_state,
|
| 2632 |
+
currency_state,
|
| 2633 |
+
symbol_state,
|
| 2634 |
+
currency_display,
|
| 2635 |
+
price,
|
| 2636 |
+
],
|
| 2637 |
+
api_visibility="private",
|
| 2638 |
+
)
|
| 2639 |
+
|
| 2640 |
+
# --------------------------------------------------------
|
| 2641 |
+
# CALCULATE
|
| 2642 |
+
# --------------------------------------------------------
|
| 2643 |
+
|
| 2644 |
calculate_button.click(
|
| 2645 |
+
fn=calculate_feed,
|
| 2646 |
inputs=[
|
| 2647 |
+
birds,
|
|
|
|
|
|
|
| 2648 |
weeks,
|
| 2649 |
+
days,
|
| 2650 |
bag_size,
|
| 2651 |
+
price,
|
| 2652 |
+
email,
|
| 2653 |
+
country_state,
|
| 2654 |
+
currency_state,
|
| 2655 |
+
symbol_state,
|
| 2656 |
],
|
| 2657 |
outputs=[
|
| 2658 |
result,
|
| 2659 |
+
calculation_status,
|
| 2660 |
],
|
| 2661 |
+
api_name="calculate_feed",
|
| 2662 |
+
api_visibility="private",
|
| 2663 |
)
|
| 2664 |
|
| 2665 |
+
# --------------------------------------------------------
|
| 2666 |
+
# ASK QUESTION
|
| 2667 |
+
#
|
| 2668 |
+
# Clicking Get Answer:
|
| 2669 |
+
# 1. Saves question
|
| 2670 |
+
# 2. Answers question
|
| 2671 |
+
#
|
| 2672 |
+
# There is NO separate Save Question button.
|
| 2673 |
+
# --------------------------------------------------------
|
| 2674 |
+
|
| 2675 |
+
ask_button.click(
|
| 2676 |
+
fn=answer_question,
|
| 2677 |
inputs=[
|
|
|
|
| 2678 |
question,
|
| 2679 |
+
email,
|
| 2680 |
+
country_state,
|
| 2681 |
+
currency_state,
|
| 2682 |
+
symbol_state,
|
| 2683 |
+
birds,
|
| 2684 |
+
weeks,
|
| 2685 |
+
days,
|
| 2686 |
+
bag_size,
|
| 2687 |
+
price,
|
| 2688 |
],
|
| 2689 |
outputs=[
|
| 2690 |
+
question_answer,
|
| 2691 |
question_status,
|
| 2692 |
],
|
| 2693 |
+
api_name="ask_question",
|
| 2694 |
+
api_visibility="private",
|
| 2695 |
)
|
| 2696 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2697 |
|
| 2698 |
+
# ============================================================
|
| 2699 |
+
# MOUNT GRADIO INSIDE FASTAPI
|
| 2700 |
+
#
|
| 2701 |
+
# IMPORTANT:
|
| 2702 |
+
# There is NO demo.launch().
|
| 2703 |
+
#
|
| 2704 |
+
# This prevents the previous:
|
| 2705 |
+
#
|
| 2706 |
+
# ERROR [Errno 98]
|
| 2707 |
+
# address already in use
|
| 2708 |
+
#
|
| 2709 |
+
# problem caused by multiple server starts.
|
| 2710 |
+
# ============================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2711 |
|
| 2712 |
+
app = gr.mount_gradio_app(
|
| 2713 |
+
app,
|
| 2714 |
+
demo,
|
| 2715 |
+
path="/",
|
| 2716 |
+
)
|
| 2717 |
|
| 2718 |
|
| 2719 |
# ============================================================
|
| 2720 |
+
# SINGLE SERVER START
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2721 |
# ============================================================
|
| 2722 |
|
| 2723 |
if __name__ == "__main__":
|
| 2724 |
+
|
| 2725 |
+
import uvicorn
|
| 2726 |
+
|
| 2727 |
+
uvicorn.run(
|
| 2728 |
+
app,
|
| 2729 |
+
host="0.0.0.0",
|
| 2730 |
+
port=PORT,
|
| 2731 |
)
|