File size: 12,651 Bytes
b5434b8
 
 
2605d55
aa4f694
2605d55
 
ed77618
1d82a0b
c1acf68
1d82a0b
8953d62
1d82a0b
e9617d4
1d82a0b
b5434b8
2605d55
 
3e68ccf
ed77618
 
 
 
3e68ccf
57daf6a
3e68ccf
9fcc9ee
2605d55
 
 
 
 
 
3e68ccf
2605d55
 
aa4f694
 
f845b93
2605d55
f845b93
2605d55
f845b93
2605d55
 
f845b93
 
2605d55
 
 
 
 
 
 
 
f845b93
 
b5434b8
 
e611c5a
 
 
 
 
 
b5434b8
f845b93
 
e611c5a
 
 
 
 
 
 
 
ed77618
e611c5a
 
2605d55
f845b93
2605d55
 
 
 
b5434b8
f845b93
2605d55
f845b93
2605d55
 
 
 
 
 
 
 
f845b93
 
4d7e0d8
3e68ccf
4d7e0d8
3e68ccf
 
 
 
76508d0
 
 
3e68ccf
2605d55
4d7e0d8
e55d16a
2605d55
3e68ccf
 
2605d55
3e68ccf
2605d55
3e68ccf
 
2605d55
294b6dd
2605d55
4d7e0d8
 
 
2605d55
3e68ccf
4d7e0d8
 
 
 
 
 
 
2605d55
e55d16a
2605d55
 
 
3e68ccf
2605d55
3e68ccf
2605d55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57daf6a
 
 
 
 
 
2605d55
 
6e4a820
2605d55
 
 
 
 
 
 
 
 
 
 
 
 
e611c5a
 
2605d55
 
 
 
 
 
e611c5a
2605d55
 
 
b5434b8
 
2605d55
e9617d4
 
 
 
 
7bd7f55
 
 
e9617d4
 
 
 
 
 
 
 
815286a
e9617d4
 
 
8953d62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f809d41
b5434b8
 
f809d41
b5434b8
 
 
 
f809d41
b5434b8
 
 
 
815286a
f809d41
b5434b8
e611c5a
 
 
 
b5434b8
 
f809d41
b5434b8
c1acf68
 
 
b5434b8
 
 
 
 
abd7b16
2605d55
c1acf68
2605d55
b5434b8
 
3e68ccf
2605d55
c1acf68
2605d55
3e68ccf
c1acf68
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9617d4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c1acf68
2605d55
 
3e68ccf
2605d55
 
 
3e68ccf
1189403
 
 
2605d55
c1acf68
2605d55
c1acf68
e065b20
 
abd7b16
2605d55
 
 
b5434b8
 
3e68ccf
2605d55
 
 
 
 
 
 
c0b5a2b
2605d55
 
3e68ccf
2605d55
 
 
 
3e68ccf
2605d55
3e68ccf
 
2605d55
e55d16a
2605d55
3e68ccf
2605d55
e55d16a
33d58d5
2605d55
 
 
e55d16a
94d93d2
2605d55
 
 
e55d16a
2605d55
 
 
 
33d58d5
c6643c7
2605d55
 
 
 
 
 
33d58d5
2605d55
 
c6643c7
2605d55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e68ccf
 
 
ae312d0
 
 
 
3e68ccf
 
 
 
 
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
"""
Streamlit app containing the UI and the application logic.
"""
import datetime
import logging
import pathlib
import random
import sys
import tempfile
from typing import List, Union

import huggingface_hub
import json5
import requests
import streamlit as st
from langchain_community.chat_message_histories import StreamlitChatMessageHistory
from langchain_core.messages import HumanMessage
from langchain_core.prompts import ChatPromptTemplate

sys.path.append('..')
sys.path.append('../..')

import helpers.icons_embeddings as ice
from global_config import GlobalConfig
from helpers import llm_helper, pptx_helper, text_helper


@st.cache_data
def _load_strings() -> dict:
    """
    Load various strings to be displayed in the app.
    :return: The dictionary of strings.
    """

    with open(GlobalConfig.APP_STRINGS_FILE, 'r', encoding='utf-8') as in_file:
        return json5.loads(in_file.read())


@st.cache_data
def _get_prompt_template(is_refinement: bool) -> str:
    """
    Return a prompt template.

    :param is_refinement: Whether this is the initial or refinement prompt.
    :return: The prompt template as f-string.
    """

    if is_refinement:
        with open(GlobalConfig.REFINEMENT_PROMPT_TEMPLATE, 'r', encoding='utf-8') as in_file:
            template = in_file.read()
    else:
        with open(GlobalConfig.INITIAL_PROMPT_TEMPLATE, 'r', encoding='utf-8') as in_file:
            template = in_file.read()

    return template


@st.cache_resource
def _get_llm():
    """
    Get an LLM instance.

    :return: The LLM.
    """

    return llm_helper.get_hf_endpoint()


