-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapp.py
More file actions
131 lines (111 loc) · 4.2 KB
/
app.py
File metadata and controls
131 lines (111 loc) · 4.2 KB
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
import requests
import streamlit as st
from datetime import datetime
BASE_API_URL = "https://api.langflow.astra.datastax.com"
LANGFLOW_ID = st.secrets['LANGFLOW_ID']
ENDPOINT = st.secrets['ENDPOINT']
APPLICATION_TOKEN = st.secrets['APP_TOKEN']
def run_flow(message: str) -> dict:
api_url = f"{BASE_API_URL}/lf/{LANGFLOW_ID}/api/v1/run/{ENDPOINT}"
payload = {
"input_value": message,
"output_type": "chat",
"input_type": "chat",
}
headers = {"Authorization": "Bearer " + APPLICATION_TOKEN, "Content-Type": "application/json"}
response = requests.post(api_url, json=payload, headers=headers)
return response.json()
def main():
# Apply custom styles
st.markdown(
"""
<style>
.chat-bubble {
background-color: #007bff;
color: white;
padding: 10px;
border-radius: 15px;
max-width: 80%;
margin-bottom: 10px;
font-size: 16px;
line-height: 1.5;
}
.chat-bubble-user {
background-color: #f1f1f1;
color: black;
padding: 10px;
border-radius: 15px;
max-width: 80%;
margin-bottom: 10px;
align-self: flex-end;
font-size: 16px;
line-height: 1.5;
}
.timestamp {
font-size: 12px;
color: gray;
margin-bottom: 5px;
}
</style>
""",
unsafe_allow_html=True,
)
st.title("📞 Customer Support Assistant")
st.subheader("Get instant answers to your questions!")
# Initialize session state for messages
if "messages" not in st.session_state:
st.session_state["messages"] = []
if "current_input" not in st.session_state:
st.session_state["current_input"] = ""
# Clear chat button
if st.button("Clear Chat"):
st.session_state["messages"] = []
st.session_state["current_input"] = ""
# Sample questions
st.markdown("### Try asking:")
col1, col2, col3 = st.columns(3)
if col1.button("How can I track my order?"):
st.session_state["current_input"] = "How can I track my order?"
if col2.button("What is your return policy?"):
st.session_state["current_input"] = "What is your return policy?"
if col3.button("Can I cancel order #1004?"):
st.session_state["current_input"] = "Can I cancel order #1004?"
# Input form for question submission
with st.form(key="chat_form", clear_on_submit=False):
user_input = st.text_input(
"Type your question here",
placeholder="How can we help you today?",
value=st.session_state["current_input"],
)
submit_button = st.form_submit_button("Submit")
if submit_button:
if not user_input.strip():
st.error("Please enter a question.")
else:
try:
response = run_flow(user_input)
# Extract and store bot response
bot_response = response["outputs"][0]["outputs"][0]["results"]["message"]["text"]
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Add the latest interaction to the chat history
st.session_state["messages"].insert(0, {"user": user_input, "bot": bot_response, "timestamp": timestamp})
# Clear the current input
st.session_state["current_input"] = ""
except Exception as e:
st.error("Oops! Something went wrong. Please try again.")
# Display chat history (latest at the top)
for message in st.session_state["messages"]:
# Timestamp
st.markdown(f'<div class="timestamp">{message["timestamp"]}</div>', unsafe_allow_html=True)
# User's message
st.markdown(
f'<div class="chat-bubble-user">{message["user"]}</div>',
unsafe_allow_html=True,
)
# Bot's response
st.markdown(
f'<div class="chat-bubble">{message["bot"]}</div>',
unsafe_allow_html=True,
)
if __name__ == "__main__":
main()