File size: 8,488 Bytes
1695c47
 
 
 
 
 
 
5b7fdba
1695c47
 
5b7fdba
 
 
1695c47
 
 
 
 
 
722ee9c
 
 
 
 
 
 
1695c47
 
 
2a856ec
 
5b7fdba
 
1695c47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a6db83f
1695c47
 
 
 
 
a6db83f
1695c47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b7fdba
 
 
 
 
 
722ee9c
 
 
 
 
 
 
5b7fdba
722ee9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b7fdba
1695c47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2a856ec
1695c47
 
 
 
2a856ec
1695c47
 
 
 
 
5b7fdba
 
1695c47
722ee9c
 
 
1695c47
 
 
 
 
 
 
 
 
 
 
722ee9c
5b7fdba
 
722ee9c
 
 
 
 
 
 
 
 
 
 
5b7fdba
1695c47
 
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import streamlit as st
from io import BytesIO
import ibm_watsonx_ai
import secretsload
import anton_ego_jimmy
import requests
import time
import regex
import re

from datetime import datetime
from jinja2 import Template

from ibm_watsonx_ai.foundation_models import ModelInference
from ibm_watsonx_ai import Credentials, APIClient
from ibm_watsonx_ai.metanames import GenTextParamsMetaNames as GenParams
from ibm_watsonx_ai.metanames import GenTextReturnOptMetaNames as RetParams
from secretsload import load_stsecrets # script to load credentials from HuggingFace secrets section

# New imports for ReportLab
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.lib import colors
from reportlab.lib.enums import TA_LEFT, TA_RIGHT

credentials = load_stsecrets()

st.set_page_config(
    page_title="Jimmy",
    page_icon="🧐",
    initial_sidebar_state="collapsed",
    layout="centered"
)

# Password protection
def check_password():
    def password_entered():
        if st.session_state["password"] == st.secrets["app_password"]:
            st.session_state["password_correct"] = True
            del st.session_state["password"]
        else:
            st.session_state["password_correct"] = False

    if "password_correct" not in st.session_state:
        st.markdown("\n\n")
        st.text_input("Enter the password", type="password", on_change=password_entered, key="password")
        st.divider()
        st.info("Designed and developed by Milan Mrdenovic Β© IBM Norway 2025")
        return False
    elif not st.session_state["password_correct"]:
        st.markdown("\n\n")
        st.text_input("Enter the password", type="password", on_change=password_entered, key="password")
        st.divider()
        st.info("Designed and developed by Milan Mrdenovic Β© IBM Norway 2025")
        st.error("πŸ˜• Password incorrect")
        return False
    else:
        return True

if not check_password():
    st.stop()

# Initialize session state
if 'current_page' not in st.session_state:
    st.session_state.current_page = 0

def initialize_session_state():
    if 'chat_history' not in st.session_state:
        st.session_state.chat_history = []

def setup_client():
    credentials = Credentials(
        url=st.secrets["url"],
        api_key=st.secrets["api_key"]
    )
    apo = st.secrets["api_key"]
    client = APIClient(credentials, project_id=st.secrets["project_id"])
    return client

client = setup_client()

def prepare_prompt(prompt, chat_history):
    if anton_ego_jimmy.TYPE == "chat" and chat_history:
        chats = "\n".join([f"{message['role']}: \"{message['content']}\"" for message in chat_history])
        return f"Conversation History:\n{chats}\n\nNew Message: {prompt}"
    return prompt

def apply_prompt_syntax(prompt, system_prompt, prompt_template, bake_in_prompt_syntax):
    model_family_syntax = {
        "llama3-instruct (llama-3 & 3.1) - system": """\n<|begin_of_text|><|start_header_id|>system<|end_header_id|>\n\n{system_prompt}<|eot_id|><|start_header_id|>user<|end_header_id|>\n\n{prompt}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n""",
        "llama3-instruct (llama-3 & 3.1) - user": """\n<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n{prompt}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n""",
        "granite-13b-chat & instruct - system": """\n<|system|>\n{system_prompt}\n<|user|>\n{prompt}\n<|assistant|>\n\n""",
        "granite-13b-chat & instruct - user": """\n<|user|>\n{prompt}\n<|assistant|>\n\n""",
        "llama2-chat - system": """\n[INST] <<SYS>>\n{system_prompt}\n<</SYS>>\n\n{prompt} [/INST]\n""",
        "llama2-chat - user": """\n[INST] {prompt} [/INST] """,
        "mistral & mixtral v2 tokenizer - system": """\n<s>[INST]System Prompt:[{system_prompt}]\n\n{prompt} [/INST]\n""",
        "mistral & mixtral v2 tokenizer - system segmented": """\n<s>[INST]System Prompt:{system_prompt}[/INST][INST]{prompt} [/INST]\n""",
        "mistral & mixtral v2 tokenizer - user": """\n<s>[INST]{prompt} [/INST]\n"""
    }
    
    if bake_in_prompt_syntax:
        template = model_family_syntax[prompt_template]
        if system_prompt:
            return template.format(system_prompt=system_prompt, prompt=prompt)
    return prompt

