dineshabey commited on
Commit
0ec77bf
1 Parent(s): 893c843

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+ import streamlit as st
3
+
4
+ # Set up Streamlit app title and page width
5
+ st.set_page_config(page_title='Simple Chatbot with Streamlit', layout='wide')
6
+
7
+ # Load conversational pipeline
8
+ chatbot = pipeline("text2text-generation", model="facebook/blenderbot-400M-distill")
9
+
10
+ # Initialize chat history in session state
11
+ if 'chat_history' not in st.session_state:
12
+ st.session_state['chat_history'] = []
13
+
14
+ # Define Streamlit app layout
15
+ st.markdown("""
16
+ <style>
17
+ body {
18
+ background-color: #f0f2f6;
19
+ }
20
+ .stApp {
21
+ max-width: 800px;
22
+ margin: auto;
23
+ padding-top: 50px;
24
+ }
25
+ .stTextInput {
26
+ margin-bottom: 20px;
27
+ }
28
+ .stButton button {
29
+ background-color: #4CAF50; /* Green */
30
+ border: none;
31
+ color: white;
32
+ padding: 15px 32px;
33
+ text-align: center;
34
+ text-decoration: none;
35
+ display: inline-block;
36
+ font-size: 16px;
37
+ margin: 4px 2px;
38
+ cursor: pointer;
39
+ border-radius: 5px;
40
+ }
41
+ .stButton button:hover {
42
+ background-color: #7EC0EE; /* Darker sky blue on hover */
43
+ }
44
+ .chat-container {
45
+ padding:6px;
46
+ border-radius: 5px;
47
+ overflow: hidden;
48
+ box-shadow: 0 4px 8px rgba(255, 255, 255, 0.3);
49
+ # border: 1px solid #d3d3d3;
50
+ overflow: hidden;
51
+ }
52
+ .user-message {
53
+ background-color: #4CAF50;
54
+ color: white;
55
+ padding: 10px 20px;
56
+ margin: 5px;
57
+ border-radius: 5px;
58
+ clear: both;
59
+ float: left;
60
+ }
61
+ .bot-message {
62
+ background-color: #f1f1f1;
63
+ color: black;
64
+ padding: 10px 20px;stButton
65
+ margin: 5px;
66
+ border-radius: 5px;
67
+ clear: both;
68
+ float: right;
69
+ }
70
+
71
+ </style>
72
+ """, unsafe_allow_html=True)
73
+
74
+ # Define Streamlit app layout
75
+ st.markdown("<h1 style='color: orange;'>💬 Chatbot</h1>", unsafe_allow_html=True)
76
+ st.caption("🚀 Chat bot created By :- [Dinesh Abeysinghe (AI-ML Engineer)](https://www.linkedin.com/in/dinesh-abeysinghe-bb773293)")
77
+
78
+ # Create text area for user input
79
+ user_input = st.text_input("", placeholder="Your message")
80
+
81
+
82
+ # Define Streamlit app behavior
83
+ if st.button('Send'):
84
+ if not user_input.strip(): # Check if input is empty or whitespace
85
+ st.error('Please enter a chat')
86
+ else:
87
+ chat_history = st.session_state['chat_history']
88
+ chat_history.append({"role": "user", "message": user_input})
89
+
90
+ with st.spinner(text='Thinking ...'):
91
+ conversation_bot_result = chatbot(user_input)
92
+
93
+ bot_response = conversation_bot_result[0]["generated_text"]
94
+ chat_history.append({"role": "bot", "message": bot_response})
95
+
96
+ # Update chat history in session state
97
+ st.session_state['chat_history'] = chat_history
98
+
99
+
100
+ # Create text area for chat history
101
+ chat_area = st.empty()
102
+
103
+ # Display the chat history with alternating user and bot messages
104
+ chat_text = ""
105
+ user_messages = [chat for chat in st.session_state['chat_history'] if chat['role'] == 'user']
106
+ bot_messages = [chat for chat in st.session_state['chat_history'] if chat['role'] == 'bot']
107
+
108
+ for user_chat, bot_chat in zip(reversed(user_messages), reversed(bot_messages)):
109
+ chat_text += f"<div class='user-message'>{user_chat['message']}</div>\n"
110
+ chat_text += f"<div class='bot-message'>{bot_chat['message']}</div>\n"
111
+
112
+ # Add any remaining user messages if there are more user messages than bot messages
113
+ for user_chat in reversed(user_messages[len(bot_messages):]):
114
+ chat_text += f"<div class='user-message'>{user_chat['message']}</div>\n"
115
+
116
+ chat_area.markdown(f"<div class='chat-container'>{chat_text}</div>", unsafe_allow_html=True)