Spaces:
Sleeping
Sleeping
File size: 6,765 Bytes
47589e6 |
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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
import os
import streamlit as st
from langchain_openai import ChatOpenAI
from langchain.vectorstores import FAISS
import pandas as pd
# Environment setup
os.environ["OPENAI_API_KEY"] = os.getenv("OPENAI_API_KEY")
# Streamlit page setup
st.set_page_config(
page_title="Airtel AI Agent System",
page_icon="🤖",
layout="wide",
initial_sidebar_state="expanded"
)
# Sidebar setup
with st.sidebar:
airtel_logo = "https://1000logos.net/wp-content/uploads/2023/06/Airtel-logo.jpg" # Airtel logo URL
st.image(airtel_logo, use_column_width=True)
st.markdown("### Welcome to Airtel AI Help Bot")
st.markdown("**Instructions:**")
st.markdown("- Use the chat interface to ask questions about Airtel services.")
st.markdown("- You can also initiate specific tasks like SIM swap or plan details.")
st.markdown("Feel free to ask any queries you have!")
# Apply custom CSS for the red-and-white theme
st.markdown(
"""
<style>
.stApp {
background-color: white;
}
header {
background-color: #e60000;
}
.stButton>button {
background-color: #e60000;
color: white;
}
.stTextInput>div>input {
background-color: #ffffff;
border: 2px solid #e60000;
color: black;
}
.css-1mgb8k9.e1fqkh3o3 {
background-color: white;
}
.css-18ni7ap.e8zbici0 {
background-color: white;
}
footer {
background-color: white;
}
</style>
""",
unsafe_allow_html=True,
)
# Page title
st.title("Airtel AI Agent System Demo")
# Initialize session state to store chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# User input for chat interface
user_input = st.text_input("Enter your message:")
# Initialize agents' tasks
def conversation_agent(state):
"""Handles general conversations with the user."""
llm = ChatOpenAI(model_name="gpt-3.5-turbo")
try:
response = llm.invoke(state["question"])
# Ensure that response is not None
if response and response.content:
parsed_response = response.content # Directly access content from AIMessage object
else:
parsed_response = "I'm sorry, I couldn't generate a response."
except Exception as e:
parsed_response = f"An error occurred: {e}"
return {"question": state["question"], "response": parsed_response, "intent": "conversation"}
def website_query_agent(state):
"""Handles queries by retrieving relevant data from Airtel website stored in vector DB."""
vectorstore = FAISS.load_local("path_to_faiss_index") # Assuming Airtel data is preloaded in FAISS
docs = vectorstore.similarity_search(state["question"])
response = "\n".join([doc.page_content for doc in docs[:3]]) # Return top 3 relevant docs
return {"question": state["question"], "response": response, "intent": "website_query"}
def fallback_agent(state):
"""Returns a fallback message for unrelated queries."""
return {"question": state["question"], "response": "Sorry, I don't have an answer for that.", "intent": "fallback"}
def sim_swap_agent(state):
"""Handles SIM swap workflow by triggering a form to collect user details."""
st.write("---Sim Swap Request---")
# File path to store the data
file_path = "sim_swap_requests.xlsx"
# Form for collecting user details
with st.form("sim_swap_form"):
name = st.text_input("Enter your name:")
phone_number = st.text_input("Enter your phone number:")
submit = st.form_submit_button("Submit")
if submit:
new_data = {"Name": [name], "Phone Number": [phone_number]}
df_new = pd.DataFrame(new_data)
# Check if the Excel file already exists
if os.path.exists(file_path):
df_existing = pd.read_excel(file_path)
df_updated = pd.concat([df_existing, df_new], ignore_index=True)
else:
df_updated = df_new
# Save the updated DataFrame to Excel
df_updated.to_excel(file_path, index=False)
return {"question": state["question"], "response": f"SIM Swap request submitted for {name} with phone number {phone_number}.", "intent": "sim_swap"}
return {"question": state["question"], "response": "Please fill in your details for SIM swap.", "intent": "sim_swap"}
def plan_details_agent(state):
"""Handles plan details workflow by collecting phone number and returning a standard plan."""
with st.form("plan_details_form"):
phone_number = st.text_input("Enter your phone number:")
submit = st.form_submit_button("Submit")
if submit:
return {"question": state["question"], "response": f"Plan details for {phone_number}: You are on a standard plan of 999 INR/month.", "intent": "plan_details"}
return {"question": state["question"], "response": "Please enter your phone number to check plan details.", "intent": "plan_details"}
# Decision logic
def decide_agent(state):
question = state["question"].lower()
if "sim swap" in question or "swap sim" in question:
return "sim_swap"
elif "plan details" in question or "active plan" in question:
return "plan_details"
elif "airtel" in question:
return "conversation"
elif "website" in question or "airtel website" in question:
return "website_query"
else:
return "fallback"
# Workflow logic
workflow = {
"conversation": conversation_agent,
"website_query": website_query_agent,
"sim_swap": sim_swap_agent,
"plan_details": plan_details_agent,
"fallback": fallback_agent,
}
# Function to handle user input and generate responses
def handle_query(query):
state = {"question": query, "response": "", "intent": ""}
next_agent = decide_agent(state)
if next_agent in workflow:
agent = workflow[next_agent]
output = agent(state)
return output["response"]
# Chat Interface
if user_input:
st.session_state.messages.append({"role": "user", "content": user_input})
bot_response = handle_query(user_input)
st.session_state.messages.append({"role": "bot", "content": bot_response})
# Display chat messages in reverse order (latest message on top)
for message in reversed(st.session_state.messages):
if message["role"] == "user":
st.chat_message("user").markdown(message["content"])
else:
st.chat_message("bot").markdown(message["content"]) |