NTEIGBANAM OKE commited on
Commit
d34c4ed
Β·
verified Β·
1 Parent(s): 7053e6f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +205 -237
app.py CHANGED
@@ -371,37 +371,29 @@ def calculate_feed(
371
  bag_size = float(bag_size)
372
  price_per_bag = float(price_per_bag)
373
  except Exception:
374
- return (
375
- "⚠️ Please enter valid numbers.",
376
- "",
377
- )
378
 
379
  if birds <= 0:
380
- return "⚠️ Number of birds must be greater than zero.", ""
381
 
382
  if weeks < 0:
383
- return "⚠️ Weeks cannot be negative.", ""
384
 
385
  if days < 0 or days > 6:
386
- return "⚠️ Days must be between 0 and 6.", ""
387
 
388
  if bag_size <= 0:
389
- return "⚠️ Feed bag size must be greater than zero.", ""
390
 
391
  if price_per_bag < 0:
392
- return "⚠️ Price per bag cannot be negative.", ""
393
 
394
  # --------------------------------------------------------
395
  # EMAIL IS REQUIRED BEFORE CALCULATION
396
  # --------------------------------------------------------
397
 
398
  if not valid_email(email):
399
- return (
400
- "πŸ“§ **Email required**\n\n"
401
- "Please enter your email address before calculating "
402
- "your feed requirement.",
403
- "",
404
- )
405
 
406
  # --------------------------------------------------------
407
  # COUNTRY / CURRENCY
@@ -489,7 +481,7 @@ def calculate_feed(
489
  )
490
 
491
  # --------------------------------------------------------
492
- # OUTPUT
493
  # --------------------------------------------------------
494
 
495
  starter_bags_display = f"{starter_bags:.1f}"
@@ -499,63 +491,71 @@ def calculate_feed(
499
  if days:
500
  age_text += f" + {days} day{'s' if days != 1 else ''}"
501
 
502
- answer = f"""
503
- ## 🎯 Quick Answer
504
-
505
- ### To raise **{birds:,} birds** for **{age_text} ({total_days} days)**
506
-
507
- You will need an estimated:
508
-
509
- # **{total_bags:.1f} bags of feed**
510
-
511
- That's approximately:
512
-
513
- # **{rounded_bags} bags**
514
-
515
- ---
516
-
517
- ### 🌾 Feed breakdown
518
-
519
- | Feed | Quantity | Bags |
520
- |---|---:|---:|
521
- | 🌱 Starter (Day 0–{STARTER_END_DAY}) | **{starter_kg:,.1f} kg** | **{starter_bags_display}** |
522
- | 🌽 Finisher (Day {STARTER_END_DAY+1}–{total_days}) | **{finisher_kg:,.1f} kg** | **{finisher_bags_display}** |
523
- | **TOTAL** | **{total_feed_kg:,.1f} kg** | **{total_bags:.1f}** |
524
-
525
- ---
526
-
527
- ### πŸ’° Feed cost
528
-
529
- At **{currency}{price_per_bag:,.0f} per bag**:
530
-
531
- # **Approximately {currency}{total_cost:,.0f}**
532
-
533
- This is the estimated cost of **feed alone**.
534
-
535
- ---
536
-
537
- ### πŸ“Š Expected Performance
538
-
539
- | Metric | Value |
540
- |---|---:|
541
- | Expected body weight per bird | **{expected_weight:.3f} kg** |
542
- | Expected flock live weight | **{flock_weight:,.1f} kg** |
543
- | Cumulative feed per bird | **{total_feed_per_bird:.3f} kg** |
544
-
545
- ---
546
-
547
- ### πŸ“ Calculation
548
 
549
- **{birds:,} birds Γ— {total_feed_per_bird:.3f} kg per bird = {total_feed_kg:,.1f} kg feed**
550
-
551
- **{total_feed_kg:,.1f} kg Γ· {bag_size:g} kg = {total_bags:.2f} bags**
552
-
553
- The calculator then rounds up to the next whole bag for purchasing:
554
-
555
- **{total_bags:.2f} β†’ {rounded_bags} bags**
556
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
557
 
558
- return answer, f"βœ… Calculation saved for {email.strip().lower()}."
559
 
560
 
561
  # ============================================================
@@ -691,7 +691,8 @@ body {
691
  }
692
 
693
  .gradio-container {
694
- max-width: 1050px !important;
 
695
  }
696
 
697
  .main-title {
@@ -710,8 +711,12 @@ body {
710
  }
711
 
712
  .answer-box {
 
713
  border-radius: 16px;
714
- padding: 8px;
 
 
 
715
  }
716
 
717
  .email-box {
@@ -721,22 +726,6 @@ body {
721
  background: #fbfdf9;
722
  }
723
 
724
- .big-answer {
725
- font-size: 19px;
726
- }
727
-
728
- .big-answer h1 {
729
- font-size: 30px !important;
730
- }
731
-
732
- .big-answer h2 {
733
- font-size: 25px !important;
734
- }
735
-
736
- .big-answer h3 {
737
- font-size: 20px !important;
738
- }
739
-
740
  button {
741
  min-height: 48px !important;
742
  }
@@ -761,136 +750,117 @@ with gr.Blocks(title=APP_NAME, css=CSS) as demo:
761
  )
762
 
763
  # --------------------------------------------------------
764
- # FARM INPUTS
765
- # --------------------------------------------------------
766
-
767
- with gr.Group():
768
-
769
- gr.Markdown("## πŸ” 1. Number of birds")
770
-
771
- birds = gr.Dropdown(
772
- choices=[
773
- 25,
774
- 50,
775
- 75,
776
- 100,
777
- 150,
778
- 200,
779
- 500,
780
- 1000,
781
- ],
782
- value=50,
783
- label="Number of birds",
784
- allow_custom_value=True,
785
- )
786
-
787
- gr.Markdown("## πŸ“… 2. Age")
788
-
789
- with gr.Row():
790
-
791
- weeks = gr.Dropdown(
792
- choices=list(range(0, 21)),
793
- value=6,
794
- label="Weeks",
795
- info="Select the number of complete weeks.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
796
  )
797
 
798
- days = gr.Dropdown(
799
- choices=list(range(0, 7)),
800
- value=0,
801
- label="Days",
802
- info="Additional days after the complete weeks.",
803
  )
804
 
805
- gr.Markdown("## 🌾 3. Feed")
 
806
 
807
- with gr.Row():
 
808
 
809
- bag_size = gr.Dropdown(
810
- choices=[
811
- 10,
812
- 20,
813
- 25,
814
- 50,
815
- ],
816
- value=25,
817
- label="Feed bag size (kg)",
818
- )
819
 
820
- price = gr.Number(
821
- value=24000,
822
- label=f"Price per bag ({DEFAULT_CURRENCY})",
823
- minimum=0,
 
 
 
 
 
824
  )
825
 
826
  # --------------------------------------------------------
827
- # EMAIL BEFORE CALCULATE
828
- # --------------------------------------------------------
829
-
830
- gr.Markdown("## πŸ“§ 4. Your email")
831
-
832
- gr.Markdown(
833
- """
834
- Enter your email before calculating.
835
-
836
- Your email lets us save your calculation and helps us
837
- understand what farmers need from the app.
838
- """
839
- )
840
-
841
- email = gr.Textbox(
842
- label="Email address",
843
- placeholder="farmer@example.com",
844
- type="email",
845
- elem_classes=["email-box"],
846
- )
847
-
848
- calculate_btn = gr.Button(
849
- "πŸ” Calculate Feed Requirements",
850
- variant="primary",
851
- size="lg",
852
- )
853
-
854
- result = gr.Markdown(
855
- value="",
856
- elem_classes=["answer-box", "big-answer"],
857
- )
858
-
859
- save_status = gr.Markdown("")
860
-
861
- # Hidden country state
862
- country_state = gr.State(DEFAULT_COUNTRY)
863
-
864
- # --------------------------------------------------------
865
- # QUESTION BOX
866
  # --------------------------------------------------------
867
 
868
  gr.Markdown("---")
869
 
870
- gr.Markdown(
871
- """
872
- # ❓ Ask a question
 
 
873
 
874
- ### What would you like to know about raising your birds?
875
- """
876
- )
877
 
878
- question = gr.Textbox(
879
- label="",
880
- placeholder="Type or speak your question...",
881
- lines=4,
882
- )
883
 
884
- ask_btn = gr.Button(
885
- "πŸ’¬ Get Answer",
886
- variant="secondary",
887
- size="lg",
888
- )
889
 
890
- question_answer = gr.Markdown(
891
- "",
892
- elem_classes=["answer-box"],
893
- )
894
 
895
  # --------------------------------------------------------
896
  # BENCHMARK REFERENCE
@@ -898,38 +868,41 @@ with gr.Blocks(title=APP_NAME, css=CSS) as demo:
898
 
899
  gr.Markdown("---")
900
 
901
- gr.Markdown(
902
- """
903
- ### πŸ“Š Ross 308 Weekly Benchmark (As-Hatched)
904
-
905
- | Days | Body Weight (kg) | Cumulative Feed (kg) |
906
- |------|------------------|----------------------|
907
- | 7 | 0.213 | 0.213 |
908
- | 14 | 0.533 | 0.533 |
909
- | 21 | 1.012 | 1.012 |
910
- | 28 | 1.616 | 1.616 |
911
- | 35 | 2.296 | 2.296 |
912
- | 42 | 2.998 | 2.998 |
913
- | 49 | 3.681 | 3.681 |
914
- | 56 | 4.316 | 4.316 |
915
-
916
- *Source: Aviagen Ross 308 Performance Objectives (2022)*
917
- """
918
- )
 
 
919
 
920
- gr.Markdown(
921
- """
922
- ### πŸ“Œ How the estimate works
 
923
 
924
- - Age is calculated in **total days** from Weeks + Days.
925
- - Starter is counted through **day 21**; Finisher is after day 21.
926
- - Total bags are calculated from **feed Γ· bag size** and **rounded up**.
927
- - Ross 308 performance objectives are used as the benchmark.
 
928
 
929
- ⚠️ Actual farm consumption can vary with genetics, sex, mortality,
930
- feed quality, temperature, management and other conditions.
931
- """
932
- )
933
 
934
  # --------------------------------------------------------
935
  # EVENTS
@@ -946,10 +919,7 @@ with gr.Blocks(title=APP_NAME, css=CSS) as demo:
946
  email,
947
  country_state,
948
  ],
949
- outputs=[
950
- result,
951
- save_status,
952
- ],
953
  )
954
 
955
  ask_btn.click(
@@ -958,9 +928,7 @@ with gr.Blocks(title=APP_NAME, css=CSS) as demo:
958
  question,
959
  email,
960
  ],
961
- outputs=[
962
- question_answer,
963
- ],
964
  )
965
 
966
 
 
371
  bag_size = float(bag_size)
372
  price_per_bag = float(price_per_bag)
373
  except Exception:
374
+ return "⚠️ Please enter valid numbers."
 
 
 
375
 
376
  if birds <= 0:
377
+ return "⚠️ Number of birds must be greater than zero."
378
 
379
  if weeks < 0:
380
+ return "⚠️ Weeks cannot be negative."
381
 
382
  if days < 0 or days > 6:
383
+ return "⚠️ Days must be between 0 and 6."
384
 
385
  if bag_size <= 0:
386
+ return "⚠️ Feed bag size must be greater than zero."
387
 
388
  if price_per_bag < 0:
389
+ return "⚠️ Price per bag cannot be negative."
390
 
391
  # --------------------------------------------------------
392
  # EMAIL IS REQUIRED BEFORE CALCULATION
393
  # --------------------------------------------------------
394
 
395
  if not valid_email(email):
396
+ return "πŸ“§ **Email required**\n\nPlease enter your email address before calculating your feed requirement."
 
 
 
 
 
397
 
398
  # --------------------------------------------------------
399
  # COUNTRY / CURRENCY
 
481
  )
482
 
483
  # --------------------------------------------------------
484
+ # OUTPUT β€” HTML FOR SIDE-BY-SIDE DISPLAY
485
  # --------------------------------------------------------
486
 
487
  starter_bags_display = f"{starter_bags:.1f}"
 
491
  if days:
492
  age_text += f" + {days} day{'s' if days != 1 else ''}"
493
 
494
+ html = f"""
495
+ <div style="font-size: 18px; line-height: 1.6;">
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
496
 
497
+ <div style="background: #f0fdf4; padding: 16px 20px; border-radius: 12px; border-left: 5px solid #22c55e; margin-bottom: 20px;">
498
+ <div style="font-size: 14px; color: #16a34a; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">🎯 Quick Answer</div>
499
+ <div style="font-size: 22px; font-weight: 700; color: #166534;">
500
+ {rounded_bags} bags of {bag_size:g} kg feed
501
+ </div>
502
+ <div style="color: #4b5563; margin-top: 4px;">
503
+ To raise <strong>{birds:,} birds</strong> for <strong>{age_text} ({total_days} days)</strong>
504
+ </div>
505
+ </div>
506
+
507
+ <div style="margin-bottom: 16px;">
508
+ <div style="font-size: 14px; color: #6b7280; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">🌾 Feed Breakdown</div>
509
+ <table style="width: 100%; border-collapse: collapse; margin-top: 6px;">
510
+ <tr style="border-bottom: 1px solid #e5e7eb;">
511
+ <td style="padding: 8px 4px;"><strong>🌱 Starter</strong></td>
512
+ <td style="padding: 8px 4px; text-align: right;">{starter_kg:,.1f} kg</td>
513
+ <td style="padding: 8px 4px; text-align: right; font-weight: 600;">{starter_bags_display} bags</td>
514
+ </tr>
515
+ <tr style="border-bottom: 1px solid #e5e7eb;">
516
+ <td style="padding: 8px 4px;"><strong>🌽 Finisher</strong></td>
517
+ <td style="padding: 8px 4px; text-align: right;">{finisher_kg:,.1f} kg</td>
518
+ <td style="padding: 8px 4px; text-align: right; font-weight: 600;">{finisher_bags_display} bags</td>
519
+ </tr>
520
+ <tr>
521
+ <td style="padding: 8px 4px; font-weight: 700;">TOTAL</td>
522
+ <td style="padding: 8px 4px; text-align: right; font-weight: 700;">{total_feed_kg:,.1f} kg</td>
523
+ <td style="padding: 8px 4px; text-align: right; font-weight: 700;">{total_bags:.1f}</td>
524
+ </tr>
525
+ </table>
526
+ </div>
527
+
528
+ <div style="margin-bottom: 16px;">
529
+ <div style="font-size: 14px; color: #6b7280; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">πŸ’° Feed Cost</div>
530
+ <div style="font-size: 28px; font-weight: 800; color: #1e40af;">
531
+ {currency}{total_cost:,.0f}
532
+ </div>
533
+ <div style="color: #6b7280; font-size: 14px;">
534
+ {rounded_bags} bags Γ— {currency}{price_per_bag:,.0f} per bag
535
+ </div>
536
+ </div>
537
+
538
+ <div style="border-top: 1px solid #e5e7eb; padding-top: 14px;">
539
+ <div style="font-size: 14px; color: #6b7280; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px;">πŸ“Š Expected Performance</div>
540
+ <table style="width: 100%; border-collapse: collapse; margin-top: 6px;">
541
+ <tr style="border-bottom: 1px solid #e5e7eb;">
542
+ <td style="padding: 6px 4px;">Body weight / bird</td>
543
+ <td style="padding: 6px 4px; text-align: right; font-weight: 600;">{expected_weight:.3f} kg</td>
544
+ </tr>
545
+ <tr>
546
+ <td style="padding: 6px 4px;">Flock live weight</td>
547
+ <td style="padding: 6px 4px; text-align: right; font-weight: 600;">{flock_weight:,.1f} kg</td>
548
+ </tr>
549
+ </table>
550
+ </div>
551
+
552
+ <div style="margin-top: 14px; font-size: 12px; color: #9ca3af; border-top: 1px solid #f3f4f6; padding-top: 10px;">
553
+ Estimate based on Ross 308 benchmark. Actual results may vary.
554
+ </div>
555
+ </div>
556
+ """
557
 
558
+ return html
559
 
560
 
561
  # ============================================================
 
691
  }
692
 
693
  .gradio-container {
694
+ max-width: 1200px !important;
695
+ margin: auto !important;
696
  }
697
 
698
  .main-title {
 
711
  }
712
 
713
  .answer-box {
714
+ background: white;
715
  border-radius: 16px;
716
+ padding: 16px 20px;
717
+ border: 1px solid #e5e7eb;
718
+ box-shadow: 0 1px 3px rgba(0,0,0,0.05);
719
+ min-height: 400px;
720
  }
721
 
722
  .email-box {
 
726
  background: #fbfdf9;
727
  }
728
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
729
  button {
730
  min-height: 48px !important;
731
  }
 
750
  )
751
 
752
  # --------------------------------------------------------
753
+ # TWO-COLUMN LAYOUT: INPUTS | RESULTS
754
+ # --------------------------------------------------------
755
+
756
+ with gr.Row(equal_height=True):
757
+
758
+ # ---------- LEFT COLUMN: INPUTS ----------
759
+ with gr.Column(scale=1, min_width=350):
760
+
761
+ with gr.Group():
762
+ gr.Markdown("## πŸ” 1. Number of birds")
763
+
764
+ birds = gr.Dropdown(
765
+ choices=[
766
+ 25, 50, 75, 100, 150, 200, 500, 1000,
767
+ ],
768
+ value=50,
769
+ label="Number of birds",
770
+ allow_custom_value=True,
771
+ )
772
+
773
+ gr.Markdown("## πŸ“… 2. Age")
774
+
775
+ with gr.Row():
776
+ weeks = gr.Dropdown(
777
+ choices=list(range(0, 21)),
778
+ value=6,
779
+ label="Weeks",
780
+ )
781
+ days = gr.Dropdown(
782
+ choices=list(range(0, 7)),
783
+ value=0,
784
+ label="+ Days",
785
+ )
786
+
787
+ gr.Markdown("## 🌾 3. Feed")
788
+
789
+ with gr.Row():
790
+ bag_size = gr.Dropdown(
791
+ choices=[10, 20, 25, 50],
792
+ value=25,
793
+ label="Bag size (kg)",
794
+ )
795
+ price = gr.Number(
796
+ value=24000,
797
+ label=f"Price per bag ({DEFAULT_CURRENCY})",
798
+ minimum=0,
799
+ )
800
+
801
+ gr.Markdown("## πŸ“§ 4. Your email")
802
+
803
+ email = gr.Textbox(
804
+ label="",
805
+ placeholder="farmer@example.com",
806
+ type="email",
807
+ elem_classes=["email-box"],
808
  )
809
 
810
+ calculate_btn = gr.Button(
811
+ "πŸ” Calculate",
812
+ variant="primary",
813
+ size="lg",
 
814
  )
815
 
816
+ # Hidden country state
817
+ country_state = gr.State(DEFAULT_COUNTRY)
818
 
819
+ # ---------- RIGHT COLUMN: RESULTS ----------
820
+ with gr.Column(scale=1, min_width=350):
821
 
822
+ gr.Markdown("## 🎯 Quick Answer")
 
 
 
 
 
 
 
 
 
823
 
824
+ result = gr.HTML(
825
+ value="""
826
+ <div style="font-size: 16px; color: #9ca3af; text-align: center; padding: 40px 20px;">
827
+ <div style="font-size: 48px; margin-bottom: 12px;">πŸ”</div>
828
+ <div>Enter your details and click <strong>Calculate</strong></div>
829
+ <div style="font-size: 14px; margin-top: 8px;">Your feed estimate will appear here</div>
830
+ </div>
831
+ """,
832
+ elem_classes=["answer-box"],
833
  )
834
 
835
  # --------------------------------------------------------
836
+ # QUESTION SECTION (Below the two columns)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
837
  # --------------------------------------------------------
838
 
839
  gr.Markdown("---")
840
 
841
+ with gr.Row():
842
+ with gr.Column(scale=2):
843
+ gr.Markdown(
844
+ """
845
+ # ❓ Ask a question
846
 
847
+ ### What would you like to know about raising your birds?
848
+ """
849
+ )
850
 
851
+ question = gr.Textbox(
852
+ label="",
853
+ placeholder="Type or speak your question...",
854
+ lines=3,
855
+ )
856
 
857
+ ask_btn = gr.Button(
858
+ "πŸ’¬ Get Answer",
859
+ variant="secondary",
860
+ size="lg",
861
+ )
862
 
863
+ question_answer = gr.Markdown("")
 
 
 
864
 
865
  # --------------------------------------------------------
866
  # BENCHMARK REFERENCE
 
868
 
869
  gr.Markdown("---")
870
 
871
+ with gr.Row():
872
+ with gr.Column(scale=1):
873
+ gr.Markdown(
874
+ """
875
+ ### πŸ“Š Ross 308 Weekly Benchmark
876
+
877
+ | Days | Body Weight (kg) | Cum Feed (kg) |
878
+ |------|------------------|---------------|
879
+ | 7 | 0.213 | 0.213 |
880
+ | 14 | 0.533 | 0.533 |
881
+ | 21 | 1.012 | 1.012 |
882
+ | 28 | 1.616 | 1.616 |
883
+ | 35 | 2.296 | 2.296 |
884
+ | 42 | 2.998 | 2.998 |
885
+ | 49 | 3.681 | 3.681 |
886
+ | 56 | 4.316 | 4.316 |
887
+
888
+ *Source: Aviagen Ross 308 (2022)*
889
+ """
890
+ )
891
 
892
+ with gr.Column(scale=1):
893
+ gr.Markdown(
894
+ """
895
+ ### πŸ“Œ How it works
896
 
897
+ - **Weeks + Days** β†’ total days
898
+ - **Starter** β†’ Day 0–21
899
+ - **Finisher** β†’ Day 22+
900
+ - **Bags** β†’ rounded up to next whole bag
901
+ - **Ross 308 benchmark** used for estimates
902
 
903
+ ⚠️ Actual results may vary with management, feed quality, and other conditions.
904
+ """
905
+ )
 
906
 
907
  # --------------------------------------------------------
908
  # EVENTS
 
919
  email,
920
  country_state,
921
  ],
922
+ outputs=result,
 
 
 
923
  )
924
 
925
  ask_btn.click(
 
928
  question,
929
  email,
930
  ],
931
+ outputs=question_answer,
 
 
932
  )
933
 
934