Spaces:
Paused
Paused
NTEIGBANAM OKE commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,368 +1,208 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import math
|
| 3 |
import re
|
| 4 |
-
import
|
| 5 |
-
from datetime import datetime
|
| 6 |
from pathlib import Path
|
| 7 |
|
| 8 |
import gradio as gr
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
#
|
| 19 |
-
# Values are
|
| 20 |
-
#
|
| 21 |
-
#
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
#
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
}
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
if keys[i] <= day <= keys[i + 1]:
|
| 63 |
-
ratio = (day - keys[i]) / (keys[i + 1] - keys[i])
|
| 64 |
-
return WEIGHT_BY_DAY[keys[i]] + (WEIGHT_BY_DAY[keys[i + 1]] - WEIGHT_BY_DAY[keys[i]]) * ratio
|
| 65 |
-
|
| 66 |
-
return WEIGHT_BY_DAY[56]
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
# ============================================================
|
| 70 |
-
# CURRENCY
|
| 71 |
-
# ============================================================
|
| 72 |
-
|
| 73 |
-
CURRENCIES = {
|
| 74 |
-
"NG": ("Nigeria", "β¦", "NGN"),
|
| 75 |
-
"GH": ("Ghana", "β΅", "GHS"),
|
| 76 |
-
"KE": ("Kenya", "KSh", "KES"),
|
| 77 |
-
"UG": ("Uganda", "USh", "UGX"),
|
| 78 |
-
"TZ": ("Tanzania", "TSh", "TZS"),
|
| 79 |
-
"ZA": ("South Africa", "R", "ZAR"),
|
| 80 |
-
"ZM": ("Zambia", "ZK", "ZMW"),
|
| 81 |
-
"ZW": ("Zimbabwe", "ZiG", "ZWG"),
|
| 82 |
-
"CM": ("Cameroon", "FCFA", "XAF"),
|
| 83 |
-
"CI": ("CΓ΄te d'Ivoire", "CFA", "XOF"),
|
| 84 |
-
"SN": ("Senegal", "CFA", "XOF"),
|
| 85 |
-
"US": ("United States", "$", "USD"),
|
| 86 |
-
"GB": ("United Kingdom", "Β£", "GBP"),
|
| 87 |
-
"CA": ("Canada", "C$", "CAD"),
|
| 88 |
-
"AU": ("Australia", "A$", "AUD"),
|
| 89 |
-
}
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
def detect_country():
|
| 93 |
-
"""Best-effort country detection from environment."""
|
| 94 |
-
candidates = [
|
| 95 |
-
os.environ.get("COUNTRY_CODE"),
|
| 96 |
-
os.environ.get("CF-IPCOUNTRY"),
|
| 97 |
-
os.environ.get("GEOIP_COUNTRY"),
|
| 98 |
-
]
|
| 99 |
-
|
| 100 |
-
for value in candidates:
|
| 101 |
-
if value:
|
| 102 |
-
value = value.strip().upper()
|
| 103 |
-
if value in CURRENCIES:
|
| 104 |
-
return value
|
| 105 |
-
|
| 106 |
return "NG"
|
| 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 |
-
cur.execute(
|
| 147 |
-
"""
|
| 148 |
-
CREATE TABLE IF NOT EXISTS calculations (
|
| 149 |
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 150 |
-
email TEXT,
|
| 151 |
-
country TEXT,
|
| 152 |
-
currency TEXT,
|
| 153 |
-
birds INTEGER,
|
| 154 |
-
weeks INTEGER,
|
| 155 |
-
days INTEGER,
|
| 156 |
-
total_days INTEGER,
|
| 157 |
-
bag_size REAL,
|
| 158 |
-
price_per_bag REAL,
|
| 159 |
-
total_feed_kg REAL,
|
| 160 |
-
starter_kg REAL,
|
| 161 |
-
finisher_kg REAL,
|
| 162 |
-
starter_bags REAL,
|
| 163 |
-
finisher_bags REAL,
|
| 164 |
-
total_bags REAL,
|
| 165 |
-
rounded_bags INTEGER,
|
| 166 |
-
total_cost REAL,
|
| 167 |
-
created_at TEXT
|
| 168 |
-
)
|
| 169 |
-
"""
|
| 170 |
-
)
|
| 171 |
-
|
| 172 |
-
conn.commit()
|
| 173 |
-
conn.close()
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
init_db()
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
# ============================================================
|
| 180 |
-
# DATABASE HELPERS
|
| 181 |
-
# ============================================================
|
| 182 |
-
|
| 183 |
-
def valid_email(email):
|
| 184 |
-
if not email:
|
| 185 |
-
return False
|
| 186 |
-
|
| 187 |
-
email = email.strip()
|
| 188 |
-
|
| 189 |
-
pattern = r"^[^@\s]+@[^@\s]+\.[^@\s]+$"
|
| 190 |
-
return bool(re.match(pattern, email))
|
| 191 |
-
|
| 192 |
|
| 193 |
def save_lead(email, country, currency):
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
"""
|
| 202 |
-
INSERT OR IGNORE INTO leads
|
| 203 |
-
(email, country, currency, created_at)
|
| 204 |
-
VALUES (?, ?, ?, ?)
|
| 205 |
-
""",
|
| 206 |
-
(
|
| 207 |
-
email.strip().lower(),
|
| 208 |
country,
|
| 209 |
currency,
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
)
|
| 213 |
-
|
| 214 |
-
conn.commit()
|
| 215 |
-
conn.close()
|
| 216 |
-
|
| 217 |
|
| 218 |
def save_question(email, question):
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
)
|
| 237 |
|
| 238 |
-
|
| 239 |
-
conn.close()
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
def save_calculation(
|
| 243 |
email,
|
| 244 |
-
|
| 245 |
-
currency,
|
| 246 |
birds,
|
| 247 |
weeks,
|
| 248 |
days,
|
| 249 |
-
total_days,
|
| 250 |
bag_size,
|
| 251 |
price_per_bag,
|
| 252 |
-
|
| 253 |
-
starter_kg,
|
| 254 |
-
finisher_kg,
|
| 255 |
-
starter_bags,
|
| 256 |
-
finisher_bags,
|
| 257 |
-
total_bags,
|
| 258 |
-
rounded_bags,
|
| 259 |
-
total_cost,
|
| 260 |
):
|
| 261 |
-
|
| 262 |
-
cur = conn.cursor()
|
| 263 |
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
country,
|
| 269 |
-
currency,
|
| 270 |
-
birds,
|
| 271 |
-
weeks,
|
| 272 |
-
days,
|
| 273 |
-
total_days,
|
| 274 |
-
bag_size,
|
| 275 |
-
price_per_bag,
|
| 276 |
-
total_feed_kg,
|
| 277 |
-
starter_kg,
|
| 278 |
-
finisher_kg,
|
| 279 |
-
starter_bags,
|
| 280 |
-
finisher_bags,
|
| 281 |
-
total_bags,
|
| 282 |
-
rounded_bags,
|
| 283 |
-
total_cost,
|
| 284 |
-
created_at
|
| 285 |
)
|
| 286 |
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
| 287 |
-
""",
|
| 288 |
-
(
|
| 289 |
-
email.strip().lower() if email else "",
|
| 290 |
-
country,
|
| 291 |
-
currency,
|
| 292 |
-
birds,
|
| 293 |
-
weeks,
|
| 294 |
-
days,
|
| 295 |
-
total_days,
|
| 296 |
-
bag_size,
|
| 297 |
-
price_per_bag,
|
| 298 |
-
total_feed_kg,
|
| 299 |
-
starter_kg,
|
| 300 |
-
finisher_kg,
|
| 301 |
-
starter_bags,
|
| 302 |
-
finisher_bags,
|
| 303 |
-
total_bags,
|
| 304 |
-
rounded_bags,
|
| 305 |
-
total_cost,
|
| 306 |
-
datetime.utcnow().isoformat(),
|
| 307 |
-
),
|
| 308 |
-
)
|
| 309 |
-
|
| 310 |
-
conn.commit()
|
| 311 |
-
conn.close()
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
# ============================================================
|
| 315 |
-
# FEED CALCULATION
|
| 316 |
-
# ============================================================
|
| 317 |
-
|
| 318 |
-
def cumulative_feed_per_bird(total_days):
|
| 319 |
-
"""
|
| 320 |
-
Returns benchmark cumulative feed consumption per bird.
|
| 321 |
-
|
| 322 |
-
For days beyond the table, the last known daily increment
|
| 323 |
-
is extended.
|
| 324 |
-
"""
|
| 325 |
-
|
| 326 |
-
if total_days <= 0:
|
| 327 |
-
return 0.0
|
| 328 |
-
|
| 329 |
-
if total_days in FEED_PER_BIRD_BY_DAY:
|
| 330 |
-
return FEED_PER_BIRD_BY_DAY[total_days]
|
| 331 |
-
|
| 332 |
-
max_day = max(FEED_PER_BIRD_BY_DAY.keys())
|
| 333 |
-
|
| 334 |
-
if total_days > max_day:
|
| 335 |
-
last = FEED_PER_BIRD_BY_DAY[max_day]
|
| 336 |
-
previous = FEED_PER_BIRD_BY_DAY[max_day - 1]
|
| 337 |
-
daily_increment = last - previous
|
| 338 |
-
|
| 339 |
-
return last + ((total_days - max_day) * daily_increment)
|
| 340 |
-
|
| 341 |
-
# fallback
|
| 342 |
-
closest_day = max(
|
| 343 |
-
day for day in FEED_PER_BIRD_BY_DAY
|
| 344 |
-
if day <= total_days
|
| 345 |
-
)
|
| 346 |
-
|
| 347 |
-
return FEED_PER_BIRD_BY_DAY[closest_day]
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
def feed_at_day(day):
|
| 351 |
-
return cumulative_feed_per_bird(day)
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
def calculate_feed(
|
| 355 |
-
birds,
|
| 356 |
-
weeks,
|
| 357 |
-
days,
|
| 358 |
-
bag_size,
|
| 359 |
-
price_per_bag,
|
| 360 |
-
email,
|
| 361 |
-
country_code,
|
| 362 |
-
):
|
| 363 |
-
# --------------------------------------------------------
|
| 364 |
-
# VALIDATION
|
| 365 |
-
# --------------------------------------------------------
|
| 366 |
|
| 367 |
try:
|
| 368 |
birds = int(birds)
|
|
@@ -371,577 +211,269 @@ def calculate_feed(
|
|
| 371 |
bag_size = float(bag_size)
|
| 372 |
price_per_bag = float(price_per_bag)
|
| 373 |
except Exception:
|
| 374 |
-
return
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
if weeks < 0:
|
| 380 |
-
return "β οΈ Weeks cannot be negative."
|
| 381 |
-
|
| 382 |
-
if days < 0 or days > 6:
|
| 383 |
-
return "β οΈ Days must be between 0 and 6."
|
| 384 |
-
|
| 385 |
-
if bag_size <= 0:
|
| 386 |
-
return "β οΈ Feed bag size must be greater than zero."
|
| 387 |
-
|
| 388 |
-
if price_per_bag < 0:
|
| 389 |
-
return "β οΈ Price per bag cannot be negative."
|
| 390 |
-
|
| 391 |
-
# --------------------------------------------------------
|
| 392 |
-
# EMAIL IS REQUIRED BEFORE CALCULATION
|
| 393 |
-
# --------------------------------------------------------
|
| 394 |
-
|
| 395 |
-
if not valid_email(email):
|
| 396 |
-
return "π§ **Email required**\n\nPlease enter your email address before calculating your feed requirement."
|
| 397 |
-
|
| 398 |
-
# --------------------------------------------------------
|
| 399 |
-
# COUNTRY / CURRENCY
|
| 400 |
-
# --------------------------------------------------------
|
| 401 |
-
|
| 402 |
-
country_code = country_code or "NG"
|
| 403 |
-
|
| 404 |
-
if country_code not in CURRENCIES:
|
| 405 |
-
country_code = "NG"
|
| 406 |
-
|
| 407 |
-
currency = currency_for_country(country_code)
|
| 408 |
-
|
| 409 |
-
save_lead(email, country_code, currency)
|
| 410 |
-
|
| 411 |
-
# --------------------------------------------------------
|
| 412 |
-
# TOTAL DAYS
|
| 413 |
-
# --------------------------------------------------------
|
| 414 |
-
|
| 415 |
-
total_days = (weeks * 7) + days
|
| 416 |
-
|
| 417 |
-
# --------------------------------------------------------
|
| 418 |
-
# TOTAL FEED
|
| 419 |
-
# --------------------------------------------------------
|
| 420 |
-
|
| 421 |
-
total_feed_per_bird = cumulative_feed_per_bird(total_days)
|
| 422 |
-
|
| 423 |
-
total_feed_kg = birds * total_feed_per_bird
|
| 424 |
-
|
| 425 |
-
# --------------------------------------------------------
|
| 426 |
-
# STARTER / FINISHER
|
| 427 |
-
# --------------------------------------------------------
|
| 428 |
-
|
| 429 |
-
starter_days = min(total_days, STARTER_END_DAY)
|
| 430 |
-
|
| 431 |
-
starter_per_bird = feed_at_day(starter_days)
|
| 432 |
-
|
| 433 |
-
starter_kg = birds * starter_per_bird
|
| 434 |
-
|
| 435 |
-
finisher_kg = max(total_feed_kg - starter_kg, 0)
|
| 436 |
-
|
| 437 |
-
# --------------------------------------------------------
|
| 438 |
-
# BAGS
|
| 439 |
-
# --------------------------------------------------------
|
| 440 |
-
|
| 441 |
-
starter_bags = starter_kg / bag_size
|
| 442 |
-
finisher_bags = finisher_kg / bag_size
|
| 443 |
-
|
| 444 |
-
total_bags = total_feed_kg / bag_size
|
| 445 |
-
|
| 446 |
-
# Exact estimate, then practical whole bags to purchase.
|
| 447 |
-
# Example: 5.8 bags -> approximately 6 bags
|
| 448 |
-
rounded_bags = math.ceil(total_bags)
|
| 449 |
-
|
| 450 |
-
total_cost = rounded_bags * price_per_bag
|
| 451 |
-
|
| 452 |
-
# --------------------------------------------------------
|
| 453 |
-
# EXPECTED PERFORMANCE
|
| 454 |
-
# --------------------------------------------------------
|
| 455 |
-
|
| 456 |
-
expected_weight = interpolate_weight(total_days)
|
| 457 |
-
flock_weight = expected_weight * birds
|
| 458 |
-
|
| 459 |
-
# --------------------------------------------------------
|
| 460 |
-
# SAVE CALCULATION
|
| 461 |
-
# --------------------------------------------------------
|
| 462 |
-
|
| 463 |
-
save_calculation(
|
| 464 |
-
email=email,
|
| 465 |
-
country=country_code,
|
| 466 |
-
currency=currency,
|
| 467 |
-
birds=birds,
|
| 468 |
-
weeks=weeks,
|
| 469 |
-
days=days,
|
| 470 |
-
total_days=total_days,
|
| 471 |
-
bag_size=bag_size,
|
| 472 |
-
price_per_bag=price_per_bag,
|
| 473 |
-
total_feed_kg=total_feed_kg,
|
| 474 |
-
starter_kg=starter_kg,
|
| 475 |
-
finisher_kg=finisher_kg,
|
| 476 |
-
starter_bags=starter_bags,
|
| 477 |
-
finisher_bags=finisher_bags,
|
| 478 |
-
total_bags=total_bags,
|
| 479 |
-
rounded_bags=rounded_bags,
|
| 480 |
-
total_cost=total_cost,
|
| 481 |
-
)
|
| 482 |
-
|
| 483 |
-
# --------------------------------------------------------
|
| 484 |
-
# OUTPUT β HTML FOR SIDE-BY-SIDE DISPLAY
|
| 485 |
-
# --------------------------------------------------------
|
| 486 |
-
|
| 487 |
-
starter_bags_display = f"{starter_bags:.1f}"
|
| 488 |
-
finisher_bags_display = f"{finisher_bags:.1f}"
|
| 489 |
-
|
| 490 |
-
age_text = f"{weeks} week{'s' if weeks != 1 else ''}"
|
| 491 |
-
if days:
|
| 492 |
-
age_text += f" + {days} day{'s' if days != 1 else ''}"
|
| 493 |
-
|
| 494 |
-
html = f"""
|
| 495 |
-
<div style="font-size: 18px; line-height: 1.6;">
|
| 496 |
-
|
| 497 |
-
<div style="background: #f0fdf4; padding: 16px 20px; border-radius: 12px; border-left: 5px solid #22c55e; margin-bottom: 20px;">
|
| 498 |
-
<div style="font-size: 14px; color: #16a34a; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">π― Quick Answer</div>
|
| 499 |
-
<div style="font-size: 22px; font-weight: 700; color: #166534;">
|
| 500 |
-
{rounded_bags} bags of {bag_size:g} kg feed
|
| 501 |
-
</div>
|
| 502 |
-
<div style="color: #4b5563; margin-top: 4px;">
|
| 503 |
-
To raise <strong>{birds:,} birds</strong> for <strong>{age_text} ({total_days} days)</strong>
|
| 504 |
-
</div>
|
| 505 |
-
</div>
|
| 506 |
-
|
| 507 |
-
<div style="margin-bottom: 16px;">
|
| 508 |
-
<div style="font-size: 14px; color: #6b7280; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">πΎ Feed Breakdown</div>
|
| 509 |
-
<table style="width: 100%; border-collapse: collapse; margin-top: 6px;">
|
| 510 |
-
<tr style="border-bottom: 1px solid #e5e7eb;">
|
| 511 |
-
<td style="padding: 8px 4px;"><strong>π± Starter</strong></td>
|
| 512 |
-
<td style="padding: 8px 4px; text-align: right;">{starter_kg:,.1f} kg</td>
|
| 513 |
-
<td style="padding: 8px 4px; text-align: right; font-weight: 600;">{starter_bags_display} bags</td>
|
| 514 |
-
</tr>
|
| 515 |
-
<tr style="border-bottom: 1px solid #e5e7eb;">
|
| 516 |
-
<td style="padding: 8px 4px;"><strong>π½ Finisher</strong></td>
|
| 517 |
-
<td style="padding: 8px 4px; text-align: right;">{finisher_kg:,.1f} kg</td>
|
| 518 |
-
<td style="padding: 8px 4px; text-align: right; font-weight: 600;">{finisher_bags_display} bags</td>
|
| 519 |
-
</tr>
|
| 520 |
-
<tr>
|
| 521 |
-
<td style="padding: 8px 4px; font-weight: 700;">TOTAL</td>
|
| 522 |
-
<td style="padding: 8px 4px; text-align: right; font-weight: 700;">{total_feed_kg:,.1f} kg</td>
|
| 523 |
-
<td style="padding: 8px 4px; text-align: right; font-weight: 700;">{total_bags:.1f}</td>
|
| 524 |
-
</tr>
|
| 525 |
-
</table>
|
| 526 |
-
</div>
|
| 527 |
-
|
| 528 |
-
<div style="margin-bottom: 16px;">
|
| 529 |
-
<div style="font-size: 14px; color: #6b7280; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">π° Feed Cost</div>
|
| 530 |
-
<div style="font-size: 28px; font-weight: 800; color: #1e40af;">
|
| 531 |
-
{currency}{total_cost:,.0f}
|
| 532 |
-
</div>
|
| 533 |
-
<div style="color: #6b7280; font-size: 14px;">
|
| 534 |
-
{rounded_bags} bags Γ {currency}{price_per_bag:,.0f} per bag
|
| 535 |
-
</div>
|
| 536 |
-
</div>
|
| 537 |
-
|
| 538 |
-
<div style="border-top: 1px solid #e5e7eb; padding-top: 14px;">
|
| 539 |
-
<div style="font-size: 14px; color: #6b7280; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">π Expected Performance</div>
|
| 540 |
-
<table style="width: 100%; border-collapse: collapse; margin-top: 6px;">
|
| 541 |
-
<tr style="border-bottom: 1px solid #e5e7eb;">
|
| 542 |
-
<td style="padding: 6px 4px;">Body weight / bird</td>
|
| 543 |
-
<td style="padding: 6px 4px; text-align: right; font-weight: 600;">{expected_weight:.3f} kg</td>
|
| 544 |
-
</tr>
|
| 545 |
-
<tr>
|
| 546 |
-
<td style="padding: 6px 4px;">Flock live weight</td>
|
| 547 |
-
<td style="padding: 6px 4px; text-align: right; font-weight: 600;">{flock_weight:,.1f} kg</td>
|
| 548 |
-
</tr>
|
| 549 |
-
</table>
|
| 550 |
-
</div>
|
| 551 |
-
|
| 552 |
-
<div style="margin-top: 14px; font-size: 12px; color: #9ca3af; border-top: 1px solid #f3f4f6; padding-top: 10px;">
|
| 553 |
-
Estimate based on Ross 308 benchmark. Actual results may vary.
|
| 554 |
-
</div>
|
| 555 |
-
</div>
|
| 556 |
-
"""
|
| 557 |
-
|
| 558 |
-
return html
|
| 559 |
-
|
| 560 |
-
|
| 561 |
-
# ============================================================
|
| 562 |
-
# QUESTION / ANSWER
|
| 563 |
-
# ============================================================
|
| 564 |
-
|
| 565 |
-
def answer_farmer_question(question, email):
|
| 566 |
-
if not question or not question.strip():
|
| 567 |
-
return "Please type or speak your question first."
|
| 568 |
-
|
| 569 |
-
# Automatically save whenever Get Answer is clicked.
|
| 570 |
-
save_question(email or "", question)
|
| 571 |
-
|
| 572 |
-
q = question.lower().strip()
|
| 573 |
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
|
|
|
|
|
|
|
| 577 |
|
| 578 |
-
|
| 579 |
-
|
| 580 |
-
|
|
|
|
|
|
|
|
|
|
| 581 |
|
| 582 |
-
|
|
|
|
| 583 |
|
| 584 |
-
|
|
|
|
|
|
|
| 585 |
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
| 589 |
-
|
| 590 |
-
|
| 591 |
|
| 592 |
-
|
| 593 |
-
|
|
|
|
| 594 |
|
| 595 |
-
|
| 596 |
-
|
| 597 |
-
### πΎ Starter feed
|
| 598 |
|
| 599 |
-
|
|
|
|
| 600 |
|
| 601 |
-
|
| 602 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 603 |
|
| 604 |
-
|
| 605 |
-
|
| 606 |
-
|
| 607 |
|
| 608 |
-
|
|
|
|
| 609 |
|
| 610 |
-
|
| 611 |
-
"""
|
| 612 |
|
| 613 |
-
|
| 614 |
-
answer = """
|
| 615 |
-
### π° Feed cost
|
| 616 |
|
| 617 |
-
|
| 618 |
|
| 619 |
-
|
| 620 |
|
| 621 |
-
**
|
| 622 |
|
| 623 |
-
|
| 624 |
-
"""
|
| 625 |
|
| 626 |
-
|
| 627 |
-
answer = """
|
| 628 |
-
### π Expected weight
|
| 629 |
|
| 630 |
-
|
| 631 |
-
- Expected body weight per bird
|
| 632 |
-
- Expected flock live weight
|
| 633 |
|
| 634 |
-
|
| 635 |
-
"""
|
| 636 |
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
|
|
|
|
|
|
| 640 |
|
| 641 |
-
|
| 642 |
|
| 643 |
-
|
| 644 |
-
- Selling price
|
| 645 |
-
- Feed cost
|
| 646 |
-
- Chick cost
|
| 647 |
-
- Medication
|
| 648 |
-
- Vaccination
|
| 649 |
-
- Labour
|
| 650 |
-
- Mortality
|
| 651 |
-
- Other farm expenses
|
| 652 |
|
| 653 |
-
|
| 654 |
-
"""
|
| 655 |
|
| 656 |
-
|
| 657 |
-
answer = f"""
|
| 658 |
-
### π‘ Question received
|
| 659 |
|
| 660 |
-
|
| 661 |
|
| 662 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 663 |
|
| 664 |
-
|
| 665 |
"""
|
| 666 |
|
| 667 |
-
return
|
| 668 |
-
|
| 669 |
-
|
| 670 |
-
# ============================================================
|
| 671 |
-
# COUNTRY / CURRENCY UI
|
| 672 |
-
# ============================================================
|
| 673 |
|
| 674 |
-
def
|
| 675 |
-
|
| 676 |
-
|
|
|
|
|
|
|
|
|
|
| 677 |
|
| 678 |
-
|
| 679 |
-
|
| 680 |
-
|
| 681 |
-
DEFAULT_COUNTRY, DEFAULT_CURRENCY = get_country_info()
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
# ============================================================
|
| 685 |
-
# CSS
|
| 686 |
-
# ============================================================
|
| 687 |
|
| 688 |
CSS = """
|
| 689 |
-
body {
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
max-width: 1200px !important;
|
| 695 |
-
margin: auto !important;
|
| 696 |
-
}
|
| 697 |
-
|
| 698 |
-
.main-title {
|
| 699 |
-
text-align: center;
|
| 700 |
-
padding: 15px 10px 5px 10px;
|
| 701 |
}
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
|
| 705 |
-
margin-bottom: 4px;
|
| 706 |
-
}
|
| 707 |
-
|
| 708 |
-
.main-title p {
|
| 709 |
-
font-size: 17px;
|
| 710 |
-
color: #555;
|
| 711 |
-
}
|
| 712 |
-
|
| 713 |
-
.answer-box {
|
| 714 |
-
background: white;
|
| 715 |
-
border-radius: 16px;
|
| 716 |
-
padding: 16px 20px;
|
| 717 |
-
border: 1px solid #e5e7eb;
|
| 718 |
-
box-shadow: 0 1px 3px rgba(0,0,0,0.05);
|
| 719 |
-
min-height: 400px;
|
| 720 |
-
}
|
| 721 |
-
|
| 722 |
-
.email-box {
|
| 723 |
-
border: 2px solid #d9e7d2;
|
| 724 |
-
border-radius: 12px;
|
| 725 |
-
padding: 10px;
|
| 726 |
-
background: #fbfdf9;
|
| 727 |
-
}
|
| 728 |
-
|
| 729 |
-
button {
|
| 730 |
-
min-height: 48px !important;
|
| 731 |
}
|
| 732 |
"""
|
| 733 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 734 |
|
| 735 |
-
|
| 736 |
-
|
| 737 |
-
#
|
| 738 |
-
|
| 739 |
-
with gr.Blocks(title=APP_NAME, css=CSS) as demo:
|
| 740 |
-
|
| 741 |
-
gr.HTML(
|
| 742 |
-
"""
|
| 743 |
-
<div class="main-title">
|
| 744 |
-
<h1>π Poultry Feed Calculator</h1>
|
| 745 |
-
<p>
|
| 746 |
-
Know exactly how much feed you need before buying.
|
| 747 |
-
</p>
|
| 748 |
-
</div>
|
| 749 |
-
"""
|
| 750 |
-
)
|
| 751 |
-
|
| 752 |
-
# --------------------------------------------------------
|
| 753 |
-
# TWO-COLUMN LAYOUT: INPUTS | RESULTS
|
| 754 |
-
# --------------------------------------------------------
|
| 755 |
-
|
| 756 |
-
with gr.Row(equal_height=True):
|
| 757 |
-
|
| 758 |
-
# ---------- LEFT COLUMN: INPUTS ----------
|
| 759 |
-
with gr.Column(scale=1, min_width=350):
|
| 760 |
-
|
| 761 |
-
with gr.Group():
|
| 762 |
-
gr.Markdown("## π 1. Number of birds")
|
| 763 |
-
|
| 764 |
-
birds = gr.Dropdown(
|
| 765 |
-
choices=[
|
| 766 |
-
25, 50, 75, 100, 150, 200, 500, 1000,
|
| 767 |
-
],
|
| 768 |
-
value=50,
|
| 769 |
-
label="Number of birds",
|
| 770 |
-
allow_custom_value=True,
|
| 771 |
-
)
|
| 772 |
-
|
| 773 |
-
gr.Markdown("## π
2. Age")
|
| 774 |
-
|
| 775 |
-
with gr.Row():
|
| 776 |
-
weeks = gr.Dropdown(
|
| 777 |
-
choices=list(range(0, 21)),
|
| 778 |
-
value=6,
|
| 779 |
-
label="Weeks",
|
| 780 |
-
)
|
| 781 |
-
days = gr.Dropdown(
|
| 782 |
-
choices=list(range(0, 7)),
|
| 783 |
-
value=0,
|
| 784 |
-
label="+ Days",
|
| 785 |
-
)
|
| 786 |
-
|
| 787 |
-
gr.Markdown("## πΎ 3. Feed")
|
| 788 |
-
|
| 789 |
-
with gr.Row():
|
| 790 |
-
bag_size = gr.Dropdown(
|
| 791 |
-
choices=[10, 20, 25, 50],
|
| 792 |
-
value=25,
|
| 793 |
-
label="Bag size (kg)",
|
| 794 |
-
)
|
| 795 |
-
price = gr.Number(
|
| 796 |
-
value=24000,
|
| 797 |
-
label=f"Price per bag ({DEFAULT_CURRENCY})",
|
| 798 |
-
minimum=0,
|
| 799 |
-
)
|
| 800 |
-
|
| 801 |
-
gr.Markdown("## π§ 4. Your email")
|
| 802 |
-
|
| 803 |
email = gr.Textbox(
|
| 804 |
-
label="",
|
| 805 |
placeholder="farmer@example.com",
|
| 806 |
type="email",
|
| 807 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 808 |
)
|
| 809 |
|
| 810 |
calculate_btn = gr.Button(
|
| 811 |
-
"π Calculate",
|
| 812 |
variant="primary",
|
| 813 |
size="lg",
|
| 814 |
)
|
| 815 |
|
| 816 |
-
|
| 817 |
-
country_state = gr.State(DEFAULT_COUNTRY)
|
| 818 |
-
|
| 819 |
-
# ---------- RIGHT COLUMN: RESULTS ----------
|
| 820 |
-
with gr.Column(scale=1, min_width=350):
|
| 821 |
-
|
| 822 |
gr.Markdown("## π― Quick Answer")
|
| 823 |
-
|
| 824 |
-
|
| 825 |
-
|
| 826 |
-
<div style="font-size: 16px; color: #9ca3af; text-align: center; padding: 40px 20px;">
|
| 827 |
-
<div style="font-size: 48px; margin-bottom: 12px;">π</div>
|
| 828 |
-
<div>Enter your details and click <strong>Calculate</strong></div>
|
| 829 |
-
<div style="font-size: 14px; margin-top: 8px;">Your feed estimate will appear here</div>
|
| 830 |
-
</div>
|
| 831 |
-
""",
|
| 832 |
-
elem_classes=["answer-box"],
|
| 833 |
)
|
| 834 |
|
| 835 |
-
# --------------------------------------------------------
|
| 836 |
-
# QUESTION SECTION (Below the two columns)
|
| 837 |
-
# --------------------------------------------------------
|
| 838 |
-
|
| 839 |
gr.Markdown("---")
|
|
|
|
|
|
|
|
|
|
| 840 |
|
| 841 |
with gr.Row():
|
| 842 |
-
|
| 843 |
-
|
| 844 |
-
|
| 845 |
-
|
| 846 |
-
|
| 847 |
-
|
| 848 |
-
|
| 849 |
-
)
|
| 850 |
-
|
| 851 |
-
question = gr.Textbox(
|
| 852 |
-
label="",
|
| 853 |
-
placeholder="Type or speak your question...",
|
| 854 |
-
lines=3,
|
| 855 |
-
)
|
| 856 |
-
|
| 857 |
-
ask_btn = gr.Button(
|
| 858 |
-
"π¬ Get Answer",
|
| 859 |
-
variant="secondary",
|
| 860 |
-
size="lg",
|
| 861 |
-
)
|
| 862 |
-
|
| 863 |
-
question_answer = gr.Markdown("")
|
| 864 |
|
| 865 |
-
|
| 866 |
-
# BENCHMARK REFERENCE
|
| 867 |
-
# --------------------------------------------------------
|
| 868 |
|
| 869 |
gr.Markdown("---")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 870 |
|
| 871 |
-
|
| 872 |
-
|
| 873 |
-
gr.Markdown(
|
| 874 |
-
"""
|
| 875 |
-
### π Ross 308 Weekly Benchmark
|
| 876 |
-
|
| 877 |
-
| Days | Body Weight (kg) | Cum Feed (kg) |
|
| 878 |
-
|------|------------------|---------------|
|
| 879 |
-
| 7 | 0.213 | 0.213 |
|
| 880 |
-
| 14 | 0.533 | 0.533 |
|
| 881 |
-
| 21 | 1.012 | 1.012 |
|
| 882 |
-
| 28 | 1.616 | 1.616 |
|
| 883 |
-
| 35 | 2.296 | 2.296 |
|
| 884 |
-
| 42 | 2.998 | 2.998 |
|
| 885 |
-
| 49 | 3.681 | 3.681 |
|
| 886 |
-
| 56 | 4.316 | 4.316 |
|
| 887 |
-
|
| 888 |
-
*Source: Aviagen Ross 308 (2022)*
|
| 889 |
-
"""
|
| 890 |
-
)
|
| 891 |
-
|
| 892 |
-
with gr.Column(scale=1):
|
| 893 |
-
gr.Markdown(
|
| 894 |
-
"""
|
| 895 |
-
### π How it works
|
| 896 |
-
|
| 897 |
-
- **Weeks + Days** β total days
|
| 898 |
-
- **Starter** β Day 0β21
|
| 899 |
-
- **Finisher** β Day 22+
|
| 900 |
-
- **Bags** β rounded up to next whole bag
|
| 901 |
-
- **Ross 308 benchmark** used for estimates
|
| 902 |
-
|
| 903 |
-
β οΈ Actual results may vary with management, feed quality, and other conditions.
|
| 904 |
-
"""
|
| 905 |
-
)
|
| 906 |
|
| 907 |
-
|
| 908 |
-
#
|
| 909 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 910 |
|
| 911 |
calculate_btn.click(
|
| 912 |
-
|
| 913 |
-
inputs=[
|
| 914 |
-
|
| 915 |
-
weeks,
|
| 916 |
-
days,
|
| 917 |
-
bag_size,
|
| 918 |
-
price,
|
| 919 |
-
email,
|
| 920 |
-
country_state,
|
| 921 |
-
],
|
| 922 |
-
outputs=result,
|
| 923 |
)
|
| 924 |
|
|
|
|
| 925 |
ask_btn.click(
|
| 926 |
-
|
| 927 |
-
inputs=[
|
| 928 |
-
|
| 929 |
-
email,
|
| 930 |
-
],
|
| 931 |
-
outputs=question_answer,
|
| 932 |
)
|
| 933 |
|
| 934 |
-
|
| 935 |
-
|
| 936 |
-
|
| 937 |
-
|
|
|
|
|
|
|
| 938 |
|
| 939 |
if __name__ == "__main__":
|
| 940 |
-
|
| 941 |
port = int(os.environ.get("PORT", "7860"))
|
| 942 |
-
|
| 943 |
demo.launch(
|
| 944 |
server_name="0.0.0.0",
|
| 945 |
server_port=port,
|
| 946 |
-
|
| 947 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
|
| 3 |
+
app_code = r'''
|
| 4 |
import os
|
| 5 |
+
import csv
|
| 6 |
import math
|
| 7 |
import re
|
| 8 |
+
from datetime import datetime, timezone
|
|
|
|
| 9 |
from pathlib import Path
|
| 10 |
|
| 11 |
import gradio as gr
|
| 12 |
|
| 13 |
+
APP_NAME = "PoultryFeedCalc"
|
| 14 |
+
DATA_DIR = Path(os.environ.get("DATA_DIR", "/data"))
|
| 15 |
+
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
| 16 |
+
LEADS_FILE = DATA_DIR / "farmer_leads.csv"
|
| 17 |
+
QUESTIONS_FILE = DATA_DIR / "farmer_questions.csv"
|
| 18 |
+
|
| 19 |
+
# ---------------------------------------------------------------------
|
| 20 |
+
# BREED BENCHMARKS
|
| 21 |
+
# Source basis: manufacturer 2022 "As-Hatched" performance objectives.
|
| 22 |
+
# Values are grams/bird at the stated day.
|
| 23 |
+
# For days between the published checkpoints, the app linearly
|
| 24 |
+
# interpolates between the adjacent daily benchmark values.
|
| 25 |
+
# ---------------------------------------------------------------------
|
| 26 |
+
|
| 27 |
+
# Ross 308 AP - As-Hatched, 2022
|
| 28 |
+
# Daily body weight and cumulative feed intake, days 0-56.
|
| 29 |
+
ROSS_WEIGHT_G = [
|
| 30 |
+
44, 62, 80, 101, 125, 151, 181, 214, 250, 289, 332, 377, 426, 479,
|
| 31 |
+
537, 600, 668, 740, 818, 900, 986, 1076, 1170, 1268, 1370, 1476,
|
| 32 |
+
1586, 1700, 1818, 1940, 2066, 2196, 2330, 2468, 2610, 2756, 2906,
|
| 33 |
+
3060, 3218, 3380, 3546, 3716, 3890, 4068, 4250, 4436, 4626, 4819,
|
| 34 |
+
5016, 5217, 5422, 5631, 5843, 6060, 6280, 6503, 6730, 6961, 7195
|
| 35 |
+
]
|
| 36 |
+
ROSS_CUM_FEED_G = [
|
| 37 |
+
0, 12, 28, 48, 71, 98, 130, 165, 204, 245, 290, 337, 387, 441,
|
| 38 |
+
498, 560, 624, 691, 762, 838, 917, 1000, 1086, 1176, 1270, 1368,
|
| 39 |
+
1469, 1574, 1683, 1795, 1910, 2027, 2147, 2270, 2396, 2525, 2657,
|
| 40 |
+
2792, 2930, 3071, 3215, 3362, 3512, 3665, 3821, 3980, 4142, 4307,
|
| 41 |
+
4475, 4646, 4820, 4997, 5177, 5359, 5544, 5732, 5923
|
| 42 |
+
]
|
| 43 |
+
|
| 44 |
+
# Cobb 500 - As-Hatched, 2022
|
| 45 |
+
# Daily values are taken from the published metric table.
|
| 46 |
+
COBB_WEIGHT_G = [
|
| 47 |
+
42, 55, 71, 90, 112, 138, 168, 202, 240, 283, 330, 382, 440, 503,
|
| 48 |
+
570, 639, 711, 786, 864, 945, 1029, 1116, 1205, 1296, 1390, 1486,
|
| 49 |
+
1583, 1682, 1783, 1886, 1989, 2094, 2200, 2306, 2413, 2521, 2629,
|
| 50 |
+
2738, 2846, 2954, 3062, 3170, 3278, 3384, 3490, 3595, 3699, 3801,
|
| 51 |
+
3902, 4001, 4099, 4195, 4289, 4380, 4470, 4557, 4641
|
| 52 |
+
]
|
| 53 |
+
COBB_CUM_FEED_G = [
|
| 54 |
+
0, 0, 0, 0, 0, 0, 0, 180, 220, 264, 314, 371, 435, 508,
|
| 55 |
+
588, 672, 763, 861, 966, 1077, 1195, 1320, 1451, 1588, 1731,
|
| 56 |
+
1880, 2034, 2194, 2359, 2528, 2702, 2880, 3063, 3250, 3441, 3635,
|
| 57 |
+
3833, 4035, 4241, 4450, 4663, 4880, 5100, 5324, 5552, 5784, 6020,
|
| 58 |
+
6259, 6502, 6749, 6999, 7252, 7508, 7766, 8026, 8287, 8549
|
| 59 |
+
]
|
| 60 |
+
# Cobb's published table does not list cumulative feed for days 1-6.
|
| 61 |
+
# The calculator therefore uses the first published cumulative-feed
|
| 62 |
+
# checkpoint (day 7) for feed calculations in that early period.
|
| 63 |
+
|
| 64 |
+
# Arbor Acres Plus - As-Hatched, 2022.
|
| 65 |
+
# Daily values transcribed from the published objective table.
|
| 66 |
+
AA_WEIGHT_G = [
|
| 67 |
+
43, 60, 78, 99, 123, 149, 179, 209, 246, 287, 333, 382, 435, 492,
|
| 68 |
+
527, 596, 666, 736, 808, 881, 956, 1006, 1089, 1174, 1260, 1347,
|
| 69 |
+
1435, 1523, 1611, 1710, 1809, 1908, 2008, 2107, 2206, 2297, 2386,
|
| 70 |
+
2486, 2585, 2684, 2784, 2882, 2981, 3079, 3176, 3272, 3368, 3462,
|
| 71 |
+
3556, 3649, 3740, 3831, 3921, 4010, 4098, 4185, 4272
|
| 72 |
+
]
|
| 73 |
+
AA_CUM_FEED_G = [
|
| 74 |
+
0, 12, 28, 48, 71, 98, 130, 168, 210, 257, 308, 363, 422, 486,
|
| 75 |
+
538, 605, 675, 749, 827, 909, 994, 1163, 1253, 1350, 1453, 1561,
|
| 76 |
+
1673, 1789, 1909, 2033, 2161, 2293, 2429, 2569, 2713, 2862, 3015,
|
| 77 |
+
3173, 3336, 3503, 3673, 3849, 4029, 4214, 4405, 4601, 4801, 5006,
|
| 78 |
+
5216, 5429, 5648, 5869, 6093, 6320, 6550, 6783, 7019
|
| 79 |
+
]
|
| 80 |
+
|
| 81 |
+
BREEDS = {
|
| 82 |
+
"Ross 308 AP": (ROSS_WEIGHT_G, ROSS_CUM_FEED_G),
|
| 83 |
+
"Cobb 500": (COBB_WEIGHT_G, COBB_CUM_FEED_G),
|
| 84 |
+
"Arbor Acres Plus": (AA_WEIGHT_G, AA_CUM_FEED_G),
|
| 85 |
}
|
| 86 |
|
| 87 |
+
# Currency defaults. Country detection uses proxy headers when available.
|
| 88 |
+
CURRENCY_BY_COUNTRY = {
|
| 89 |
+
"NG": ("β¦", "NGN", 1.0),
|
| 90 |
+
"GH": ("β΅", "GHS", 0.00072),
|
| 91 |
+
"KE": ("KSh", "KES", 0.00047),
|
| 92 |
+
"UG": ("USh", "UGX", 0.00093),
|
| 93 |
+
"TZ": ("TSh", "TZS", 0.00061),
|
| 94 |
+
"ZA": ("R", "ZAR", 0.00055),
|
| 95 |
+
"ZM": ("ZK", "ZMW", 0.00071),
|
| 96 |
+
"US": ("$", "USD", 0.00062),
|
| 97 |
+
"GB": ("Β£", "GBP", 0.00049),
|
| 98 |
+
"CA": ("C$", "CAD", 0.00084),
|
| 99 |
+
"AU": ("A$", "AUD", 0.00095),
|
| 100 |
}
|
| 101 |
|
| 102 |
+
# These are only used to suggest a local currency and convert the entered
|
| 103 |
+
# local bag price into the app's internal NGN-equivalent benchmark.
|
| 104 |
+
# The farmer can always edit the price per bag.
|
| 105 |
+
def detect_country(request: gr.Request | None) -> str:
|
| 106 |
+
if request is not None:
|
| 107 |
+
headers = request.headers or {}
|
| 108 |
+
for key in ("x-country", "cf-ipcountry", "x-vercel-ip-country", "x-country-code"):
|
| 109 |
+
value = headers.get(key)
|
| 110 |
+
if value and len(value) == 2:
|
| 111 |
+
return value.upper()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 112 |
return "NG"
|
| 113 |
|
| 114 |
+
def money(value: float, symbol: str) -> str:
|
| 115 |
+
return f"{symbol}{value:,.0f}"
|
| 116 |
+
|
| 117 |
+
def interpolate(values, day_float):
|
| 118 |
+
if day_float <= 0:
|
| 119 |
+
return float(values[0])
|
| 120 |
+
max_day = len(values) - 1
|
| 121 |
+
if day_float >= max_day:
|
| 122 |
+
return float(values[-1])
|
| 123 |
+
lo = int(math.floor(day_float))
|
| 124 |
+
hi = lo + 1
|
| 125 |
+
frac = day_float - lo
|
| 126 |
+
return values[lo] + (values[hi] - values[lo]) * frac
|
| 127 |
+
|
| 128 |
+
def feed_at_day(feed_values, day_float, breed):
|
| 129 |
+
# Cobb has no published cumulative-feed entries for days 1-6.
|
| 130 |
+
# Use the first published point rather than inventing early cumulative
|
| 131 |
+
# feed values. For days <= 7, use a proportional interpolation from
|
| 132 |
+
# hatch to day 7 as a transparent estimate.
|
| 133 |
+
if breed == "Cobb 500" and day_float < 7:
|
| 134 |
+
return 180.0 * (day_float / 7.0)
|
| 135 |
+
return interpolate(feed_values, day_float)
|
| 136 |
+
|
| 137 |
+
def age_label(weeks, days):
|
| 138 |
+
total = weeks * 7 + days
|
| 139 |
+
return f"{weeks} week{'s' if weeks != 1 else ''} + {days} day{'s' if days != 1 else ''} ({total} days)"
|
| 140 |
+
|
| 141 |
+
def ensure_file(path: Path, header):
|
| 142 |
+
if not path.exists():
|
| 143 |
+
with path.open("w", newline="", encoding="utf-8") as f:
|
| 144 |
+
csv.writer(f).writerow(header)
|
| 145 |
+
|
| 146 |
+
ensure_file(LEADS_FILE, ["timestamp_utc", "email", "country", "currency"])
|
| 147 |
+
ensure_file(QUESTIONS_FILE, ["timestamp_utc", "email", "question"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
def save_lead(email, country, currency):
|
| 150 |
+
email = (email or "").strip().lower()
|
| 151 |
+
if not re.fullmatch(r"[^@\s]+@[^@\s]+\.[^@\s]+", email):
|
| 152 |
+
return False
|
| 153 |
+
with LEADS_FILE.open("a", newline="", encoding="utf-8") as f:
|
| 154 |
+
csv.writer(f).writerow([
|
| 155 |
+
datetime.now(timezone.utc).isoformat(),
|
| 156 |
+
email,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
country,
|
| 158 |
currency,
|
| 159 |
+
])
|
| 160 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
def save_question(email, question):
|
| 163 |
+
email = (email or "").strip().lower()
|
| 164 |
+
question = (question or "").strip()
|
| 165 |
+
if not question:
|
| 166 |
+
return False
|
| 167 |
+
with QUESTIONS_FILE.open("a", newline="", encoding="utf-8") as f:
|
| 168 |
+
csv.writer(f).writerow([
|
| 169 |
+
datetime.now(timezone.utc).isoformat(),
|
| 170 |
+
email,
|
| 171 |
+
question,
|
| 172 |
+
])
|
| 173 |
+
return True
|
| 174 |
+
|
| 175 |
+
def country_currency(request):
|
| 176 |
+
country = detect_country(request)
|
| 177 |
+
symbol, code, _ = CURRENCY_BY_COUNTRY.get(country, ("$", "USD", 0.00062))
|
| 178 |
+
return country, symbol, code
|
| 179 |
+
|
| 180 |
+
def update_currency(request):
|
| 181 |
+
country, symbol, code = country_currency(request)
|
| 182 |
+
default_price = 24000 if code == "NGN" else 0
|
| 183 |
+
return (
|
| 184 |
+
gr.update(value=f"{symbol} {code}"),
|
| 185 |
+
gr.update(value=default_price),
|
| 186 |
+
f"Detected country: **{country}** Β· Currency: **{symbol} {code}**"
|
| 187 |
)
|
| 188 |
|
| 189 |
+
def calculate(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
email,
|
| 191 |
+
breed,
|
|
|
|
| 192 |
birds,
|
| 193 |
weeks,
|
| 194 |
days,
|
|
|
|
| 195 |
bag_size,
|
| 196 |
price_per_bag,
|
| 197 |
+
request: gr.Request | None = None,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
):
|
| 199 |
+
email = (email or "").strip().lower()
|
|
|
|
| 200 |
|
| 201 |
+
if not re.fullmatch(r"[^@\s]+@[^@\s]+\.[^@\s]+", email):
|
| 202 |
+
return (
|
| 203 |
+
gr.update(value="### Email required\nPlease enter a valid email address before calculating."),
|
| 204 |
+
gr.update(visible=True),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 206 |
|
| 207 |
try:
|
| 208 |
birds = int(birds)
|
|
|
|
| 211 |
bag_size = float(bag_size)
|
| 212 |
price_per_bag = float(price_per_bag)
|
| 213 |
except Exception:
|
| 214 |
+
return (
|
| 215 |
+
gr.update(value="### Please check your inputs\nEnter valid numbers."),
|
| 216 |
+
gr.update(visible=True),
|
| 217 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
+
if birds <= 0 or weeks < 0 or days < 0 or days > 6 or bag_size <= 0 or price_per_bag < 0:
|
| 220 |
+
return (
|
| 221 |
+
gr.update(value="### Please check your inputs\nThe age days must be 0β6 and all quantities must be positive."),
|
| 222 |
+
gr.update(visible=True),
|
| 223 |
+
)
|
| 224 |
|
| 225 |
+
total_days = weeks * 7 + days
|
| 226 |
+
if total_days > 56:
|
| 227 |
+
return (
|
| 228 |
+
gr.update(value="### Maximum benchmark age is 8 weeks (56 days).\nPlease select an age from 0β56 days."),
|
| 229 |
+
gr.update(visible=True),
|
| 230 |
+
)
|
| 231 |
|
| 232 |
+
country, symbol, code = country_currency(request)
|
| 233 |
+
save_lead(email, country, code)
|
| 234 |
|
| 235 |
+
weight_values, feed_values = BREEDS[breed]
|
| 236 |
+
weight_g = interpolate(weight_values, total_days)
|
| 237 |
+
cumulative_g = feed_at_day(feed_values, total_days, breed)
|
| 238 |
|
| 239 |
+
# Starter is the benchmark cumulative feed through day 21.
|
| 240 |
+
starter_cutoff = min(total_days, 21)
|
| 241 |
+
starter_g_per_bird = feed_at_day(feed_values, starter_cutoff, breed)
|
| 242 |
+
total_g_per_bird = cumulative_g
|
| 243 |
+
finisher_g_per_bird = max(0.0, total_g_per_bird - starter_g_per_bird)
|
| 244 |
|
| 245 |
+
total_feed_kg = birds * total_g_per_bird / 1000
|
| 246 |
+
starter_kg = birds * starter_g_per_bird / 1000
|
| 247 |
+
finisher_kg = birds * finisher_g_per_bird / 1000
|
| 248 |
|
| 249 |
+
exact_bags = total_feed_kg / bag_size
|
| 250 |
+
whole_bags = math.ceil(exact_bags)
|
|
|
|
| 251 |
|
| 252 |
+
starter_exact_bags = starter_kg / bag_size
|
| 253 |
+
finisher_exact_bags = finisher_kg / bag_size
|
| 254 |
|
| 255 |
+
# Purchase bags are rounded separately by feed type, because farmers
|
| 256 |
+
# normally buy whole bags of starter and finisher.
|
| 257 |
+
starter_buy = math.ceil(starter_exact_bags) if starter_kg > 0 else 0
|
| 258 |
+
finisher_buy = math.ceil(finisher_exact_bags) if finisher_kg > 0 else 0
|
| 259 |
+
purchase_bags = starter_buy + finisher_buy
|
| 260 |
+
purchase_cost = purchase_bags * price_per_bag
|
| 261 |
|
| 262 |
+
# The feed quantity itself is exact benchmark estimate; only the
|
| 263 |
+
# purchasable whole-bag quantity is rounded up.
|
| 264 |
+
expected_flock_weight_kg = birds * weight_g / 1000
|
| 265 |
|
| 266 |
+
result = f"""
|
| 267 |
+
# π Your Feed Requirement
|
| 268 |
|
| 269 |
+
## To raise **{birds:,} {breed} birds** to **{age_label(weeks, days)}**
|
|
|
|
| 270 |
|
| 271 |
+
### π― Quick answer
|
|
|
|
|
|
|
| 272 |
|
| 273 |
+
**You will require an estimated {exact_bags:.2f} bags of {bag_size:g} kg feed.**
|
| 274 |
|
| 275 |
+
Because feed is sold in whole bags, **buy approximately {purchase_bags} bags**:
|
| 276 |
|
| 277 |
+
**{starter_buy} Starter + {finisher_buy} Finisher**
|
| 278 |
|
| 279 |
+
### π° Estimated feed cost
|
|
|
|
| 280 |
|
| 281 |
+
**Approximately {money(purchase_cost, symbol)}**
|
|
|
|
|
|
|
| 282 |
|
| 283 |
+
This is the estimated cost of **feed alone**, using **{money(price_per_bag, symbol)} per {bag_size:g}-kg bag**.
|
|
|
|
|
|
|
| 284 |
|
| 285 |
+
### πΎ Feed breakdown
|
|
|
|
| 286 |
|
| 287 |
+
| Feed | Estimated quantity | Exact bag equivalent | Whole bags to buy |
|
| 288 |
+
|---|---:|---:|---:|
|
| 289 |
+
| π± Starter | **{starter_kg:.1f} kg** | {starter_exact_bags:.2f} | **{starter_buy}** |
|
| 290 |
+
| π½ Finisher | **{finisher_kg:.1f} kg** | {finisher_exact_bags:.2f} | **{finisher_buy}** |
|
| 291 |
+
| **TOTAL** | **{total_feed_kg:.1f} kg** | **{exact_bags:.2f}** | **{purchase_bags}** |
|
| 292 |
|
| 293 |
+
### π Expected benchmark weight
|
| 294 |
|
| 295 |
+
**{weight_g/1000:.2f} kg per bird**
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 296 |
|
| 297 |
+
Expected live weight of the flock:
|
|
|
|
| 298 |
|
| 299 |
+
**{expected_flock_weight_kg:.1f} kg**
|
|
|
|
|
|
|
| 300 |
|
| 301 |
+
### π Breed benchmark
|
| 302 |
|
| 303 |
+
- Breed: **{breed}**
|
| 304 |
+
- Age: **{total_days} days**
|
| 305 |
+
- Cumulative benchmark feed: **{cumulative_g/1000:.3f} kg/bird**
|
| 306 |
+
- No artificial 20% allowance has been added.
|
| 307 |
+
- The bag quantity is rounded **up only because bags are purchased whole**.
|
| 308 |
|
| 309 |
+
> **Important:** These are manufacturer performance objectives used as a benchmark. Actual farm consumption and growth can differ with genetics, sex, mortality, feed quality, temperature, health and management.
|
| 310 |
"""
|
| 311 |
|
| 312 |
+
return gr.update(value=result), gr.update(visible=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
|
| 314 |
+
def ask_question(email, question):
|
| 315 |
+
if not email or not re.fullmatch(r"[^@\s]+@[^@\s]+\.[^@\s]+", email.strip()):
|
| 316 |
+
return "Please enter your email above first so we can attach the question to your farmer record."
|
| 317 |
+
if save_question(email, question):
|
| 318 |
+
return "β
Question saved. We will use farmer questions to guide future upgrades."
|
| 319 |
+
return "Please type a question first."
|
| 320 |
|
| 321 |
+
# ---------------------------------------------------------------------
|
| 322 |
+
# UI
|
| 323 |
+
# ---------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 324 |
|
| 325 |
CSS = """
|
| 326 |
+
body { background: #f7f7f7; }
|
| 327 |
+
.gradio-container { max-width: 1250px !important; }
|
| 328 |
+
.hero {
|
| 329 |
+
padding: 22px 26px; border-radius: 16px; background: white;
|
| 330 |
+
border: 1px solid #e6e6e6; margin-bottom: 18px;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
}
|
| 332 |
+
.answer {
|
| 333 |
+
background: white; border-radius: 16px; padding: 20px;
|
| 334 |
+
border: 1px solid #e6e6e6;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
}
|
| 336 |
"""
|
| 337 |
|
| 338 |
+
with gr.Blocks(title=APP_NAME) as demo:
|
| 339 |
+
gr.HTML("""
|
| 340 |
+
<div class="hero">
|
| 341 |
+
<h1>π PoultryFeedCalc</h1>
|
| 342 |
+
<h3>Simple broiler feed calculator for farmers</h3>
|
| 343 |
+
<p>Select the breed, number of birds, age, bag size and price.
|
| 344 |
+
<b>No calculations required.</b></p>
|
| 345 |
+
</div>
|
| 346 |
+
""")
|
| 347 |
|
| 348 |
+
with gr.Row():
|
| 349 |
+
with gr.Column(scale=1):
|
| 350 |
+
gr.Markdown("## π§ Before you calculate")
|
| 351 |
+
gr.Markdown("Enter your email so we can send PoultryFeedCalc updates and improvements. Your email is used for PoultryFeedCalc communications.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
email = gr.Textbox(
|
| 353 |
+
label="Email address *",
|
| 354 |
placeholder="farmer@example.com",
|
| 355 |
type="email",
|
| 356 |
+
)
|
| 357 |
+
|
| 358 |
+
country_status = gr.Markdown("Detecting country and currencyβ¦")
|
| 359 |
+
currency_display = gr.Textbox(label="Detected currency", interactive=False)
|
| 360 |
+
|
| 361 |
+
gr.Markdown("## π Farm inputs")
|
| 362 |
+
breed = gr.Dropdown(
|
| 363 |
+
choices=list(BREEDS.keys()),
|
| 364 |
+
value="Ross 308 AP",
|
| 365 |
+
label="Broiler breed",
|
| 366 |
+
)
|
| 367 |
+
birds = gr.Dropdown(
|
| 368 |
+
choices=[25, 50, 75, 100, 150, 200, 500, 1000],
|
| 369 |
+
value=50,
|
| 370 |
+
allow_custom_value=True,
|
| 371 |
+
label="Number of birds",
|
| 372 |
+
)
|
| 373 |
+
with gr.Row():
|
| 374 |
+
weeks = gr.Dropdown(
|
| 375 |
+
choices=list(range(0, 9)),
|
| 376 |
+
value=6,
|
| 377 |
+
label="Age β Weeks",
|
| 378 |
+
)
|
| 379 |
+
days = gr.Dropdown(
|
| 380 |
+
choices=list(range(0, 7)),
|
| 381 |
+
value=0,
|
| 382 |
+
label="Age β Days",
|
| 383 |
+
)
|
| 384 |
+
bag_size = gr.Dropdown(
|
| 385 |
+
choices=[10, 20, 25, 50],
|
| 386 |
+
value=25,
|
| 387 |
+
label="Feed bag size (kg)",
|
| 388 |
+
)
|
| 389 |
+
price = gr.Number(
|
| 390 |
+
value=24000,
|
| 391 |
+
label="Price per bag",
|
| 392 |
+
minimum=0,
|
| 393 |
)
|
| 394 |
|
| 395 |
calculate_btn = gr.Button(
|
| 396 |
+
"π Calculate Feed Requirements",
|
| 397 |
variant="primary",
|
| 398 |
size="lg",
|
| 399 |
)
|
| 400 |
|
| 401 |
+
with gr.Column(scale=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 402 |
gr.Markdown("## π― Quick Answer")
|
| 403 |
+
output = gr.Markdown(
|
| 404 |
+
"Enter your email and farm details, then click **Calculate Feed Requirements**.",
|
| 405 |
+
elem_classes=["answer"],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 406 |
)
|
| 407 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 408 |
gr.Markdown("---")
|
| 409 |
+
gr.Markdown("## β Ask a question")
|
| 410 |
+
gr.Markdown("**What would you like to know about raising your birds?**")
|
| 411 |
+
gr.Markdown("Type your question. Questions are saved to guide future app upgrades and answers.")
|
| 412 |
|
| 413 |
with gr.Row():
|
| 414 |
+
question = gr.Textbox(
|
| 415 |
+
label="Your question",
|
| 416 |
+
placeholder="Example: How much water will 100 broilers drink at 5 weeks?",
|
| 417 |
+
lines=3,
|
| 418 |
+
scale=4,
|
| 419 |
+
)
|
| 420 |
+
ask_btn = gr.Button("πΎ Save Question", scale=1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 421 |
|
| 422 |
+
question_status = gr.Markdown()
|
|
|
|
|
|
|
| 423 |
|
| 424 |
gr.Markdown("---")
|
| 425 |
+
gr.Markdown("## π Common broiler breeds in Nigeria")
|
| 426 |
+
gr.Markdown("""
|
| 427 |
+
| Breed | What farmers commonly look for |
|
| 428 |
+
|---|---|
|
| 429 |
+
| **Ross 308 AP** | Fast growth, strong meat-production performance |
|
| 430 |
+
| **Cobb 500** | Strong growth and feed-efficiency performance |
|
| 431 |
+
| **Arbor Acres Plus** | Balanced growth, feed conversion and livability |
|
| 432 |
|
| 433 |
+
**Tip:** Ask the hatchery what breed you are buying before purchasing chicks.
|
| 434 |
+
""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 435 |
|
| 436 |
+
gr.Markdown("---")
|
| 437 |
+
gr.Markdown("### π Benchmark note")
|
| 438 |
+
gr.Markdown(
|
| 439 |
+
"The calculator uses manufacturer **As-Hatched** performance objectives. "
|
| 440 |
+
"As-Hatched means the benchmark represents the mixed-sex flock objective, "
|
| 441 |
+
"rather than male-only or female-only performance."
|
| 442 |
+
)
|
| 443 |
|
| 444 |
calculate_btn.click(
|
| 445 |
+
calculate,
|
| 446 |
+
inputs=[email, breed, birds, weeks, days, bag_size, price],
|
| 447 |
+
outputs=[output, output],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 448 |
)
|
| 449 |
|
| 450 |
+
# Save question automatically when the farmer asks/saves it.
|
| 451 |
ask_btn.click(
|
| 452 |
+
ask_question,
|
| 453 |
+
inputs=[email, question],
|
| 454 |
+
outputs=[question_status],
|
|
|
|
|
|
|
|
|
|
| 455 |
)
|
| 456 |
|
| 457 |
+
# Detect country/currency when the app loads.
|
| 458 |
+
demo.load(
|
| 459 |
+
update_currency,
|
| 460 |
+
inputs=[],
|
| 461 |
+
outputs=[currency_display, price, country_status],
|
| 462 |
+
)
|
| 463 |
|
| 464 |
if __name__ == "__main__":
|
|
|
|
| 465 |
port = int(os.environ.get("PORT", "7860"))
|
|
|
|
| 466 |
demo.launch(
|
| 467 |
server_name="0.0.0.0",
|
| 468 |
server_port=port,
|
| 469 |
+
show_error=True,
|
| 470 |
+
)
|
| 471 |
+
'''
|
| 472 |
+
|
| 473 |
+
path = Path("/mnt/data/app.py")
|
| 474 |
+
path.write_text(app_code, encoding="utf-8")
|
| 475 |
+
compile(app_code, str(path), "exec")
|
| 476 |
+
|
| 477 |
+
print(f"Created: {path}")
|
| 478 |
+
print("Syntax check: OK")
|
| 479 |
+
print("Important: replace the existing app.py with this file; do not keep the old patching script.")
|