File size: 3,452 Bytes
1c6c5d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os

import gradio as gr

from common import org_search_component as oss
from formatting import process_reasons, parse_pcs_descriptions, parse_geo_descriptions
from services import RfpRecommend

api = RfpRecommend()


def recommend_invoke(recipient: gr.State):
    response = api(candid_entity_id=recipient[0])
    output = []
    for rfp in (response.get("recommendations", []) or []):
        output.append([
            rfp["funder_id"],
            rfp["funder_name"],
            rfp["funder_address"],
            rfp["amount"],
            (
                f"<a href='{rfp['application_url']}' target='_blank' rel='noopener noreferrer'>"
                f"{rfp['application_url']}</a>"
            ),
            rfp["deadline"],
            rfp["description"],
            parse_pcs_descriptions(rfp["taxonomy"]),
            parse_geo_descriptions(rfp["area_served"])
        ])
    return (
        output,
        process_reasons(response.get("meta", {}) or {}),
        response.get("recommendations", [])
    )


def build_demo():
    with gr.Blocks(theme=gr.themes.Soft(), title="RFP recommendations") as demo:
        gr.Markdown(
            """
            <h1>RFP recommendations</h1>

            <p>Receive recommendations for funding opportunities relevant to your work.</p>
            <p>To get started lookup your nonprofit and then click **Get recommendations**.</p>
            """
        )

        with gr.Row():
            with gr.Column():
                _, selected_org_state = oss.render()
        with gr.Row():
            recommend = gr.Button("Get recommendations", scale=5, variant="primary")
        with gr.Row():
            with gr.Accordion(label="Parameters used for recommendations", open=False):
                reasons_output = gr.DataFrame(
                    col_count=3,
                    headers=["Reason category", "Reason value", "Reason description"],
                    interactive=False
                )

        rec_outputs = gr.DataFrame(
            label="Recommended RFPs",
            type="array",
            headers=[
                "Funder ID", "Name", "Address",
                "Amount", "Application URL", "Deadline",
                "Description", "About", "Where"
            ],
            col_count=(9, "fixed"),
            datatype=[
                "number", "str", "str",
                "str", "markdown", "date",
                "str", "markdown", "markdown"
            ],
            wrap=True,
            max_height=1000,
            column_widths=[
                "5%", "10%", "20%",
                "5", "15%", "5%",
                "10%", "10%", "20%"
            ],
            interactive=False
        )

        with gr.Accordion("JSON output", open=False):
            recommendations_json = gr.JSON(label="Recommended RFPs JSON")

        # pylint: disable=no-member
        recommend.click(
            fn=recommend_invoke,
            inputs=[selected_org_state],
            outputs=[rec_outputs, reasons_output, recommendations_json]
        )

    return demo


if __name__ == '__main__':
    app = build_demo()
    app.queue(max_size=5).launch(
        show_api=False,
        auth=[
            (os.getenv("APP_USERNAME"), os.getenv("APP_PASSWORD")),
            (os.getenv("APP_PUBLIC_USERNAME"), os.getenv("APP_PUBLIC_PASSWORD")),
        ],
        auth_message="Login to Candid's letter of intent demo",
        ssr_mode=False
    )