JenetGhumman commited on
Commit
379e7c1
Β·
verified Β·
1 Parent(s): 3577f87

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +163 -0
app.py ADDED
@@ -0,0 +1,163 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Untitled13.ipynb
3
+
4
+ Automatically generated by Colab.
5
+
6
+ Original file is located at
7
+ https://colab.research.google.com/drive/1glmxSLUi1Bpo3p91wMqTNmzipbGn_j-j
8
+ """
9
+
10
+ from groq import Groq
11
+ import gradio as gr
12
+ import os
13
+
14
+ client = Groq(api_key=groqkey)
15
+
16
+ # Define chatbot response function
17
+ def get_chatbot_response(user_message, insurance_type, country, conversation_history):
18
+ """Fetch insurance-related responses from the AI."""
19
+
20
+ system_message = (
21
+ f"You are an insurance expert providing accurate information on {insurance_type} insurance in {country}. "
22
+ "Your responses should be clear and informative but must not be considered official legal or financial advice. "
23
+ "Always use factual information about policies, coverage, and common practices."
24
+ )
25
+
26
+ # Maintain conversation history
27
+ if conversation_history:
28
+ if conversation_history[0]["role"] == "system":
29
+ conversation_history[0]["content"] = system_message
30
+ else:
31
+ conversation_history.insert(0, {"role": "system", "content": system_message})
32
+ else:
33
+ conversation_history.append({"role": "system", "content": system_message})
34
+
35
+ conversation_history.append({"role": "user", "content": user_message})
36
+
37
+ # API call to Groq
38
+ completion = client.chat.completions.create(
39
+ model="mixtral-8x7b",
40
+ messages=conversation_history,
41
+ temperature=0.3,
42
+ top_p=0.95
43
+ )
44
+
45
+ response = ""
46
+ for chunk in completion:
47
+ response += chunk.choices[0].delta.content or ""
48
+
49
+ conversation_history.append({"role": "assistant", "content": response})
50
+
51
+ chat_display = [
52
+ (msg["content"], conversation_history[i + 1]["content"])
53
+ for i, msg in enumerate(conversation_history[:-1]) if msg["role"] == "user"
54
+ ]
55
+
56
+ return conversation_history, chat_display
57
+
58
+ # Styling and Theme
59
+ theme = gr.themes.Base().set(
60
+ body_background_fill="linear-gradient(to right, #0052CC, #00AFFF)",
61
+ button_primary_background_fill="#0052CC",
62
+ button_primary_text_color="white"
63
+ )
64
+ custom_css = """
65
+ .title-text {
66
+ background: #0052CC;
67
+ -webkit-background-clip: text;
68
+ background-clip: text;
69
+ color: transparent;
70
+ -webkit-text-fill-color: transparent;
71
+ display: inline-block;
72
+ width: fit-content;
73
+ font-weight: bold;
74
+ text-align: center;
75
+ font-size: 45px;
76
+ }
77
+ .insurance-button {
78
+ border: 1px solid #0052CC;
79
+ background-color: transparent;
80
+ font-size: 15px;
81
+ padding: 5px 15px;
82
+ border-radius: 16px;
83
+ margin: 0 5px;
84
+ }
85
+ .insurance-button:hover {
86
+ background: linear-gradient(90deg, #0052CC, #00AFFF);
87
+ color: white;
88
+ }
89
+ """
90
+
91
+ def clear_history():
92
+ return []
93
+
94
+ # Build Gradio Interface
95
+ with gr.Blocks(theme=theme, css=custom_css) as demo:
96
+ gr.HTML("<h2 class='title-text'>πŸ›‘οΈ AI Insurance Chatbot</h2>")
97
+ gr.Markdown("### Select your insurance type, country, and ask your question!")
98
+
99
+ with gr.Row():
100
+ insurance_type_input = gr.Dropdown(
101
+ ["Health", "Car", "Home", "Travel", "Life", "Disability", "Business", "Pet"],
102
+ label="πŸ›‘οΈ Select Insurance Type",
103
+ interactive=True
104
+ )
105
+ country_input = gr.Dropdown(
106
+ ["USA", "Canada", "UK", "Germany", "France", "India", "Australia", "Other"],
107
+ label="🌍 Select Country",
108
+ interactive=True
109
+ )
110
+
111
+ custom_country_input = gr.Textbox(label="Enter Country (if not listed)", visible=False)
112
+
113
+ conversation_state = gr.State([])
114
+
115
+ clear_button = gr.Button("Clear Chat History")
116
+ clear_button.click(lambda: [], outputs=[conversation_state, chatbot])
117
+
118
+ with gr.Row():
119
+ health_btn = gr.Button("πŸ₯ Health", elem_classes="insurance-button")
120
+ car_btn = gr.Button("πŸš— Car", elem_classes="insurance-button")
121
+ home_btn = gr.Button("🏠 Home", elem_classes="insurance-button")
122
+ travel_btn = gr.Button("✈️ Travel", elem_classes="insurance-button")
123
+ life_btn = gr.Button("πŸ’– Life", elem_classes="insurance-button")
124
+ disability_btn = gr.Button("β™Ώ Disability", elem_classes="insurance-button")
125
+ business_btn = gr.Button("🏒 Business", elem_classes="insurance-button")
126
+ pet_btn = gr.Button("🐾 Pet", elem_classes="insurance-button")
127
+
128
+ question_input = gr.Textbox(label="πŸ’‘ Ask your question...", placeholder="Describe your insurance question...", interactive=True)
129
+
130
+ def update_insurance_selection(current, new_selection):
131
+ if "Insurance:" in current:
132
+ parts = current.split("Insurance:", 1)
133
+ additional_text = parts[1] if len(parts) > 1 else ""
134
+ else:
135
+ additional_text = current
136
+
137
+ return f"{new_selection} Insurance: {additional_text}"
138
+
139
+ for btn, insurance in zip(
140
+ [health_btn, car_btn, home_btn, travel_btn, life_btn, disability_btn, business_btn, pet_btn],
141
+ ["Health", "Car", "Home", "Travel", "Life", "Disability", "Business", "Pet"]
142
+ ):
143
+ btn.click(lambda current, insurance=insurance: update_insurance_selection(current, insurance),
144
+ inputs=question_input, outputs=question_input)
145
+
146
+ submit_btn = gr.Button("Send", variant="primary")
147
+
148
+ def submit(insurance_type, country, custom_country, question, conversation_state):
149
+ selected_country = custom_country if country == "Other" else country
150
+ updated_history, chat_display = get_chatbot_response(
151
+ question, insurance_type, selected_country, conversation_state
152
+ )
153
+ return updated_history, chat_display, ""
154
+
155
+ country_input.change(lambda c: gr.update(visible=c == "Other"), inputs=country_input, outputs=custom_country_input)
156
+
157
+ submit_btn.click(
158
+ submit,
159
+ inputs=[insurance_type_input, country_input, custom_country_input, question_input, conversation_state],
160
+ outputs=[conversation_state, chatbot, question_input]
161
+ )
162
+
163
+ demo.launch()