flow / app.py
rashid01's picture
Update app.py
0d59219 verified
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()