Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,7 @@ import gradio as gr
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# β
Securely retrieve the Groq API key from Hugging Face Secrets
|
| 6 |
-
groqkey = os.getenv("groqkey")
|
| 7 |
|
| 8 |
# β
Ensure the API key exists
|
| 9 |
if not groqkey:
|
|
@@ -13,7 +13,7 @@ if not groqkey:
|
|
| 13 |
client = Groq(api_key=groqkey)
|
| 14 |
|
| 15 |
# β
Define chatbot response function
|
| 16 |
-
def get_chatbot_response(user_message, insurance_type, country,
|
| 17 |
"""Fetch insurance-related responses from the AI."""
|
| 18 |
|
| 19 |
system_message = (
|
|
@@ -33,9 +33,6 @@ def get_chatbot_response(user_message, insurance_type, country, image, conversat
|
|
| 33 |
|
| 34 |
conversation_history.append({"role": "user", "content": user_message})
|
| 35 |
|
| 36 |
-
if image:
|
| 37 |
-
conversation_history.append({"role": "user", "content": "[User uploaded an image]"})
|
| 38 |
-
|
| 39 |
# β
API call to Groq
|
| 40 |
completion = client.chat.completions.create(
|
| 41 |
model="mixtral-8x7b",
|
|
@@ -57,7 +54,7 @@ def get_chatbot_response(user_message, insurance_type, country, image, conversat
|
|
| 57 |
|
| 58 |
return conversation_history, chat_display
|
| 59 |
|
| 60 |
-
# β
Styling and Theme
|
| 61 |
theme = gr.themes.Base().set(
|
| 62 |
body_background_fill="linear-gradient(to right, #001F3F, #0052CC)", # Dark navy to royal blue
|
| 63 |
button_primary_background_fill="linear-gradient(135deg, #FFD700, #FFAA00)", # Golden gradient
|
|
@@ -65,7 +62,7 @@ theme = gr.themes.Base().set(
|
|
| 65 |
)
|
| 66 |
|
| 67 |
custom_css = """
|
| 68 |
-
/*
|
| 69 |
.title-text {
|
| 70 |
background: linear-gradient(90deg, #FFD700, #FFAA00);
|
| 71 |
-webkit-background-clip: text;
|
|
@@ -73,105 +70,136 @@ custom_css = """
|
|
| 73 |
color: transparent;
|
| 74 |
-webkit-text-fill-color: transparent;
|
| 75 |
font-weight: 700;
|
| 76 |
-
font-size:
|
| 77 |
text-align: center;
|
| 78 |
text-transform: uppercase;
|
| 79 |
-
letter-spacing:
|
| 80 |
-
text-shadow:
|
| 81 |
}
|
| 82 |
|
| 83 |
-
/*
|
| 84 |
body, .gradio-container {
|
| 85 |
-
color: #FFD700 !important;
|
| 86 |
font-family: 'Inter', sans-serif;
|
| 87 |
}
|
| 88 |
|
| 89 |
-
/* Chat messages -
|
| 90 |
.chat-message {
|
| 91 |
-
background: rgba(255, 255, 255, 0.
|
| 92 |
border-radius: 12px;
|
| 93 |
padding: 12px;
|
| 94 |
-
backdrop-filter: blur(
|
| 95 |
-
color:
|
| 96 |
}
|
| 97 |
|
| 98 |
-
/* Buttons */
|
| 99 |
.insurance-button {
|
| 100 |
border: 2px solid rgba(255, 215, 0, 0.8);
|
| 101 |
background: rgba(0, 0, 0, 0.2);
|
| 102 |
-
font-size:
|
| 103 |
font-weight: bold;
|
| 104 |
-
padding: 8px
|
| 105 |
-
border-radius:
|
| 106 |
margin: 5px;
|
| 107 |
-
color: #FFD700;
|
| 108 |
transition: all 0.3s ease-in-out;
|
| 109 |
}
|
| 110 |
|
|
|
|
| 111 |
.insurance-button:hover {
|
| 112 |
background: linear-gradient(90deg, #FFD700, #FFAA00);
|
| 113 |
color: black !important;
|
| 114 |
transform: scale(1.05);
|
| 115 |
-
box-shadow: 0 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
}
|
| 117 |
|
| 118 |
-
/* Input field */
|
| 119 |
textarea {
|
| 120 |
background: rgba(255, 255, 255, 0.05);
|
| 121 |
border: 1px solid rgba(255, 215, 0, 0.5);
|
| 122 |
border-radius: 8px;
|
| 123 |
-
color:
|
| 124 |
font-size: 16px;
|
| 125 |
padding: 10px;
|
| 126 |
}
|
| 127 |
|
|
|
|
| 128 |
textarea:focus {
|
| 129 |
box-shadow: 0 0 10px rgba(255, 215, 0, 0.8);
|
| 130 |
border-color: #FFD700;
|
| 131 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
"""
|
| 133 |
|
|
|
|
| 134 |
def clear_history():
|
| 135 |
return []
|
| 136 |
|
| 137 |
# β
Build Gradio Interface
|
| 138 |
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
| 139 |
-
gr.HTML("<h2 class='title-text'
|
| 140 |
-
gr.Markdown("### Select your insurance type, country, and ask your question
|
| 141 |
|
| 142 |
with gr.Row():
|
| 143 |
insurance_type_input = gr.Dropdown(
|
| 144 |
["Health", "Car", "Home", "Travel", "Life", "Disability", "Business", "Pet"],
|
| 145 |
-
label="Select Insurance Type",
|
| 146 |
interactive=True
|
| 147 |
)
|
| 148 |
country_input = gr.Dropdown(
|
| 149 |
["USA", "Canada", "UK", "Germany", "France", "India", "Australia", "Other"],
|
| 150 |
-
label="Select Country",
|
| 151 |
interactive=True
|
| 152 |
)
|
| 153 |
|
| 154 |
custom_country_input = gr.Textbox(label="Enter Country (if not listed)", visible=False)
|
| 155 |
|
| 156 |
conversation_state = gr.State([])
|
| 157 |
-
|
| 158 |
-
chatbot = gr.Chatbot(label="Chat History")
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
clear_button = gr.Button("Clear Chat History")
|
| 161 |
clear_button.click(lambda: [], outputs=[conversation_state, chatbot])
|
| 162 |
|
| 163 |
with gr.Row():
|
| 164 |
-
health_btn = gr.Button("Health", elem_classes="insurance-button")
|
| 165 |
-
car_btn = gr.Button("Car", elem_classes="insurance-button")
|
| 166 |
-
home_btn = gr.Button("Home", elem_classes="insurance-button")
|
| 167 |
-
travel_btn = gr.Button("Travel", elem_classes="insurance-button")
|
| 168 |
-
life_btn = gr.Button("Life", elem_classes="insurance-button")
|
| 169 |
-
disability_btn = gr.Button("Disability", elem_classes="insurance-button")
|
| 170 |
-
business_btn = gr.Button("Business", elem_classes="insurance-button")
|
| 171 |
-
pet_btn = gr.Button("Pet", elem_classes="insurance-button")
|
| 172 |
-
|
| 173 |
-
question_input = gr.Textbox(label="Ask your question...", placeholder="Describe your insurance question...", interactive=True)
|
| 174 |
-
image_input = gr.File(label="Upload an image (optional)", type="file")
|
| 175 |
|
| 176 |
def update_insurance_selection(current, new_selection):
|
| 177 |
if "Insurance:" in current:
|
|
@@ -189,12 +217,21 @@ with gr.Blocks(theme=theme, css=custom_css) as demo:
|
|
| 189 |
btn.click(lambda current, insurance=insurance: update_insurance_selection(current, insurance),
|
| 190 |
inputs=question_input, outputs=question_input)
|
| 191 |
|
| 192 |
-
submit_btn = gr.Button("Send")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 193 |
|
| 194 |
submit_btn.click(
|
| 195 |
-
|
| 196 |
-
inputs=[
|
| 197 |
-
outputs=[conversation_state, chatbot]
|
| 198 |
)
|
| 199 |
|
| 200 |
-
demo.launch()
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
# β
Securely retrieve the Groq API key from Hugging Face Secrets
|
| 6 |
+
groqkey = os.getenv("groqkey") # Make sure this matches the secret name in Hugging Face
|
| 7 |
|
| 8 |
# β
Ensure the API key exists
|
| 9 |
if not groqkey:
|
|
|
|
| 13 |
client = Groq(api_key=groqkey)
|
| 14 |
|
| 15 |
# β
Define chatbot response function
|
| 16 |
+
def get_chatbot_response(user_message, insurance_type, country, conversation_history):
|
| 17 |
"""Fetch insurance-related responses from the AI."""
|
| 18 |
|
| 19 |
system_message = (
|
|
|
|
| 33 |
|
| 34 |
conversation_history.append({"role": "user", "content": user_message})
|
| 35 |
|
|
|
|
|
|
|
|
|
|
| 36 |
# β
API call to Groq
|
| 37 |
completion = client.chat.completions.create(
|
| 38 |
model="mixtral-8x7b",
|
|
|
|
| 54 |
|
| 55 |
return conversation_history, chat_display
|
| 56 |
|
| 57 |
+
# β
Styling and Theme with a Sophisticated UI
|
| 58 |
theme = gr.themes.Base().set(
|
| 59 |
body_background_fill="linear-gradient(to right, #001F3F, #0052CC)", # Dark navy to royal blue
|
| 60 |
button_primary_background_fill="linear-gradient(135deg, #FFD700, #FFAA00)", # Golden gradient
|
|
|
|
| 62 |
)
|
| 63 |
|
| 64 |
custom_css = """
|
| 65 |
+
/* Elegant title styling with a gold-illuminated effect */
|
| 66 |
.title-text {
|
| 67 |
background: linear-gradient(90deg, #FFD700, #FFAA00);
|
| 68 |
-webkit-background-clip: text;
|
|
|
|
| 70 |
color: transparent;
|
| 71 |
-webkit-text-fill-color: transparent;
|
| 72 |
font-weight: 700;
|
| 73 |
+
font-size: 48px;
|
| 74 |
text-align: center;
|
| 75 |
text-transform: uppercase;
|
| 76 |
+
letter-spacing: 1.5px;
|
| 77 |
+
text-shadow: 2px 2px 10px rgba(255, 215, 0, 0.3);
|
| 78 |
}
|
| 79 |
|
| 80 |
+
/* Sophisticated body text - Gold on Dark Background */
|
| 81 |
body, .gradio-container {
|
| 82 |
+
color: #FFD700 !important; /* Premium gold text */
|
| 83 |
font-family: 'Inter', sans-serif;
|
| 84 |
}
|
| 85 |
|
| 86 |
+
/* Chat messages - Glassmorphic styling */
|
| 87 |
.chat-message {
|
| 88 |
+
background: rgba(255, 255, 255, 0.1); /* Subtle transparent white */
|
| 89 |
border-radius: 12px;
|
| 90 |
padding: 12px;
|
| 91 |
+
backdrop-filter: blur(10px);
|
| 92 |
+
color: black !important;
|
| 93 |
}
|
| 94 |
|
| 95 |
+
/* Buttons - Glassmorphism with subtle glow */
|
| 96 |
.insurance-button {
|
| 97 |
border: 2px solid rgba(255, 215, 0, 0.8);
|
| 98 |
background: rgba(0, 0, 0, 0.2);
|
| 99 |
+
font-size: 16px;
|
| 100 |
font-weight: bold;
|
| 101 |
+
padding: 8px 20px;
|
| 102 |
+
border-radius: 24px;
|
| 103 |
margin: 5px;
|
| 104 |
+
color: #FFD700; /* Gold text */
|
| 105 |
transition: all 0.3s ease-in-out;
|
| 106 |
}
|
| 107 |
|
| 108 |
+
/* Button hover effect with glowing effect */
|
| 109 |
.insurance-button:hover {
|
| 110 |
background: linear-gradient(90deg, #FFD700, #FFAA00);
|
| 111 |
color: black !important;
|
| 112 |
transform: scale(1.05);
|
| 113 |
+
box-shadow: 0 0 15px rgba(255, 215, 0, 0.6);
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
/* Chatbot container - Transparent glass panel */
|
| 117 |
+
.gradio-container .block.markdown {
|
| 118 |
+
background: rgba(255, 255, 255, 0.1);
|
| 119 |
+
border-radius: 12px;
|
| 120 |
+
padding: 15px;
|
| 121 |
+
backdrop-filter: blur(12px);
|
| 122 |
+
color: #FFD700 !important; /* Gold text */
|
| 123 |
}
|
| 124 |
|
| 125 |
+
/* Input field - Premium styling */
|
| 126 |
textarea {
|
| 127 |
background: rgba(255, 255, 255, 0.05);
|
| 128 |
border: 1px solid rgba(255, 215, 0, 0.5);
|
| 129 |
border-radius: 8px;
|
| 130 |
+
color: black !important;
|
| 131 |
font-size: 16px;
|
| 132 |
padding: 10px;
|
| 133 |
}
|
| 134 |
|
| 135 |
+
/* Glow effect on input focus */
|
| 136 |
textarea:focus {
|
| 137 |
box-shadow: 0 0 10px rgba(255, 215, 0, 0.8);
|
| 138 |
border-color: #FFD700;
|
| 139 |
}
|
| 140 |
+
|
| 141 |
+
/* Send button - Luxurious design */
|
| 142 |
+
button.primary {
|
| 143 |
+
background: linear-gradient(135deg, #FFD700, #FFAA00);
|
| 144 |
+
border: none;
|
| 145 |
+
color: black !important;
|
| 146 |
+
font-weight: bold;
|
| 147 |
+
text-transform: uppercase;
|
| 148 |
+
border-radius: 20px;
|
| 149 |
+
padding: 10px 20px;
|
| 150 |
+
transition: all 0.3s ease-in-out;
|
| 151 |
+
}
|
| 152 |
+
|
| 153 |
+
/* Send button hover */
|
| 154 |
+
button.primary:hover {
|
| 155 |
+
transform: scale(1.1);
|
| 156 |
+
box-shadow: 0 0 15px rgba(255, 215, 0, 0.7);
|
| 157 |
+
}
|
| 158 |
"""
|
| 159 |
|
| 160 |
+
|
| 161 |
def clear_history():
|
| 162 |
return []
|
| 163 |
|
| 164 |
# β
Build Gradio Interface
|
| 165 |
with gr.Blocks(theme=theme, css=custom_css) as demo:
|
| 166 |
+
gr.HTML("<h2 class='title-text'>π‘οΈ AI Insurance Chatbot</h2>")
|
| 167 |
+
gr.Markdown("### Select your insurance type, country, and ask your question!")
|
| 168 |
|
| 169 |
with gr.Row():
|
| 170 |
insurance_type_input = gr.Dropdown(
|
| 171 |
["Health", "Car", "Home", "Travel", "Life", "Disability", "Business", "Pet"],
|
| 172 |
+
label=" Select Insurance Type",
|
| 173 |
interactive=True
|
| 174 |
)
|
| 175 |
country_input = gr.Dropdown(
|
| 176 |
["USA", "Canada", "UK", "Germany", "France", "India", "Australia", "Other"],
|
| 177 |
+
label=" Select Country",
|
| 178 |
interactive=True
|
| 179 |
)
|
| 180 |
|
| 181 |
custom_country_input = gr.Textbox(label="Enter Country (if not listed)", visible=False)
|
| 182 |
|
| 183 |
conversation_state = gr.State([])
|
|
|
|
|
|
|
| 184 |
|
| 185 |
+
# β
Define chatbot BEFORE using it in clear button
|
| 186 |
+
chatbot = gr.Chatbot(label="π¬ Chat History")
|
| 187 |
+
|
| 188 |
+
# β
Define the clear chat button AFTER chatbot
|
| 189 |
clear_button = gr.Button("Clear Chat History")
|
| 190 |
clear_button.click(lambda: [], outputs=[conversation_state, chatbot])
|
| 191 |
|
| 192 |
with gr.Row():
|
| 193 |
+
health_btn = gr.Button(" Health", elem_classes="insurance-button")
|
| 194 |
+
car_btn = gr.Button(" Car", elem_classes="insurance-button")
|
| 195 |
+
home_btn = gr.Button(" Home", elem_classes="insurance-button")
|
| 196 |
+
travel_btn = gr.Button(" Travel", elem_classes="insurance-button")
|
| 197 |
+
life_btn = gr.Button(" Life", elem_classes="insurance-button")
|
| 198 |
+
disability_btn = gr.Button(" Disability", elem_classes="insurance-button")
|
| 199 |
+
business_btn = gr.Button(" Business", elem_classes="insurance-button")
|
| 200 |
+
pet_btn = gr.Button(" Pet", elem_classes="insurance-button")
|
| 201 |
+
|
| 202 |
+
question_input = gr.Textbox(label="π‘ Ask your question...", placeholder="Describe your insurance question...", interactive=True)
|
|
|
|
| 203 |
|
| 204 |
def update_insurance_selection(current, new_selection):
|
| 205 |
if "Insurance:" in current:
|
|
|
|
| 217 |
btn.click(lambda current, insurance=insurance: update_insurance_selection(current, insurance),
|
| 218 |
inputs=question_input, outputs=question_input)
|
| 219 |
|
| 220 |
+
submit_btn = gr.Button("Send", variant="primary")
|
| 221 |
+
|
| 222 |
+
def submit(insurance_type, country, custom_country, question, conversation_state):
|
| 223 |
+
selected_country = custom_country if country == "Other" else country
|
| 224 |
+
updated_history, chat_display = get_chatbot_response(
|
| 225 |
+
question, insurance_type, selected_country, conversation_state
|
| 226 |
+
)
|
| 227 |
+
return updated_history, chat_display, ""
|
| 228 |
+
|
| 229 |
+
country_input.change(lambda c: gr.update(visible=c == "Other"), inputs=country_input, outputs=custom_country_input)
|
| 230 |
|
| 231 |
submit_btn.click(
|
| 232 |
+
submit,
|
| 233 |
+
inputs=[insurance_type_input, country_input, custom_country_input, question_input, conversation_state],
|
| 234 |
+
outputs=[conversation_state, chatbot, question_input]
|
| 235 |
)
|
| 236 |
|
| 237 |
+
demo.launch()
|