def generate_response(watsonx_llm, prompt_data, params):
    generated_response = watsonx_llm.generate_text_stream(prompt=prompt_data, params=params)
    for chunk in generated_response:
        yield chunk

emoji_pattern = regex.compile(r'\p{Emoji}', flags=regex.UNICODE)

def remove_emojis(text):
    return emoji_pattern.sub(r'', text)

def create_pdf_from_chat(chat_history):
    buffer = BytesIO()
    doc = SimpleDocTemplate(buffer, pagesize=letter, topMargin=30, bottomMargin=30)
    styles = getSampleStyleSheet()
    flowables = []

    title_style = ParagraphStyle('Title', parent=styles['Heading1'], fontSize=18, spaceAfter=20)
    flowables.append(Paragraph(f"Chat History - Generated on {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", title_style))
    
    user_style = ParagraphStyle('UserStyle', parent=styles['Normal'], 
                                backColor=colors.lightblue, borderPadding=10, 
                                alignment=TA_RIGHT)
    jimmy_style = ParagraphStyle('JimmyStyle', parent=styles['Normal'], 
                                 backColor=colors.lavender, borderPadding=10)

    for message in chat_history:
        role = message["role"]
        content = remove_emojis(message["content"])
        style = user_style if role == "user" else jimmy_style
        flowables.append(Paragraph(f"<b>{role.capitalize()}:</b> {content}", style))
        flowables.append(Spacer(1, 12))

    doc.build(flowables)
    buffer.seek(0)
    return buffer

def get_response(user_input):
    # Prepare the prompt
    prompt = prepare_prompt(user_input, st.session_state.chat_history)

    # Apply prompt syntax
    prompt_data = apply_prompt_syntax(
        prompt, 
        anton_ego_jimmy.SYSTEM_PROMPT,
        anton_ego_jimmy.PROMPT_TEMPLATE,
        anton_ego_jimmy.BAKE_IN_PROMPT_SYNTAX
    )

    watsonx_llm = ModelInference(
        api_client=client, 
        model_id=anton_ego_jimmy.SELECTED_MODEL,
        verify=anton_ego_jimmy.VERIFY
    )

    # Prepare parameters
    params = {
        GenParams.DECODING_METHOD: anton_ego_jimmy.DECODING_METHOD,
        GenParams.MAX_NEW_TOKENS: anton_ego_jimmy.MAX_NEW_TOKENS,
        GenParams.MIN_NEW_TOKENS: anton_ego_jimmy.MIN_NEW_TOKENS,
        GenParams.REPETITION_PENALTY: anton_ego_jimmy.REPETITION_PENALTY,
        GenParams.STOP_SEQUENCES: anton_ego_jimmy.STOP_SEQUENCES
    }

    print(prompt_data, params)

    # Generate and stream response
    with st.chat_message("Jimmy", avatar="🧐"):
        stream = generate_response(watsonx_llm, prompt_data, params)
        response = st.write_stream(stream)

    # Add AI response to chat history
    st.session_state.chat_history.append({"role": "Jimmy", "content": response})

    return response

def main():
    initialize_session_state()
    st.subheader("Jimmy 🧐")
    
    if anton_ego_jimmy.DISPLAY_CHAT_HISTORY == 1:
        for message in st.session_state.chat_history:
            with st.chat_message(message["role"], avatar="πŸ₯·πŸ»" if message["role"] == "user" else "🧐"):
                st.markdown(message["content"])
    user_input = st.chat_input("You:", key="user_input")

    if user_input:
        # Add user message to chat history
        st.session_state.chat_history.append({"role": "user", "content": user_input})
        with st.chat_message("user", avatar="πŸ₯·πŸ»"):
            st.markdown(user_input)

        # Get response
        get_response(user_input)

    if st.session_state.chat_history:
        now = datetime.now()
        date_str = now.strftime("%Y-%m-%d")
        try:
            pdf_buffer = create_pdf_from_chat(st.session_state.chat_history)
            st.download_button(
                label="Download Chat History as PDF",
                data=pdf_buffer,
                file_name=f"chat_history_{date_str}.pdf",
                mime="application/pdf"
            )
        except Exception as e:
            st.error(f"An error occurred while generating the PDF: {str(e)}")
            st.error("If this persists, please contact support.")

if __name__ == "__main__":
    main()