Spaces:
Running
Running
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 <b>Get recommendations</b>.</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 | |
) | |