sohail-shaikh-s07 commited on
Commit
27dc5c5
Β·
verified Β·
1 Parent(s): 89cea4c

Upload 9 files

Browse files
Files changed (9) hide show
  1. README.md +6 -7
  2. abc.md +87 -0
  3. app.py +108 -0
  4. chat.py +223 -0
  5. chatbot_model.pth +3 -0
  6. product_intents.json +109 -0
  7. requirements.txt +6 -0
  8. reviews_data.json +44 -0
  9. train.py +242 -0
README.md CHANGED
@@ -1,14 +1,13 @@
1
  ---
2
- title: Review Analyzer
3
- emoji: πŸš€
4
- colorFrom: yellow
5
- colorTo: green
6
  sdk: streamlit
7
- sdk_version: 1.42.1
8
  app_file: app.py
9
  pinned: false
10
- license: mit
11
- short_description: A streamlit application for taking review and analysis.
12
  ---
13
 
14
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Chatbot Review
3
+ emoji: πŸ”₯
4
+ colorFrom: indigo
5
+ colorTo: blue
6
  sdk: streamlit
7
+ sdk_version: 1.42.0
8
  app_file: app.py
9
  pinned: false
10
+ short_description: A chatbot for taking review
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
abc.md ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # πŸ€– **Chatbot Review**
2
+
3
+ ## πŸ“– **Overview**
4
+
5
+ Chatbot Review is a **Streamlit**-based application designed to provide product reviews and inquiries. Utilizing **Natural Language Processing (NLP)**, this app allows users to ask questions about products, submit reviews, and analyze customer sentiments. Built with **Python**, it integrates various libraries for efficient data handling and interactive visualizations.
6
+
7
+ ## πŸš€ **Features**
8
+
9
+ - πŸ“ **Product Review**: Collect and display customer reviews for each product.
10
+ - πŸ’² **Price Inquiry**: Find pricing details for different products.
11
+ - ✍️ **Submit Review**: Share your own reviews and ratings.
12
+ - πŸ’¬ **Review Analysis**: Analyze customer feedback to determine the overall product performance with graphical representation.
13
+
14
+ ## πŸ—‚οΈ **Project Structure**
15
+
16
+ ```plaintext
17
+ Chatbot-Review/
18
+ β”‚
19
+ β”œβ”€β”€ app.py # Main app file to run the Streamlit application
20
+ β”œβ”€β”€ chat.py # File handling the chatbot interactions and user queries
21
+ β”œβ”€β”€ chat_model.pth # Pre-trained model for chatbot's response generation
22
+ β”œβ”€β”€ requirements.txt # Lists dependencies required to run the app
23
+ β”œβ”€β”€ product_intents.json # Contains predefined intents and responses for handling queries
24
+ β”œβ”€β”€ reviews_data.json # Stores customer-submitted reviews and ratings
25
+ β”‚
26
+ β”œβ”€β”€ utils/
27
+ β”‚ └── analytics.py # Contains functions for product data handling and analytics
28
+ β”‚
29
+ β”œβ”€β”€ images/ # Folder containing product images for display
30
+ β”‚ β”œβ”€β”€ camera.jpg # Product image of camera
31
+ β”‚ β”œβ”€β”€ console.jpg # Product image of gaming console
32
+ β”‚ β”œβ”€β”€ earbuds.jpg # Product image of earbuds
33
+ β”‚ β”œβ”€β”€ laptop.jpg # Product image of laptop
34
+ β”‚ β”œβ”€β”€ smartphone.jpg # Product image of smartphone
35
+ β”‚ β”œβ”€β”€ smartwatch.jpg # Product image of smartwatch
36
+ β”‚ └── tablet.jpg # Product image of tablet
37
+ β”‚
38
+ β”œβ”€β”€ pages/ # Contains individual Streamlit pages for different app views
39
+ β”‚ β”œβ”€β”€ admin_dashboard.py # Page for admin dashboard to view reviews and data
40
+ β”‚ β”œβ”€β”€ chatbot_page.py # Page with the chatbot interface for product queries
41
+ β”‚ β”œβ”€β”€ product_catalog.py # Page displaying the list of available products
42
+ β”‚ └── submit_review.py # Page for submitting new reviews
43
+ β”‚
44
+ β”œβ”€β”€ train.py # Script to train model on a custom dataset for review analysis
45
+ └── README.md # Project documentation
46
+ ```
47
+
48
+ ## βš™οΈ **Setup Instructions**
49
+
50
+ 1. **Clone the Repository**:
51
+ ```bash
52
+ git clone <repository-url>
53
+ cd chatbot-review
54
+ ```
55
+ 2. **Install Dependencies**:
56
+ Ensure **Python** is installed and run:
57
+ ```bash
58
+ pip install -r requirements.txt
59
+ ```
60
+ 3. **Run the Application**:
61
+ Start the Streamlit app by executing:
62
+ ```bash
63
+ streamlit run app.py
64
+ ```
65
+
66
+ ## πŸ–₯️ **Usage**
67
+
68
+ Launch the app and interact with the chatbot to inquire about products, submit reviews, and more. The app will guide you through various options based on your queries.
69
+
70
+ ## πŸ”§ **Configuration**
71
+
72
+ The app's configuration can be adjusted through the `README.md` file, which includes the Streamlit SDK version and other settings.
73
+
74
+ ## πŸ“Š **Data Handling**
75
+
76
+ - Reviews are stored in **reviews_data.json** and processed using **Pandas** for analytics.
77
+ - Product details are managed within the **ReviewAnalytics** class in **utils/analytics.py**.
78
+
79
+ ## πŸ’‘ **Future Enhancements**
80
+
81
+ - Integrate more advanced NLP models for more accurate responses.
82
+ - Expand the product catalog and review database.
83
+ - Improve UI/UX for a more intuitive user experience.
84
+
85
+ ## πŸ“ **License**
86
+
87
+ This project is licensed under the **MIT License**.
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•— β–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•— β–ˆβ–ˆβ•—
3
+ # β–ˆβ–ˆβ•”β•β•β•β•β• β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘
4
+ # β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—
5
+ # β•šβ•β•β•β•β•β•šβ•β•β•β•β•β•šβ•β•β•β•β•β•šβ•β•β•β•β• β•šβ•β•β•β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β•šβ•β•β•β•β•β•šβ•β•β•β•β•β•šβ•β•β•β•β•β•šβ•β•β•β•β•
6
+ # β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘ β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β• β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—
7
+ # β•šβ•β•β•β•β•β•β• β•šβ•β•β•β•β•β• β•šβ•β• β•šβ•β• β•šβ•β• β•šβ•β• β•šβ•β• β•šβ•β•β•β•β•β•β•
8
+
9
+ # *******************************************************
10
+ # Created with πŸ’“ by - π’π¨π‘πšπ’π₯ π’π‘πšπ’π€π‘ | 😁
11
+ # *******************************************************
12
+
13
+ # -------------------------------------------------------
14
+ # ✨ Support the Code πŸ’»:
15
+ # β˜• Buy Me A Coffee: https://buymeacoffee.com/sohails07
16
+ # -------------------------------------------------------
17
+
18
+ # πŸ“± LET'S CONNECT 🌐:
19
+ # πŸš€ **Best way to contact**:
20
+ # πŸ“² Telegram: https://t.me/sohails_07
21
+
22
+ # 🌍 **Other Platforms**:
23
+ # πŸ“Έ Instagram:
24
+ # - Personal: https://www.instagram.com/sohails_07/
25
+ # -------------------------------------------------------
26
+
27
+ # πŸ”₯ **For Any Questions or Concerns**:
28
+ # Feel free to reach out to us! Whether it's about coding or life, we're here to help you grow. Let's chat!
29
+
30
+ # -------------------------------------------------------
31
+ # πŸ”” **Stay Tuned**:
32
+ # Don't miss out on more exciting content! Hit the Follow button on our Github page:
33
+ # πŸ‘‰ Github: https://github.com/Sohail-Shaikh-07/
34
+ # -------------------------------------------------------
35
+
36
+ # ⚑ **Pro Tip**:
37
+ # For the best experience, stick with the default library versions shown.
38
+ # We’ve got you covered for the smoothest coding ride! πŸš€
39
+ # -------------------------------------------------------
40
+
41
+
42
+
43
+ # ************************************************************* γ€ŽIγ€γ€ŽMγ€γ€ŽPγ€γ€ŽOγ€γ€ŽRγ€γ€ŽTγ€γ€ŽS』 *************************************************************
44
+
45
+ import streamlit as st
46
+ from utils.analytics import ReviewAnalytics
47
+ from pages.product_catalog import show_product_catalog
48
+ from pages.submit_review import show_submit_review
49
+ from pages.chatbot_page import show_chatbot
50
+ from pages.admin_dashboard import show_admin_dashboard
51
+
52
+
53
+ # ************************************************************* γ€ŽMγ€γ€ŽAγ€γ€ŽIγ€γ€ŽN』 *************************************************************
54
+
55
+ def main():
56
+ st.set_page_config(
57
+ page_title="Product Review ChatBot",
58
+ layout="wide",
59
+ initial_sidebar_state="expanded",
60
+ )
61
+
62
+ # Initialize systems
63
+ analytics = ReviewAnalytics()
64
+
65
+ # Initialize session state for page if not exists
66
+ if "current_page" not in st.session_state:
67
+ st.session_state.current_page = "Products Catalog"
68
+
69
+ # Hide all default elements and the text navigation --- because it is visible on app and looks unpleasant
70
+ st.markdown(
71
+ """
72
+ <style>
73
+ #MainMenu {visibility: hidden;}
74
+ footer {visibility: hidden;}
75
+ header {visibility: hidden;}
76
+ div[data-testid="stSidebarNav"] {display: none;}
77
+ </style>
78
+ """,
79
+ unsafe_allow_html=True,
80
+ )
81
+
82
+ # Only show the selectbox for navigation on app
83
+ page = st.sidebar.selectbox(
84
+ "Choose a page",
85
+ ["Products Catalog", "Submit Review", "ChatBot", "Admin Dashboard"],
86
+ index=["Products Catalog", "Submit Review", "ChatBot", "Admin Dashboard"].index(
87
+ st.session_state.current_page
88
+ ),
89
+ )
90
+
91
+ # Update current page in session state when changed
92
+ if page != st.session_state.current_page:
93
+ st.session_state.current_page = page
94
+ st.rerun()
95
+
96
+ # Route to appropriate page
97
+ if st.session_state.current_page == "Products Catalog":
98
+ show_product_catalog(analytics)
99
+ elif st.session_state.current_page == "Submit Review":
100
+ show_submit_review(analytics)
101
+ elif st.session_state.current_page == "ChatBot":
102
+ show_chatbot()
103
+ else: # Admin Dashboard
104
+ show_admin_dashboard(analytics)
105
+
106
+
107
+ if __name__ == "__main__":
108
+ main()
chat.py ADDED
@@ -0,0 +1,223 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•— β–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•— β–ˆβ–ˆβ•—
3
+ # β–ˆβ–ˆβ•”β•β•β•β•β• β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘
4
+ # β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—
5
+ # β•šβ•β•β•β•β•β•šβ•β•β•β•β•β•šβ•β•β•β•β•β•šβ•β•β•β•β• β•šβ•β•β•β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β•šβ•β•β•β•β•β•šβ•β•β•β•β•β•šβ•β•β•β•β•β•šβ•β•β•β•β•
6
+ # β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘ β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β• β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—
7
+ # β•šβ•β•β•β•β•β•β• β•šβ•β•β•β•β•β• β•šβ•β• β•šβ•β• β•šβ•β• β•šβ•β• β•šβ•β• β•šβ•β•β•β•β•β•β•
8
+
9
+ # *******************************************************
10
+ # Created with πŸ’“ by - π’π¨π‘πšπ’π₯ π’π‘πšπ’π€π‘ | 😁
11
+ # *******************************************************
12
+
13
+ # -------------------------------------------------------
14
+ # ✨ Support the Code πŸ’»:
15
+ # β˜• Buy Me A Coffee: https://buymeacoffee.com/sohails07
16
+ # -------------------------------------------------------
17
+
18
+ # πŸ“± LET'S CONNECT 🌐:
19
+ # πŸš€ **Best way to contact**:
20
+ # πŸ“² Telegram: https://t.me/sohails_07
21
+
22
+ # 🌍 **Other Platforms**:
23
+ # πŸ“Έ Instagram:
24
+ # - Personal: https://www.instagram.com/sohails_07/
25
+ # -------------------------------------------------------
26
+
27
+ # πŸ”₯ **For Any Questions or Concerns**:
28
+ # Feel free to reach out to us! Whether it's about coding or life, we're here to help you grow. Let's chat!
29
+
30
+ # -------------------------------------------------------
31
+ # πŸ”” **Stay Tuned**:
32
+ # Don't miss out on more exciting content! Hit the Follow button on our Github page:
33
+ # πŸ‘‰ Github: https://github.com/Sohail-Shaikh-07/
34
+ # -------------------------------------------------------
35
+
36
+ # ⚑ **Pro Tip**:
37
+ # For the best experience, stick with the default library versions shown.
38
+ # We’ve got you covered for the smoothest coding ride! πŸš€
39
+ # -------------------------------------------------------
40
+
41
+
42
+
43
+ # ************************************************************* γ€ŽIγ€γ€ŽMγ€γ€ŽPγ€γ€ŽOγ€γ€ŽRγ€γ€ŽTγ€γ€ŽS』 *************************************************************
44
+
45
+ import random
46
+ import json
47
+ import torch
48
+ import numpy as np
49
+ import string
50
+
51
+ # ************************************************************* γ€ŽCγ€γ€ŽLγ€γ€ŽAγ€γ€ŽSγ€γ€ŽS』 *************************************************************
52
+
53
+ class ChatBot:
54
+ def __init__(
55
+ self,
56
+ model_path="chatbot_model.pth",
57
+ intents_path="product_intents.json",
58
+ confidence_threshold=0.75,
59
+ ):
60
+ self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
61
+ self.confidence_threshold = confidence_threshold
62
+
63
+ try:
64
+ # loading the intents file where the chatbot data is stored in json format
65
+ with open(intents_path, "r", encoding="utf-8") as f:
66
+ self.intents = json.load(f)
67
+
68
+ # Load trained model
69
+ data = torch.load(model_path, map_location=self.device)
70
+ self.input_size = data["input_size"]
71
+ self.hidden_size = data["hidden_size"]
72
+ self.output_size = data["output_size"]
73
+ self.all_words = data["all_words"]
74
+ self.tags = data["tags"]
75
+
76
+ # Initialize model
77
+ self.model = NeuralNet(
78
+ self.input_size, self.hidden_size, self.output_size
79
+ ).to(self.device)
80
+ self.model.load_state_dict(data["model_state"])
81
+ self.model.eval()
82
+
83
+ print("ChatBot initialized successfully!")
84
+
85
+ except FileNotFoundError as e:
86
+ print(f"Error: Could not find {e.filename}")
87
+ raise
88
+ except json.JSONDecodeError:
89
+ print("Error: Invalid JSON format in intents file")
90
+ raise
91
+ except Exception as e:
92
+ print(f"Error initializing chatbot: {str(e)}")
93
+ raise
94
+
95
+ def clean_text(self, text):
96
+ """Clean and tokenize text"""
97
+ return text.lower().split()
98
+
99
+ def stem(self, word):
100
+ """Simple stemming function"""
101
+
102
+ suffixes = ["ing", "ly", "ed", "es", "s", "er", "est", "y"]
103
+ for suffix in suffixes:
104
+ if word.endswith(suffix):
105
+ return word[: -len(suffix)]
106
+ return word
107
+
108
+ def bag_of_words(self, tokenized_sentence):
109
+ """Convert sentence into bag of words"""
110
+
111
+ sentence_words = [self.stem(word) for word in tokenized_sentence]
112
+
113
+ bag = np.zeros(len(self.all_words), dtype=np.float32)
114
+
115
+ for idx, w in enumerate(self.all_words):
116
+ if w in sentence_words:
117
+ bag[idx] = 1
118
+ return bag
119
+
120
+ def get_response(self, sentence):
121
+ try:
122
+
123
+ sentence_tokens = self.clean_text(sentence)
124
+
125
+ if not sentence_tokens:
126
+ return "Please say something!", 0.0
127
+
128
+ X = self.bag_of_words(sentence_tokens)
129
+ X = X.reshape(1, X.shape[0])
130
+ X = torch.from_numpy(X).to(self.device)
131
+
132
+ output = self.model(X)
133
+ _, predicted = torch.max(output, dim=1)
134
+
135
+ probs = torch.softmax(output, dim=1)
136
+ prob = probs[0][predicted.item()]
137
+
138
+ if prob.item() > self.confidence_threshold:
139
+ tag = self.tags[predicted.item()]
140
+
141
+ for intent in self.intents["intents"]:
142
+ if intent["tag"] == tag:
143
+ responses = intent["responses"]
144
+ return random.choice(responses), prob.item()
145
+
146
+ return (
147
+ "I'm not quite sure about that. Could you rephrase or ask something else?", # Bot will display message if something is unrelevant
148
+ prob.item(),
149
+ )
150
+
151
+ except Exception as e:
152
+ print(f"Error generating response: {str(e)}")
153
+ return "I'm having trouble processing that right now.", 0.0
154
+
155
+
156
+ class NeuralNet(torch.nn.Module):
157
+ def __init__(self, input_size, hidden_size, num_classes):
158
+ super(NeuralNet, self).__init__()
159
+ self.l1 = torch.nn.Linear(input_size, hidden_size)
160
+ self.l2 = torch.nn.Linear(hidden_size, hidden_size)
161
+ self.l3 = torch.nn.Linear(hidden_size, num_classes)
162
+ self.relu = torch.nn.ReLU()
163
+
164
+ def forward(self, x):
165
+ out = self.l1(x)
166
+ out = self.relu(out)
167
+ out = self.l2(out)
168
+ out = self.relu(out)
169
+ out = self.l3(out)
170
+ return out
171
+
172
+
173
+ # ************************************************************* γ€ŽMγ€γ€ŽAγ€γ€ŽIγ€γ€ŽN』 *************************************************************
174
+
175
+ def main():
176
+ print("\n=== ChatBot System ===")
177
+ print("Commands:")
178
+ print("- 'quit' or 'exit': Close the program")
179
+ print("- 'restart': Start a new conversation")
180
+ print("=" * 50)
181
+
182
+ while True:
183
+ # Initialize chatbot
184
+ print("\nInitializing ChatBot...")
185
+ try:
186
+ chatbot = ChatBot()
187
+ print("\nChatBot is ready! (Type 'restart' to reset, 'quit' to exit)")
188
+ print("-" * 50)
189
+
190
+ # Inner loop for conversation
191
+ while True:
192
+ user_input = input("You: ").strip()
193
+
194
+ if user_input.lower() in ["quit", "exit"]:
195
+ print("ChatBot: Goodbye! Have a great day!")
196
+ return # Exit the program
197
+
198
+ if user_input.lower() == "restart":
199
+ print("\nRestarting conversation...")
200
+ break
201
+
202
+ if not user_input:
203
+ print("ChatBot: Please say something!")
204
+ continue
205
+
206
+ response, confidence = chatbot.get_response(user_input)
207
+ print(f"ChatBot: {response}")
208
+ if confidence > 0:
209
+ print(f"Confidence: {confidence:.2%}")
210
+ print("-" * 50)
211
+
212
+ except KeyboardInterrupt:
213
+ print("\nChatBot: Goodbye! Have a great day!")
214
+ return
215
+ except Exception as e:
216
+ print(f"An error occurred: {str(e)}")
217
+ user_input = input("Would you like to restart? (yes/no): ").strip().lower()
218
+ if user_input != "yes":
219
+ return
220
+
221
+
222
+ if __name__ == "__main__":
223
+ main()
chatbot_model.pth ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fbb81f062654abfd983eeb3d24000aec714f3370b94211304c8fc12e495791e4
3
+ size 5508
product_intents.json ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "intents": [
3
+ {
4
+ "tag": "product_inquiry",
5
+ "patterns": [
6
+ "Tell me about your products",
7
+ "What products do you offer",
8
+ "What can I buy here",
9
+ "Show me your products",
10
+ "What do you sell"
11
+ ],
12
+ "responses": [
13
+ "We offer a wide range of products. What specific category are you interested in?",
14
+ "Our product catalog includes various items. Is there something specific you're looking for?",
15
+ "I'd be happy to tell you about our products. What type of product interests you?"
16
+ ]
17
+ },
18
+ {
19
+ "tag": "product_review",
20
+ "patterns": [
21
+ "How good is this product",
22
+ "What do people say about this",
23
+ "Is this product worth buying",
24
+ "Reviews for this product",
25
+ "Customer feedback"
26
+ ],
27
+ "responses": [
28
+ "Based on customer reviews, this product has received positive feedback for its quality and value.",
29
+ "Our customers have generally been satisfied with this product. Would you like to see specific reviews?",
30
+ "This product has been well-received by our customers. Let me share some highlights from recent reviews."
31
+ ]
32
+ },
33
+ {
34
+ "tag": "price_inquiry",
35
+ "patterns": [
36
+ "How much does it cost",
37
+ "What's the price",
38
+ "Is it expensive",
39
+ "Price range",
40
+ "Cost of product"
41
+ ],
42
+ "responses": [
43
+ "I can help you with pricing information. Could you specify which product you're interested in?",
44
+ "Prices vary depending on the specific product and model. Which item would you like to know about?",
45
+ "Let me check the current price for you. Which product are you looking at?"
46
+ ]
47
+ },
48
+ {
49
+ "tag": "comparison",
50
+ "patterns": [
51
+ "Compare products",
52
+ "Which is better",
53
+ "Difference between products",
54
+ "Compare features",
55
+ "Product comparison"
56
+ ],
57
+ "responses": [
58
+ "I can help you compare products. Which items would you like to compare?",
59
+ "Let me help you understand the differences between our products. Which ones are you considering?",
60
+ "I'll be happy to compare features for you. Which products would you like to know more about?"
61
+ ]
62
+ },
63
+ {
64
+ "tag": "availability",
65
+ "patterns": [
66
+ "Is it in stock",
67
+ "When will it be available",
68
+ "Do you have this product",
69
+ "Check stock",
70
+ "Available now"
71
+ ],
72
+ "responses": [
73
+ "I can check the current availability for you. Which product are you interested in?",
74
+ "Let me verify the stock status. Could you specify the product?",
75
+ "I'll help you check if the item is in stock. Which product would you like to know about?"
76
+ ]
77
+ },
78
+ {
79
+ "tag": "submit_review",
80
+ "patterns": [
81
+ "I want to write a review",
82
+ "How do I submit a review",
83
+ "Can I review this product",
84
+ "Leave feedback",
85
+ "Rate this product"
86
+ ],
87
+ "responses": [
88
+ "I'll help you submit a review. Please rate the product from 1-5 stars and share your experience.",
89
+ "You can submit a review by rating the product and describing your experience. Would you like to proceed?",
90
+ "To submit a review, please tell me which product you'd like to review and share your experience."
91
+ ]
92
+ },
93
+ {
94
+ "tag": "sentiment_analysis",
95
+ "patterns": [
96
+ "What do people think about this product",
97
+ "Is this product good or bad",
98
+ "Overall sentiment",
99
+ "Product satisfaction",
100
+ "Customer satisfaction"
101
+ ],
102
+ "responses": [
103
+ "Based on our sentiment analysis, this product has received mostly positive feedback.",
104
+ "Let me analyze the reviews for you. The overall sentiment for this product is positive.",
105
+ "I can tell you that customers generally rate this product favorably."
106
+ ]
107
+ }
108
+ ]
109
+ }
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ streamlit==1.31.1
2
+ pandas==2.2.0
3
+ plotly==5.18.0
4
+ torch==2.2.0
5
+ numpy==1.26.3
6
+
reviews_data.json ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "product": "Smart Watch",
4
+ "rating": 4,
5
+ "review": "nice",
6
+ "timestamp": "2025-02-17T14:15:39.313956"
7
+ },
8
+ {
9
+ "product": "Wireless Earbuds",
10
+ "rating": 5,
11
+ "review": "Nice build quality",
12
+ "timestamp": "2025-02-17T14:18:47.944078"
13
+ },
14
+ {
15
+ "product": "Gaming Console",
16
+ "rating": 5,
17
+ "review": "Processing power is superb",
18
+ "timestamp": "2025-02-17T14:19:00.395747"
19
+ },
20
+ {
21
+ "product": "Gaming Console",
22
+ "rating": 5,
23
+ "review": "Processing power is superb",
24
+ "timestamp": "2025-02-17T14:19:07.256996"
25
+ },
26
+ {
27
+ "product": "Gaming Console",
28
+ "rating": 1,
29
+ "review": "too bad.. lagging",
30
+ "timestamp": "2025-02-17T14:34:51.456843"
31
+ },
32
+ {
33
+ "product": "Tablet Ultra",
34
+ "rating": 4,
35
+ "review": "nice",
36
+ "timestamp": "2025-02-17T14:50:55.533737"
37
+ },
38
+ {
39
+ "product": "Gaming Console",
40
+ "rating": 5,
41
+ "review": "Superb Amazing Console",
42
+ "timestamp": "2025-02-18T18:01:56.433664"
43
+ }
44
+ ]
train.py ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•— β–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•— β–ˆβ–ˆβ•—
3
+ # β–ˆβ–ˆβ•”β•β•β•β•β• β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘
4
+ # β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—
5
+ # β•šβ•β•β•β•β•β•šβ•β•β•β•β•β•šβ•β•β•β•β•β•šβ•β•β•β•β• β•šβ•β•β•β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β•šβ•β•β•β•β•β•šβ•β•β•β•β•β•šβ•β•β•β•β•β•šβ•β•β•β•β•
6
+ # β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•‘ β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β• β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ•‘ β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—
7
+ # β•šβ•β•β•β•β•β•β• β•šβ•β•β•β•β•β• β•šβ•β• β•šβ•β• β•šβ•β• β•šβ•β• β•šβ•β• β•šβ•β•β•β•β•β•β•
8
+
9
+ # *******************************************************
10
+ # Created with πŸ’“ by - π’π¨π‘πšπ’π₯ π’π‘πšπ’π€π‘ | 😁
11
+ # *******************************************************
12
+
13
+ # -------------------------------------------------------
14
+ # ✨ Support the Code πŸ’»:
15
+ # β˜• Buy Me A Coffee: https://buymeacoffee.com/sohails07
16
+ # -------------------------------------------------------
17
+
18
+ # πŸ“± LET'S CONNECT 🌐:
19
+ # πŸš€ **Best way to contact**:
20
+ # πŸ“² Telegram: https://t.me/sohails_07
21
+
22
+ # 🌍 **Other Platforms**:
23
+ # πŸ“Έ Instagram:
24
+ # - Personal: https://www.instagram.com/sohails_07/
25
+ # -------------------------------------------------------
26
+
27
+ # πŸ”₯ **For Any Questions or Concerns**:
28
+ # Feel free to reach out to us! Whether it's about coding or life, we're here to help you grow. Let's chat!
29
+
30
+ # -------------------------------------------------------
31
+ # πŸ”” **Stay Tuned**:
32
+ # Don't miss out on more exciting content! Hit the Follow button on our Github page:
33
+ # πŸ‘‰ Github: https://github.com/Sohail-Shaikh-07/
34
+ # -------------------------------------------------------
35
+
36
+ # ⚑ **Pro Tip**:
37
+ # For the best experience, stick with the default library versions shown.
38
+ # We’ve got you covered for the smoothest coding ride! πŸš€
39
+ # -------------------------------------------------------
40
+
41
+
42
+
43
+ # ************************************************************* γ€ŽIγ€γ€ŽMγ€γ€ŽPγ€γ€ŽOγ€γ€ŽRγ€γ€ŽTγ€γ€ŽS』 *************************************************************
44
+
45
+ import torch
46
+ import torch.nn as nn
47
+ import numpy as np
48
+ import json
49
+ import nltk
50
+ from nltk.stem.porter import PorterStemmer
51
+ from torch.utils.data import Dataset, DataLoader
52
+
53
+ # ************************************************************* γ€ŽCγ€γ€ŽLγ€γ€ŽAγ€γ€ŽSγ€γ€ŽS』 *************************************************************
54
+
55
+ class NeuralNet(nn.Module):
56
+ def __init__(self, input_size, hidden_size, num_classes):
57
+ super(NeuralNet, self).__init__()
58
+ self.l1 = nn.Linear(input_size, hidden_size)
59
+ self.l2 = nn.Linear(hidden_size, hidden_size)
60
+ self.l3 = nn.Linear(hidden_size, num_classes)
61
+ self.relu = nn.ReLU()
62
+
63
+ def forward(self, x):
64
+ out = self.l1(x)
65
+ out = self.relu(out)
66
+ out = self.l2(out)
67
+ out = self.relu(out)
68
+ out = self.l3(out)
69
+ return out
70
+
71
+
72
+ class ChatDataset(Dataset):
73
+ def __init__(self, X_train, y_train):
74
+ self.n_samples = len(X_train)
75
+ self.x_data = torch.FloatTensor(X_train)
76
+ self.y_data = torch.LongTensor(y_train)
77
+
78
+ def __getitem__(self, index):
79
+ return self.x_data[index], self.y_data[index]
80
+
81
+ def __len__(self):
82
+ return self.n_samples
83
+
84
+
85
+ def tokenize(sentence):
86
+ """
87
+ Split sentence into array of words/tokens
88
+ a token can be a word or punctuation character, or number
89
+ """
90
+ return sentence.lower().split()
91
+
92
+
93
+ def stem(word):
94
+ stemmer = PorterStemmer()
95
+ return stemmer.stem(word.lower())
96
+
97
+
98
+ def bag_of_words(tokenized_sentence, words):
99
+ sentence_words = [stem(word) for word in tokenized_sentence]
100
+ bag = np.zeros(len(words), dtype=np.float32)
101
+ for idx, w in enumerate(words):
102
+ if w in sentence_words:
103
+ bag[idx] = 1
104
+ return bag
105
+
106
+
107
+ def train_model():
108
+ try:
109
+ # Download required NLTK data for model training
110
+ print("Downloading required NLTK data...")
111
+ nltk.download('punkt')
112
+ nltk.download('averaged_perceptron_tagger')
113
+ nltk.download('wordnet')
114
+
115
+ # Load the intents file
116
+ print("Loading product_intents.json...")
117
+ with open("product_intents.json", "r", encoding="utf-8") as f:
118
+ intents = json.load(f)
119
+
120
+ print("Processing training data...")
121
+ all_words = []
122
+ tags = []
123
+ xy = []
124
+
125
+ # Validate intents structure
126
+ if not isinstance(intents, dict) or "intents" not in intents:
127
+ raise ValueError(
128
+ "Invalid intents.json format. Root should be a dictionary with 'intents' key."
129
+ )
130
+
131
+ # Process each intent
132
+ for intent in intents["intents"]:
133
+ # Skip if intent is missing required fields
134
+ if not isinstance(intent, dict):
135
+ continue
136
+ if "tag" not in intent or "patterns" not in intent:
137
+ continue
138
+
139
+ tag = intent["tag"]
140
+ tags.append(tag)
141
+
142
+ for pattern in intent["patterns"]:
143
+ w = tokenize(pattern)
144
+ all_words.extend(w)
145
+ xy.append((w, tag))
146
+
147
+ # Stem and lower each word
148
+ print("Processing words...")
149
+ ignore_words = ["?", ".", "!", ","]
150
+ all_words = [stem(w) for w in all_words if w not in ignore_words]
151
+ all_words = sorted(set(all_words))
152
+ tags = sorted(set(tags))
153
+
154
+ print(f"Number of patterns: {len(xy)}")
155
+ print(f"Number of tags: {len(tags)}")
156
+ print(f"Number of unique stemmed words: {len(all_words)}")
157
+
158
+ # Create training data
159
+ X_train = []
160
+ y_train = []
161
+
162
+ for pattern_sentence, tag in xy:
163
+ bag = bag_of_words(pattern_sentence, all_words)
164
+ X_train.append(bag)
165
+ label = tags.index(tag)
166
+ y_train.append(label)
167
+
168
+ # Convert to numpy arrays
169
+ X_train = np.array(X_train)
170
+ y_train = np.array(y_train)
171
+
172
+ # Hyperparameters
173
+ num_epochs = 100 # You can increase epochs -- An epoch refers to one complete pass through the training data, where the model adjusts its parameters to minimize loss.
174
+ batch_size = 8
175
+ learning_rate = 0.001
176
+ input_size = len(X_train[0])
177
+ hidden_size = 8
178
+ output_size = len(tags)
179
+
180
+ print(
181
+ f"Training with: input_size={input_size}, hidden_size={hidden_size}, output_size={output_size}"
182
+ )
183
+
184
+ # Create dataset
185
+ dataset = ChatDataset(X_train, y_train)
186
+ train_loader = DataLoader(dataset=dataset, batch_size=batch_size, shuffle=True)
187
+
188
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
189
+ model = NeuralNet(input_size, hidden_size, output_size).to(device)
190
+
191
+ # Loss and optimizer
192
+ criterion = nn.CrossEntropyLoss()
193
+ optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
194
+
195
+ # Training loop
196
+ print("Starting training...")
197
+ for epoch in range(num_epochs):
198
+ for words, labels in train_loader:
199
+ words = words.to(device)
200
+ labels = labels.to(device)
201
+
202
+ # Forward pass
203
+ outputs = model(words)
204
+ loss = criterion(outputs, labels)
205
+
206
+ # Backward and optimize
207
+ optimizer.zero_grad()
208
+ loss.backward()
209
+ optimizer.step()
210
+
211
+ if (epoch + 1) % 100 == 0:
212
+ print(f"Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}")
213
+
214
+ print(f"Final loss: {loss.item():.4f}")
215
+
216
+ # Save the model and data
217
+ data = {
218
+ "model_state": model.state_dict(),
219
+ "input_size": input_size,
220
+ "hidden_size": hidden_size,
221
+ "output_size": output_size,
222
+ "all_words": all_words,
223
+ "tags": tags,
224
+ }
225
+
226
+ torch.save(data, "chatbot_model.pth")
227
+ print(f"Training complete. Model saved to chatbot_model.pth")
228
+
229
+ return model, all_words, tags
230
+
231
+ except Exception as e:
232
+ print(f"An error occurred during training: {str(e)}")
233
+ import traceback
234
+
235
+ traceback.print_exc()
236
+ return None, None, None
237
+
238
+
239
+ # ************************************************************* γ€ŽMγ€γ€ŽAγ€γ€ŽIγ€γ€ŽN』 *************************************************************
240
+
241
+ if __name__ == "__main__":
242
+ train_model()