Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -9,7 +9,6 @@ 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
|
@@ -18,6 +17,13 @@ from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams
|
|
18 |
from ibm_watsonx_ai.metanames import GenTextReturnOptMetaNames as RetParams
|
19 |
from secretsload import load_stsecrets # script to load credentials from HuggingFace secrets section
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
credentials = load_stsecrets()
|
22 |
|
23 |
st.set_page_config(
|
@@ -55,7 +61,6 @@ def check_password():
|
|
55 |
if not check_password():
|
56 |
st.stop()
|
57 |
|
58 |
-
|
59 |
# Initialize session state
|
60 |
if 'current_page' not in st.session_state:
|
61 |
st.session_state.current_page = 0
|
@@ -111,51 +116,30 @@ def remove_emojis(text):
|
|
111 |
return emoji_pattern.sub(r'', text)
|
112 |
|
113 |
def create_pdf_from_chat(chat_history):
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
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 |
-
|
158 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
159 |
|
160 |
def get_response(user_input):
|
161 |
# Prepare the prompt
|
@@ -180,7 +164,6 @@ def get_response(user_input):
|
|
180 |
GenParams.DECODING_METHOD: anton_ego_jimmy.DECODING_METHOD,
|
181 |
GenParams.MAX_NEW_TOKENS: anton_ego_jimmy.MAX_NEW_TOKENS,
|
182 |
GenParams.MIN_NEW_TOKENS: anton_ego_jimmy.MIN_NEW_TOKENS,
|
183 |
-
# GenParams.LENGTH_PENALTY: anton_ego_jimmy.LENGTH_PENALTY,
|
184 |
GenParams.REPETITION_PENALTY: anton_ego_jimmy.REPETITION_PENALTY,
|
185 |
GenParams.STOP_SEQUENCES: anton_ego_jimmy.STOP_SEQUENCES
|
186 |
}
|
@@ -201,17 +184,10 @@ 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 |
-
|
212 |
-
|
213 |
-
|
214 |
-
st.markdown(message["content"])
|
215 |
user_input = st.chat_input("You:", key="user_input")
|
216 |
|
217 |
if user_input:
|
@@ -223,17 +199,20 @@ def main():
|
|
223 |
# Get response
|
224 |
get_response(user_input)
|
225 |
|
226 |
-
|
227 |
-
# with col2:
|
228 |
now = datetime.now()
|
229 |
date_str = now.strftime("%Y-%m-%d")
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
|
|
|
|
|
|
|
|
237 |
|
238 |
if __name__ == "__main__":
|
239 |
main()
|
|
|
9 |
import re
|
10 |
|
11 |
from datetime import datetime
|
|
|
12 |
from jinja2 import Template
|
13 |
|
14 |
from ibm_watsonx_ai.foundation_models import ModelInference
|
|
|
17 |
from ibm_watsonx_ai.metanames import GenTextReturnOptMetaNames as RetParams
|
18 |
from secretsload import load_stsecrets # script to load credentials from HuggingFace secrets section
|
19 |
|
20 |
+
# New imports for ReportLab
|
21 |
+
from reportlab.lib.pagesizes import letter
|
22 |
+
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
23 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
24 |
+
from reportlab.lib import colors
|
25 |
+
from reportlab.lib.enums import TA_LEFT, TA_RIGHT
|
26 |
+
|
27 |
credentials = load_stsecrets()
|
28 |
|
29 |
st.set_page_config(
|
|
|
61 |
if not check_password():
|
62 |
st.stop()
|
63 |
|
|
|
64 |
# Initialize session state
|
65 |
if 'current_page' not in st.session_state:
|
66 |
st.session_state.current_page = 0
|
|
|
116 |
return emoji_pattern.sub(r'', text)
|
117 |
|
118 |
def create_pdf_from_chat(chat_history):
|
119 |
+
buffer = BytesIO()
|
120 |
+
doc = SimpleDocTemplate(buffer, pagesize=letter, topMargin=30, bottomMargin=30)
|
121 |
+
styles = getSampleStyleSheet()
|
122 |
+
flowables = []
|
123 |
+
|
124 |
+
title_style = ParagraphStyle('Title', parent=styles['Heading1'], fontSize=18, spaceAfter=20)
|
125 |
+
flowables.append(Paragraph(f"Chat History - Generated on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", title_style))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
126 |
|
127 |
+
user_style = ParagraphStyle('UserStyle', parent=styles['Normal'],
|
128 |
+
backColor=colors.lightblue, borderPadding=10,
|
129 |
+
alignment=TA_RIGHT)
|
130 |
+
jimmy_style = ParagraphStyle('JimmyStyle', parent=styles['Normal'],
|
131 |
+
backColor=colors.lavender, borderPadding=10)
|
132 |
+
|
133 |
+
for message in chat_history:
|
134 |
+
role = message["role"]
|
135 |
+
content = remove_emojis(message["content"])
|
136 |
+
style = user_style if role == "user" else jimmy_style
|
137 |
+
flowables.append(Paragraph(f"<b>{role.capitalize()}:</b> {content}", style))
|
138 |
+
flowables.append(Spacer(1, 12))
|
139 |
+
|
140 |
+
doc.build(flowables)
|
141 |
+
buffer.seek(0)
|
142 |
+
return buffer
|
143 |
|
144 |
def get_response(user_input):
|
145 |
# Prepare the prompt
|
|
|
164 |
GenParams.DECODING_METHOD: anton_ego_jimmy.DECODING_METHOD,
|
165 |
GenParams.MAX_NEW_TOKENS: anton_ego_jimmy.MAX_NEW_TOKENS,
|
166 |
GenParams.MIN_NEW_TOKENS: anton_ego_jimmy.MIN_NEW_TOKENS,
|
|
|
167 |
GenParams.REPETITION_PENALTY: anton_ego_jimmy.REPETITION_PENALTY,
|
168 |
GenParams.STOP_SEQUENCES: anton_ego_jimmy.STOP_SEQUENCES
|
169 |
}
|
|
|
184 |
initialize_session_state()
|
185 |
st.subheader("Jimmy π§")
|
186 |
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
if anton_ego_jimmy.DISPLAY_CHAT_HISTORY == 1:
|
188 |
+
for message in st.session_state.chat_history:
|
189 |
+
with st.chat_message(message["role"], avatar="π₯·π»" if message["role"] == "user" else "π§"):
|
190 |
+
st.markdown(message["content"])
|
|
|
191 |
user_input = st.chat_input("You:", key="user_input")
|
192 |
|
193 |
if user_input:
|
|
|
199 |
# Get response
|
200 |
get_response(user_input)
|
201 |
|
202 |
+
if st.session_state.chat_history:
|
|
|
203 |
now = datetime.now()
|
204 |
date_str = now.strftime("%Y-%m-%d")
|
205 |
+
try:
|
206 |
+
pdf_buffer = create_pdf_from_chat(st.session_state.chat_history)
|
207 |
+
st.download_button(
|
208 |
+
label="Download Chat History as PDF",
|
209 |
+
data=pdf_buffer,
|
210 |
+
file_name=f"chat_history_{date_str}.pdf",
|
211 |
+
mime="application/pdf"
|
212 |
+
)
|
213 |
+
except Exception as e:
|
214 |
+
st.error(f"An error occurred while generating the PDF: {str(e)}")
|
215 |
+
st.error("If this persists, please contact support.")
|
216 |
|
217 |
if __name__ == "__main__":
|
218 |
main()
|