Spaces:
Paused
Paused
NTEIGBANAM OKE commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,2731 +1,723 @@
|
|
| 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
|
| 31 |
-
import
|
| 32 |
-
import
|
| 33 |
-
import
|
| 34 |
-
import
|
| 35 |
-
|
| 36 |
-
from
|
| 37 |
-
|
| 38 |
-
import
|
| 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 |
-
|
| 2049 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2050 |
|
| 2051 |
-
|
| 2052 |
-
|
| 2053 |
-
|
|
|
|
|
|
|
|
|
|
| 2054 |
|
| 2055 |
-
|
| 2056 |
-
|
| 2057 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2058 |
}
|
| 2059 |
|
| 2060 |
-
|
| 2061 |
-
|
|
|
|
|
|
|
|
|
|
| 2062 |
}
|
| 2063 |
|
| 2064 |
-
|
| 2065 |
-
|
| 2066 |
-
|
| 2067 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2068 |
}
|
| 2069 |
|
| 2070 |
-
|
| 2071 |
-
|
| 2072 |
-
|
| 2073 |
-
|
| 2074 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2075 |
}
|
| 2076 |
|
| 2077 |
-
|
| 2078 |
-
|
| 2079 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2080 |
|
| 2081 |
-
|
| 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 |
-
|
| 2091 |
-
|
| 2092 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2093 |
|
| 2094 |
-
|
| 2095 |
-
|
| 2096 |
-
|
| 2097 |
-
|
| 2098 |
-
|
| 2099 |
-
|
| 2100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2101 |
|
| 2102 |
-
|
| 2103 |
-
|
| 2104 |
-
|
| 2105 |
-
|
| 2106 |
-
|
| 2107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2108 |
|
| 2109 |
-
|
| 2110 |
-
|
| 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 |
-
|
| 2121 |
-
|
| 2122 |
-
|
| 2123 |
|
| 2124 |
-
|
| 2125 |
-
|
| 2126 |
-
|
| 2127 |
-
|
| 2128 |
-
|
| 2129 |
-
|
| 2130 |
-
|
| 2131 |
-
|
| 2132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2133 |
|
| 2134 |
-
.
|
| 2135 |
-
|
| 2136 |
-
|
| 2137 |
-
|
|
|
|
|
|
|
| 2138 |
|
| 2139 |
-
|
| 2140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2141 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2142 |
|
| 2143 |
-
|
| 2144 |
-
# GOOGLE LOGIN UI
|
| 2145 |
-
# ============================================================
|
| 2146 |
|
| 2147 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2148 |
|
| 2149 |
-
|
| 2150 |
-
|
| 2151 |
-
|
| 2152 |
-
|
| 2153 |
-
|
| 2154 |
-
|
| 2155 |
-
|
| 2156 |
-
</a>
|
| 2157 |
-
</div>
|
| 2158 |
|
| 2159 |
-
<
|
| 2160 |
-
|
| 2161 |
-
|
| 2162 |
-
|
| 2163 |
-
">
|
| 2164 |
-
Google sign-in can automatically provide your email.
|
| 2165 |
-
You can also enter your email manually.
|
| 2166 |
-
</div>
|
| 2167 |
-
"""
|
| 2168 |
|
| 2169 |
-
|
| 2170 |
-
|
| 2171 |
-
|
| 2172 |
-
|
| 2173 |
-
|
| 2174 |
-
|
| 2175 |
-
|
| 2176 |
-
|
| 2177 |
-
|
| 2178 |
-
|
| 2179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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2194 |
-
|
| 2195 |
-
|
| 2196 |
-
|
| 2197 |
-
|
| 2198 |
-
|
| 2199 |
-
|
| 2200 |
-
|
| 2201 |
-
|
| 2202 |
-
|
| 2203 |
-
|
| 2204 |
-
|
| 2205 |
-
|
| 2206 |
-
|
| 2207 |
-
|
| 2208 |
-
|
| 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 |
-
|
| 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 |
-
"""
|
| 2311 |
-
# 🐔 PoultryFeedCalc
|
| 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="
|
| 2373 |
placeholder="farmer@example.com",
|
| 2374 |
type="email",
|
|
|
|
| 2375 |
)
|
| 2376 |
-
|
| 2377 |
-
|
| 2378 |
-
|
| 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 |
-
"
|
|
|
|
| 2404 |
)
|
| 2405 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2406 |
birds = gr.Dropdown(
|
| 2407 |
-
choices=
|
| 2408 |
-
|
| 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 |
-
|
| 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 |
-
|
| 2465 |
"🐔 Calculate Feed Requirement",
|
| 2466 |
variant="primary",
|
| 2467 |
-
|
| 2468 |
-
"orange-button"
|
| 2469 |
-
],
|
| 2470 |
-
)
|
| 2471 |
-
|
| 2472 |
-
# ====================================================
|
| 2473 |
-
# RESULTS
|
| 2474 |
-
# ====================================================
|
| 2475 |
-
|
| 2476 |
-
with gr.Column(
|
| 2477 |
-
scale=1,
|
| 2478 |
-
):
|
| 2479 |
-
|
| 2480 |
-
gr.Markdown(
|
| 2481 |
-
"## 🐔 Your feed requirement"
|
| 2482 |
)
|
| 2483 |
|
| 2484 |
-
|
| 2485 |
-
|
| 2486 |
-
|
| 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 |
-
|
| 2504 |
-
|
| 2505 |
-
""
|
| 2506 |
-
|
| 2507 |
-
|
| 2508 |
-
|
| 2509 |
-
|
| 2510 |
-
|
| 2511 |
-
|
| 2512 |
-
|
| 2513 |
-
|
| 2514 |
-
to improve future versions of PoultryFeedCalc.
|
| 2515 |
-
"""
|
| 2516 |
-
)
|
| 2517 |
-
|
| 2518 |
-
question = gr.Textbox(
|
| 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 |
-
|
| 2570 |
-
|
| 2571 |
-
|
| 2572 |
-
|
| 2573 |
-
|
| 2574 |
-
|
| 2575 |
-
|
| 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 |
-
|
| 2618 |
inputs=None,
|
| 2619 |
-
outputs=
|
| 2620 |
-
api_visibility="private",
|
| 2621 |
)
|
| 2622 |
-
|
| 2623 |
-
# --------------------------------------------------------
|
| 2624 |
-
# Automatically detect country and currency.
|
| 2625 |
-
# --------------------------------------------------------
|
| 2626 |
-
|
| 2627 |
demo.load(
|
| 2628 |
-
|
| 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 |
-
|
| 2642 |
-
# --------------------------------------------------------
|
| 2643 |
-
|
| 2644 |
-
calculate_button.click(
|
| 2645 |
-
fn=calculate_feed,
|
| 2646 |
inputs=[
|
| 2647 |
-
birds,
|
| 2648 |
-
|
| 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 |
-
|
| 2662 |
-
api_visibility="private",
|
| 2663 |
)
|
| 2664 |
|
| 2665 |
-
|
| 2666 |
-
|
| 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 |
-
|
| 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 |
-
|
| 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 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
|
| 2 |
+
import os
|
| 3 |
+
import re
|
| 4 |
+
import sqlite3
|
| 5 |
+
import math
|
| 6 |
+
import json
|
| 7 |
+
import urllib.request
|
| 8 |
+
from datetime import datetime, timezone
|
| 9 |
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
import gradio as gr
|
| 13 |
+
from fastapi import FastAPI, Request
|
| 14 |
+
from fastapi.responses import RedirectResponse
|
| 15 |
+
from starlette.config import Config
|
| 16 |
+
from starlette.middleware.sessions import SessionMiddleware
|
| 17 |
+
from authlib.integrations.starlette_client import OAuth, OAuthError
|
| 18 |
|
| 19 |
+
# -----------------------------
|
| 20 |
+
# Configuration
|
| 21 |
+
# -----------------------------
|
| 22 |
+
APP_NAME = "PoultryFeedCalc"
|
| 23 |
+
DB_PATH = Path(os.getenv("LEADS_DB", "/data/poultryfeedcalc.db"))
|
| 24 |
+
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
| 25 |
|
| 26 |
+
# Feed-stage assumption used by this calculator:
|
| 27 |
+
# Starter through day 21; Finisher after day 21.
|
| 28 |
+
STARTER_END_DAY = 21
|
| 29 |
+
PLANNING_BUFFER = 0.20 # practical 20% allowance for farm variation/wastage
|
| 30 |
+
|
| 31 |
+
# Ross 308 as-hatched performance objective cumulative feed intake (kg/bird)
|
| 32 |
+
# from Aviagen's 2022 performance objectives. Daily values are used so
|
| 33 |
+
# Weeks + Days are calculated in exact total days rather than only at week
|
| 34 |
+
# checkpoints. Values below cover day 0-56 and are interpolated for any
|
| 35 |
+
# in-between day if needed.
|
| 36 |
+
ROSS_CUM_FEED_KG = {
|
| 37 |
+
0: 0.000, 1: 0.012, 2: 0.028, 3: 0.048, 4: 0.072, 5: 0.100,
|
| 38 |
+
6: 0.131, 7: 0.166, 8: 0.206, 9: 0.249, 10: 0.297,
|
| 39 |
+
11: 0.349, 12: 0.406, 13: 0.468, 14: 0.535, 15: 0.608,
|
| 40 |
+
16: 0.685, 17: 0.768, 18: 0.856, 19: 0.950, 20: 1.050,
|
| 41 |
+
21: 1.155, 22: 1.266, 23: 1.383, 24: 1.505, 25: 1.633,
|
| 42 |
+
26: 1.767, 27: 1.907, 28: 2.047, 29: 2.202, 30: 2.357,
|
| 43 |
+
31: 2.518, 32: 2.684, 33: 2.855, 34: 3.031, 35: 3.390,
|
| 44 |
+
36: 3.390, 37: 3.690, 38: 3.930, 39: 4.180, 40: 4.430,
|
| 45 |
+
41: 4.660, 42: 4.890, 43: 5.100, 44: 5.310, 45: 5.520,
|
| 46 |
+
46: 5.444, 47: 5.665, 48: 5.889, 49: 6.116, 50: 6.346,
|
| 47 |
+
51: 6.578, 52: 6.812, 53: 7.049, 54: 7.287, 55: 7.528,
|
| 48 |
+
56: 7.771,
|
| 49 |
}
|
| 50 |
|
| 51 |
+
# Approximate expected live body weight (kg/bird) from the same Ross 308
|
| 52 |
+
# objective family. Used as a benchmark, not a farm guarantee.
|
| 53 |
+
ROSS_WEIGHT_KG = {
|
| 54 |
+
0: 0.044, 7: 0.213, 14: 0.533, 21: 1.012, 28: 1.616,
|
| 55 |
+
35: 2.296, 42: 2.998, 49: 3.668, 56: 4.344,
|
| 56 |
}
|
| 57 |
|
| 58 |
+
# Country -> currency code and symbol. This is a display/default mapping,
|
| 59 |
+
# not a live FX service.
|
| 60 |
+
COUNTRIES = {
|
| 61 |
+
"NG": ("NGN", "₦"),
|
| 62 |
+
"GH": ("GHS", "GH₵"),
|
| 63 |
+
"KE": ("KES", "KSh"),
|
| 64 |
+
"UG": ("UGX", "USh"),
|
| 65 |
+
"TZ": ("TZS", "TSh"),
|
| 66 |
+
"ZA": ("ZAR", "R"),
|
| 67 |
+
"ZM": ("ZMW", "ZK"),
|
| 68 |
+
"ZW": ("ZWL", "Z$"),
|
| 69 |
+
"CM": ("XAF", "FCFA"),
|
| 70 |
+
"CI": ("XOF", "CFA"),
|
| 71 |
+
"SN": ("XOF", "CFA"),
|
| 72 |
+
"BJ": ("XOF", "CFA"),
|
| 73 |
+
"BF": ("XOF", "CFA"),
|
| 74 |
+
"US": ("USD", "$"),
|
| 75 |
+
"CA": ("CAD", "C$"),
|
| 76 |
+
"GB": ("GBP", "£"),
|
| 77 |
+
"IE": ("EUR", "€"),
|
| 78 |
+
"DE": ("EUR", "€"),
|
| 79 |
+
"FR": ("EUR", "€"),
|
| 80 |
+
"ES": ("EUR", "€"),
|
| 81 |
+
"IT": ("EUR", "€"),
|
| 82 |
+
"NL": ("EUR", "€"),
|
| 83 |
+
"PT": ("EUR", "€"),
|
| 84 |
+
"BE": ("EUR", "€"),
|
| 85 |
+
"AU": ("AUD", "A$"),
|
| 86 |
+
"NZ": ("NZD", "NZ$"),
|
| 87 |
+
"IN": ("INR", "₹"),
|
| 88 |
+
"PK": ("PKR", "₨"),
|
| 89 |
+
"BD": ("BDT", "৳"),
|
| 90 |
+
"BR": ("BRL", "R$"),
|
| 91 |
}
|
| 92 |
|
| 93 |
+
DEFAULT_PRICE = {
|
| 94 |
+
"NGN": 24000,
|
| 95 |
+
"GHS": 300,
|
| 96 |
+
"KES": 2200,
|
| 97 |
+
"UGX": 100000,
|
| 98 |
+
"TZS": 30000,
|
| 99 |
+
"ZAR": 300,
|
| 100 |
+
"ZMW": 350,
|
| 101 |
+
"USD": 25,
|
| 102 |
+
"GBP": 20,
|
| 103 |
+
"EUR": 23,
|
| 104 |
+
"INR": 2200,
|
| 105 |
+
"PKR": 7000,
|
| 106 |
+
"BDT": 2500,
|
| 107 |
+
"AUD": 35,
|
| 108 |
+
"CAD": 35,
|
| 109 |
+
"NZD": 40,
|
| 110 |
+
"BRL": 130,
|
| 111 |
}
|
| 112 |
|
| 113 |
+
def db_init():
|
| 114 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 115 |
+
conn.execute("""
|
| 116 |
+
CREATE TABLE IF NOT EXISTS leads (
|
| 117 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 118 |
+
email TEXT UNIQUE NOT NULL,
|
| 119 |
+
country TEXT,
|
| 120 |
+
currency TEXT,
|
| 121 |
+
source TEXT,
|
| 122 |
+
created_at TEXT NOT NULL,
|
| 123 |
+
last_seen_at TEXT NOT NULL
|
| 124 |
+
)
|
| 125 |
+
""")
|
| 126 |
+
conn.execute("""
|
| 127 |
+
CREATE TABLE IF NOT EXISTS questions (
|
| 128 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 129 |
+
email TEXT,
|
| 130 |
+
question TEXT NOT NULL,
|
| 131 |
+
answer TEXT,
|
| 132 |
+
created_at TEXT NOT NULL
|
| 133 |
+
)
|
| 134 |
+
""")
|
| 135 |
+
conn.commit()
|
| 136 |
|
| 137 |
+
db_init()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
+
def save_lead(email: str, country: str = "", currency: str = "", source: str = "email"):
|
| 140 |
+
email = (email or "").strip().lower()
|
| 141 |
+
if not re.match(r"^[^@\s]+@[^@\s]+\.[^@\s]+$", email):
|
| 142 |
+
return False
|
| 143 |
+
now = datetime.now(timezone.utc).isoformat()
|
| 144 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 145 |
+
conn.execute("""
|
| 146 |
+
INSERT INTO leads(email,country,currency,source,created_at,last_seen_at)
|
| 147 |
+
VALUES(?,?,?,?,?,?)
|
| 148 |
+
ON CONFLICT(email) DO UPDATE SET
|
| 149 |
+
country=excluded.country,
|
| 150 |
+
currency=excluded.currency,
|
| 151 |
+
source=excluded.source,
|
| 152 |
+
last_seen_at=excluded.last_seen_at
|
| 153 |
+
""", (email, country, currency, source, now, now))
|
| 154 |
+
conn.commit()
|
| 155 |
+
return True
|
| 156 |
|
| 157 |
+
def save_question(email: str, question: str, answer: str = ""):
|
| 158 |
+
q = (question or "").strip()
|
| 159 |
+
if not q:
|
| 160 |
+
return
|
| 161 |
+
with sqlite3.connect(DB_PATH) as conn:
|
| 162 |
+
conn.execute(
|
| 163 |
+
"INSERT INTO questions(email,question,answer,created_at) VALUES(?,?,?,?)",
|
| 164 |
+
((email or "").strip().lower(), q, answer or "",
|
| 165 |
+
datetime.now(timezone.utc).isoformat())
|
| 166 |
+
)
|
| 167 |
+
conn.commit()
|
| 168 |
|
| 169 |
+
def interpolate(table, day):
|
| 170 |
+
day = float(day)
|
| 171 |
+
keys = sorted(table)
|
| 172 |
+
if day <= keys[0]:
|
| 173 |
+
return table[keys[0]]
|
| 174 |
+
if day >= keys[-1]:
|
| 175 |
+
# Linear extension after the last published point.
|
| 176 |
+
a, b = keys[-2], keys[-1]
|
| 177 |
+
return table[b] + (table[b] - table[a]) * (day - b) / (b - a)
|
| 178 |
+
lo = max(k for k in keys if k <= day)
|
| 179 |
+
hi = min(k for k in keys if k >= day)
|
| 180 |
+
if lo == hi:
|
| 181 |
+
return table[lo]
|
| 182 |
+
ratio = (day - lo) / (hi - lo)
|
| 183 |
+
return table[lo] + (table[hi] - table[lo]) * ratio
|
| 184 |
+
|
| 185 |
+
def feed_per_bird(days):
|
| 186 |
+
return max(0.0, interpolate(ROSS_CUM_FEED_KG, days))
|
| 187 |
+
|
| 188 |
+
def weight_per_bird(days):
|
| 189 |
+
return max(0.0, interpolate(ROSS_WEIGHT_KG, days))
|
| 190 |
+
|
| 191 |
+
def money(value, symbol):
|
| 192 |
+
return f"{symbol}{value:,.0f}"
|
| 193 |
+
|
| 194 |
+
def parse_country(request: Optional[gr.Request]):
|
| 195 |
+
if not request:
|
| 196 |
+
return "NG", "NGN", "₦"
|
| 197 |
+
|
| 198 |
+
headers = request.headers or {}
|
| 199 |
+
|
| 200 |
+
# First use a country header when the hosting proxy provides one.
|
| 201 |
+
country = (
|
| 202 |
+
headers.get("x-country-code")
|
| 203 |
+
or headers.get("cf-ipcountry")
|
| 204 |
+
or headers.get("x-vercel-ip-country")
|
| 205 |
+
or headers.get("x-appengine-country")
|
| 206 |
+
or ""
|
| 207 |
+
).upper().strip()
|
| 208 |
+
|
| 209 |
+
if country in COUNTRIES:
|
| 210 |
+
code, sym = COUNTRIES[country]
|
| 211 |
+
return country, code, sym
|
| 212 |
+
|
| 213 |
+
# Fallback: try the visitor IP with ipapi.co. This is best-effort and
|
| 214 |
+
# deliberately has a short timeout so a geo service outage never breaks
|
| 215 |
+
# the calculator.
|
| 216 |
+
try:
|
| 217 |
+
forwarded = headers.get("x-forwarded-for", "")
|
| 218 |
+
client_ip = forwarded.split(",")[0].strip() if forwarded else ""
|
| 219 |
+
if not client_ip and getattr(request, "client", None):
|
| 220 |
+
client_ip = request.client.host
|
| 221 |
+
if client_ip and client_ip not in {"127.0.0.1", "::1"}:
|
| 222 |
+
with urllib.request.urlopen(
|
| 223 |
+
f"https://ipapi.co/{client_ip}/json/",
|
| 224 |
+
timeout=2.5
|
| 225 |
+
) as response:
|
| 226 |
+
data = json.loads(response.read().decode("utf-8"))
|
| 227 |
+
country = str(data.get("country_code", "")).upper()
|
| 228 |
+
if country in COUNTRIES:
|
| 229 |
+
code, sym = COUNTRIES[country]
|
| 230 |
+
return country, code, sym
|
| 231 |
+
except Exception:
|
| 232 |
+
pass
|
| 233 |
|
| 234 |
+
# Final fallback for the Nigerian-first MVP.
|
| 235 |
+
return "NG", "NGN", "₦"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
|
| 237 |
+
def country_on_load(request: gr.Request):
|
| 238 |
+
country, currency, symbol = parse_country(request)
|
| 239 |
+
return country, currency, symbol, DEFAULT_PRICE.get(currency, 25)
|
| 240 |
|
| 241 |
+
def user_email_on_load(request: gr.Request):
|
| 242 |
+
try:
|
| 243 |
+
if request and hasattr(request, "request") and request.request:
|
| 244 |
+
pass
|
| 245 |
+
except Exception:
|
| 246 |
+
pass
|
| 247 |
+
return ""
|
| 248 |
+
|
| 249 |
+
# -----------------------------
|
| 250 |
+
# Google OAuth (optional)
|
| 251 |
+
# -----------------------------
|
| 252 |
+
GOOGLE_CLIENT_ID = os.getenv("GOOGLE_CLIENT_ID", "")
|
| 253 |
+
GOOGLE_CLIENT_SECRET = os.getenv("GOOGLE_CLIENT_SECRET", "")
|
| 254 |
+
SESSION_SECRET = os.getenv("SESSION_SECRET", "change-this-secret-in-production")
|
| 255 |
+
|
| 256 |
+
fastapi_app = FastAPI()
|
| 257 |
+
fastapi_app.add_middleware(SessionMiddleware, secret_key=SESSION_SECRET)
|
| 258 |
+
|
| 259 |
+
google_enabled = bool(GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET)
|
| 260 |
+
oauth = None
|
| 261 |
+
|
| 262 |
+
if google_enabled:
|
| 263 |
+
config = Config(environ={
|
| 264 |
+
"GOOGLE_CLIENT_ID": GOOGLE_CLIENT_ID,
|
| 265 |
+
"GOOGLE_CLIENT_SECRET": GOOGLE_CLIENT_SECRET,
|
| 266 |
+
})
|
| 267 |
+
oauth = OAuth(config)
|
| 268 |
+
oauth.register(
|
| 269 |
+
name="google",
|
| 270 |
+
server_metadata_url="https://accounts.google.com/.well-known/openid-configuration",
|
| 271 |
+
client_kwargs={"scope": "openid email profile"},
|
| 272 |
+
)
|
| 273 |
|
| 274 |
+
@fastapi_app.get("/login/google")
|
| 275 |
+
async def google_login(request: Request):
|
| 276 |
+
if not google_enabled:
|
| 277 |
+
return RedirectResponse(url="/?google=not-configured")
|
| 278 |
+
redirect_uri = request.url_for("google_auth")
|
| 279 |
+
return await oauth.google.authorize_redirect(request, redirect_uri)
|
| 280 |
|
| 281 |
+
@fastapi_app.get("/auth/google")
|
| 282 |
+
async def google_auth(request: Request):
|
| 283 |
+
if not google_enabled:
|
| 284 |
+
return RedirectResponse(url="/?google=not-configured")
|
| 285 |
+
try:
|
| 286 |
+
token = await oauth.google.authorize_access_token(request)
|
| 287 |
+
userinfo = dict(token).get("userinfo") or await oauth.google.userinfo(token=token)
|
| 288 |
+
request.session["user"] = {
|
| 289 |
+
"email": userinfo.get("email", ""),
|
| 290 |
+
"name": userinfo.get("name", ""),
|
| 291 |
+
}
|
| 292 |
+
email = userinfo.get("email", "")
|
| 293 |
+
if email:
|
| 294 |
+
country, currency, _ = parse_country(None)
|
| 295 |
+
save_lead(email, country, currency, "google")
|
| 296 |
+
return RedirectResponse(url="/")
|
| 297 |
+
except OAuthError:
|
| 298 |
+
return RedirectResponse(url="/?google=error")
|
| 299 |
+
|
| 300 |
+
@fastapi_app.get("/logout")
|
| 301 |
+
async def logout(request: Request):
|
| 302 |
+
request.session.pop("user", None)
|
| 303 |
+
return RedirectResponse(url="/")
|
| 304 |
+
|
| 305 |
+
@fastapi_app.get("/health")
|
| 306 |
+
def health():
|
| 307 |
+
return {"status": "ok", "app": APP_NAME}
|
| 308 |
+
|
| 309 |
+
# -----------------------------
|
| 310 |
+
# Calculator
|
| 311 |
+
# -----------------------------
|
| 312 |
+
BIRD_OPTIONS = ["25", "50", "75", "100", "150", "200", "500", "1,000", "Custom"]
|
| 313 |
+
|
| 314 |
+
def parse_birds(value):
|
| 315 |
+
if value is None:
|
| 316 |
+
raise ValueError("Select the number of birds.")
|
| 317 |
+
text = str(value).replace(",", "").strip()
|
| 318 |
+
if text.lower() == "custom":
|
| 319 |
+
raise ValueError("Enter a custom number of birds.")
|
| 320 |
+
n = int(text)
|
| 321 |
+
if n < 1:
|
| 322 |
+
raise ValueError("Number of birds must be at least 1.")
|
| 323 |
+
return n
|
| 324 |
|
| 325 |
+
def calculate_feed(
|
| 326 |
+
birds_value, custom_birds, weeks, days_extra, bag_size, price_per_bag,
|
| 327 |
+
email, currency_code, currency_symbol, request: gr.Request
|
| 328 |
+
):
|
| 329 |
+
# Determine bird count.
|
| 330 |
+
try:
|
| 331 |
+
birds = parse_birds(custom_birds if str(birds_value).lower() == "custom" else birds_value)
|
| 332 |
+
except Exception as e:
|
| 333 |
+
return f"⚠️ {e}"
|
| 334 |
|
| 335 |
+
email = (email or "").strip().lower()
|
|
|
|
|
|
|
| 336 |
|
| 337 |
+
# If the browser session has Google login, use that email automatically.
|
| 338 |
+
# The public Gradio event request may expose the underlying FastAPI session
|
| 339 |
+
# only when mounted; this is handled through the hidden/session email state
|
| 340 |
+
# populated by the separate route where possible. Manual email remains the
|
| 341 |
+
# reliable fallback.
|
| 342 |
+
if not re.match(r"^[^@\s]+@[^@\s]+\.[^@\s]+$", email):
|
| 343 |
+
return "⚠️ Please enter a valid email address before calculating."
|
| 344 |
|
| 345 |
+
try:
|
| 346 |
+
weeks = int(weeks)
|
| 347 |
+
days_extra = int(days_extra)
|
| 348 |
+
bag_size = float(bag_size)
|
| 349 |
+
price = float(price_per_bag)
|
| 350 |
+
except Exception:
|
| 351 |
+
return "⚠️ Please select valid age, bag size and price values."
|
|
|
|
|
|
|
| 352 |
|
| 353 |
+
if weeks < 0 or weeks > 16 or days_extra < 0 or days_extra > 6:
|
| 354 |
+
return "⚠️ Please enter a valid age."
|
| 355 |
+
if bag_size <= 0 or price < 0:
|
| 356 |
+
return "⚠️ Feed bag size must be positive and price cannot be negative."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 357 |
|
| 358 |
+
total_days = weeks * 7 + days_extra
|
| 359 |
+
if total_days <= 0:
|
| 360 |
+
return "⚠️ Bird age must be at least 1 day."
|
| 361 |
+
|
| 362 |
+
# Save the lead automatically when Calculate is clicked.
|
| 363 |
+
country, detected_currency, detected_symbol = parse_country(request)
|
| 364 |
+
if not currency_code:
|
| 365 |
+
currency_code = detected_currency
|
| 366 |
+
if not currency_symbol:
|
| 367 |
+
currency_symbol = detected_symbol
|
| 368 |
+
save_lead(email, country, currency_code, "calculator")
|
| 369 |
+
|
| 370 |
+
cumulative = feed_per_bird(total_days)
|
| 371 |
+
benchmark_feed = cumulative * birds
|
| 372 |
+
total_feed = benchmark_feed * (1 + PLANNING_BUFFER)
|
| 373 |
+
|
| 374 |
+
starter_per_bird = feed_per_bird(min(total_days, STARTER_END_DAY))
|
| 375 |
+
finisher_per_bird = max(0.0, cumulative - starter_per_bird)
|
| 376 |
+
|
| 377 |
+
# Apply the same practical planning allowance across the two stages.
|
| 378 |
+
starter_kg = starter_per_bird * birds * (1 + PLANNING_BUFFER)
|
| 379 |
+
finisher_kg = finisher_per_bird * birds * (1 + PLANNING_BUFFER)
|
| 380 |
+
|
| 381 |
+
# Calculate theoretical whole bags by stage and total bags by total feed.
|
| 382 |
+
# This avoids the previous error where Starter + Finisher bag counts could
|
| 383 |
+
# exceed the correct purchase total.
|
| 384 |
+
exact_total_bags = total_feed / bag_size
|
| 385 |
+
whole_total_bags = math.ceil(exact_total_bags)
|
| 386 |
+
|
| 387 |
+
# Stage purchasing recommendation. To avoid double-buying due to separate
|
| 388 |
+
# rounding, allocate starter bags first, then use the remaining total bags
|
| 389 |
+
# for finisher.
|
| 390 |
+
starter_bags = math.ceil(starter_kg / bag_size) if starter_kg > 0 else 0
|
| 391 |
+
finisher_bags = max(0, whole_total_bags - starter_bags)
|
| 392 |
+
|
| 393 |
+
# If the stage split is close to a bag boundary, ensure the total capacity
|
| 394 |
+
# covers the total feed.
|
| 395 |
+
while (starter_bags + finisher_bags) * bag_size < total_feed:
|
| 396 |
+
finisher_bags += 1
|
| 397 |
+
|
| 398 |
+
total_cost = whole_total_bags * price
|
| 399 |
+
expected_weight = weight_per_bird(total_days)
|
| 400 |
+
live_weight = expected_weight * birds
|
| 401 |
+
|
| 402 |
+
age_text = f"{weeks} week" + ("" if weeks == 1 else "s")
|
| 403 |
+
if days_extra:
|
| 404 |
+
age_text += f" + {days_extra} day" + ("" if days_extra == 1 else "s")
|
| 405 |
+
age_text += f" ({total_days} days)"
|
| 406 |
+
|
| 407 |
+
quick = (
|
| 408 |
+
f"### 🎯 Quick answer\n"
|
| 409 |
+
f"**To raise {birds:,} birds for {total_days} days ({age_text.replace(' (' + str(total_days) + ' days)', '')}), "
|
| 410 |
+
f"you will need an estimated → {whole_total_bags} bags of feed "
|
| 411 |
+
f"({starter_bags} Starter + {finisher_bags} Finisher), costing approximately "
|
| 412 |
+
f"{money(total_cost, currency_symbol)} on feed alone.**"
|
| 413 |
+
)
|
| 414 |
+
|
| 415 |
+
html = f"""
|
| 416 |
+
<div class="answer-card">
|
| 417 |
+
<div class="answer-title">🐔 Your Feed Requirement</div>
|
| 418 |
+
<div class="big-answer">
|
| 419 |
+
You will require approximately <b>{whole_total_bags} bags of {bag_size:g} kg feed</b>
|
| 420 |
+
</div>
|
| 421 |
+
<div class="sub-answer">To raise <b>{birds:,} birds</b> for <b>{total_days} days</b>.</div>
|
| 422 |
+
|
| 423 |
+
<table class="feed-table">
|
| 424 |
+
<thead><tr><th>Feed</th><th>Estimated quantity</th><th>Bags to buy</th></tr></thead>
|
| 425 |
+
<tbody>
|
| 426 |
+
<tr><td>🌱 Starter</td><td>{starter_kg:,.1f} kg</td><td>{starter_bags} bags</td></tr>
|
| 427 |
+
<tr><td>🌾 Finisher</td><td>{finisher_kg:,.1f} kg</td><td>{finisher_bags} bags</td></tr>
|
| 428 |
+
<tr class="total"><td><b>TOTAL</b></td><td><b>{total_feed:,.1f} kg</b></td><td><b>{whole_total_bags} bags</b></td></tr>
|
| 429 |
+
</tbody>
|
| 430 |
+
</table>
|
| 431 |
+
|
| 432 |
+
<div class="cost-box">
|
| 433 |
+
<div class="cost-title">💰 Estimated feed cost</div>
|
| 434 |
+
<div class="cost-value">{money(total_cost, currency_symbol)}</div>
|
| 435 |
+
<div>on feed alone.</div>
|
| 436 |
+
<div class="small">Based on {money(price, currency_symbol)} per {bag_size:g}-kg bag.</div>
|
| 437 |
+
</div>
|
| 438 |
+
|
| 439 |
+
<div class="quick-box">
|
| 440 |
+
<b>🎯 Quick answer:</b> To raise <b>{birds:,} birds for {total_days} days</b>,
|
| 441 |
+
you will need an estimate → <b>{whole_total_bags} bags of feed</b>
|
| 442 |
+
({starter_bags} Starter + {finisher_bags} Finisher), costing approximately
|
| 443 |
+
<b>{money(total_cost, currency_symbol)}</b>.
|
| 444 |
+
</div>
|
| 445 |
+
|
| 446 |
+
<div class="benchmark">
|
| 447 |
+
<h3>📊 Ross 308 benchmark at {total_days} days</h3>
|
| 448 |
+
<ul>
|
| 449 |
+
<li>Expected body weight: <b>{expected_weight:,.2f} kg/bird</b></li>
|
| 450 |
+
<li>Expected flock live weight: <b>{live_weight:,.1f} kg</b></li>
|
| 451 |
+
<li>Benchmark cumulative feed: <b>{cumulative:,.2f} kg/bird</b></li>
|
| 452 |
+
<li>Planning allowance: <b>{PLANNING_BUFFER*100:.0f}%</b> for farm variation/wastage</li>
|
| 453 |
+
</ul>
|
| 454 |
+
</div>
|
| 455 |
+
|
| 456 |
+
<div class="note">
|
| 457 |
+
<b>Important:</b> This is an estimate based on Ross 308 performance objectives.
|
| 458 |
+
Actual farm consumption varies with genetics, sex, mortality, feed quality,
|
| 459 |
+
temperature, management and other farm conditions.
|
| 460 |
+
</div>
|
| 461 |
</div>
|
| 462 |
"""
|
| 463 |
+
return html
|
| 464 |
+
|
| 465 |
+
def ask_question(question, email, birds_value, custom_birds, weeks, days_extra,
|
| 466 |
+
bag_size, price, currency_code, currency_symbol, request: gr.Request):
|
| 467 |
+
q = (question or "").strip()
|
| 468 |
+
if not q:
|
| 469 |
+
return "⚠️ Please type or speak your question."
|
| 470 |
+
|
| 471 |
+
# Auto-save the question whenever Get Answer is clicked.
|
| 472 |
+
if email:
|
| 473 |
+
save_lead(email, *parse_country(request)[0:1], currency_code, "question")
|
| 474 |
+
|
| 475 |
+
# Try to answer common calculator questions automatically.
|
| 476 |
+
lower = q.lower()
|
| 477 |
+
bird_match = re.search(r"(\d[\d,]*)\s*(?:birds|broilers|chickens?)", lower)
|
| 478 |
+
week_match = re.search(r"(\d+)\s*(?:weeks?|wks?)", lower)
|
| 479 |
+
day_match = re.search(r"(\d+)\s*(?:days?|dys?)", lower)
|
| 480 |
+
bag_match = re.search(r"(\d+(?:\.\d+)?)\s*(?:bags?|bag)", lower)
|
| 481 |
+
|
| 482 |
+
birds = int(bird_match.group(1).replace(",", "")) if bird_match else None
|
| 483 |
+
w = int(week_match.group(1)) if week_match else None
|
| 484 |
+
d = int(day_match.group(1)) if day_match else 0
|
| 485 |
+
|
| 486 |
+
if birds and w is not None:
|
| 487 |
+
total_days = w * 7 + d
|
| 488 |
+
cumulative = feed_per_bird(total_days)
|
| 489 |
+
total_feed = birds * cumulative * (1 + PLANNING_BUFFER)
|
| 490 |
+
try:
|
| 491 |
+
bag = float(bag_size)
|
| 492 |
+
p = float(price)
|
| 493 |
+
except Exception:
|
| 494 |
+
bag, p = 25.0, 24000.0
|
| 495 |
+
total_bags = math.ceil(total_feed / bag)
|
| 496 |
+
starter_kg = birds * feed_per_bird(min(total_days, STARTER_END_DAY)) * (1 + PLANNING_BUFFER)
|
| 497 |
+
starter_bags = math.ceil(starter_kg / bag)
|
| 498 |
+
finisher_bags = max(0, total_bags - starter_bags)
|
| 499 |
+
cost = total_bags * p
|
| 500 |
+
answer = (
|
| 501 |
+
f"### 🤖 Answer\n"
|
| 502 |
+
f"For **{birds:,} birds** at **{total_days} days ({w} weeks"
|
| 503 |
+
f"{f' + {d} days' if d else ''})**, the estimate is **{total_feed:,.1f} kg of feed** "
|
| 504 |
+
f"or approximately **{total_bags} bags of {bag:g} kg** "
|
| 505 |
+
f"(**{starter_bags} Starter + {finisher_bags} Finisher**).\n\n"
|
| 506 |
+
f"At the current bag price, that is approximately **{money(cost, currency_symbol)}** on feed alone.\n\n"
|
| 507 |
+
f"_This is an estimate based on the Ross 308 benchmark and actual farm use can vary._"
|
| 508 |
+
)
|
| 509 |
+
elif bag_match and birds:
|
| 510 |
+
answer = (
|
| 511 |
+
"I can compare the bags you mentioned with the benchmark, but I also need the "
|
| 512 |
+
"target age in weeks. Please include it, for example: "
|
| 513 |
+
"`Is 3 bags enough for 51 birds at 3 weeks?`"
|
| 514 |
+
)
|
| 515 |
+
else:
|
| 516 |
+
answer = (
|
| 517 |
+
"### 🤖 Question saved\n"
|
| 518 |
+
"I have saved this question for future PoultryFeedCalc improvements. "
|
| 519 |
+
"For a direct feed calculation, include the **number of birds and target age** "
|
| 520 |
+
"in weeks/days, for example: `How many bags for 500 broilers for 8 weeks?`"
|
| 521 |
+
)
|
| 522 |
+
|
| 523 |
+
save_question(email, q, answer)
|
| 524 |
+
return answer
|
| 525 |
+
|
| 526 |
+
def get_session_email(request: gr.Request):
|
| 527 |
+
# Gradio's request wrapper exposes the Starlette request in different
|
| 528 |
+
# versions. Try the known shapes without failing the app.
|
| 529 |
+
try:
|
| 530 |
+
raw = getattr(request, "request", None)
|
| 531 |
+
if raw is not None:
|
| 532 |
+
user = raw.session.get("user")
|
| 533 |
+
if user and user.get("email"):
|
| 534 |
+
return user["email"]
|
| 535 |
+
except Exception:
|
| 536 |
+
pass
|
| 537 |
+
return ""
|
| 538 |
|
| 539 |
+
# -----------------------------
|
| 540 |
+
# UI
|
| 541 |
+
# -----------------------------
|
| 542 |
+
CSS = """
|
| 543 |
+
body { background: #f7f8fa; }
|
| 544 |
+
.gradio-container { max-width: 1200px !important; }
|
| 545 |
+
.hero { padding: 18px 4px 10px; }
|
| 546 |
+
.hero h1 { font-size: 38px; margin: 0 0 6px; }
|
| 547 |
+
.hero p { font-size: 17px; color: #4b5563; }
|
| 548 |
+
.section { border-radius: 14px; }
|
| 549 |
+
.answer-card { background: white; border: 1px solid #e5e7eb; border-radius: 16px; padding: 24px; box-shadow: 0 4px 18px rgba(0,0,0,.05); }
|
| 550 |
+
.answer-title { font-size: 28px; font-weight: 800; margin-bottom: 12px; }
|
| 551 |
+
.big-answer { font-size: 24px; line-height: 1.35; margin-bottom: 8px; }
|
| 552 |
+
.sub-answer { font-size: 17px; margin-bottom: 18px; }
|
| 553 |
+
.feed-table { width: 100%; border-collapse: collapse; margin: 12px 0 20px; }
|
| 554 |
+
.feed-table th, .feed-table td { border: 1px solid #d1d5db; padding: 11px; text-align: left; }
|
| 555 |
+
.feed-table th { background: #f3f4f6; }
|
| 556 |
+
.feed-table .total { background: #fafafa; }
|
| 557 |
+
.cost-box { margin: 18px 0; }
|
| 558 |
+
.cost-title { font-size: 20px; font-weight: 800; }
|
| 559 |
+
.cost-value { font-size: 30px; font-weight: 900; margin-top: 4px; }
|
| 560 |
+
.small { color: #6b7280; margin-top: 6px; }
|
| 561 |
+
.quick-box { border-left: 5px solid #f97316; background: #fff7ed; padding: 14px; margin: 20px 0; font-size: 18px; line-height: 1.5; }
|
| 562 |
+
.benchmark { margin-top: 20px; }
|
| 563 |
+
.note { margin-top: 18px; color: #6b7280; font-size: 13px; line-height: 1.5; }
|
| 564 |
+
.email-box { background: #eef2ff; border-radius: 14px; padding: 16px; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 565 |
"""
|
| 566 |
|
| 567 |
+
with gr.Blocks(css=CSS, title=APP_NAME) as demo:
|
| 568 |
+
gr.HTML("""
|
| 569 |
+
<div class="hero">
|
| 570 |
+
<h1>🐔 PoultryFeedCalc</h1>
|
| 571 |
+
<p>Simple broiler feed calculator for farmers — no calculations required.</p>
|
| 572 |
+
<p><b>Choose your birds, age, bag size and price.</b> The app calculates feed, Starter/Finisher bags and estimated feed cost.</p>
|
| 573 |
+
</div>
|
| 574 |
+
""")
|
| 575 |
|
| 576 |
+
# Hidden country/currency values populated on page load.
|
| 577 |
+
detected_country = gr.State("NG")
|
| 578 |
+
detected_currency = gr.State("NGN")
|
| 579 |
+
currency_symbol = gr.State("₦")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 580 |
|
| 581 |
+
with gr.Group(elem_classes=["email-box"]):
|
| 582 |
+
gr.Markdown("### 🔐 Before you calculate")
|
| 583 |
+
gr.Markdown("Enter your email so we can send you future PoultryFeedCalc updates and improvements.")
|
| 584 |
email = gr.Textbox(
|
| 585 |
+
label="Email address",
|
| 586 |
placeholder="farmer@example.com",
|
| 587 |
type="email",
|
| 588 |
+
info="Required before calculating. If Google sign-in is enabled, your Google email can be used automatically."
|
| 589 |
)
|
| 590 |
+
if google_enabled:
|
| 591 |
+
gr.Markdown("**Already have Google?**")
|
| 592 |
+
gr.Button("🇬 Sign in with Google", link="/login/google", variant="secondary")
|
| 593 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 594 |
gr.Markdown(
|
| 595 |
+
"Google sign-in is optional. Add `GOOGLE_CLIENT_ID` and `GOOGLE_CLIENT_SECRET` "
|
| 596 |
+
"to your Space secrets to enable it."
|
| 597 |
)
|
| 598 |
|
| 599 |
+
with gr.Row():
|
| 600 |
+
with gr.Column(scale=1):
|
| 601 |
+
gr.Markdown("## 🧮 Farm inputs")
|
| 602 |
+
|
| 603 |
birds = gr.Dropdown(
|
| 604 |
+
choices=BIRD_OPTIONS,
|
| 605 |
+
value="25",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 606 |
label="🐔 Number of birds",
|
| 607 |
+
info="Select a common flock size or choose Custom."
|
| 608 |
+
)
|
| 609 |
+
custom_birds = gr.Number(
|
| 610 |
+
label="Custom number of birds",
|
| 611 |
+
value=None,
|
| 612 |
+
precision=0,
|
| 613 |
+
minimum=1,
|
| 614 |
+
visible=False
|
| 615 |
+
)
|
| 616 |
+
birds.change(
|
| 617 |
+
lambda x: gr.update(visible=str(x).lower() == "custom"),
|
| 618 |
+
inputs=birds,
|
| 619 |
+
outputs=custom_birds
|
| 620 |
)
|
| 621 |
|
| 622 |
weeks = gr.Dropdown(
|
| 623 |
+
choices=list(range(0, 17)),
|
|
|
|
|
|
|
| 624 |
value=5,
|
| 625 |
+
label="📅 Age — Weeks"
|
| 626 |
)
|
| 627 |
+
days_extra = gr.Dropdown(
|
| 628 |
+
choices=list(range(0, 7)),
|
|
|
|
|
|
|
|
|
|
| 629 |
value=0,
|
| 630 |
label="📅 Age — Days",
|
| 631 |
+
info="Weeks + Days are converted to total days."
|
| 632 |
)
|
|
|
|
| 633 |
bag_size = gr.Dropdown(
|
| 634 |
+
choices=[10, 20, 25, 50],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 635 |
value=25,
|
| 636 |
+
label="🌾 Feed bag size (kg)"
|
| 637 |
)
|
|
|
|
| 638 |
price = gr.Number(
|
| 639 |
value=24000,
|
| 640 |
precision=0,
|
| 641 |
+
label="💰 Price per bag",
|
| 642 |
+
info="Price is shown in your detected local currency."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 643 |
)
|
| 644 |
|
| 645 |
+
calculate_btn = gr.Button(
|
| 646 |
"🐔 Calculate Feed Requirement",
|
| 647 |
variant="primary",
|
| 648 |
+
size="lg"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 649 |
)
|
| 650 |
|
| 651 |
+
with gr.Column(scale=1):
|
| 652 |
+
gr.Markdown("## 🐔 Your feed requirement")
|
| 653 |
+
result = gr.HTML(
|
| 654 |
+
"<div class='answer-card'><b>Enter your email, choose your farm inputs, then click Calculate.</b></div>"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 655 |
)
|
| 656 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 657 |
gr.Markdown("---")
|
| 658 |
+
with gr.Group():
|
| 659 |
+
gr.Markdown("## ❓ Ask a question")
|
| 660 |
+
gr.Markdown("### What would you like to know about raising your birds?")
|
| 661 |
+
gr.Markdown("Type your question. You can also use your browser/device microphone if supported by the browser.")
|
| 662 |
+
question = gr.Textbox(
|
| 663 |
+
label="Your question",
|
| 664 |
+
placeholder="e.g. How many bags of feed for 500 broilers for 8 weeks?",
|
| 665 |
+
lines=3
|
| 666 |
+
)
|
| 667 |
+
ask_btn = gr.Button("🤖 Get Answer", variant="primary", size="lg")
|
| 668 |
+
question_answer = gr.Markdown()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 669 |
|
| 670 |
gr.Markdown("---")
|
|
|
|
| 671 |
gr.Markdown(
|
| 672 |
+
"### 📌 How the estimate works\n"
|
| 673 |
+
"- Age is calculated in **total days** from Weeks + Days.\n"
|
| 674 |
+
"- Starter is counted through **day 21**; Finisher is after day 21.\n"
|
| 675 |
+
"- A **20% planning allowance** is added to the benchmark feed estimate before bags are calculated.\n"
|
| 676 |
+
"- The total number of bags is calculated from **planned feed ÷ bag size and rounded up**.\n"
|
| 677 |
+
"- The total bag count is the purchase recommendation; Starter + Finisher bag counts are allocated so they do not double-count bags.\n"
|
| 678 |
+
"- Ross 308 performance objectives are used as the numerical benchmark. Actual farm consumption can vary."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 679 |
)
|
| 680 |
|
| 681 |
+
# Load country/currency and Google session email.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 682 |
demo.load(
|
| 683 |
+
country_on_load,
|
| 684 |
inputs=None,
|
| 685 |
+
outputs=[detected_country, detected_currency, currency_symbol, price]
|
|
|
|
| 686 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 687 |
demo.load(
|
| 688 |
+
get_session_email,
|
| 689 |
inputs=None,
|
| 690 |
+
outputs=email
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 691 |
)
|
| 692 |
|
| 693 |
+
calculate_btn.click(
|
| 694 |
+
calculate_feed,
|
|
|
|
|
|
|
|
|
|
|
|
|
| 695 |
inputs=[
|
| 696 |
+
birds, custom_birds, weeks, days_extra, bag_size, price,
|
| 697 |
+
email, detected_currency, currency_symbol
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 698 |
],
|
| 699 |
+
outputs=result
|
|
|
|
| 700 |
)
|
| 701 |
|
| 702 |
+
ask_btn.click(
|
| 703 |
+
ask_question,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 704 |
inputs=[
|
| 705 |
+
question, email, birds, custom_birds, weeks, days_extra,
|
| 706 |
+
bag_size, price, detected_currency, currency_symbol
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 707 |
],
|
| 708 |
+
outputs=question_answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 709 |
)
|
| 710 |
|
| 711 |
+
# Mount once; do not call demo.launch() as well.
|
| 712 |
+
fastapi_app = gr.mount_gradio_app(
|
| 713 |
+
fastapi_app,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 714 |
demo,
|
| 715 |
path="/",
|
| 716 |
+
root_path="/",
|
| 717 |
+
footer_links=[]
|
| 718 |
)
|
| 719 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 720 |
if __name__ == "__main__":
|
|
|
|
| 721 |
import uvicorn
|
| 722 |
+
port = int(os.environ.get("PORT", "7860"))
|
| 723 |
+
uvicorn.run(fastapi_app, host="0.0.0.0", port=port)
|
|
|
|
|
|
|
|
|
|
|
|