import streamlit as st from sentencetransformers import SentenceTransformer import…
- User
import streamlit as st from sentence_transformers import SentenceTransformer import pandas as pd import numpy as np import os
Load your retrieval model
retrieval_model_path = "model_jatin//" retrieval_model = SentenceTransformer(retrieval_model_path)
Load and prepare your data
data = pd.read_csv('sales_portal.txt', sep="\t", encoding='cp1252', header=None) headers = data.iloc[0] new_df = pd.DataFrame(data.values[1:], columns=headers) new_df.rename(columns={' Query': 'user_query', 'Response': 'answer'}, inplace=True)
new_df = new_df[:5]
Add image_path and pdf_path columns (replace with actual paths)
new_df['image_path'] = 'NlpSql//images//1.PNG' new_df['pdf_path'] = 'NlpSql//images//Tech&Talk_Newsletter_Feb_2024.pdf'
def get_most_similar_question(userText): new_df['user_query'] = new_df['user_query'].str.lower() question_list = new_df["user_query"].to_list() question_embeddings = retrieval_model.encode(question_list) user_text_embedding = retrieval_model.encode([userText.lower()])
# Calculate similarity scores similarity_scores = np.inner(user_text_embedding, question_embeddings).flatten() top_match_idx = np.argmax(similarity_scores) if similarity_scores[top_match_idx] == 0: return "Sorry, no similar questions were found for your query." matched_row = new_df.iloc[top_match_idx] answer = matched_row['answer'] image_path = matched_row['image_path'] pdf_path = matched_row['pdf_path'] print('answer',matched_row['answer']) print('image_path','NlpSql/images/1.PNG') print('pdf_path',matched_row['pdf_path']) results = { "answer": answer, "image": image_path, "pdf": pdf_path } return resultsStreamlit app interface
st.title("Sales Portal Chatbot")
user_input = st.text_input("Ask your question:") if st.button("Submit"): response = get_most_similar_question(user_input)
if isinstance(response, str): st.write(response) else: if response['answer']: print('response',response['answer']) st.write(response['answer']) if response['image']: if os.path.exists(response['image']): print('response',response['image']) from PIL import Image from numpy import asarray # load the image and convert into # numpy array img = Image.open(response['image']) # asarray() class is used to convert # PIL images into NumPy arrays numpydata = asarray(img) # <class 'numpy.ndarray'> print(type(numpydata)) # shape print(numpydata.shape) st.image(numpydata, caption='Related Image') else: st.write("Image not found or path is invalid.") if response['pdf']: if os.path.exists(response['pdf']): with open(response['pdf'], 'rb') as file: btn = st.download_button( label="Download related PDF", data=file, file_name=response['pdf'].split('/')[-1], mime="application/pdf" ) else: st.write("PDF not found or path is invalid.")always giving image not found or path is invalid
- Assistant
It seems you are encountering an issue with file paths when trying to load images and PDFs in your Streamlit application. This typically happens due to incorrect file paths.
Here are a few debugging steps and improvements you can try:
-
Check Current Working Directory: Ensure that your file paths are relative to the directory from which you are running your Streamlit app. You can check the current working directory using
os.getcwd(). -
Use Absolute Paths for Debugging: For debugging purposes, try to use absolute paths to make sure the code is working and then revert to relative paths.
-
Streamlit File Uploader: It can also be beneficial to use Streamlit's file uploader widget to check that your files are accessible.
Here’s how you can incorporate these steps:
Updated Code:
import streamlit as st from sentence_transformers import SentenceTransformer import pandas as pd import numpy as np import os # Load your retrieval model retrieval_model_path = "model_jatin//" retrieval_model = SentenceTransformer(retrieval_model_path) # Load and prepare your data data = pd.read_csv('sales_portal.txt', sep="\t", encoding='cp1252', header=None) headers = data.iloc[0] new_df = pd.DataFrame(data.values[1:], columns=headers) new_df.rename(columns={' Query': 'user_query', 'Response': 'answer'}, inplace=True) # new_df = new_df[:5] # Add image_path and pdf_path columns (replace with actual paths) new_df['image_path'] = 'NlpSql//images//1.PNG' new_df['pdf_path'] = 'NlpSql//images//Tech&Talk_Newsletter_Feb_2024.pdf' def get_most_similar_question(userText): new_df['user_query'] = new_df['user_query'].str.lower() question_list = new_df["user_query"].to_list() question_embeddings = retrieval_model.encode(question_list) user_text_embedding = retrieval_model.encode([userText.lower()]) # Calculate similarity scores similarity_scores = np.inner(user_text_embedding, question_embeddings).flatten() top_match_idx = np.argmax(similarity_scores) if similarity_scores[top_match_idx] == 0: return "Sorry, no similar questions were found for your query." matched_row = new_df.iloc[top_match_idx] answer = matched_row['answer'] image_path = matched_row['image_path'] pdf_path = matched_row['pdf_path'] results = { "answer": answer, "image": image_path, "pdf": pdf_path } return results # Streamlit app interface st.title("Sales Portal Chatbot") user_input = st.text_input("Ask your question:") if st.button("Submit"): response = get_most_similar_question(user_input) if isinstance(response, str): st.write(response) else: if response['answer']: st.write(response['answer']) if response['image']: image_path = os.path.abspath(response['image']) if os.path.exists(image_path): from PIL import Image img = Image.open(image_path) st.image(img, caption='Related Image') else: st.write(f"Image not found or path is invalid: {image_path}") if response['pdf']: pdf_path = os.path.abspath(response['pdf']) if os.path.exists(pdf_path): with open(pdf_path, 'rb') as file: btn = st.download_button( label="Download related PDF", data=file, file_name=response['pdf'].split('/')[-1], mime="application/pdf" ) else: st.write(f"PDF not found or path is invalid: {pdf_path}") # Debugging: Check the current working directory st.write("Current working directory:", os.getcwd())Summary of Updates:
- Added
os.path.abspath()to convert relative paths to absolute paths. - Added debugging information to print the current working directory.
- Enhanced error messages to include the invalid paths.
Run the Streamlit App: Don't forget to run your Streamlit app using
streamlit run your_script.pycommand in terminal.This should help you identify if there's an issue with the path resolution. If the paths are correct and files exist, the images and PDFs should load correctly.
-
- User
RuntimeError: Failed to import transformers.pipelines because of the following error (look up to see its traceback): Descriptors cannot be created directly. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0. If you cannot immediately regenerate your protos, some other possible workarounds are:
- Downgrade the protobuf package to 3.20.x or lower.
- Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).
- Assistant
The error you're seeing indicates a problem with the
protobufpackage version in your environment. This is a common issue when working with libraries liketransformers,sentence-transformers, or other libraries that depend on protocol buffers.Here are some steps you can follow to resolve this issue:
Solution 1: Downgrade the
protobufPackageDowngrade the
protobufpackage to a compatible version like 3.20.x:pip install protobuf==3.20.3Solution 2: Set the Environment Variable
Setting the environment variable
PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATIONtopythoncan mitigate the issue, though with a performance trade-off:On Linux or macOS, you can set the environment variable in the shell:
export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=pythonOn Windows Command Prompt or PowerShell, you can set the environment variable as follows:
set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=pythonSolution 3: Regenerate Protobuf Code
Regenerate the protobuf code with a newer version of
protoc(>= 3.19.0). This may not be feasible in all cases, especially if you do not control the generated proto files. Hence the previous two solutions are preferable as a quick fix.Verifying Installation & Environment
Make sure your environment is consistent. You might want to also check for any conflicting versions:
Check installed packages and versions:
pip listUpgrade or reinstall relevant packages:
If you need to upgrade the
transformerslibrary:pip install --upgrade transformersThen, run your
streamlitapp again after applying one of these solutions.Final Code Execution
After applying one of the above solutions, you should be able to run your Streamlit app without encountering the
protobuferror:streamlit run your_script.pyLet me know if you run into any more issues!
- User
dict_predefined_answers ={ "Who are you":"I am the SAMS bot, your Virtual Sales Assistant. I'm here to help you navigate through SAMS databases and provide the information you need.", "Hi":"Hello, Welcome to SAMS Virtual Sales Assistant. I am designed to assist you in retrieving information from various SAMS databases. Please feel free to ask your queries, such as, 'What is the total sellout across India for July 16, 2022? among others.", "Hello":"Hello, Welcome to SAMS Virtual Sales Assistant. I am designed to assist you in retrieving information from various SAMS databases. Please feel free to ask your queries, such as, 'What is the total sellout across India for July 16, 2022? among others.", "What can you do":"I am equipped to offer you comprehensive insights and data from SAMS databases. Whether you need sales figures or specific reports, just ask, and I'll provide the most accurate information available.Although my training is still going on.", "How can I use this service":"Using this service is simple. Just type in your query about any sales or database-related information, like 'Show the sales trend for product XYZ during the last month,' and I'll fetch the details for you.", "I'm not sure what to ask.":"No problem at all. You can start with general queries like 'What were the top-selling products last month?'' or 'Update me on the current monthly sales report.' I am here to guide you through accessing the precise information you need.", "Thank you":"You're most welcome! If you have any more questions or need further assistance, I'm here to help. Your success is my priority.", "Goodbye":"Farewell! If you ever require any assistance or have more queries in the future, don't hesitate to reach out. Have a great day ahead!", "Bye":"Farewell! If you ever require any assistance or have more queries in the future, don't hesitate to reach out. Have a great day ahead!", "How are you":"I am good,Please ask anything related to SAMS and I will try to provide you best possible solution.Thanks" }
i ama creating a chatbot, thiese are default messgaes,I wnat to return the response in JSON whenever user asks "Hi" or anything like this,Please do it
- Assistant
To integrate predefined responses into your chatbot for known greetings and queries, you can enhance your Streamlit app to handle these cases before you use the semantic similarity matching. Below is the updated version of your Streamlit chatbot that considers predefined responses and returns them in JSON format:
import streamlit as st from sentence_transformers import SentenceTransformer import pandas as pd import numpy as np import os import json # Load your retrieval model retrieval_model_path = "model_jatin//" retrieval_model = SentenceTransformer(retrieval_model_path) # Load and prepare your data data = pd.read_csv('sales_portal.txt', sep="\t", encoding='cp1252', header=None) headers = data.iloc[0] new_df = pd.DataFrame(data.values[1:], columns=headers) new_df.rename(columns={' Query': 'user_query', 'Response': 'answer'}, inplace=True) # new_df = new_df[:5] # Add image_path and pdf_path columns (replace with actual paths) new_df['image_path'] = 'NlpSql//images//1.PNG' new_df['pdf_path'] = 'NlpSql//images//Tech&Talk_Newsletter_Feb_2024.pdf' dict_predefined_answers = { "Who are you": "I am the SAMS bot, your Virtual Sales Assistant. I'm here to help you navigate through SAMS databases and provide the information you need.", "Hi": "Hello, Welcome to SAMS Virtual Sales Assistant. I am designed to assist you in retrieving information from various SAMS databases. Please feel free to ask your queries, such as, 'What is the total sellout across India for July 16, 2022? among others.", "Hello": "Hello, Welcome to SAMS Virtual Sales Assistant. I am designed to assist you in retrieving information from various SAMS databases. Please feel free to ask your queries, such as, 'What is the total sellout across India for July 16, 2022? among others.", "What can you do": "I am equipped to offer you comprehensive insights and data from SAMS databases. Whether you need sales figures or specific reports, just ask, and I'll provide the most accurate information available.Although my training is still going on.", "How can I use this service": "Using this service is simple. Just type in your query about any sales or database-related information, like 'Show the sales trend for product XYZ during the last month,' and I'll fetch the details for you.", "I'm not sure what to ask.": "No problem at all. You can start with general queries like 'What were the top-selling products last month?'' or 'Update me on the current monthly sales report.' I am here to guide you through accessing the precise information you need.", "Thank you": "You're most welcome! If you have any more questions or need further assistance, I'm here to help. Your success is my priority.", "Goodbye": "Farewell! If you ever require any assistance or have more queries in the future, don't hesitate to reach out. Have a great day ahead!", "Bye": "Farewell! If you ever require any assistance or have more queries in the future, don't hesitate to reach out. Have a great day ahead!", "How are you": "I am good,Please ask anything related to SAMS and I will try to provide you best possible solution.Thanks" } def get_most_similar_question(userText): user_text_lower = userText.lower() new_df['user_query'] = new_df['user_query'].str.lower() # Check for predefined answers first if user_text_lower in dict_predefined_answers: return { "answer": dict_predefined_answers[userText], "image": None, "pdf": None } question_list = new_df["user_query"].to_list() question_embeddings = retrieval_model.encode(question_list) user_text_embedding = retrieval_model.encode([user_text_lower]) # Calculate similarity scores similarity_scores = np.inner(user_text_embedding, question_embeddings).flatten() top_match_idx = np.argmax(similarity_scores) if similarity_scores[top_match_idx] == 0: return { "answer": "Sorry, no similar questions were found for your query.", "image": None, "pdf": None } matched_row = new_df.iloc[top_match_idx] answer = matched_row['answer'] image_path = matched_row['image_path'] pdf_path = matched_row['pdf_path'] results = { "answer": answer, "image": image_path, "pdf": pdf_path } return results # Streamlit app interface st.title("Sales Portal Chatbot") user_input = st.text_input("Ask your question:") if st.button("Submit"): response = get_most_similar_question(user_input) if response["answer"]: st.json(response) # Return response in JSON format if response['image']: image_path = os.path.abspath(response['image']) if os.path.exists(image_path): from PIL import Image img = Image.open(image_path) st.image(img, caption='Related Image') else: st.write(f"Image not found or path is invalid: {image_path}") if response['pdf']: pdf_path = os.path.abspath(response['pdf']) if os.path.exists(pdf_path): with open(pdf_path, 'rb') as file: btn = st.download_button( label="Download related PDF", data=file, file_name=response['pdf'].split('/')[-1], mime="application/pdf" ) else: st.write(f"PDF not found or path is invalid: {pdf_path}") # Debugging: Check the current working directory st.write("Current working directory:", os.getcwd())Explanation:
- Predefined Answers: The
dict_predefined_answersdictionary has predefined responses for specific user inputs. - Custom Handling: The
get_most_similar_questionfunction first checks if the input is indict_predefined_answers. If it is, it directly returns the predefined response. - Streamlit Interface: When the user submits their query, if a predefined response is found, it is returned in JSON format.
- Error Handling: The updated code also includes handling for missing files and displaying appropriate messages.
Running the App:
Ensure all dependencies are installed and run the Streamlit app using:
streamlit run your_script.pyThis should give your chatbot the ability to respond with predefined messages when applicable while continuing to handle other queries using semantic similarity matching.
- Predefined Answers: The