-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtry.py
More file actions
369 lines (341 loc) · 13.1 KB
/
Copy pathtry.py
File metadata and controls
369 lines (341 loc) · 13.1 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import streamlit as st
import sqlite3
import hashlib
import json
# Set page config as the first Streamlit command
st.set_page_config(page_title="Xfinity SmartMatch", layout="wide")
# Custom CSS (updated for better chat container styling)
st.markdown("""
<style>
body {
font-family: 'Inter', 'Segoe UI', sans-serif;
background-color: #280e8b;
}
.main {
background-color: #ffffff;
padding: 30px;
border-radius: 12px;
box-shadow: 0 6px 12px rgba(0, 0, 0, 0.05);
max-width: 900px;
margin: 0 auto;
}
h2 {
color: #1e293b;
font-weight: 600;
text-align: center;
}
h4 {
color: #64748b;
font-weight: 400;
text-align: center;
}
.stTextInput > div > div > input {
border-radius: 10px;
border: 1px solid #e2e8f0;
padding: 12px;
background-color: #f9fafb;
color: #64748b;
}
.stTextInput > div > div > input::placeholder {
color: #64748b;
opacity: 1;
}
.stButton > button {
background-color: #3b82f6;
color: white;
border-radius: 10px;
padding: 12px 24px;
border: none;
font-weight: 500;
transition: background-color 0.3s ease, transform 0.2s ease;
}
.stButton > button:hover {
background-color: #2563eb;
transform: translateY(-2px);
}
.toggle-container {
display: flex;
flex-wrap: wrap;
gap: 15px;
justify-content: space-between;
}
.toggle-card {
flex: 1 1 calc(25% - 15px);
background-color: #ffffff;
border-radius: 10px;
padding: 15px;
text-align: center;
box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
transition: all 0.3s ease;
cursor: pointer;
}
.toggle-card.active {
background-color: #3b82f6;
color: white;
}
.toggle-card:hover {
transform: translateY(-3px);
}
.plan-card {
background-color: #ffffff;
border-radius: 12px;
padding: 15px;
margin: 10px 0;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
border-left: 5px solid #3b82f6;
}
.plan-card h3 {
color: #1e293b;
margin: 0;
font-size: 1.25rem;
}
.plan-card p {
color: #64748b;
margin: 5px 0;
}
.logout-btn {
position: fixed;
top: 20px;
right: 20px;
}
.chat-container {
background-color: #f3f4f6;
padding: 15px;
border-radius: 10px;
margin: 20px 0;
}
.user-message {
background-color: #e2e8f0;
color: #1e293b;
padding: 10px;
border-radius: 8px;
margin-bottom: 10px;
word-wrap: break-word;
}
.bot-message {
background-color: #6b7280;
color: white;
padding: 10px;
border-radius: 8px;
margin-bottom: 10px;
}
</style>
""", unsafe_allow_html=True)
# Database functions (unchanged)
def init_db():
conn = sqlite3.connect('user_data.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
name TEXT NOT NULL,
age INTEGER NOT NULL,
plans TEXT
)
''')
conn.commit()
conn.close()
def hash_password(password):
return hashlib.sha256(password.encode()).hexdigest()
def get_user(username):
conn = sqlite3.connect('user_data.db')
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE username = ?", (username,))
user = cursor.fetchone()
conn.close()
return user
def signup_user(username, password, name, age):
if get_user(username):
return "Username already exists!"
conn = sqlite3.connect('user_data.db')
cursor = conn.cursor()
hashed_password = hash_password(password)
cursor.execute("INSERT INTO users (username, password, name, age, plans) VALUES (?, ?, ?, ?, ?)",
(username, hashed_password, name, age, ''))
conn.commit()
conn.close()
return "User signed up successfully! Please log in."
def login_user(username, password):
user = get_user(username)
if user and user[2] == hash_password(password):
return user
return None
# Updated get_LLM_response function
def get_LLM_response(prompt, keywords="", api_key="SUPER_SECRET_API_KEY"):
import json
import streamlit as st
from google import genai
from google.genai import types
SUPER_SECRET_API_KEY = "AIzaSyAerG1bzf9EdiA_HfxnW1rA_PoeATo9G5c"
with open('Xfinity_data.json', 'r') as file:
xfinity_products = json.load(file)
context = f"""
We have the following Xfinity products under 4 categories: {json.dumps(xfinity_products)}.
Users can be recommended a single service or a combination of them based on their input prompt.
Based on the user prompt and some keywords, return a JSON list of exactly one service or combination of services
that best fit their needs. Each item must have 'name', 'price', and 'features' keys, matching the product names EXACTLY
as given in the dictionary. User prompt: {prompt} Keywords: {keywords}
Example output: [
{{"name": "Xfinity Internet Essential", "price": "$29.99/month", "features": ["50 Mbps", "Unlimited Data"]}},
]
"""
def generate():
client = genai.Client(api_key=SUPER_SECRET_API_KEY)
model = "gemini-2.0-flash"
contents = [types.Content(role="user", parts=[types.Part.from_text(text=context)])]
config = types.GenerateContentConfig(
temperature=1,
top_p=0.95,
top_k=40,
max_output_tokens=8192,
response_mime_type="application/json",
)
full_response = ""
for chunk in client.models.generate_content_stream(model=model, contents=contents, config=config):
if chunk.text:
full_response += chunk.text
return full_response
llm_response = generate()
try:
parsed_response = json.loads(llm_response)
if isinstance(parsed_response, list) and all(isinstance(item, dict) and 'name' in item and 'price' in item and 'features' in item for item in parsed_response):
return parsed_response[:3] # Ensure only 3 items are returned
else:
st.error(f"LLM response format invalid: {parsed_response}")
return [{"name": "Error", "price": "N/A", "features": ["Invalid response format"]}]
except json.JSONDecodeError as e:
st.error(f"Failed to parse LLM response: {e}")
return [{"name": "Error", "price": "N/A", "features": ["Failed to parse response"]}]
# Streamlit App
def app():
if 'logged_in' not in st.session_state:
st.session_state.logged_in = False
if 'logged_in' in st.session_state:
st.session_state.chat_mode=True
if 'user_data' not in st.session_state:
st.session_state.user_data = {}
if 'show_signup' not in st.session_state:
st.session_state.show_signup = False
if 'active_toggle' not in st.session_state:
st.session_state.active_toggle = None
if 'chat_mode' not in st.session_state:
st.session_state.chat_mode = False
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
with st.container():
st.markdown("<div>", unsafe_allow_html=True)
if st.session_state.logged_in:
# Logout button
st.markdown('<div class="logout-btn">', unsafe_allow_html=True)
if st.button("Logout"):
st.session_state.logged_in = False
st.session_state.user_data = {}
st.session_state.chat_mode = False
st.session_state.chat_history = []
st.session_state.active_toggle = None
st.rerun()
st.markdown('</div>', unsafe_allow_html=True)
st.markdown(f"<h2>Welcome, {st.session_state.user_data['name']} 👋</h2>", unsafe_allow_html=True)
st.markdown("<h4>Find Your Perfect Xfinity Plan</h4>", unsafe_allow_html=True)
# Toggle cards
st.subheader("What are you interested in?")
st.markdown('<div class="toggle-container">', unsafe_allow_html=True)
options = ["Internet", "TV", "Mobile", "Home Security"]
cols = st.columns(4)
for i, option in enumerate(options):
with cols[i]:
if st.button(option, key=option):
st.session_state.active_toggle = option
st.session_state.chat_mode = True
recommendations = get_LLM_response(f"Show me {option} plans", st.session_state.active_toggle)
st.session_state.chat_history = [{"user": f"Show me {option} plans", "bot": recommendations}]
st.rerun()
st.markdown('</div>', unsafe_allow_html=True)
# Display chat history in a single chat container
for entry in st.session_state.chat_history:
for plan in entry['bot']:
st.markdown(
f"""
<div class='chat-container'>
<div class='user-message'>
<strong class='user-message'>You:</strong> {entry['user']}
</div>
<div class='bot-message'>
<strong style='color: #3b82f6;'>SmartMatch: </strong>
<div class='plan-card'>
<h3>{plan['name']}</h3>
<p><strong>Price:</strong> {plan['price']}</p>
<p><strong>Features:</strong> {', '.join(plan['features'])}</p>
</div>
</div>
</div>
""",
unsafe_allow_html=True
)
if st.session_state.chat_mode:
# Prompt input (centered)
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
user_prompt = st.text_input("", placeholder="Describe your needs...", key="user_input", help="Example: 'I work from home and need fast internet.'")
if st.button("Get Recommendations", use_container_width=True):
if user_prompt:
recommendations = get_LLM_response(user_prompt, st.session_state.active_toggle)
st.session_state.chat_history.append({"user": user_prompt, "bot": recommendations})
st.rerun()
else:
st.warning("Please enter a prompt.")
else:
login_or_signup()
st.markdown("</div>", unsafe_allow_html=True)
# Login & Signup Logic (unchanged)
def login_or_signup():
if not st.session_state.show_signup:
st.header("Login to Xfinity SmartMatch")
username = st.text_input("Username", placeholder="Enter your username")
password = st.text_input("Password", type="password", placeholder="Enter your password")
if st.button("Login"):
if not username or not password:
st.warning("Please fill in all fields.")
else:
user = login_user(username, password)
if user:
st.session_state.logged_in = True
st.session_state.user_data = {
"name": user[3],
"age": user[4],
"plans": user[5].split(',') if user[5] else []
}
st.rerun()
else:
st.warning("Invalid credentials. Please try again or sign up.")
if st.button("Need an account? Sign Up"):
st.session_state.show_signup = True
st.rerun()
else:
signup_form()
def signup_form():
st.header("Create Your Account")
new_username = st.text_input("Choose a Username", placeholder="Enter a username")
new_password = st.text_input("Choose a Password", type="password", placeholder="Enter a password")
name = st.text_input("Your Name", placeholder="Enter your name")
age = st.number_input("Your Age", min_value=18, max_value=100, step=1)
if st.button("Sign Up"):
if not new_username or not new_password or not name or not age:
st.warning("Please fill in all fields.")
else:
result = signup_user(new_username, new_password, name, age)
if "successfully" in result:
st.success(result)
st.session_state.show_signup = False
st.rerun()
else:
st.error(result)
if st.button("Back to Login"):
st.session_state.show_signup = False
st.rerun()
# Run the app
if __name__ == "__main__":
init_db()
app()