-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstreamlit_bot.py
180 lines (139 loc) · 6.37 KB
/
streamlit_bot.py
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
import os
import csv
import datetime
from typing import Literal, TypeAlias
from dataclasses import dataclass
from sklearn.metrics import accuracy_score, classification_report
import streamlit.components.v1 as components
import streamlit as st
from chatbot import chat_bot, X_test,y_test,clf
@dataclass
class Message:
"""Class for keeping track of a chat message."""
origin: Literal["human", "ai"]
message: str
def load_css():
try:
with open("static/style.css", "r") as f:
css = f"<style>{f.read()}</style>"
st.markdown(css, unsafe_allow_html=True)
except FileNotFoundError: st.error("CSS file not found.")
except Exception as e: st.error(f"An error occurred: {e}")
def initialize_session_state():
if "history" not in st.session_state:
st.session_state.history = []
def on_click_callback():
human_prompt = st.session_state.human_prompt
response = chat_bot(human_prompt)
st.session_state.history.append(Message("human", human_prompt))
st.session_state.history.append(Message("ai", response))
load_css()
# creating side bar
menue=["🏠Home","🕓Chat History","📊Model Evaluation","📝About"]
choice =st.sidebar.selectbox("Menue",menue)
# Initialize chat history
if os.path.exists('chat_log.csv'):
with open('chat_log.csv', 'r', newline='', encoding='utf-8') as csvfile:
csv_reader = csv.reader(csvfile)
next(csv_reader)
for row in csv_reader:
st.write(f"**User**: {row[0]}\n**Chatbot**: {row[1]}\n*Time*: {row[2]}")
else :
'chat_history' not in st.session_state
st.session_state['chat_history'] = []
if choice == "🏠Home":
initialize_session_state()
st.title("GlobeGuru 🤖 ")
st.write(" Ask me anythink relared travel 😀 ")
chat_placeholder = st.container()
prompt_placeholder = st.form("Chat-form")
with chat_placeholder:
for chat in st.session_state.history:
div = f"""
<div class="chat-row
{'' if chat.origin =='ai' else 'row-reverse'}">
<img class="chat-icon" src="app/static/{
'ai_icon.png' if chat.origin == 'ai'
else 'user_icon.png'}
"width=32 height=32>
<div class="chat-bubble
{'ai-bubble' if chat.origin == 'ai' else 'human-bubble'}">
​{chat.message}
</div>
</div>
"""
st.markdown(div, unsafe_allow_html=True)
for _ in range(3):
st.markdown("")
with prompt_placeholder:
st.markdown("Chat")
cols = st.columns((6, 1))
cols[0].text_input("chat", value="Hello bot", label_visibility="collapsed", key="human_prompt",)
cols[1].form_submit_button("Submit", type="primary", on_click=on_click_callback,)
components.html("""
<script>
const streamlitDoc = window.parent.document;
const buttons = Array.from(
streamlitDoc.querySelectorAll('.stButton > button')
);
const submitButton = buttons.find(
el => el.innerText === 'Submit'
);
streamlitDoc.addEventListener('keydown', function(e) {
switch (e.key) {
case 'Enter':
submitButton.click();
break;
}
});
</script>
""",
height=0,
width=0,
)
elif choice == "🕓Chat History":
st.subheader("📜 Conversation History")
st.write("Below is a record of your recent conversations with the chatbot.")
if st.session_state['chat_history']:
for chat in st.session_state['chat_history'][-5:]:
st.write(f"**💬 You:** {chat['user']}")
st.write(f"**🤖 Chatbot:** {chat['chatbot']}")
st.write(f"**⏱ Timestamp:** {chat['timestamp']}")
st.markdown("---")
if st.button("🗑️ Clear History"):
st.session_state['chat_history'] = []
st.success("Chat history cleared!")
else:
st.info("📂 No chat history available.")
elif choice == "📊Model Evaluation":
st.subheader("📊 Model Performance Evaluation")
st.write("Evaluate the performance of the Intent-Based Chatbot.")
model_accuracy = accuracy_score(y_test, clf.predict(X_test))
classification_rep = classification_report(y_test, clf.predict(X_test))
st.write(f"📈 **Model Accuracy:** {model_accuracy * 100:.2f}%")
st.write("### 🛠 Classification Report")
st.text(classification_rep)
elif choice== "📝About":
st.write("The goal of this project to create a chatbot that can understand and resonce on give text")
st.subheader("Project Overview:")
st.write("""This chatbot project is a conversational AI system designed to understand and respond to user queries.
Built using Python, Natural Language Processing (NLP) techniques, and the Logistic Regression algorithm,
the chatbot provides a user-friendly interface through the Streamlit web framework.""")
st.header("Key Achievements")
st.subheader(" 1. NLP Techniques:")
st.write(" This chatbot effectively utilizes NLP techniques to understand user input.")
st.subheader(" 2. Logistic Regression Algorithm:")
st.write(" The Logistic Regression algorithm is successfully employed to classify user input.")
st.subheader(" 3. Streamlit Web Framework:")
st.write("The Streamlit web framework provides an intuitive and interactive interface.")
st.subheader(" 4. User Input Understanding:")
st.write("This chatbot project successfully understands user input.")
st.subheader(" 5. Suitable Response Generation:")
st.write("The project generates suitable responses to user queries")
st.header("Future Scope")
st.subheader(" 1. Intent Identification:")
st.write(" Incorporating intent identification capabilities will improve the chatbot's understanding of user input")
st.subheader(" 2. Emotion Detection:")
st.write("Adding emotion detection capabilities will enable the chatbot to understand user emotions.")
st.subheader(" 3. Multi-Language Support:")
st.write(" Incorporating multi-language support will allow the chatbot to understand user input in various languages.")