File size: 4,485 Bytes
0d59219 2974513 0d59219 8798f05 0d59219 8798f05 0d59219 9e8dca6 0d59219 9e8dca6 0d59219 9e8dca6 0d59219 9e8dca6 0d59219 9e8dca6 0d59219 9e8dca6 0d59219 9e8dca6 0d59219 9e8dca6 0d59219 9e8dca6 0d59219 9e8dca6 |
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 |
import streamlit as st
from langchain_google_genai import ChatGoogleGenerativeAI
from transformers import pipeline
import requests
import streamlit_authenticator as stauth
# Initialize the generative AI model
llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=st.secrets["GOOGLE_API_KEY"])
# Load a pre-trained intent classification model
classifier = pipeline("text-classification", model="distilbert-base-uncased")
# Initialize session state
if "conversation_history" not in st.session_state:
st.session_state.conversation_history = []
if "feedback" not in st.session_state:
st.session_state.feedback = []
# Define user authentication
users = stauth.Authenticate(
names=['John Doe', 'Jane Smith'],
usernames=['johndoe', 'janesmith'],
passwords=['password1', 'password2'],
cookie_name='chat_auth',
key='abcdef'
)
# Function to handle user queries and generate responses
def generate_response(user_query):
prompt = f"User: {user_query}"
answers = llm.invoke(prompt)
return answers.content
# Function to classify user intents using NLP
def classify_intent(user_query):
result = classifier(user_query)[0]
label = result['label']
intents = {
"greeting": ["LABEL_0"],
"thanks": ["LABEL_1"],
"goodbye": ["LABEL_2"],
"help": ["LABEL_3"],
"custom_query": ["LABEL_4"]
}
for intent, labels in intents.items():
if label in labels:
return intent
return "unknown"
# Function to update conversation history
def update_conversation_history(user_query, bot_response):
st.session_state.conversation_history.append(f"You: {user_query}")
st.session_state.conversation_history.append(f"Bot: {bot_response}")
# Function to display conversation history
def display_conversation_history():
for message in st.session_state.conversation_history:
st.write(message)
# Function to handle feedback
def handle_feedback(feedback):
st.session_state.feedback.append(feedback)
st.write("Thank you for your feedback!")
# Function to integrate with CRM system (pseudo-code)
def integrate_with_crm(user_query, bot_response):
crm_endpoint = "https://crm.example.com/api/record_interaction"
data = {
"user_query": user_query,
"bot_response": bot_response
}
response = requests.post(crm_endpoint, json=data)
return response.status_code
# Main Streamlit app
def main():
st.sidebar.title("Customer Support Chatbot")
# User authentication
name, authentication_status, username = users.login('Login', 'main')
if authentication_status:
st.title(f"Hello, {name}!")
display_conversation_history()
user_input = st.text_input("You:")
submit_button = st.button("Send")
if submit_button:
if user_input:
intent = classify_intent(user_input)
if intent == "greeting":
bot_response = "Hello! How can I help you today?"
elif intent == "thanks":
bot_response = "You're welcome!"
elif intent == "goodbye":
bot_response = "Goodbye! Have a great day."
elif intent == "help":
bot_response = "I can assist you with account issues, billing questions, product support, and technical issues. Please specify your query."
elif intent == "custom_query":
bot_response = generate_response(user_input)
else:
bot_response = "I'm sorry, I didn't understand that. How can I assist you?"
update_conversation_history(user_input, bot_response)
display_conversation_history()
# Integrate with CRM
integrate_with_crm(user_input, bot_response)
user_input = ""
feedback = st.text_input("Provide Feedback:")
feedback_button = st.button("Submit Feedback")
if feedback_button:
if feedback:
handle_feedback(feedback)
st.text_input("Provide Feedback:", value="", key="feedback_clear")
elif authentication_status == False:
st.error('Username/password is incorrect')
elif authentication_status == None:
st.warning('Please enter your username and password')
if __name__ == "__main__":
main()
|