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+
Enhanced chatbot function with more comprehensive responses
|
| 8 |
+
"""
|
| 9 |
+
question_lower = question.lower()
|
| 10 |
+
|
| 11 |
+
if "return" in question_lower and "policy" in question_lower:
|
| 12 |
+
return """
|
| 13 |
+
**📋 Return Policy**
|
| 14 |
+
- 30-day hassle-free return policy
|
| 15 |
+
- Items must be in original condition with tags attached
|
| 16 |
+
- Free return shipping for orders over $50
|
| 17 |
+
- Start your return process in your account dashboard
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
elif "track" in question_lower and "order" in question_lower:
|
| 21 |
+
return """
|
| 22 |
+
**📦 Order Tracking**
|
| 23 |
+
You can track your order in several ways:
|
| 24 |
+
- Check your email for the tracking link sent after purchase
|
| 25 |
+
- Log into your account and visit 'My Orders'
|
| 26 |
+
- Use our tracking page with your order number
|
| 27 |
+
- Contact support for real-time updates
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
elif "laptop" in question_lower or "computer" in question_lower:
|
| 31 |
+
return """
|
| 32 |
+
**💻 Latest Laptops**
|
| 33 |
+
We have amazing deals on:
|
| 34 |
+
- **Dell XPS Series** - Premium ultrabooks starting at $899
|
| 35 |
+
- **HP Pavilion** - Great for students, from $649
|
| 36 |
+
- **MacBook Air M2** - Latest Apple technology, $1,199
|
| 37 |
+
- **Gaming Laptops** - ASUS ROG and MSI starting at $999
|
| 38 |
+
|
| 39 |
+
All laptops come with free shipping and setup support!
|
| 40 |
+
"""
|
| 41 |
+
|
| 42 |
+
elif "warranty" in question_lower:
|
| 43 |
+
return """
|
| 44 |
+
**🛡️ Warranty Information**
|
| 45 |
+
- **Standard**: 1-year manufacturer warranty on all products
|
| 46 |
+
- **Extended**: 2-3 year extended warranties available
|
| 47 |
+
- **Premium Care**: Includes accidental damage protection
|
| 48 |
+
- **On-site Support**: Available for electronics over $500
|
| 49 |
+
|
| 50 |
+
Warranty registration is automatic with purchase!
|
| 51 |
+
"""
|
| 52 |
+
|
| 53 |
+
elif "shipping" in question_lower or "delivery" in question_lower:
|
| 54 |
+
return """
|
| 55 |
+
**🚚 Shipping & Delivery**
|
| 56 |
+
- **Free Standard Shipping** on orders over $35 (3-5 business days)
|
| 57 |
+
- **Express Delivery** $9.99 (1-2 business days)
|
| 58 |
+
- **Same Day** available in major cities for $19.99
|
| 59 |
+
- **International Shipping** to 50+ countries
|
| 60 |
+
|
| 61 |
+
All orders ship within 24 hours on business days!
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
elif "payment" in question_lower or "pay" in question_lower:
|
| 65 |
+
return """
|
| 66 |
+
**💳 Payment Options**
|
| 67 |
+
We accept all major payment methods:
|
| 68 |
+
- Credit/Debit Cards (Visa, MasterCard, American Express)
|
| 69 |
+
- PayPal and PayPal Pay in 4
|
| 70 |
+
- Apple Pay and Google Pay
|
| 71 |
+
- Buy Now, Pay Later with Klarna
|
| 72 |
+
- Gift cards and store credit
|
| 73 |
+
|
| 74 |
+
All transactions are secured with 256-bit SSL encryption.
|
| 75 |
+
"""
|
| 76 |
+
|
| 77 |
+
elif "contact" in question_lower or "support" in question_lower:
|
| 78 |
+
return """
|
| 79 |
+
**📞 Contact Support**
|
| 80 |
+
Our customer service team is here to help!
|
| 81 |
+
- **Live Chat**: Available 24/7 on our website
|
| 82 |
+
- **Phone**: 1-800-SHOP-NOW (Mon-Fri 9AM-8PM EST)
|
| 83 |
+
- **Email**: support@ecommerce.com
|
| 84 |
+
- **Social Media**: @EcommerceStore on Twitter/Facebook
|
| 85 |
+
|
| 86 |
+
Average response time: Live Chat <2 minutes, Email <4 hours
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
elif "price" in question_lower or "discount" in question_lower or "deal" in question_lower:
|
| 90 |
+
return """
|
| 91 |
+
**🏷️ Current Deals**
|
| 92 |
+
Amazing offers available now!
|
| 93 |
+
- **Weekly Deals**: Up to 40% off selected items
|
| 94 |
+
- **Student Discount**: 10% off with valid student ID
|
| 95 |
+
- **Newsletter Signup**: Get 15% off your first order
|
| 96 |
+
- **Bulk Orders**: Special pricing for quantities over 10
|
| 97 |
+
|
| 98 |
+
Check our deals page for flash sales and limited-time offers!
|
| 99 |
+
"""
|
| 100 |
+
|
| 101 |
+
elif "hello" in question_lower or "hi" in question_lower or "hey" in question_lower:
|
| 102 |
+
return """
|
| 103 |
+
**👋 Hello there!**
|
| 104 |
+
Welcome to our E-commerce AI Assistant! I'm here to help you with:
|
| 105 |
+
- Product information and recommendations
|
| 106 |
+
- Order tracking and returns
|
| 107 |
+
- Shipping and payment questions
|
| 108 |
+
- Technical support
|
| 109 |
+
|
| 110 |
+
How can I assist you today?
|
| 111 |
+
"""
|
| 112 |
+
|
| 113 |
+
else:
|
| 114 |
+
return f"""
|
| 115 |
+
**🤔 I'd love to help you!**
|
| 116 |
+
While I don't have specific information about '{question}' right now, I can definitely assist you with:
|
| 117 |
+
|
| 118 |
+
- **Product Info** - Laptops, electronics, and more
|
| 119 |
+
- **Orders** - Tracking, returns, and shipping
|
| 120 |
+
- **Support** - Payments, warranties, and general help
|
| 121 |
+
|
| 122 |
+
Try asking about any of these topics, or contact our live support for specialized assistance!
|
| 123 |
+
"""
|
| 124 |
+
|
| 125 |
+
# Page configuration
|
| 126 |
+
st.set_page_config(
|
| 127 |
+
page_title="ShopSmart AI Assistant",
|
| 128 |
+
page_icon="🛍️",
|
| 129 |
+
layout="wide",
|
| 130 |
+
initial_sidebar_state="expanded"
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
# Initialize session state
|
| 134 |
+
if "conversation" not in st.session_state:
|
| 135 |
+
st.session_state.conversation = []
|
| 136 |
+
if "message_count" not in st.session_state:
|
| 137 |
+
st.session_state.message_count = 0
|
| 138 |
+
|
| 139 |
+
# Header
|
| 140 |
+
st.title("🛍️ ShopSmart AI Assistant")
|
| 141 |
+
st.subheader("Your intelligent shopping companion - Ask me anything!")
|
| 142 |
+
|
| 143 |
+
# Sidebar
|
| 144 |
+
with st.sidebar:
|
| 145 |
+
st.header("🚀 Quick Questions")
|
| 146 |
+
st.divider()
|
| 147 |
+
|
| 148 |
+
recommended_questions = [
|
| 149 |
+
"What is the return policy?",
|
| 150 |
+
"How can I track my order?",
|
| 151 |
+
"Show me latest laptops",
|
| 152 |
+
"What are shipping options?",
|
| 153 |
+
"Any current deals or discounts?"
|
| 154 |
+
]
|
| 155 |
+
|
| 156 |
+
for i, question in enumerate(recommended_questions):
|
| 157 |
+
if st.button(f"💬 {question}", key=f"quick_q_{i}", use_container_width=True):
|
| 158 |
+
response = ask_bot(question)
|
| 159 |
+
st.session_state.conversation.append({
|
| 160 |
+
"user": question,
|
| 161 |
+
"bot": response,
|
| 162 |
+
"timestamp": datetime.now().strftime("%H:%M")
|
| 163 |
+
})
|
| 164 |
+
st.session_state.message_count += 1
|
| 165 |
+
st.rerun()
|
| 166 |
+
|
| 167 |
+
st.divider()
|
| 168 |
+
|
| 169 |
+
# Stats section
|
| 170 |
+
st.header("📊 Chat Statistics")
|
| 171 |
+
|
| 172 |
+
col1, col2 = st.columns(2)
|
| 173 |
+
with col1:
|
| 174 |
+
st.metric("Messages Sent", st.session_state.message_count)
|
| 175 |
+
|
| 176 |
+
with col2:
|
| 177 |
+
st.metric("Conversations", len(st.session_state.conversation))
|
| 178 |
+
|
| 179 |
+
st.divider()
|
| 180 |
+
|
| 181 |
+
if st.button("🗑️ Clear Chat History", type="secondary", use_container_width=True):
|
| 182 |
+
st.session_state.conversation = []
|
| 183 |
+
st.session_state.message_count = 0
|
| 184 |
+
st.success("Chat history cleared!")
|
| 185 |
+
st.rerun()
|
| 186 |
+
|
| 187 |
+
# Main chat area
|
| 188 |
+
st.header("💬 Chat")
|
| 189 |
+
|
| 190 |
+
# Display conversation or welcome message
|
| 191 |
+
if not st.session_state.conversation:
|
| 192 |
+
st.info("""
|
| 193 |
+
🛍️ **ShopSmart Assistant** - Your 24/7 shopping companion
|
| 194 |
+
|
| 195 |
+
Ready to help with orders, products, returns, and more!
|
| 196 |
+
|
| 197 |
+
⚡ **Quick Start:** Use sidebar buttons or ask directly below
|
| 198 |
+
""")
|
| 199 |
+
else:
|
| 200 |
+
# Create a container for chat messages
|
| 201 |
+
chat_container = st.container()
|
| 202 |
+
|
| 203 |
+
with chat_container:
|
| 204 |
+
for i, turn in enumerate(st.session_state.conversation):
|
| 205 |
+
# Display messages with proper formatting
|
| 206 |
+
with st.expander(f"💬 Conversation #{i+1} - {turn.get('timestamp', '')}", expanded=True):
|
| 207 |
+
# User message
|
| 208 |
+
st.write(f"**You:** {turn['user']}")
|
| 209 |
+
|
| 210 |
+
# Bot response
|
| 211 |
+
st.success(turn['bot'])
|
| 212 |
+
|
| 213 |
+
st.divider()
|
| 214 |
+
|
| 215 |
+
# Input form
|
| 216 |
+
st.subheader("✍️ Ask a Question")
|
| 217 |
+
|
| 218 |
+
with st.form(key="chat_form", clear_on_submit=True):
|
| 219 |
+
# Create columns for input and button
|
| 220 |
+
col1, col2 = st.columns([4, 1])
|
| 221 |
+
|
| 222 |
+
with col1:
|
| 223 |
+
user_input = st.text_input(
|
| 224 |
+
"Type your message here:",
|
| 225 |
+
placeholder="Ask about products, orders, returns, or anything else!",
|
| 226 |
+
label_visibility="collapsed"
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
+
with col2:
|
| 230 |
+
st.write("") # Add some spacing
|
| 231 |
+
send_button = st.form_submit_button(
|
| 232 |
+
"Send 📤",
|
| 233 |
+
type="primary",
|
| 234 |
+
use_container_width=True
|
| 235 |
+
)
|
| 236 |
+
|
| 237 |
+
# Process user input
|
| 238 |
+
if send_button and user_input.strip():
|
| 239 |
+
# Get bot response
|
| 240 |
+
bot_response = ask_bot(user_input)
|
| 241 |
+
|
| 242 |
+
# Add to conversation
|
| 243 |
+
st.session_state.conversation.append({
|
| 244 |
+
"user": user_input,
|
| 245 |
+
"bot": bot_response,
|
| 246 |
+
"timestamp": datetime.now().strftime("%H:%M")
|
| 247 |
+
})
|
| 248 |
+
st.session_state.message_count += 1
|
| 249 |
+
|
| 250 |
+
# Show success message
|
| 251 |
+
st.success(f"Message sent: '{user_input}'")
|
| 252 |
+
|
| 253 |
+
# Rerun to update the display
|
| 254 |
+
st.rerun()
|
| 255 |
+
|
| 256 |
+
# Footer information
|
| 257 |
+
st.divider()
|
| 258 |
+
|
| 259 |
+
# Create columns for footer info
|
| 260 |
+
footer_col1, footer_col2, footer_col3 = st.columns(3)
|
| 261 |
+
|
| 262 |
+
with footer_col1:
|
| 263 |
+
st.info("""
|
| 264 |
+
**🕐 Available 24/7**
|
| 265 |
+
Our AI assistant is always ready to help you with your shopping needs.
|
| 266 |
+
""")
|
| 267 |
+
|
| 268 |
+
with footer_col2:
|
| 269 |
+
st.info("""
|
| 270 |
+
**📞 Need Human Support?**
|
| 271 |
+
Contact us at support@shopmart.com or call 1-800-SHOP-NOW
|
| 272 |
+
""")
|
| 273 |
+
|
| 274 |
+
with footer_col3:
|
| 275 |
+
st.info("""
|
| 276 |
+
**🔒 Secure & Private**
|
| 277 |
+
Your conversations are secure and we respect your privacy.
|
| 278 |
+
""")
|
| 279 |
+
|
| 280 |
+
# Additional help section
|
| 281 |
+
with st.expander("❓ Need Help Using This Assistant?"):
|
| 282 |
+
st.write("""
|
| 283 |
+
**How to use the ShopSmart AI Assistant:**
|
| 284 |
+
|
| 285 |
+
1. **Quick Questions**: Click any button in the sidebar for instant answers
|
| 286 |
+
2. **Custom Questions**: Type your own question in the input field below
|
| 287 |
+
3. **View History**: All conversations are saved and displayed above
|
| 288 |
+
4. **Clear History**: Use the sidebar button to clear all conversations
|
| 289 |
+
|
| 290 |
+
**What I can help with:**
|
| 291 |
+
- Product information and recommendations
|
| 292 |
+
- Order tracking and status updates
|
| 293 |
+
- Return and exchange policies
|
| 294 |
+
- Shipping and delivery options
|
| 295 |
+
- Payment methods and security
|
| 296 |
+
- Warranty and support information
|
| 297 |
+
- Current deals and promotions
|
| 298 |
+
|
| 299 |
+
**Tips for better responses:**
|
| 300 |
+
- Be specific in your questions
|
| 301 |
+
- Include relevant details (product names, order numbers, etc.)
|
| 302 |
+
- Ask one question at a time for clearer answers
|
| 303 |
+
""")
|
faqs.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[
|
| 2 |
+
{
|
| 3 |
+
"question": "How can I track my order?",
|
| 4 |
+
"answer": "You can track your order by logging into your account, going to 'My Orders', and clicking on 'Track Order'."
|
| 5 |
+
},
|
| 6 |
+
{
|
| 7 |
+
"question": "What payment methods do you accept?",
|
| 8 |
+
"answer": "We accept credit/debit cards, PayPal, Cash on Delivery, and bank transfers."
|
| 9 |
+
},
|
| 10 |
+
{
|
| 11 |
+
"question": "How long does delivery take?",
|
| 12 |
+
"answer": "Standard delivery takes 3–5 business days. Express delivery is available within 24–48 hours in major cities."
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
"question": "Can I return or exchange a product?",
|
| 16 |
+
"answer": "Yes, you can return or exchange products within 7 days if they are unused, undamaged, and in original packaging."
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
"question": "Do you ship internationally?",
|
| 20 |
+
"answer": "Currently, we only ship within Pakistan. International shipping will be available soon."
|
| 21 |
+
},
|
| 22 |
+
{
|
| 23 |
+
"question": "What is your refund policy?",
|
| 24 |
+
"answer": "Refunds are issued within 5–7 business days after we receive and inspect the returned item."
|
| 25 |
+
},
|
| 26 |
+
{
|
| 27 |
+
"question": "Do your products come with a warranty?",
|
| 28 |
+
"answer": "Yes, most electronics come with a 1-year manufacturer warranty. Please check the product details for warranty coverage."
|
| 29 |
+
},
|
| 30 |
+
{
|
| 31 |
+
"question": "What should I do if I receive a damaged product?",
|
| 32 |
+
"answer": "If your order arrives damaged, please contact our support within 48 hours with photos, and we’ll arrange a replacement or refund."
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"question": "How can I contact customer support?",
|
| 36 |
+
"answer": "You can reach our support team via live chat on the website, email at support@example.com, or call us at +92-300-1234567."
|
| 37 |
+
}
|
| 38 |
+
]
|
main.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 2 |
+
from langchain_community.document_loaders import TextLoader
|
| 3 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 4 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 5 |
+
from langchain_community.vectorstores import FAISS
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
load_dotenv()
|
| 9 |
+
|
| 10 |
+
llm = ChatGoogleGenerativeAI(model="gemini-1.5-flash")
|
| 11 |
+
|
| 12 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/text-embedding-004")
|
| 13 |
+
|
| 14 |
+
# --- Load products ---
|
| 15 |
+
loader = TextLoader("products.json")
|
| 16 |
+
docs = loader.load()
|
| 17 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 18 |
+
text = splitter.split_documents(docs)
|
| 19 |
+
product_store = FAISS.from_documents(documents=text, embedding=embeddings)
|
| 20 |
+
|
| 21 |
+
# --- Load FAQs ---
|
| 22 |
+
loader = TextLoader("faqs.json")
|
| 23 |
+
docs = loader.load()
|
| 24 |
+
splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 25 |
+
text = splitter.split_documents(docs)
|
| 26 |
+
faq_store = FAISS.from_documents(documents=text, embedding=embeddings)
|
| 27 |
+
|
| 28 |
+
# --- Retrievers ---
|
| 29 |
+
product_retriever = product_store.as_retriever(search_kwargs={"k": 3})
|
| 30 |
+
faq_retriever = faq_store.as_retriever(search_kwargs={"k": 3})
|
| 31 |
+
|
| 32 |
+
# --- Keywords ---
|
| 33 |
+
FAQ_KEYWORDS = {
|
| 34 |
+
"delivery", "ship", "shipping", "return", "refund", "warranty",
|
| 35 |
+
"payment", "pay", "exchange", "order", "track", "policy"
|
| 36 |
+
}
|
| 37 |
+
PRODUCT_KEYWORDS = {
|
| 38 |
+
"price", "spec", "specs", "specifications", "feature", "features",
|
| 39 |
+
"compare", "details", "model", "brand", "laptop", "mobile",
|
| 40 |
+
"phone", "shoes", "camera", "ram", "ssd", "storage", "gpu", "cpu"
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
# --- Conversation history ---
|
| 44 |
+
conversation_history = []
|
| 45 |
+
|
| 46 |
+
# --- Functions ---
|
| 47 |
+
def get_relevant_retriever(query: str):
|
| 48 |
+
q = query.lower()
|
| 49 |
+
if any(word in q for word in FAQ_KEYWORDS):
|
| 50 |
+
return faq_retriever
|
| 51 |
+
elif any(word in q for word in PRODUCT_KEYWORDS):
|
| 52 |
+
return product_retriever
|
| 53 |
+
else:
|
| 54 |
+
return product_retriever # default
|
| 55 |
+
|
| 56 |
+
def ask_bot(query: str):
|
| 57 |
+
retriever = get_relevant_retriever(query)
|
| 58 |
+
docs = retriever.get_relevant_documents(query)
|
| 59 |
+
context = "\n".join([d.page_content for d in docs])
|
| 60 |
+
|
| 61 |
+
# --- Add previous conversation history to context ---
|
| 62 |
+
history_text = ""
|
| 63 |
+
for turn in conversation_history[-6:]: # last 3 user-bot pairs
|
| 64 |
+
history_text += f"User: {turn['user']}\nBot: {turn['bot']}\n"
|
| 65 |
+
|
| 66 |
+
full_prompt = f"{history_text}\nContext:\n{context}\n\nUser question: {query}\nAnswer:"
|
| 67 |
+
response = llm.invoke(full_prompt)
|
| 68 |
+
|
| 69 |
+
# --- Save this turn to history ---
|
| 70 |
+
conversation_history.append({"user": query, "bot": response.content})
|
| 71 |
+
|
| 72 |
+
return response.content
|
| 73 |
+
|
| 74 |
+
# --- Main Loop ---
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
print("Chatbot started. Type 'exit' or 'quit' to stop.\n")
|
| 77 |
+
while True:
|
| 78 |
+
query = input("You: ")
|
| 79 |
+
if query.lower() in ["exit", "quit", "q"]:
|
| 80 |
+
break
|
| 81 |
+
answer = ask_bot(query)
|
| 82 |
+
print("Bot:", answer)
|
products.json
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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i5-10300H, 16GB DDR4 RAM, NVIDIA GTX 1660Ti 6GB, 512GB NVMe SSD, Windows 11",
|
| 9 |
+
"features": ["Gaming Performance", "RGB Keyboard", "Bang & Olufsen Audio", "Fast Charging"],
|
| 10 |
+
"rating": 4.5,
|
| 11 |
+
"availability": "In Stock",
|
| 12 |
+
"warranty": "1 Year International Warranty",
|
| 13 |
+
"description": "Perfect gaming laptop for enthusiasts with powerful graphics and high-speed performance"
|
| 14 |
+
},
|
| 15 |
+
{
|
| 16 |
+
"id": 2,
|
| 17 |
+
"name": "Dell G5 Gaming",
|
| 18 |
+
"category": "laptop",
|
| 19 |
+
"price": 50000,
|
| 20 |
+
"brand": "Dell",
|
| 21 |
+
"specs": "15.6-inch Full HD 120Hz, Intel Core i7-10750H, 16GB DDR4 RAM, NVIDIA RTX 3050 4GB, 1TB NVMe SSD, Windows 11",
|
| 22 |
+
"features": ["120Hz Refresh Rate", "Game Shift Technology", "Dual Fan Cooling", "RGB Lighting"],
|
| 23 |
+
"rating": 4.6,
|
| 24 |
+
"availability": "In Stock",
|
| 25 |
+
"warranty": "2 Year Premium Support",
|
| 26 |
+
"description": "High-performance gaming laptop with advanced cooling and stunning visuals"
|
| 27 |
+
},
|
| 28 |
+
{
|
| 29 |
+
"id": 3,
|
| 30 |
+
"name": "Asus TUF Gaming F15",
|
| 31 |
+
"category": "laptop",
|
| 32 |
+
"price": 46000,
|
| 33 |
+
"brand": "Asus",
|
| 34 |
+
"specs": "15.6-inch Full HD IPS, Intel Core i5-10300H, 8GB DDR4 RAM, NVIDIA GTX 1650 4GB, 512GB NVMe SSD, Windows 11",
|
| 35 |
+
"features": ["Military Grade Durability", "HyperCool Technology", "RGB Keyboard", "DTS Audio"],
|
| 36 |
+
"rating": 4.3,
|
| 37 |
+
"availability": "In Stock",
|
| 38 |
+
"warranty": "1 Year International Warranty",
|
| 39 |
+
"description": "Durable gaming laptop built for intense gaming sessions with military-grade toughness"
|
| 40 |
+
},
|
| 41 |
+
{
|
| 42 |
+
"id": 4,
|
| 43 |
+
"name": "MacBook Air M2",
|
| 44 |
+
"category": "laptop",
|
| 45 |
+
"price": 125000,
|
| 46 |
+
"brand": "Apple",
|
| 47 |
+
"specs": "13.6-inch Liquid Retina, Apple M2 Chip, 8GB Unified Memory, 256GB SSD, macOS Monterey",
|
| 48 |
+
"features": ["All-Day Battery Life", "Ultra-Thin Design", "MagSafe Charging", "Touch ID"],
|
| 49 |
+
"rating": 4.8,
|
| 50 |
+
"availability": "In Stock",
|
| 51 |
+
"warranty": "1 Year AppleCare",
|
| 52 |
+
"description": "Ultra-portable laptop with Apple's latest M2 chip for professional productivity"
|
| 53 |
+
},
|
| 54 |
+
{
|
| 55 |
+
"id": 5,
|
| 56 |
+
"name": "Lenovo ThinkPad E15",
|
| 57 |
+
"category": "laptop",
|
| 58 |
+
"price": 65000,
|
| 59 |
+
"brand": "Lenovo",
|
| 60 |
+
"specs": "15.6-inch Full HD, Intel Core i7-1165G7, 16GB DDR4 RAM, Intel Iris Xe Graphics, 512GB NVMe SSD, Windows 11 Pro",
|
| 61 |
+
"features": ["Business Grade Security", "Spill-Resistant Keyboard", "Rapid Charge", "HD Camera"],
|
| 62 |
+
"rating": 4.4,
|
| 63 |
+
"availability": "In Stock",
|
| 64 |
+
"warranty": "3 Year On-Site Warranty",
|
| 65 |
+
"description": "Professional business laptop with enterprise-grade security and reliability"
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"id": 6,
|
| 69 |
+
"name": "Samsung Galaxy S21",
|
| 70 |
+
"category": "mobile",
|
| 71 |
+
"price": 70000,
|
| 72 |
+
"brand": "Samsung",
|
| 73 |
+
"specs": "6.2-inch Dynamic AMOLED 2X, Exynos 2100, 8GB RAM, 128GB Storage, 64MP Triple Camera, 4000mAh Battery",
|
| 74 |
+
"features": ["5G Ready", "Wireless Charging", "IP68 Water Resistant", "120Hz Display"],
|
| 75 |
+
"rating": 4.6,
|
| 76 |
+
"availability": "In Stock",
|
| 77 |
+
"warranty": "1 Year Manufacturer Warranty",
|
| 78 |
+
"description": "Premium flagship smartphone with professional-grade camera system and 5G connectivity"
|
| 79 |
+
},
|
| 80 |
+
{
|
| 81 |
+
"id": 7,
|
| 82 |
+
"name": "iPhone 13",
|
| 83 |
+
"category": "mobile",
|
| 84 |
+
"price": 95000,
|
| 85 |
+
"brand": "Apple",
|
| 86 |
+
"specs": "6.1-inch Super Retina XDR, A15 Bionic Chip, 4GB RAM, 128GB Storage, 12MP Dual Camera System, Lightning Port",
|
| 87 |
+
"features": ["Face ID", "MagSafe Compatible", "Ceramic Shield", "iOS 15"],
|
| 88 |
+
"rating": 4.7,
|
| 89 |
+
"availability": "In Stock",
|
| 90 |
+
"warranty": "1 Year Limited Warranty",
|
| 91 |
+
"description": "Latest iPhone with advanced A15 Bionic chip and professional photography capabilities"
|
| 92 |
+
},
|
| 93 |
+
{
|
| 94 |
+
"id": 8,
|
| 95 |
+
"name": "OnePlus 10 Pro",
|
| 96 |
+
"category": "mobile",
|
| 97 |
+
"price": 85000,
|
| 98 |
+
"brand": "OnePlus",
|
| 99 |
+
"specs": "6.7-inch Fluid AMOLED, Snapdragon 8 Gen 1, 12GB RAM, 256GB Storage, 48MP Hasselblad Camera, 5000mAh Battery",
|
| 100 |
+
"features": ["65W SuperVOOC Charging", "Hasselblad Partnership", "120Hz LTPO 2.0", "OxygenOS 12"],
|
| 101 |
+
"rating": 4.5,
|
| 102 |
+
"availability": "In Stock",
|
| 103 |
+
"warranty": "1 Year Manufacturer Warranty",
|
| 104 |
+
"description": "Flagship killer with Hasselblad-tuned cameras and ultra-fast charging technology"
|
| 105 |
+
},
|
| 106 |
+
{
|
| 107 |
+
"id": 9,
|
| 108 |
+
"name": "Xiaomi 12 Pro",
|
| 109 |
+
"category": "mobile",
|
| 110 |
+
"price": 65000,
|
| 111 |
+
"brand": "Xiaomi",
|
| 112 |
+
"specs": "6.73-inch AMOLED, Snapdragon 8 Gen 1, 8GB RAM, 128GB Storage, 50MP Triple Camera, 4600mAh Battery",
|
| 113 |
+
"features": ["120W HyperCharge", "50MP Ultra-Wide", "Harman Kardon Audio", "MIUI 13"],
|
| 114 |
+
"rating": 4.4,
|
| 115 |
+
"availability": "In Stock",
|
| 116 |
+
"warranty": "1 Year Manufacturer Warranty",
|
| 117 |
+
"description": "High-performance smartphone with incredibly fast charging and premium camera system"
|
| 118 |
+
},
|
| 119 |
+
{
|
| 120 |
+
"id": 10,
|
| 121 |
+
"name": "Nike Air Zoom Pegasus 38",
|
| 122 |
+
"category": "shoes",
|
| 123 |
+
"price": 12000,
|
| 124 |
+
"brand": "Nike",
|
| 125 |
+
"specs": "Running Shoes, Mesh Upper, Zoom Air Units, React Foam Midsole, Size Range: 7-11",
|
| 126 |
+
"features": ["Zoom Air Technology", "Breathable Mesh", "Durable Rubber Outsole", "Lightweight Design"],
|
| 127 |
+
"rating": 4.7,
|
| 128 |
+
"availability": "In Stock",
|
| 129 |
+
"warranty": "6 Months Manufacturing Defect",
|
| 130 |
+
"description": "Premium running shoes with responsive cushioning for daily runs and training"
|
| 131 |
+
},
|
| 132 |
+
{
|
| 133 |
+
"id": 11,
|
| 134 |
+
"name": "Adidas Ultraboost 21",
|
| 135 |
+
"category": "shoes",
|
| 136 |
+
"price": 15000,
|
| 137 |
+
"brand": "Adidas",
|
| 138 |
+
"specs": "Casual/Running Shoes, Primeknit Upper, Boost Midsole, Continental Rubber Outsole, Size Range: 7-12",
|
| 139 |
+
"features": ["Boost Energy Return", "Primeknit Upper", "Torsion System", "Continental Rubber"],
|
| 140 |
+
"rating": 4.8,
|
| 141 |
+
"availability": "In Stock",
|
| 142 |
+
"warranty": "6 Months Manufacturing Defect",
|
| 143 |
+
"description": "Revolutionary running shoes with maximum energy return and superior comfort"
|
| 144 |
+
},
|
| 145 |
+
{
|
| 146 |
+
"id": 12,
|
| 147 |
+
"name": "Puma RS-X3",
|
| 148 |
+
"category": "shoes",
|
| 149 |
+
"price": 9000,
|
| 150 |
+
"brand": "Puma",
|
| 151 |
+
"specs": "Lifestyle Sneakers, Synthetic Leather Upper, RS Foam Midsole, Rubber Outsole, Size Range: 6-11",
|
| 152 |
+
"features": ["Retro Design", "Bold Colors", "Cushioned Sole", "Durable Construction"],
|
| 153 |
+
"rating": 4.3,
|
| 154 |
+
"availability": "In Stock",
|
| 155 |
+
"warranty": "6 Months Manufacturing Defect",
|
| 156 |
+
"description": "Bold lifestyle sneakers with retro-futuristic design and comfortable cushioning"
|
| 157 |
+
},
|
| 158 |
+
{
|
| 159 |
+
"id": 13,
|
| 160 |
+
"name": "Canon EOS R5",
|
| 161 |
+
"category": "camera",
|
| 162 |
+
"price": 350000,
|
| 163 |
+
"brand": "Canon",
|
| 164 |
+
"specs": "45MP Full-Frame CMOS Sensor, DIGIC X Processor, 8K Video Recording, In-Body Image Stabilization, RF Mount",
|
| 165 |
+
"features": ["8K Video Recording", "45MP Resolution", "In-Body Stabilization", "Dual Card Slots"],
|
| 166 |
+
"rating": 4.9,
|
| 167 |
+
"availability": "Limited Stock",
|
| 168 |
+
"warranty": "2 Year International Warranty",
|
| 169 |
+
"description": "Professional mirrorless camera with groundbreaking 8K video and exceptional image quality"
|
| 170 |
+
},
|
| 171 |
+
{
|
| 172 |
+
"id": 14,
|
| 173 |
+
"name": "Sony A7 IV",
|
| 174 |
+
"category": "camera",
|
| 175 |
+
"price": 280000,
|
| 176 |
+
"brand": "Sony",
|
| 177 |
+
"specs": "33MP Full-Frame Exmor R Sensor, BIONZ XR Processor, 4K 60p Video, 5-Axis Stabilization, E Mount",
|
| 178 |
+
"features": ["33MP Resolution", "Real-time Eye AF", "4K 60p Recording", "693 AF Points"],
|
| 179 |
+
"rating": 4.8,
|
| 180 |
+
"availability": "In Stock",
|
| 181 |
+
"warranty": "2 Year International Warranty",
|
| 182 |
+
"description": "Versatile full-frame mirrorless camera perfect for both photography and videography"
|
| 183 |
+
},
|
| 184 |
+
{
|
| 185 |
+
"id": 15,
|
| 186 |
+
"name": "iPad Pro 12.9",
|
| 187 |
+
"category": "tablet",
|
| 188 |
+
"price": 110000,
|
| 189 |
+
"brand": "Apple",
|
| 190 |
+
"specs": "12.9-inch Liquid Retina XDR, Apple M2 Chip, 8GB RAM, 128GB Storage, 12MP Camera System, USB-C",
|
| 191 |
+
"features": ["M2 Chip Performance", "Liquid Retina XDR", "Apple Pencil Support", "Face ID"],
|
| 192 |
+
"rating": 4.7,
|
| 193 |
+
"availability": "In Stock",
|
| 194 |
+
"warranty": "1 Year Limited Warranty",
|
| 195 |
+
"description": "Professional tablet with desktop-class performance for creative professionals and students"
|
| 196 |
+
}
|
| 197 |
+
]
|
requirements.txt
CHANGED
|
@@ -1,3 +1,12 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
langchain-google-genai
|
| 3 |
+
google-generativeai
|
| 4 |
+
chromadb
|
| 5 |
+
faiss-cpu
|
| 6 |
+
tiktoken
|
| 7 |
+
pydantic
|
| 8 |
+
fastapi
|
| 9 |
+
uvicorn
|
| 10 |
+
streamlit
|
| 11 |
+
pandas
|
| 12 |
+
python-dotenv
|