Spaces:
Running on Zero
Running on Zero
| import gradio as gr | |
| import spaces | |
| # Africa Science & Finance AI Demo β Full Portfolio | |
| # AutoScientist Challenge 2026, Part 2 | |
| # Author: Hussein Adeiza (mabera) β Licensed Environmental Health Officer, Abuja Nigeria | |
| # ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # ββ HIV Funding & Response Interpreter ββββββββββββββββββββββ | |
| # ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| HIV_EXAMPLES = { | |
| "Regional donor dependency split (West/Central vs East/Southern Africa)": { | |
| "stats": "Donors cover approximately 90 percent of antiretroviral (ARV) drug costs in West and Central Africa, compared to approximately 38 percent in East and Southern Africa. Several high-burden East and Southern African countries are almost entirely dependent on PEPFAR specifically for HIV prevention programs: Malawi at 88.5 percent, Zimbabwe at 82.7 percent, and Mozambique at 81.8 percent.", | |
| "interpretation": "The regional split matters more than the continental average suggests: West and Central Africa's near-total donor dependency for treatment costs means any disruption there would affect ongoing ARV supply directly, while East and Southern Africa's lower regional average masks enormous country-level variance specifically in prevention funding." | |
| }, | |
| "The 2025 PrEP collapse and Nigeria's disproportionate hit": { | |
| "stats": "PrEP use across 62 reporting countries fell from 3.3 million people in 2024 to 2.1 million in 2025, a 38 percent decline. In Nigeria specifically, monthly PrEP initiations fell from approximately 40,000 to approximately 6,000 following budget cuts, an 85 percent reduction.", | |
| "interpretation": "The gap between the 38 percent multi-country decline and Nigeria's 85 percent collapse suggests Nigeria's prevention infrastructure was disproportionately exposed, likely reflecting how concentrated Nigeria's PrEP delivery was within externally funded programs rather than blended with domestic health system capacity." | |
| }, | |
| } | |
| def interpret_hiv_stats(selected_example, custom_stats): | |
| if custom_stats and custom_stats.strip(): | |
| return f"## π HIV Funding Interpretation\n\n**Input:** {custom_stats}\n\n*Demo mode, select a dropdown example for a cited interpretation.*" | |
| example = HIV_EXAMPLES.get(selected_example, list(HIV_EXAMPLES.values())[0]) | |
| return f""" | |
| ## π HIV Funding & Response Interpretation | |
| **Raw cited statistics:** | |
| {example['stats']} | |
| **Structured interpretation:** | |
| {example['interpretation']} | |
| --- | |
| β οΈ Domain classification issue disclosed: trained on Llama 4 Scout (News-domain routing), not Llama 3.3 70B. Win rate 71% on dataset, 60% General Win Rate. | |
| *Africa HIV Funding Analysis Model | AutoScientist 2026 Part 2* | |
| """ | |
| # ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # ββ AMR Closed-Label Classifier ββββββββββββββββββββββββββββββ | |
| # ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def classify_rate(n_tested, n_nonsusceptible): | |
| if not n_tested or n_tested <= 0: | |
| return "Please enter a valid number of isolates tested." | |
| if n_nonsusceptible is None or n_nonsusceptible < 0 or n_nonsusceptible > n_tested: | |
| return "Please enter a valid non-susceptible count (0 to N)." | |
| rate = round((n_nonsusceptible / n_tested) * 100, 1) | |
| level, color = ("Low", "π’") if rate < 20 else ("Moderate", "π‘") if rate <= 50 else ("High", "π΄") | |
| return f""" | |
| ## {color} Resistance Rate: {rate}% β {level} Resistance | |
| **Calculation:** {int(n_nonsusceptible)} / {int(n_tested)} Γ 100 = {rate}% | |
| Same calculation method used to build and verify all 28 national surveillance rows against Nigeria's real MAAP/Fleming Fund report. | |
| --- | |
| *Nigeria AMR Classifier v2 | Win rate 58% (71% general) | AutoScientist 2026* | |
| """ | |
| # ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # ββ Loan Classification Closed-Label Classifier ββββββββββββββ | |
| # ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def classify_loan_days(days): | |
| if days is None or days < 0: | |
| return "Please enter a valid number of days past due." | |
| days = int(days) | |
| if days <= 89: | |
| c, p, color = "Performing", "1%", "π’" | |
| elif days <= 179: | |
| c, p, color = "Substandard", "10%", "π‘" | |
| elif days <= 359: | |
| c, p, color = "Doubtful", "50%", "π " | |
| else: | |
| c, p, color = "Lost", "100%", "π΄" | |
| return f""" | |
| ## {color} Classification: {c} | |
| **Input:** {days} days past due | **Required provisioning:** {p} | |
| **CBN thresholds:** Performing (0-89), Substandard (90-179), Doubtful (180-359), Lost (360+) | |
| --- | |
| *Nigeria Loan Classifier v2 | Win rate 40% (45% general), honestly disclosed | AutoScientist 2026* | |
| """ | |
| def classify_npl_ratio(npl_ratio): | |
| if npl_ratio is None or npl_ratio < 0: | |
| return "Please enter a valid NPL ratio." | |
| ratio = float(npl_ratio) | |
| status, color = ("Compliant", "π’") if ratio < 5.0 else ("Breach", "π΄") | |
| return f""" | |
| ## {color} Classification: {status} | |
| **Input:** {ratio}% NPL ratio | **CBN threshold:** 5.0% | |
| Same method used to verify all 9 bank/industry rows, including a real near-miss case at 4.99%. | |
| --- | |
| *Nigeria Loan Classifier v2 | AutoScientist 2026* | |
| """ | |
| # ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # ββ Nigeria Energy Access Interpreter (strongest result) βββββ | |
| # ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| ENERGY_EXAMPLES = { | |
| "2024β2025 gas-to-hydro shift": { | |
| "stats": "Nigeria's generation mix, from Our World in Data: 2024, gas share 75.0%, hydro share 24.494%. 2025, gas share 68.681%, hydro share 30.862%.", | |
| "interpretation": "Gas share fell by 6.3 percentage points while hydro share rose by 6.4 points between 2024 and 2025. This is a real, measurable one-year shift toward renewable generation, though gas remains dominant at over two-thirds of total generation. A single year of hydro gains could reflect improved rainfall rather than new capacity, this would need checking against rainfall data to confirm the driver." | |
| }, | |
| "Nigeria vs World: the 22x per-capita gap": { | |
| "stats": "Per-capita electricity access, 2025, from Our World in Data: Nigeria 174.9 kWh/person, World average 3,859.8 kWh/person.", | |
| "interpretation": "The world average is 22.1 times higher than Nigeria's figure. With a population of over 237 million, this is not a small-country anomaly but a genuine national-scale electricity access gap. A gap this large reflects a fundamental shortfall in installed generation capacity and grid infrastructure relative to population size." | |
| }, | |
| "Nigeria vs South Africa: same continent, vastly different access": { | |
| "stats": "2025 comparison: Nigeria per-capita electricity 174.9 kWh (fossil share 68.7%), South Africa per-capita electricity 3,749.3 kWh (fossil share 82.2%).", | |
| "interpretation": "South Africa's per-capita access is roughly 21.4 times higher than Nigeria's, despite South Africa having a far smaller population (65 million vs 237 million). Both countries rely heavily on fossil fuels, so the gap isn't about fuel choice, it's about the scale of generation capacity relative to population." | |
| }, | |
| "25-year trend: the long game vs the recent uptick": { | |
| "stats": "Nigeria's generation mix: 2000, gas share 61.7%, hydro share 38.2%. 2025, gas share 68.7%, hydro share 30.9%.", | |
| "interpretation": "Over 25 years, gas share rose 6.9 percentage points while hydro fell 7.3 points, the long-term trend is toward increasing gas dependency, not away from it. A single recent year of hydro gains should not be read as reversing a 25-year structural trend without multiple years of confirmation." | |
| }, | |
| "Solar's near-total absence despite equatorial advantage": { | |
| "stats": "Solar share of generation, 2025: Nigeria 0.313%, World average 8.745%.", | |
| "interpretation": "Nigeria's solar share sits at roughly 3.6% of the world average, despite Nigeria's equatorial location giving it higher and more consistent solar irradiance than most solar-adopting nations at higher latitudes. This gap points toward solar as a large, underexploited opportunity, though it wouldn't resolve underlying grid constraints alone." | |
| }, | |
| } | |
| def interpret_energy_stats(selected_example, custom_stats): | |
| if custom_stats and custom_stats.strip(): | |
| return f"## β‘ Energy Interpretation\n\n**Input:** {custom_stats}\n\n*Demo mode, select a dropdown example for a cited interpretation computed from the real downloaded data.*" | |
| example = ENERGY_EXAMPLES.get(selected_example, list(ENERGY_EXAMPLES.values())[0]) | |
| return f""" | |
| ## β‘ Nigeria Energy Interpretation | |
| **Raw statistics (from Our World in Data, downloaded directly):** | |
| {example['stats']} | |
| **Structured interpretation:** | |
| {example['interpretation']} | |
| --- | |
| ### π Strongest result in this portfolio | |
| **Win rate: 88% adapted vs 12% base | General Win Rate: 81% adapted vs 19% base** | |
| Every number above traces directly to the raw, unmodified source file (owid_energy_full.csv), downloaded from github.com/owid/energy-data, not hand-typed. All claims independently re-verified. | |
| *Nigeria Energy Access Interpreter β Fine-tuned Llama 3.3 70B | AutoScientist 2026 | Data Visualization Category* | |
| """ | |
| # ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # ββ Build Interface βββββββββββββββββββββββββββββββββββββββββ | |
| # ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| with gr.Blocks(title="Africa Science, Finance & Energy AI") as demo: | |
| gr.Markdown(""" | |
| # ππ§«π¦β‘ Africa Science, Finance & Energy AI Demo | |
| ## AutoScientist Challenge 2026, Part 2 | |
| **Author:** Hussein Adeiza (mabera) β Licensed Environmental Health Officer, Abuja Nigeria | |
| **Four models:** HIV Funding + AMR Classifier v2 + Loan Classifier v2 + Energy Access Interpreter | |
| **Powered by Adaptive Data β Adaption Labs** | |
| """) | |
| with gr.Tabs(): | |
| with gr.Tab("π HIV Funding & Response"): | |
| gr.Markdown("### Raw HIV funding stats in, structured interpretation out") | |
| with gr.Row(): | |
| with gr.Column(): | |
| hiv_example = gr.Dropdown(choices=list(HIV_EXAMPLES.keys()), value=list(HIV_EXAMPLES.keys())[0], label="Select a cited finding") | |
| hiv_custom = gr.Textbox(label="Or paste your own stats (demo mode)", lines=2) | |
| hiv_btn = gr.Button("π Interpret", variant="primary") | |
| with gr.Column(): | |
| hiv_output = gr.Markdown() | |
| hiv_btn.click(interpret_hiv_stats, inputs=[hiv_example, hiv_custom], outputs=hiv_output) | |
| with gr.Tab("π§ͺ AMR: Surveillance Rate"): | |
| gr.Markdown("### Enter isolate counts, see resistance rate computed live") | |
| with gr.Row(): | |
| with gr.Column(): | |
| n_tested = gr.Number(label="Isolates tested (N)", value=2528, precision=0) | |
| n_nonsusceptible = gr.Number(label="Non-susceptible (n)", value=1766, precision=0) | |
| rate_btn = gr.Button("π§ͺ Calculate", variant="primary") | |
| with gr.Column(): | |
| rate_output = gr.Markdown() | |
| rate_btn.click(classify_rate, inputs=[n_tested, n_nonsusceptible], outputs=rate_output) | |
| with gr.Tab("π¦ Loan: Days Past Due"): | |
| gr.Markdown("### Enter days past due, see CBN classification computed live") | |
| with gr.Row(): | |
| with gr.Column(): | |
| loan_days = gr.Number(label="Days past due", value=45, precision=0) | |
| loan_days_btn = gr.Button("π¦ Classify", variant="primary") | |
| with gr.Column(): | |
| loan_days_output = gr.Markdown() | |
| loan_days_btn.click(classify_loan_days, inputs=[loan_days], outputs=loan_days_output) | |
| with gr.Tab("π¦ Loan: NPL Ratio"): | |
| gr.Markdown("### Enter an NPL ratio, see CBN compliance computed live") | |
| with gr.Row(): | |
| with gr.Column(): | |
| npl_input = gr.Number(label="NPL ratio (%)", value=4.99, precision=2) | |
| npl_btn = gr.Button("π¦ Check", variant="primary") | |
| with gr.Column(): | |
| npl_output = gr.Markdown() | |
| npl_btn.click(classify_npl_ratio, inputs=[npl_input], outputs=npl_output) | |
| with gr.Tab("β‘ Energy Access Interpreter π"): | |
| gr.Markdown("### Real data downloaded directly from Our World in Data") | |
| gr.Markdown("π **Strongest result in this portfolio: 88% win rate, 81% General Win Rate**") | |
| with gr.Row(): | |
| with gr.Column(): | |
| energy_example = gr.Dropdown(choices=list(ENERGY_EXAMPLES.keys()), value=list(ENERGY_EXAMPLES.keys())[0], label="Select a real finding") | |
| energy_custom = gr.Textbox(label="Or paste your own stats (demo mode)", lines=2) | |
| energy_btn = gr.Button("β‘ Interpret", variant="primary") | |
| with gr.Column(): | |
| energy_output = gr.Markdown() | |
| energy_btn.click(interpret_energy_stats, inputs=[energy_example, energy_custom], outputs=energy_output) | |
| gr.Markdown(""" | |
| --- | |
| ### Models β Open Source | |
| | Model | Category | Win Rate | Quality | Note | | |
| |-------|----------|----------|---------|------| | |
| | HIV Funding Analysis | Science | 71% (60% general) | Grade C | Domain issue disclosed | | |
| | AMR Classifier v2 | Science | 58% (71% general) | Grade B | 34/34 verified | | |
| | Loan Classifier v2 | Personal Finance | 40% (45% general) | Grade B | 20/20 verified, honest disclosure | | |
| | **Energy Access Interpreter** | **Data Visualization** | **88% (81% general)** | **Grade B** | **Strongest result, real downloaded data** | | |
| π€ [HIV](https://huggingface.co/mabera/africa-hiv-funding-analysis-model) | | |
| π€ [AMR](https://huggingface.co/mabera/nigeria-amr-classifier-v2) | | |
| π€ [Loan](https://huggingface.co/mabera/nigeria-loan-classifier-v2) | | |
| π€ [Energy](https://huggingface.co/mabera/nigeria-energy-access-interpreter) | |
| ### Other Portfolio Demos | |
| - Part 1: [nigeria-health-ai-demo](https://huggingface.co/spaces/mabera/nigeria-health-ai-demo) | |
| - Part 2 Agriculture: [nigeria-agriculture-ai](https://huggingface.co/spaces/mabera/nigeria-agriculture-ai) | |
| """) | |
| demo.launch(theme=gr.themes.Soft()) |