|
|
import streamlit as st |
|
|
from langchain_google_genai import ChatGoogleGenerativeAI |
|
|
from transformers import pipeline |
|
|
import requests |
|
|
import streamlit_authenticator as stauth |
|
|
|
|
|
|
|
|
llm = ChatGoogleGenerativeAI(model="gemini-pro", google_api_key=st.secrets["GOOGLE_API_KEY"]) |
|
|
|
|
|
|
|
|
classifier = pipeline("text-classification", model="distilbert-base-uncased") |
|
|
|
|
|
|
|
|
if "conversation_history" not in st.session_state: |
|
|
st.session_state.conversation_history = [] |
|
|
|
|
|
if "feedback" not in st.session_state: |
|
|
st.session_state.feedback = [] |
|
|
|
|
|
|
|
|
users = stauth.Authenticate( |
|
|
names=['John Doe', 'Jane Smith'], |
|
|
usernames=['johndoe', 'janesmith'], |
|
|
passwords=['password1', 'password2'], |
|
|
cookie_name='chat_auth', |
|
|
key='abcdef' |
|
|
) |
|
|
|
|
|
|
|
|
def generate_response(user_query): |
|
|
prompt = f"User: {user_query}" |
|
|
answers = llm.invoke(prompt) |
|
|
return answers.content |
|
|
|
|
|
|
|
|
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" |
|
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|
def display_conversation_history(): |
|
|
for message in st.session_state.conversation_history: |
|
|
st.write(message) |
|
|
|
|
|
|
|
|
def handle_feedback(feedback): |
|
|
st.session_state.feedback.append(feedback) |
|
|
st.write("Thank you for your feedback!") |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
def main(): |
|
|
st.sidebar.title("Customer Support Chatbot") |
|
|
|
|
|
|
|
|
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(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() |
|
|
|
|
|
|