Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,135 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 3 |
-
|
| 4 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
prompt = f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
except Exception as e:
|
| 24 |
-
return f"Error during data generation: {e}"
|
| 25 |
-
|
| 26 |
-
def parse_response_to_df(response):
|
| 27 |
-
"""Converts the response string to a DataFrame after cleaning up any formatting issues."""
|
| 28 |
-
try:
|
| 29 |
-
# Split response into lines
|
| 30 |
-
lines = response.split('\n')
|
| 31 |
-
|
| 32 |
-
# Look for the start of the table and gather relevant lines
|
| 33 |
-
table_lines = []
|
| 34 |
-
is_table = False
|
| 35 |
-
|
| 36 |
-
for line in lines:
|
| 37 |
-
if line.startswith("| Rank"):
|
| 38 |
-
is_table = True
|
| 39 |
-
if is_table and "|" in line:
|
| 40 |
-
# Clean line by removing unwanted characters and spaces
|
| 41 |
-
clean_line = line.strip().replace("∣", "|").replace(" ", "")
|
| 42 |
-
if clean_line:
|
| 43 |
-
table_lines.append(clean_line)
|
| 44 |
-
|
| 45 |
-
# Join lines and convert to DataFrame
|
| 46 |
-
cleaned_response = "\n".join(table_lines)
|
| 47 |
-
df = pd.read_csv(io.StringIO(cleaned_response), sep="|", skipinitialspace=True)
|
| 48 |
-
|
| 49 |
-
# Strip whitespace from headers and cells
|
| 50 |
-
df.columns = df.columns.str.strip()
|
| 51 |
-
df = df.apply(lambda x: x.str.strip() if x.dtype == "object" else x)
|
| 52 |
-
|
| 53 |
-
# Drop unnamed columns and rows where all elements are NaN
|
| 54 |
-
df = df.loc[:, ~df.columns.str.contains('^Unnamed')]
|
| 55 |
-
df = df.dropna(how='all')
|
| 56 |
-
|
| 57 |
-
return df
|
| 58 |
-
except Exception as e:
|
| 59 |
-
st.error(f"Error parsing response: {e}")
|
| 60 |
-
return None
|
| 61 |
-
|
| 62 |
-
# Display the chat history
|
| 63 |
-
for chat in st.session_state.chat_history:
|
| 64 |
-
st.info(f"You: {chat['user']}")
|
| 65 |
-
st.success(f"Meow: {chat['meow']}")
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
st.session_state.chat_history = []
|
| 89 |
-
st.rerun()
|
| 90 |
|
| 91 |
|
|
|
|
| 1 |
+
|
| 2 |
import streamlit as st
|
| 3 |
from langchain_google_genai import ChatGoogleGenerativeAI
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
import requests
|
| 6 |
+
import streamlit_authenticator as stauth
|
| 7 |
+
|
| 8 |
+
# Initialize the generative AI model
|
| 9 |
+
llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=st.secrets["GOOGLE_API_KEY"])
|
| 10 |
+
|
| 11 |
+
# Load a pre-trained intent classification model
|
| 12 |
+
classifier = pipeline("text-classification", model="distilbert-base-uncased")
|
| 13 |
+
|
| 14 |
+
# Initialize session state
|
| 15 |
+
if "conversation_history" not in st.session_state:
|
| 16 |
+
st.session_state.conversation_history = []
|
| 17 |
|
| 18 |
+
if "feedback" not in st.session_state:
|
| 19 |
+
st.session_state.feedback = []
|
| 20 |
|
| 21 |
+
# Define user authentication
|
| 22 |
+
users = stauth.Authenticate(
|
| 23 |
+
names=['John Doe', 'Jane Smith'],
|
| 24 |
+
usernames=['johndoe', 'janesmith'],
|
| 25 |
+
passwords=['password1', 'password2'],
|
| 26 |
+
cookie_name='chat_auth',
|
| 27 |
+
key='abcdef'
|
| 28 |
+
)
|
| 29 |
|
| 30 |
+
# Function to handle user queries and generate responses
|
| 31 |
+
def generate_response(user_query):
|
| 32 |
+
prompt = f"User: {user_query}"
|
| 33 |
+
answers = llm.invoke(prompt)
|
| 34 |
+
return answers.content
|
| 35 |
+
|
| 36 |
+
# Function to classify user intents using NLP
|
| 37 |
+
def classify_intent(user_query):
|
| 38 |
+
result = classifier(user_query)[0]
|
| 39 |
+
label = result['label']
|
| 40 |
|
| 41 |
+
intents = {
|
| 42 |
+
"greeting": ["LABEL_0"],
|
| 43 |
+
"thanks": ["LABEL_1"],
|
| 44 |
+
"goodbye": ["LABEL_2"],
|
| 45 |
+
"help": ["LABEL_3"],
|
| 46 |
+
"custom_query": ["LABEL_4"]
|
| 47 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
for intent, labels in intents.items():
|
| 50 |
+
if label in labels:
|
| 51 |
+
return intent
|
| 52 |
+
return "unknown"
|
| 53 |
+
|
| 54 |
+
# Function to update conversation history
|
| 55 |
+
def update_conversation_history(user_query, bot_response):
|
| 56 |
+
st.session_state.conversation_history.append(f"You: {user_query}")
|
| 57 |
+
st.session_state.conversation_history.append(f"Bot: {bot_response}")
|
| 58 |
+
|
| 59 |
+
# Function to display conversation history
|
| 60 |
+
def display_conversation_history():
|
| 61 |
+
for message in st.session_state.conversation_history:
|
| 62 |
+
st.write(message)
|
| 63 |
+
|
| 64 |
+
# Function to handle feedback
|
| 65 |
+
def handle_feedback(feedback):
|
| 66 |
+
st.session_state.feedback.append(feedback)
|
| 67 |
+
st.write("Thank you for your feedback!")
|
| 68 |
+
|
| 69 |
+
# Function to integrate with CRM system (pseudo-code)
|
| 70 |
+
def integrate_with_crm(user_query, bot_response):
|
| 71 |
+
crm_endpoint = "https://crm.example.com/api/record_interaction"
|
| 72 |
+
data = {
|
| 73 |
+
"user_query": user_query,
|
| 74 |
+
"bot_response": bot_response
|
| 75 |
+
}
|
| 76 |
+
response = requests.post(crm_endpoint, json=data)
|
| 77 |
+
return response.status_code
|
| 78 |
+
|
| 79 |
+
# Main Streamlit app
|
| 80 |
+
def main():
|
| 81 |
+
st.sidebar.title("Customer Support Chatbot")
|
| 82 |
+
|
| 83 |
+
# User authentication
|
| 84 |
+
name, authentication_status, username = users.login('Login', 'main')
|
| 85 |
+
|
| 86 |
+
if authentication_status:
|
| 87 |
+
st.title(f"Hello, {name}!")
|
| 88 |
|
| 89 |
+
display_conversation_history()
|
| 90 |
+
|
| 91 |
+
user_input = st.text_input("You:")
|
| 92 |
+
submit_button = st.button("Send")
|
| 93 |
+
|
| 94 |
+
if submit_button:
|
| 95 |
+
if user_input:
|
| 96 |
+
intent = classify_intent(user_input)
|
| 97 |
+
|
| 98 |
+
if intent == "greeting":
|
| 99 |
+
bot_response = "Hello! How can I help you today?"
|
| 100 |
+
elif intent == "thanks":
|
| 101 |
+
bot_response = "You're welcome!"
|
| 102 |
+
elif intent == "goodbye":
|
| 103 |
+
bot_response = "Goodbye! Have a great day."
|
| 104 |
+
elif intent == "help":
|
| 105 |
+
bot_response = "I can assist you with account issues, billing questions, product support, and technical issues. Please specify your query."
|
| 106 |
+
elif intent == "custom_query":
|
| 107 |
+
bot_response = generate_response(user_input)
|
| 108 |
+
else:
|
| 109 |
+
bot_response = "I'm sorry, I didn't understand that. How can I assist you?"
|
| 110 |
+
|
| 111 |
+
update_conversation_history(user_input, bot_response)
|
| 112 |
+
display_conversation_history()
|
| 113 |
+
|
| 114 |
+
# Integrate with CRM
|
| 115 |
+
integrate_with_crm(user_input, bot_response)
|
| 116 |
+
|
| 117 |
+
user_input = ""
|
| 118 |
+
|
| 119 |
+
feedback = st.text_input("Provide Feedback:")
|
| 120 |
+
feedback_button = st.button("Submit Feedback")
|
| 121 |
+
|
| 122 |
+
if feedback_button:
|
| 123 |
+
if feedback:
|
| 124 |
+
handle_feedback(feedback)
|
| 125 |
+
st.text_input("Provide Feedback:", value="", key="feedback_clear")
|
| 126 |
+
|
| 127 |
+
elif authentication_status == False:
|
| 128 |
+
st.error('Username/password is incorrect')
|
| 129 |
+
elif authentication_status == None:
|
| 130 |
+
st.warning('Please enter your username and password')
|
| 131 |
|
| 132 |
+
if __name__ == "__main__":
|
| 133 |
+
main()
|
|
|
|
|
|
|
| 134 |
|
| 135 |
|