@st.cache_data
def _get_icons_list() -> List[str]:
    """
    Get a list of available icons names without the dir name and file extension.

    :return: A llist of the icons.
    """

    return ice.get_icons_list()


APP_TEXT = _load_strings()

# Session variables
CHAT_MESSAGES = 'chat_messages'
DOWNLOAD_FILE_KEY = 'download_file_name'
IS_IT_REFINEMENT = 'is_it_refinement'


logger = logging.getLogger(__name__)

texts = list(GlobalConfig.PPTX_TEMPLATE_FILES.keys())
captions = [GlobalConfig.PPTX_TEMPLATE_FILES[x]['caption'] for x in texts]
pptx_template = st.sidebar.radio(
    'Select a presentation template:',
    texts,
    captions=captions,
    horizontal=True
)


def build_ui():
    """
    Display the input elements for content generation.
    """

    st.title(APP_TEXT['app_name'])
    st.subheader(APP_TEXT['caption'])
    st.markdown(
        '![Visitors](https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fbarunsaha%2Fslide-deck-ai&countColor=%23263759)'  # noqa: E501
    )

    with st.expander('Usage Policies and Limitations'):
        st.text(APP_TEXT['tos'] + '\n\n' + APP_TEXT['tos2'])

    set_up_chat_ui()


def set_up_chat_ui():
    """
    Prepare the chat interface and related functionality.
    """

    with st.expander('Usage Instructions'):
        st.markdown(GlobalConfig.CHAT_USAGE_INSTRUCTIONS)
        st.markdown(
            '[SlideDeck AI](https://github.com/barun-saha/slide-deck-ai) is an Open-Source project.'  # noqa: E501
            ' It is is powered by'  # noqa: E501
            ' [Mistral-Nemo-Instruct-2407](https://huggingface.co/mistralai/Mistral-Nemo-Instruct-2407).'  # noqa: E501
        )

    st.info(
        'If you like SlideDeck AI, please consider leaving a heart ❤️ on the'
        ' [Hugging Face Space](https://huggingface.co/spaces/barunsaha/slide-deck-ai/) or'
        ' a star ⭐ on [GitHub](https://github.com/barun-saha/slide-deck-ai).'
        ' Your [feedback](https://forms.gle/JECFBGhjvSj7moBx9) is appreciated.'
    )

    # view_messages = st.expander('View the messages in the session state')

    st.chat_message('ai').write(
        random.choice(APP_TEXT['ai_greetings'])
    )

    history = StreamlitChatMessageHistory(key=CHAT_MESSAGES)

    if _is_it_refinement():
        template = _get_prompt_template(is_refinement=True)
    else:
        template = _get_prompt_template(is_refinement=False)

    prompt_template = ChatPromptTemplate.from_template(template)

    # Since Streamlit app reloads at every interaction, display the chat history
    # from the save session state
    for msg in history.messages:
        msg_type = msg.type
        if msg_type == 'user':
            st.chat_message(msg_type).write(msg.content)
        else:
            st.chat_message(msg_type).code(msg.content, language='json')

    if prompt := st.chat_input(
        placeholder=APP_TEXT['chat_placeholder'],
        max_chars=GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH
    ):
        if not text_helper.is_valid_prompt(prompt):
            st.error(
                'Not enough information provided!'
                ' Please be a little more descriptive and type a few words'
                ' with a few characters :)'
            )
            return

        logger.info('User input: %s | #characters: %d', prompt, len(prompt))
        st.chat_message('user').write(prompt)

        user_messages = _get_user_messages()
        user_messages.append(prompt)
        list_of_msgs = [
            f'{idx + 1}. {msg}' for idx, msg in enumerate(user_messages)
        ]
        list_of_msgs = '\n'.join(list_of_msgs)

        if _is_it_refinement():
            formatted_template = prompt_template.format(
                **{
                    'instructions': list_of_msgs,
                    'previous_content': _get_last_response(),
                    'icons_list': '\n'.join(_get_icons_list())
                }
            )
        else:
            formatted_template = prompt_template.format(
                **{
                    'question': prompt,
                    'icons_list': '\n'.join(_get_icons_list())
                }
            )

        progress_bar = st.progress(0, 'Preparing to call LLM...')
        response = ''

        try:
            for chunk in _get_llm().stream(formatted_template):
                response += chunk

                # Update the progress bar
                progress_percentage = min(
                    len(response) / GlobalConfig.LLM_MODEL_MAX_OUTPUT_LENGTH, 0.95
                )
                progress_bar.progress(
                    progress_percentage,
                    text='Streaming content...this might take a while...'
                )
        except requests.exceptions.ConnectionError:
            msg = (
                'A connection error occurred while streaming content from the LLM endpoint.'
                ' Unfortunately, the slide deck cannot be generated. Please try again later.'
            )
            logger.error(msg)
            st.error(msg)
            return
        except huggingface_hub.errors.ValidationError as ve:
            msg = (
                f'An error occurred while trying to generate the content: {ve}'
                '\nPlease try again with a significantly shorter input text.'
            )
            logger.error(msg)
            st.error(msg)
            return
        except Exception as ex:
            msg = (
                f'An unexpected error occurred while generating the content: {ex}'
                '\nPlease try again later, possibly with different inputs.'
            )
            logger.error(msg)
            st.error(msg)
            return

        history.add_user_message(prompt)
        history.add_ai_message(response)

        # The content has been generated as JSON
        # There maybe trailing ``` at the end of the response -- remove them
        # To be careful: ``` may be part of the content as well when code is generated
        response_cleaned = text_helper.get_clean_json(response)

        logger.info(
            'Cleaned JSON response:: original length: %d | cleaned length: %d',
            len(response), len(response_cleaned)
        )
        # logger.debug('Cleaned JSON: %s', response_cleaned)

        # Now create the PPT file
        progress_bar.progress(
            GlobalConfig.LLM_PROGRESS_MAX,
            text='Finding photos online and generating the slide deck...'
        )
        path = generate_slide_deck(response_cleaned)
        progress_bar.progress(1.0, text='Done!')

        st.chat_message('ai').code(response, language='json')

        if path:
            _display_download_button(path)

        logger.info(
            '#messages in history / 2: %d',
            len(st.session_state[CHAT_MESSAGES]) / 2
        )


