File size: 6,985 Bytes
21aec01 | 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | import gradio as gr
from agents.chat import chat_with_gemini
from agents.compare import compare_selected_cards
from data import all_card_names,all_card_lookup
from recommender.recommender import recommend_cards_gradio
# Interface with Tabs
with gr.Blocks() as demo:
gr.Markdown("# Credit Card Recommender")
gr.Markdown("Get personalized credit card suggestions based on your lifestyle and eligibility.")
with gr.Tabs():
with gr.Tab(" Get Recommendations"):
with gr.Row():
user_query = gr.Textbox(
label="Enter your query",
info="E.g., 'Best cards for international travel' or 'I want cashback cards with lounge access'"
)
preferences = gr.CheckboxGroup(
choices=["Cashback", "Travel", "Fuel", "Airport Lounge access",
"Railways", "Dining", "Online Spends", "Grocery"],
label="Credit card categories:",
info="Select the features or benefits you want from your credit card"
)
with gr.Accordion("Eligibility filters menu", open=False):
with gr.Row():
income = gr.Slider(
minimum=1, maximum=60, step=1,
label="Annual Income (LPA) Minimum requirement is 2.5",
info="Helps filter cards based on your income eligibility (in Lakhs Per Annum)"
)
cibil = gr.Slider(
minimum=300, maximum=900, step=10,
label="CIBIL Score",
info="Most of the cards requires a credit score of 700+"
)
age = gr.Slider(
minimum=18, maximum=75, step=1,
label="Age",
info="Some cards have minimum and maximum age eligibility"
)
with gr.Row():
min_joining_fee = gr.Number(
label="Min Joining Fee (₹)", value=0,
info="Minimum one-time fee to get the card"
)
max_joining_fee = gr.Number(
label="Max Joining Fee (₹)", value=150000,
info="Maximum one-time fee to get the card"
)
with gr.Row():
min_annual_fee = gr.Number(
label="Min Annual Fee (₹)", value=0,
info="Minimum yearly fee to be paid"
)
max_annual_fee = gr.Number(
label="Max Annual Fee (₹)", value=150000,
info="Maximum yearly fee to be paid"
)
with gr.Row():
use_eligibility = gr.Checkbox(
label="Apply Eligibility Filter", value=False,
info="Enable this to get recommendations of the cards only for which you are eligible for"
)
submit_btn = gr.Button("Recommend Cards", variant='primary')
top_card_html = gr.HTML()
card_df = gr.Dataframe(headers=["Card Name", "Matched Features", "Description"])
card_file = gr.File(label="Download Full Recommendations (CSV)")
with gr.Tab(" Compare Cards"):
gr.Markdown("### Compare Recommended Cards")
compare_checkboxes = gr.CheckboxGroup(
choices=[], label="Select 2 or more cards to compare",
info="Pick 2+ cards from the recommended list to see a comparison"
)
compare_output = gr.HTML(value="<div style='min-height:100px'></div>", visible=True)
compare_btn = gr.Button("Compare Selected Cards", variant='primary')
gr.Markdown("### Compare Any Cards from Full List")
full_compare_dropdown = gr.Dropdown(
choices=all_card_names, multiselect=True, label="Select any 2+ cards",
info="Manually compare any cards from the full database"
)
full_compare_btn = gr.Button("Compare Selected Cards", variant='primary')
full_compare_output = gr.HTML(value="<div style='min-height:100px'></div>", visible=True)
with gr.Tab(" Ask Follow-up Questions"):
gr.Markdown("### Ask any follow-up question ")
chatbot = gr.Chatbot(type='messages')
user_query_for_chat = gr.Textbox(
label="Enter your question",
info="Ask follow-ups like 'Which card has better travel insurance?' or 'Which card has less annual fee'",
)
submit_query_btn = gr.Button("Submit Query", variant='primary')
card_names_state = gr.State()
card_lookup_state = gr.State()
chat_history = gr.State([])
query = gr.State([])
def wrapped_recommend_cards(user_query, preferences, income, cibil, age, min_joining_fee, max_joining_fee,
min_annual_fee, max_annual_fee, use_eligibility):
top_html, df, file, card_names, card_lookup, direct_query = recommend_cards_gradio(
user_query, preferences, income, cibil, age, min_joining_fee, max_joining_fee,
min_annual_fee, max_annual_fee, use_eligibility
)
df_label = f"Found {len(card_names)} cards"
return top_html, gr.update(value=df, label=df_label), file, card_names, card_lookup, gr.update(choices=card_names, value=[]), direct_query
submit_btn.click(
fn=wrapped_recommend_cards,
inputs=[user_query, preferences, income, cibil, age, min_joining_fee, max_joining_fee,
min_annual_fee, max_annual_fee, use_eligibility],
outputs=[top_card_html, card_df, card_file, card_names_state, card_lookup_state, compare_checkboxes,query]
)
compare_btn.click(
fn=compare_selected_cards,
inputs=[compare_checkboxes, card_lookup_state],
outputs=compare_output,
show_progress=True
)
full_compare_btn.click(
fn=lambda selected: compare_selected_cards(selected, all_card_lookup),
inputs=[full_compare_dropdown],
outputs=full_compare_output
)
submit_query_btn.click(
fn=chat_with_gemini,
inputs=[query,user_query_for_chat, chat_history, card_lookup_state],
outputs=[chatbot, chat_history]
).then(
lambda: gr.update(value=""),
inputs=[],
outputs=[user_query_for_chat]
)
#for submitting using enter button
user_query_for_chat.submit(
fn=chat_with_gemini,
inputs=[query, user_query_for_chat, chat_history, card_lookup_state],
outputs=[chatbot, chat_history],
show_progress=True
).then(
lambda: gr.update(value=""),
inputs=[],
outputs=[user_query_for_chat]
)
|