Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,8 +5,13 @@ import secretsload
|
|
5 |
import anton_ego_jimmy
|
6 |
import requests
|
7 |
import time
|
|
|
8 |
import re
|
9 |
|
|
|
|
|
|
|
|
|
10 |
from ibm_watsonx_ai.foundation_models import ModelInference
|
11 |
from ibm_watsonx_ai import Credentials, APIClient
|
12 |
from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams
|
@@ -18,7 +23,8 @@ credentials = load_stsecrets()
|
|
18 |
st.set_page_config(
|
19 |
page_title="Jimmy",
|
20 |
page_icon="π§",
|
21 |
-
initial_sidebar_state="collapsed"
|
|
|
22 |
)
|
23 |
|
24 |
# Password protection
|
@@ -34,13 +40,13 @@ def check_password():
|
|
34 |
st.markdown("\n\n")
|
35 |
st.text_input("Enter the password", type="password", on_change=password_entered, key="password")
|
36 |
st.divider()
|
37 |
-
st.info("
|
38 |
return False
|
39 |
elif not st.session_state["password_correct"]:
|
40 |
st.markdown("\n\n")
|
41 |
st.text_input("Enter the password", type="password", on_change=password_entered, key="password")
|
42 |
st.divider()
|
43 |
-
st.info("
|
44 |
st.error("π Password incorrect")
|
45 |
return False
|
46 |
else:
|
@@ -99,6 +105,58 @@ def generate_response(watsonx_llm, prompt_data, params):
|
|
99 |
for chunk in generated_response:
|
100 |
yield chunk
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
def get_response(user_input):
|
103 |
# Prepare the prompt
|
104 |
prompt = prepare_prompt(user_input, st.session_state.chat_history)
|
@@ -141,13 +199,19 @@ def get_response(user_input):
|
|
141 |
|
142 |
def main():
|
143 |
initialize_session_state()
|
144 |
-
st.
|
|
|
|
|
|
|
|
|
145 |
|
146 |
# User input
|
|
|
147 |
if anton_ego_jimmy.DISPLAY_CHAT_HISTORY == 1:
|
148 |
-
|
149 |
-
|
150 |
-
st.
|
|
|
151 |
user_input = st.chat_input("You:", key="user_input")
|
152 |
|
153 |
if user_input:
|
@@ -159,5 +223,17 @@ def main():
|
|
159 |
# Get response
|
160 |
get_response(user_input)
|
161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
162 |
if __name__ == "__main__":
|
163 |
main()
|
|
|
5 |
import anton_ego_jimmy
|
6 |
import requests
|
7 |
import time
|
8 |
+
import regex
|
9 |
import re
|
10 |
|
11 |
+
from datetime import datetime
|
12 |
+
import pdfkit
|
13 |
+
from jinja2 import Template
|
14 |
+
|
15 |
from ibm_watsonx_ai.foundation_models import ModelInference
|
16 |
from ibm_watsonx_ai import Credentials, APIClient
|
17 |
from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams
|
|
|
23 |
st.set_page_config(
|
24 |
page_title="Jimmy",
|
25 |
page_icon="π§",
|
26 |
+
initial_sidebar_state="collapsed",
|
27 |
+
layout="centered"
|
28 |
)
|
29 |
|
30 |
# Password protection
|
|
|
40 |
st.markdown("\n\n")
|
41 |
st.text_input("Enter the password", type="password", on_change=password_entered, key="password")
|
42 |
st.divider()
|
43 |
+
st.info("Designed and developed by Milan Mrdenovic Β© IBM Norway 2024")
|
44 |
return False
|
45 |
elif not st.session_state["password_correct"]:
|
46 |
st.markdown("\n\n")
|
47 |
st.text_input("Enter the password", type="password", on_change=password_entered, key="password")
|
48 |
st.divider()
|
49 |
+
st.info("Designed and developed by Milan Mrdenovic Β© IBM Norway 2024")
|
50 |
st.error("π Password incorrect")
|
51 |
return False
|
52 |
else:
|
|
|
105 |
for chunk in generated_response:
|
106 |
yield chunk
|
107 |
|
108 |
+
emoji_pattern = regex.compile(r'\p{Emoji}', flags=regex.UNICODE)
|
109 |
+
|
110 |
+
def remove_emojis(text):
|
111 |
+
return emoji_pattern.sub(r'', text)
|
112 |
+
|
113 |
+
def create_pdf_from_chat(chat_history):
|
114 |
+
html_template = """
|
115 |
+
<html>
|
116 |
+
<head>
|
117 |
+
<meta charset="UTF-8">
|
118 |
+
<style>
|
119 |
+
body { font-family: Arial, sans-serif; }
|
120 |
+
.message { margin: 10px 0; padding: 10px; border-radius: 10px; }
|
121 |
+
.user { background-color: #b0b4f5; text-align: right; }
|
122 |
+
.jimmy { background-color: #d9b0f5; }
|
123 |
+
</style>
|
124 |
+
</head>
|
125 |
+
<body>
|
126 |
+
<h1>Chat History - Generated on {{ timestamp }}</h1>
|
127 |
+
{% for message in chat_history %}
|
128 |
+
<div class="message {{ message.role }}">
|
129 |
+
<strong>{{ message.role|capitalize }}:</strong> {{ message.content }}
|
130 |
+
</div>
|
131 |
+
{% endfor %}
|
132 |
+
</body>
|
133 |
+
</html>
|
134 |
+
"""
|
135 |
+
template = Template(html_template)
|
136 |
+
|
137 |
+
# Remove emojis from chat history
|
138 |
+
emoji_free_chat_history = [
|
139 |
+
{"role": message["role"], "content": remove_emojis(message["content"])}
|
140 |
+
for message in chat_history
|
141 |
+
]
|
142 |
+
|
143 |
+
html_content = template.render(
|
144 |
+
chat_history=emoji_free_chat_history,
|
145 |
+
timestamp=datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
146 |
+
)
|
147 |
+
|
148 |
+
pdf_options = {
|
149 |
+
'page-size': 'A4',
|
150 |
+
'margin-top': '0.75in',
|
151 |
+
'margin-right': '0.75in',
|
152 |
+
'margin-bottom': '0.75in',
|
153 |
+
'margin-left': '0.75in',
|
154 |
+
'encoding': 'UTF-8',
|
155 |
+
}
|
156 |
+
|
157 |
+
pdf = pdfkit.from_string(html_content, False, options=pdf_options)
|
158 |
+
return BytesIO(pdf)
|
159 |
+
|
160 |
def get_response(user_input):
|
161 |
# Prepare the prompt
|
162 |
prompt = prepare_prompt(user_input, st.session_state.chat_history)
|
|
|
199 |
|
200 |
def main():
|
201 |
initialize_session_state()
|
202 |
+
st.subheader("Jimmy π§")
|
203 |
+
|
204 |
+
# col1, col2 = st.columns(2)
|
205 |
+
|
206 |
+
# with col1:
|
207 |
|
208 |
# User input
|
209 |
+
# with st.container(height=500,border=True):
|
210 |
if anton_ego_jimmy.DISPLAY_CHAT_HISTORY == 1:
|
211 |
+
for message in st.session_state.chat_history:
|
212 |
+
# with st.chat_message(message["role"]):
|
213 |
+
with st.chat_message(message["role"], avatar="π₯·π»" if message["role"] == "user" else "π§"):
|
214 |
+
st.markdown(message["content"])
|
215 |
user_input = st.chat_input("You:", key="user_input")
|
216 |
|
217 |
if user_input:
|
|
|
223 |
# Get response
|
224 |
get_response(user_input)
|
225 |
|
226 |
+
# if st.session_state.chat_history:
|
227 |
+
# with col2:
|
228 |
+
now = datetime.now()
|
229 |
+
date_str = now.strftime("%Y-%m-%d")
|
230 |
+
pdf_buffer = create_pdf_from_chat(st.session_state.chat_history)
|
231 |
+
st.download_button(
|
232 |
+
label="Download Chat History as PDF",
|
233 |
+
data=pdf_buffer,
|
234 |
+
file_name=f"chat_history_{date_str}.pdf",
|
235 |
+
mime="application/pdf"
|
236 |
+
)
|
237 |
+
|
238 |
if __name__ == "__main__":
|
239 |
main()
|