def generate_slide_deck(json_str: str) -> Union[pathlib.Path, None]:
    """
    Create a slide deck and return the file path. In case there is any error creating the slide
    deck, the path may be to an empty file.

    :param json_str: The content in *valid* JSON format.
    :return: The path to the .pptx file or `None` in case of error.
    """

    try:
        parsed_data = json5.loads(json_str)
    except ValueError:
        st.error(
            'Encountered error while parsing JSON...will fix it and retry'
        )
        logger.error(
            'Caught ValueError: trying again after repairing JSON...'
        )
        try:
            parsed_data = json5.loads(text_helper.fix_malformed_json(json_str))
        except ValueError:
            st.error(
                'Encountered an error again while fixing JSON...'
                'the slide deck cannot be created, unfortunately ☹'
                '\nPlease try again later.'
            )
            logger.error(
                'Caught ValueError: failed to repair JSON!'
            )

            return None
    except RecursionError:
        st.error(
            'Encountered an error while parsing JSON...'
            'the slide deck cannot be created, unfortunately ☹'
            '\nPlease try again later.'
        )
        logger.error('Caught RecursionError while parsing JSON. Cannot generate the slide deck!')

        return None
    except Exception:
        st.error(
            'Encountered an error while parsing JSON...'
            'the slide deck cannot be created, unfortunately ☹'
            '\nPlease try again later.'
        )
        logger.error(
            'Caught ValueError: failed to parse JSON!'
        )

        return None

    if DOWNLOAD_FILE_KEY in st.session_state:
        path = pathlib.Path(st.session_state[DOWNLOAD_FILE_KEY])
    else:
        temp = tempfile.NamedTemporaryFile(delete=False, suffix='.pptx')
        path = pathlib.Path(temp.name)
        st.session_state[DOWNLOAD_FILE_KEY] = str(path)

        if temp:
            temp.close()

    try:
        logger.debug('Creating PPTX file: %s...', st.session_state[DOWNLOAD_FILE_KEY])
        pptx_helper.generate_powerpoint_presentation(
            parsed_data,
            slides_template=pptx_template,
            output_file_path=path
        )
    except Exception as ex:
        st.error(APP_TEXT['content_generation_error'])
        logger.error('Caught a generic exception: %s', str(ex))

    return path


def _is_it_refinement() -> bool:
    """
    Whether it is the initial prompt or a refinement.

    :return: True if it is the initial prompt; False otherwise.
    """

    if IS_IT_REFINEMENT in st.session_state:
        return True

    if len(st.session_state[CHAT_MESSAGES]) >= 2:
        # Prepare for the next call
        st.session_state[IS_IT_REFINEMENT] = True
        return True

    return False


def _get_user_messages() -> List[str]:
    """
    Get a list of user messages submitted until now from the session state.

    :return: The list of user messages.
    """

    return [
        msg.content for msg in st.session_state[CHAT_MESSAGES] if isinstance(msg, HumanMessage)
    ]


def _get_last_response() -> str:
    """
    Get the last response generated by AI.

    :return: The response text.
    """

    return st.session_state[CHAT_MESSAGES][-1].content


def _display_messages_history(view_messages: st.expander):
    """
    Display the history of messages.

    :param view_messages: The list of AI and Human messages.
    """

    with view_messages:
        view_messages.json(st.session_state[CHAT_MESSAGES])


def _display_download_button(file_path: pathlib.Path):
    """
    Display a download button to download a slide deck.

    :param file_path: The path of the .pptx file.
    """

    with open(file_path, 'rb') as download_file:
        st.download_button(
            'Download PPTX file ⬇️',
            data=download_file,
            file_name='Presentation.pptx',
            key=datetime.datetime.now()
        )


def main():
    """
    Trigger application run.
    """

    build_ui()


if __name__ == '__main__':
    main()