barunsaha commited on
Commit
1c7f397
2 Parent(s): 437a6f6 76508d0

Merge pull request #7 from barun-saha/chat-interface

Browse files
.streamlit/config.toml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ [server]
2
+ runOnSave = true
3
+ headless = false
4
+ maxUploadSize = 0
5
+
6
+ [browser]
7
+ gatherUsageStats = false
8
+
9
+ [theme]
10
+ base = "dark"
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🏢
4
  colorFrom: yellow
5
  colorTo: green
6
  sdk: streamlit
7
- sdk_version: 1.26.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
@@ -16,36 +16,57 @@ We spend a lot of time on creating the slides and organizing our thoughts for an
16
  With SlideDeck AI, co-create slide decks on any topic with Generative Artificial Intelligence.
17
  Describe your topic and let SlideDeck AI generate a PowerPoint slide deck for you—it's as simple as that!
18
 
19
- SlideDeck AI is powered by [Mistral 7B Instruct](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1).
20
  Originally, it was built using the Llama 2 API provided by Clarifai.
21
 
 
 
 
 
 
22
  # Process
23
 
24
  SlideDeck AI works in the following way:
25
 
26
- 1. Given a topic description, it uses Mistral 7B Instruct to generate the outline/contents of the slides.
27
  The output is generated as structured JSON data based on a pre-defined schema.
28
  2. Subsequently, it uses the `python-pptx` library to generate the slides,
29
  based on the JSON data from the previous step.
30
- Here, a user can choose from a set of three pre-defined presentation templates.
31
- 3. In addition, it uses Metaphor to fetch Web pages related to the topic.
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
- 4. ~~Finally, it uses Stable Diffusion 2 to generate an image, based on the title and each slide heading.~~
 
 
34
 
35
 
36
  # Local Development
37
 
38
- SlideDeck AI uses [Mistral 7B Instruct](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.1)
39
  via the Hugging Face Inference API.
40
- To run this project by yourself, you need to provide the `HUGGINGFACEHUB_API_TOKEN` and `METAPHOR_API_KEY` API keys,
41
  for example, in a `.env` file. Visit the respective websites to obtain the keys.
42
 
43
 
44
  # Live Demo
45
 
46
- [SlideDeck AI](https://huggingface.co/spaces/barunsaha/slide-deck-ai)
 
47
 
48
 
49
  # Award
50
 
51
- SlideDeck AI has won the 3rd Place in the [Llama 2 Hackathon with Clarifai](https://lablab.ai/event/llama-2-hackathon-with-clarifai).
 
4
  colorFrom: yellow
5
  colorTo: green
6
  sdk: streamlit
7
+ sdk_version: 1.32.2
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
16
  With SlideDeck AI, co-create slide decks on any topic with Generative Artificial Intelligence.
17
  Describe your topic and let SlideDeck AI generate a PowerPoint slide deck for you—it's as simple as that!
18
 
19
+ SlideDeck AI is powered by [Mistral 7B Instruct](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2).
20
  Originally, it was built using the Llama 2 API provided by Clarifai.
21
 
22
+ *Update*: Legacy SlideDeck AI allowed one-shot generation of a slide deck based on the inputs.
23
+ In contrast, SlideDeck AI *Reloaded* enables an iterative workflow with a conversational interface,
24
+ where you can create and improve the presentation.
25
+
26
+
27
  # Process
28
 
29
  SlideDeck AI works in the following way:
30
 
31
+ 1. Given a topic description, it uses Mistral 7B Instruct to generate the *initial* content of the slides.
32
  The output is generated as structured JSON data based on a pre-defined schema.
33
  2. Subsequently, it uses the `python-pptx` library to generate the slides,
34
  based on the JSON data from the previous step.
35
+ A user can choose from a set of three pre-defined presentation templates.
36
+ 3. At this stage onward, a user can provide additional instructions to refine the content.
37
+ For example, one can ask to add another slide or modify an existing slide.
38
+ A history of instructions is maintained.
39
+ 4. Every time SlideDeck AI generates a PowerPoint presentation, a download button is provided.
40
+ Clicking on the button will download the file.
41
+
42
+
43
+ # Known Issues
44
+
45
+ - **Incorrect JSON**: Sometimes the JSON generated could be syntactically incorrect.
46
+ You can try asking SlideDeck AI to regenerate the content and fix the JSON syntax error.
47
+ This, however, is not guaranteed to work. The alternative is to reload the website and try again.
48
+ - **Connection timeout**: Requests sent to the Hugging Face Inference endpoint might time out.
49
+ A maximum of five retries are attempted. If it still does not work, wait for a while and try again.
50
 
51
+ The following is not an issue but might appear as a strange behavior:
52
+ - **Cannot paste text in the input box**: If the length of the copied text is greater than the maximum
53
+ number of allowed characters in the textbox, pasting would not work.
54
 
55
 
56
  # Local Development
57
 
58
+ SlideDeck AI uses [Mistral 7B Instruct](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2)
59
  via the Hugging Face Inference API.
60
+ To run this project by yourself, you need to provide the `HUGGINGFACEHUB_API_TOKEN` API key,
61
  for example, in a `.env` file. Visit the respective websites to obtain the keys.
62
 
63
 
64
  # Live Demo
65
 
66
+ - [SlideDeck AI](https://huggingface.co/spaces/barunsaha/slide-deck-ai) on Hugging Face Spaces
67
+ - [Demo video](https://youtu.be/QvAKzNKtk9k) of the chat interface on YouTube
68
 
69
 
70
  # Award
71
 
72
+ SlideDeck AI has won the 3rd Place in the [Llama 2 Hackathon with Clarifai](https://lablab.ai/event/llama-2-hackathon-with-clarifai) in 2023.
app.py CHANGED
@@ -1,309 +1,366 @@
1
- import pathlib
2
  import logging
 
 
3
  import tempfile
4
- from typing import List, Tuple
5
 
6
  import json5
7
- import metaphor_python as metaphor
8
  import streamlit as st
 
 
 
 
 
 
9
 
10
- import llm_helper
11
- import pptx_helper
12
  from global_config import GlobalConfig
13
-
14
-
15
- APP_TEXT = json5.loads(open(GlobalConfig.APP_STRINGS_FILE, 'r', encoding='utf-8').read())
16
- GB_CONVERTER = 2 ** 30
17
-
18
-
19
- logging.basicConfig(
20
- level=GlobalConfig.LOG_LEVEL,
21
- format='%(asctime)s - %(message)s',
22
- )
23
 
24
 
25
  @st.cache_data
26
- def get_contents_wrapper(text: str) -> str:
27
  """
28
- Fetch and cache the slide deck contents on a topic by calling an external API.
29
-
30
- :param text: The presentation topic
31
- :return: The slide deck contents or outline in JSON format
32
  """
33
 
34
- logging.info('LLM call because of cache miss...')
35
- return llm_helper.generate_slides_content(text).strip()
36
-
37
-
38
- @st.cache_resource
39
- def get_metaphor_client_wrapper() -> metaphor.Metaphor:
40
- """
41
- Create a Metaphor client for semantic Web search.
42
-
43
- :return: Metaphor instance
44
- """
45
-
46
- return metaphor.Metaphor(api_key=GlobalConfig.METAPHOR_API_KEY)
47
 
48
 
49
  @st.cache_data
50
- def get_web_search_results_wrapper(text: str) -> List[Tuple[str, str]]:
51
  """
52
- Fetch and cache the Web search results on a given topic.
53
 
54
- :param text: The topic
55
- :return: A list of (title, link) tuples
56
  """
57
 
58
- results = []
59
- search_results = get_metaphor_client_wrapper().search(
60
- text,
61
- use_autoprompt=True,
62
- num_results=5
63
- )
64
-
65
- for a_result in search_results.results:
66
- results.append((a_result.title, a_result.url))
67
 
68
- return results
69
 
70
 
71
- # def get_disk_used_percentage() -> float:
 
72
  # """
73
- # Compute the disk usage.
74
  #
75
- # :return: Percentage of the disk space currently used
76
  # """
77
  #
78
- # total, used, free = shutil.disk_usage(__file__)
79
- # total = total // GB_CONVERTER
80
- # used = used // GB_CONVERTER
81
- # free = free // GB_CONVERTER
82
- # used_perc = 100.0 * used / total
83
- #
84
- # logging.debug(f'Total: {total} GB\n'
85
- # f'Used: {used} GB\n'
86
- # f'Free: {free} GB')
87
- #
88
- # logging.debug('\n'.join(os.listdir()))
89
- #
90
- # return used_perc
91
 
92
 
93
- def build_ui():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  """
95
- Display the input elements for content generation. Only covers the first step.
96
  """
97
 
98
- # get_disk_used_percentage()
99
-
100
  st.title(APP_TEXT['app_name'])
101
  st.subheader(APP_TEXT['caption'])
102
  st.markdown(
103
- 'Powered by'
104
- ' [Mistral-7B-Instruct-v0.2](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2).'
105
- )
106
- st.markdown(
107
- '*If the JSON is generated or parsed incorrectly, try again later by making minor changes'
108
- ' to the input text.*'
109
  )
110
 
111
- with st.form('my_form'):
112
- # Topic input
113
- try:
114
- with open(GlobalConfig.PRELOAD_DATA_FILE, 'r', encoding='utf-8') as in_file:
115
- preload_data = json5.loads(in_file.read())
116
- except (FileExistsError, FileNotFoundError):
117
- preload_data = {'topic': '', 'audience': ''}
118
-
119
- topic = st.text_area(
120
- APP_TEXT['input_labels'][0],
121
- value=preload_data['topic']
122
- )
123
 
124
- texts = list(GlobalConfig.PPTX_TEMPLATE_FILES.keys())
125
- captions = [GlobalConfig.PPTX_TEMPLATE_FILES[x]['caption'] for x in texts]
126
-
127
- pptx_template = st.radio(
128
- 'Select a presentation template:',
129
- texts,
130
- captions=captions,
131
- horizontal=True
132
- )
133
 
134
- st.divider()
135
- submit = st.form_submit_button('Generate slide deck')
136
 
137
- if submit:
138
- # st.write(f'Clicked {time.time()}')
139
- st.session_state.submitted = True
140
 
141
- # https://github.com/streamlit/streamlit/issues/3832#issuecomment-1138994421
142
- if 'submitted' in st.session_state:
143
- progress_text = 'Generating the slides...give it a moment'
144
- progress_bar = st.progress(0, text=progress_text)
145
 
146
- topic_txt = topic.strip()
147
- generate_presentation(topic_txt, pptx_template, progress_bar)
148
 
149
- st.divider()
150
- st.text(APP_TEXT['tos'])
151
- st.text(APP_TEXT['tos2'])
152
 
153
- st.markdown(
154
- '![Visitors]'
155
- '(https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fbarunsaha%2Fslide-deck-ai&countColor=%23263759)'
156
- )
157
 
158
 
159
- def generate_presentation(topic: str, pptx_template: str, progress_bar):
160
  """
161
- Process the inputs to generate the slides.
162
-
163
- :param topic: The presentation topic based on which contents are to be generated
164
- :param pptx_template: The PowerPoint template name to be used
165
- :param progress_bar: Progress bar from the page
166
- :return:
167
  """
168
 
169
- topic_length = len(topic)
170
- logging.debug('Input length:: topic: %s', topic_length)
171
-
172
- if topic_length >= 10:
173
- logging.debug('Topic: %s', topic)
174
- target_length = min(topic_length, GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH)
175
-
176
- try:
177
- # Step 1: Generate the contents in JSON format using an LLM
178
- json_str = process_slides_contents(topic[:target_length], progress_bar)
179
- logging.debug('Truncated topic: %s', topic[:target_length])
180
- logging.debug('Length of JSON: %d', len(json_str))
181
-
182
- # Step 2: Generate the slide deck based on the template specified
183
- if len(json_str) > 0:
184
- st.info(
185
- 'Tip: The generated content doesn\'t look so great?'
186
- ' Need alternatives? Just change your description text and try again.',
187
- icon="💡️"
188
- )
189
- else:
190
- st.error(
191
- 'Unfortunately, JSON generation failed, so the next steps would lead'
192
- ' to nowhere. Try again or come back later.'
193
- )
194
- return
195
-
196
- all_headers = generate_slide_deck(json_str, pptx_template, progress_bar)
197
-
198
- # Step 3: Bonus stuff: Web references and AI art
199
- show_bonus_stuff(all_headers)
200
-
201
- except ValueError as ve:
202
- st.error(f'Unfortunately, an error occurred: {ve}! '
203
- f'Please change the text, try again later, or report it, sharing your inputs.')
204
 
 
 
205
  else:
206
- st.error('Not enough information provided! Please be little more descriptive :)')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
 
 
 
 
 
208
 
209
- def process_slides_contents(text: str, progress_bar: st.progress) -> str:
 
210
  """
211
- Convert given text into structured data and display. Update the UI.
212
 
213
- :param text: The topic description for the presentation
214
- :param progress_bar: Progress bar for this step
215
- :return: The contents as a JSON-formatted string
216
  """
217
 
218
- json_str = ''
 
 
 
 
 
 
 
 
 
 
219
 
220
  try:
221
- logging.info('Calling LLM for content generation on the topic: %s', text)
222
- json_str = get_contents_wrapper(text)
223
- except Exception as ex:
 
 
 
 
 
224
  st.error(
225
- f'An exception occurred while trying to convert to JSON. It could be because of heavy'
226
- f' traffic or something else. Try doing it again or try again later.'
227
- f'\nError message: {ex}'
 
 
 
 
228
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
229
 
230
- progress_bar.progress(50, text='Contents generated')
 
231
 
232
- with st.expander('The generated contents (in JSON format)'):
233
- st.code(json_str, language='json')
 
 
234
 
235
- return json_str
236
 
237
 
238
- def generate_slide_deck(json_str: str, pptx_template: str, progress_bar) -> List:
239
  """
240
- Create a slide deck.
241
 
242
- :param json_str: The contents in JSON format
243
- :param pptx_template: The PPTX template name
244
- :param progress_bar: Progress bar
245
- :return: A list of all slide headers and the title
246
  """
247
 
248
- progress_text = 'Creating the slide deck...give it a moment'
249
- progress_bar.progress(75, text=progress_text)
 
250
 
251
- # # Get a unique name for the file to save -- use the session ID
252
- # ctx = st_sr.get_script_run_ctx()
253
- # session_id = ctx.session_id
254
- # timestamp = time.time()
255
- # output_file_name = f'{session_id}_{timestamp}.pptx'
256
 
257
- temp = tempfile.NamedTemporaryFile(delete=False, suffix='.pptx')
258
- path = pathlib.Path(temp.name)
 
259
 
260
- logging.info('Creating PPTX file...')
261
- all_headers = pptx_helper.generate_powerpoint_presentation(
262
- json_str,
263
- slides_template=pptx_template,
264
- output_file_path=path
265
- )
266
- progress_bar.progress(100, text='Done!')
267
 
268
- with open(path, 'rb') as f:
269
- st.download_button('Download PPTX file', f, file_name='Presentation.pptx')
270
 
271
- return all_headers
272
 
 
 
 
273
 
274
- def show_bonus_stuff(ppt_headers: List[str]):
275
  """
276
- Show bonus stuff for the presentation.
277
 
278
- :param ppt_headers: A list of the slide headings.
 
 
 
 
 
 
 
 
279
  """
280
 
281
- # Use the presentation title and the slide headers to find relevant info online
282
- logging.info('Calling Metaphor search...')
283
- ppt_text = ' '.join(ppt_headers)
284
- search_results = get_web_search_results_wrapper(ppt_text)
285
- md_text_items = []
286
-
287
- for (title, link) in search_results:
288
- md_text_items.append(f'[{title}]({link})')
289
-
290
- with st.expander('Related Web references'):
291
- st.markdown('\n\n'.join(md_text_items))
292
-
293
- logging.info('Done!')
294
-
295
- # # Avoid image generation. It costs time and an API call, so just limit to the text generation.
296
- # with st.expander('AI-generated image on the presentation topic'):
297
- # logging.info('Calling SDXL for image generation...')
298
- # # img_empty.write('')
299
- # # img_text.write(APP_TEXT['image_info'])
300
- # image = get_ai_image_wrapper(ppt_text)
301
- #
302
- # if len(image) > 0:
303
- # image = base64.b64decode(image)
304
- # st.image(image, caption=ppt_text)
305
- # st.info('Tip: Right-click on the image to save it.', icon="💡️")
306
- # logging.info('Image added')
307
 
308
 
309
  def main():
 
1
+ import datetime
2
  import logging
3
+ import pathlib
4
+ import random
5
  import tempfile
6
+ from typing import List
7
 
8
  import json5
 
9
  import streamlit as st
10
+ from langchain_community.chat_message_histories import (
11
+ StreamlitChatMessageHistory
12
+ )
13
+ from langchain_core.messages import HumanMessage
14
+ from langchain_core.prompts import ChatPromptTemplate
15
+ # from transformers import AutoTokenizer
16
 
 
 
17
  from global_config import GlobalConfig
18
+ from helpers import llm_helper, pptx_helper, text_helper
 
 
 
 
 
 
 
 
 
19
 
20
 
21
  @st.cache_data
22
+ def _load_strings() -> dict:
23
  """
24
+ Load various strings to be displayed in the app.
25
+ :return: The dictionary of strings.
 
 
26
  """
27
 
28
+ with open(GlobalConfig.APP_STRINGS_FILE, 'r', encoding='utf-8') as in_file:
29
+ return json5.loads(in_file.read())
 
 
 
 
 
 
 
 
 
 
 
30
 
31
 
32
  @st.cache_data
33
+ def _get_prompt_template(is_refinement: bool) -> str:
34
  """
35
+ Return a prompt template.
36
 
37
+ :param is_refinement: Whether this is the initial or refinement prompt.
38
+ :return: The prompt template as f-string.
39
  """
40
 
41
+ if is_refinement:
42
+ with open(GlobalConfig.REFINEMENT_PROMPT_TEMPLATE, 'r', encoding='utf-8') as in_file:
43
+ template = in_file.read()
44
+ else:
45
+ with open(GlobalConfig.INITIAL_PROMPT_TEMPLATE, 'r', encoding='utf-8') as in_file:
46
+ template = in_file.read()
 
 
 
47
 
48
+ return template
49
 
50
 
51
+ # @st.cache_resource
52
+ # def _get_tokenizer() -> AutoTokenizer:
53
  # """
54
+ # Get Mistral tokenizer for counting tokens.
55
  #
56
+ # :return: The tokenizer.
57
  # """
58
  #
59
+ # return AutoTokenizer.from_pretrained(
60
+ # pretrained_model_name_or_path=GlobalConfig.HF_LLM_MODEL_NAME
61
+ # )
 
 
 
 
 
 
 
 
 
 
62
 
63
 
64
+ APP_TEXT = _load_strings()
65
+
66
+ # Session variables
67
+ CHAT_MESSAGES = 'chat_messages'
68
+ DOWNLOAD_FILE_KEY = 'download_file_name'
69
+ IS_IT_REFINEMENT = 'is_it_refinement'
70
+
71
+ logger = logging.getLogger(__name__)
72
+ progress_bar = st.progress(0, text='Setting up SlideDeck AI...')
73
+
74
+ texts = list(GlobalConfig.PPTX_TEMPLATE_FILES.keys())
75
+ captions = [GlobalConfig.PPTX_TEMPLATE_FILES[x]['caption'] for x in texts]
76
+ pptx_template = st.sidebar.radio(
77
+ 'Select a presentation template:',
78
+ texts,
79
+ captions=captions,
80
+ horizontal=True
81
+ )
82
+
83
+
84
+ def display_page_header_content():
85
  """
86
+ Display content in the page header.
87
  """
88
 
 
 
89
  st.title(APP_TEXT['app_name'])
90
  st.subheader(APP_TEXT['caption'])
91
  st.markdown(
92
+ '![Visitors](https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fbarunsaha%2Fslide-deck-ai&countColor=%23263759)' # noqa: E501
 
 
 
 
 
93
  )
94
 
 
 
 
 
 
 
 
 
 
 
 
 
95
 
96
+ def display_page_footer_content():
97
+ """
98
+ Display content in the page footer.
99
+ """
 
 
 
 
 
100
 
101
+ st.text(APP_TEXT['tos'] + '\n\n' + APP_TEXT['tos2'])
 
102
 
 
 
 
103
 
104
+ def build_ui():
105
+ """
106
+ Display the input elements for content generation.
107
+ """
108
 
109
+ display_page_header_content()
 
110
 
111
+ with st.expander('Usage Policies and Limitations'):
112
+ display_page_footer_content()
 
113
 
114
+ progress_bar.progress(50, text='Setting up chat interface...')
115
+ set_up_chat_ui()
 
 
116
 
117
 
118
+ def set_up_chat_ui():
119
  """
120
+ Prepare the chat interface and related functionality.
 
 
 
 
 
121
  """
122
 
123
+ with st.expander('Usage Instructions'):
124
+ st.write(GlobalConfig.CHAT_USAGE_INSTRUCTIONS)
125
+ st.markdown(
126
+ 'SlideDeck AI is powered by'
127
+ ' [Mistral-7B-Instruct-v0.2](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2)'
128
+ )
129
+
130
+ # view_messages = st.expander('View the messages in the session state')
131
+
132
+ st.chat_message('ai').write(
133
+ random.choice(APP_TEXT['ai_greetings'])
134
+ )
135
+ progress_bar.progress(100, text='Done!')
136
+ progress_bar.empty()
137
+
138
+ history = StreamlitChatMessageHistory(key=CHAT_MESSAGES)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
 
140
+ if _is_it_refinement():
141
+ template = _get_prompt_template(is_refinement=True)
142
  else:
143
+ template = _get_prompt_template(is_refinement=False)
144
+
145
+ prompt_template = ChatPromptTemplate.from_template(template)
146
+
147
+ # Since Streamlit app reloads at every interaction, display the chat history
148
+ # from the save session state
149
+ for msg in history.messages:
150
+ msg_type = msg.type
151
+ if msg_type == 'user':
152
+ st.chat_message(msg_type).write(msg.content)
153
+ else:
154
+ st.chat_message(msg_type).code(msg.content, language='json')
155
+
156
+ if prompt := st.chat_input(
157
+ placeholder=APP_TEXT['chat_placeholder'],
158
+ max_chars=GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH
159
+ ):
160
+
161
+ progress_bar_pptx = st.progress(0, 'Preparing to run...')
162
+ if not text_helper.is_valid_prompt(prompt):
163
+ st.error(
164
+ 'Not enough information provided!'
165
+ ' Please be a little more descriptive and type a few words'
166
+ ' with a few characters :)'
167
+ )
168
+ return
169
+
170
+ logger.info('User input: %s | #characters: %d', prompt, len(prompt))
171
+ st.chat_message('user').write(prompt)
172
+
173
+ user_messages = _get_user_messages()
174
+ user_messages.append(prompt)
175
+ list_of_msgs = [
176
+ f'{idx + 1}. {msg}' for idx, msg in enumerate(user_messages)
177
+ ]
178
+ list_of_msgs = '\n'.join(list_of_msgs)
179
+
180
+ if _is_it_refinement():
181
+ formatted_template = prompt_template.format(
182
+ **{
183
+ 'instructions': list_of_msgs,
184
+ 'previous_content': _get_last_response()
185
+ }
186
+ )
187
+ else:
188
+ formatted_template = prompt_template.format(
189
+ **{
190
+ 'question': prompt,
191
+ }
192
+ )
193
+
194
+ progress_bar_pptx.progress(5, 'Calling LLM...will retry if connection times out...')
195
+ response: dict = llm_helper.hf_api_query({
196
+ 'inputs': formatted_template,
197
+ 'parameters': {
198
+ 'temperature': GlobalConfig.LLM_MODEL_TEMPERATURE,
199
+ 'min_length': GlobalConfig.LLM_MODEL_MIN_OUTPUT_LENGTH,
200
+ 'max_length': GlobalConfig.LLM_MODEL_MAX_OUTPUT_LENGTH,
201
+ 'max_new_tokens': GlobalConfig.LLM_MODEL_MAX_OUTPUT_LENGTH,
202
+ 'num_return_sequences': 1,
203
+ 'return_full_text': False,
204
+ # "repetition_penalty": 0.0001
205
+ },
206
+ 'options': {
207
+ 'wait_for_model': True,
208
+
209
+ 'use_cache': True
210
+ }
211
+ })
212
+
213
+ if len(response) > 0 and 'generated_text' in response[0]:
214
+ response: str = response[0]['generated_text'].strip()
215
+
216
+ st.chat_message('ai').code(response, language='json')
217
+
218
+ history.add_user_message(prompt)
219
+ history.add_ai_message(response)
220
+
221
+ # if GlobalConfig.COUNT_TOKENS:
222
+ # tokenizer = _get_tokenizer()
223
+ # tokens_count_in = len(tokenizer.tokenize(formatted_template))
224
+ # tokens_count_out = len(tokenizer.tokenize(response))
225
+ # logger.debug(
226
+ # 'Tokens count:: input: %d, output: %d',
227
+ # tokens_count_in, tokens_count_out
228
+ # )
229
+
230
+ # _display_messages_history(view_messages)
231
+
232
+ # The content has been generated as JSON
233
+ # There maybe trailing ``` at the end of the response -- remove them
234
+ # To be careful: ``` may be part of the content as well when code is generated
235
+ progress_bar_pptx.progress(50, 'Analyzing response...')
236
+ response_cleaned = text_helper.get_clean_json(response)
237
+
238
+ logger.info(
239
+ 'Cleaned JSON response:: original length: %d | cleaned length: %d',
240
+ len(response), len(response_cleaned)
241
+ )
242
+ logger.debug('Cleaned JSON: %s', response_cleaned)
243
+
244
+ # Now create the PPT file
245
+ progress_bar_pptx.progress(75, 'Creating the slide deck...give it a moment...')
246
+ generate_slide_deck(response_cleaned)
247
+ progress_bar_pptx.progress(100, text='Done!')
248
 
249
+ logger.info(
250
+ '#messages in history / 2: %d',
251
+ len(st.session_state[CHAT_MESSAGES]) / 2
252
+ )
253
 
254
+
255
+ def generate_slide_deck(json_str: str):
256
  """
257
+ Create a slide deck.
258
 
259
+ :param json_str: The content in *valid* JSON format.
 
 
260
  """
261
 
262
+ if DOWNLOAD_FILE_KEY in st.session_state:
263
+ path = pathlib.Path(st.session_state[DOWNLOAD_FILE_KEY])
264
+ else:
265
+ temp = tempfile.NamedTemporaryFile(delete=False, suffix='.pptx')
266
+ path = pathlib.Path(temp.name)
267
+ st.session_state[DOWNLOAD_FILE_KEY] = str(path)
268
+
269
+ if temp:
270
+ temp.close()
271
+
272
+ logger.debug('Creating PPTX file: %s...', st.session_state[DOWNLOAD_FILE_KEY])
273
 
274
  try:
275
+ pptx_helper.generate_powerpoint_presentation(
276
+ json_str,
277
+ slides_template=pptx_template,
278
+ output_file_path=path
279
+ )
280
+
281
+ _display_download_button(path)
282
+ except ValueError as ve:
283
  st.error(
284
+ f"{APP_TEXT['json_parsing_error']}"
285
+ f"\n\nAdditional error info: {ve}"
286
+ f"\n\nHere are some sample instructions that you could try to possibly fix this error;"
287
+ f" if these don't work, try rephrasing or refreshing:"
288
+ f"\n\n"
289
+ "- Regenerate content and fix the JSON error."
290
+ "\n- Regenerate content and fix the JSON error. Quotes inside quotes should be escaped."
291
  )
292
+ logger.error('%s', APP_TEXT['json_parsing_error'])
293
+ logger.error('Additional error info: %s', str(ve))
294
+ except Exception as ex:
295
+ st.error(APP_TEXT['content_generation_error'])
296
+ logger.error('Caught a generic exception: %s', str(ex))
297
+
298
+
299
+ def _is_it_refinement() -> bool:
300
+ """
301
+ Whether it is the initial prompt or a refinement.
302
+
303
+ :return: True if it is the initial prompt; False otherwise.
304
+ """
305
 
306
+ if IS_IT_REFINEMENT in st.session_state:
307
+ return True
308
 
309
+ if len(st.session_state[CHAT_MESSAGES]) >= 2:
310
+ # Prepare for the next call
311
+ st.session_state[IS_IT_REFINEMENT] = True
312
+ return True
313
 
314
+ return False
315
 
316
 
317
+ def _get_user_messages() -> List[str]:
318
  """
319
+ Get a list of user messages submitted until now from the session state.
320
 
321
+ :return: The list of user messages.
 
 
 
322
  """
323
 
324
+ return [
325
+ msg.content for msg in st.session_state[CHAT_MESSAGES] if isinstance(msg, HumanMessage)
326
+ ]
327
 
 
 
 
 
 
328
 
329
+ def _get_last_response() -> str:
330
+ """
331
+ Get the last response generated by AI.
332
 
333
+ :return: The response text.
334
+ """
 
 
 
 
 
335
 
336
+ return st.session_state[CHAT_MESSAGES][-1].content
 
337
 
 
338
 
339
+ def _display_messages_history(view_messages: st.expander):
340
+ """
341
+ Display the history of messages.
342
 
343
+ :param view_messages: The list of AI and Human messages.
344
  """
 
345
 
346
+ with view_messages:
347
+ view_messages.json(st.session_state[CHAT_MESSAGES])
348
+
349
+
350
+ def _display_download_button(file_path: pathlib.Path):
351
+ """
352
+ Display a download button to download a slide deck.
353
+
354
+ :param file_path: The path of the .pptx file.
355
  """
356
 
357
+ with open(file_path, 'rb') as download_file:
358
+ st.download_button(
359
+ 'Download PPTX file ⬇️',
360
+ data=download_file,
361
+ file_name='Presentation.pptx',
362
+ key=datetime.datetime.now()
363
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
364
 
365
 
366
  def main():
global_config.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import os
2
 
3
  from dataclasses import dataclass
@@ -12,17 +13,20 @@ class GlobalConfig:
12
  HF_LLM_MODEL_NAME = 'mistralai/Mistral-7B-Instruct-v0.2'
13
  LLM_MODEL_TEMPERATURE: float = 0.2
14
  LLM_MODEL_MIN_OUTPUT_LENGTH: int = 50
15
- LLM_MODEL_MAX_OUTPUT_LENGTH: int = 2000
16
- LLM_MODEL_MAX_INPUT_LENGTH: int = 300
17
 
18
  HUGGINGFACEHUB_API_TOKEN = os.environ.get('HUGGINGFACEHUB_API_TOKEN', '')
19
  METAPHOR_API_KEY = os.environ.get('METAPHOR_API_KEY', '')
20
 
21
  LOG_LEVEL = 'DEBUG'
 
22
  APP_STRINGS_FILE = 'strings.json'
23
  PRELOAD_DATA_FILE = 'examples/example_02.json'
24
  SLIDES_TEMPLATE_FILE = 'langchain_templates/template_combined.txt'
25
  JSON_TEMPLATE_FILE = 'langchain_templates/text_to_json_template_02.txt'
 
 
26
 
27
  PPTX_TEMPLATE_FILES = {
28
  'Blank': {
@@ -38,3 +42,27 @@ class GlobalConfig:
38
  'caption': 'Marvel in a monochrome dream'
39
  }
40
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import logging
2
  import os
3
 
4
  from dataclasses import dataclass
 
13
  HF_LLM_MODEL_NAME = 'mistralai/Mistral-7B-Instruct-v0.2'
14
  LLM_MODEL_TEMPERATURE: float = 0.2
15
  LLM_MODEL_MIN_OUTPUT_LENGTH: int = 50
16
+ LLM_MODEL_MAX_OUTPUT_LENGTH: int = 4096
17
+ LLM_MODEL_MAX_INPUT_LENGTH: int = 750
18
 
19
  HUGGINGFACEHUB_API_TOKEN = os.environ.get('HUGGINGFACEHUB_API_TOKEN', '')
20
  METAPHOR_API_KEY = os.environ.get('METAPHOR_API_KEY', '')
21
 
22
  LOG_LEVEL = 'DEBUG'
23
+ COUNT_TOKENS = False
24
  APP_STRINGS_FILE = 'strings.json'
25
  PRELOAD_DATA_FILE = 'examples/example_02.json'
26
  SLIDES_TEMPLATE_FILE = 'langchain_templates/template_combined.txt'
27
  JSON_TEMPLATE_FILE = 'langchain_templates/text_to_json_template_02.txt'
28
+ INITIAL_PROMPT_TEMPLATE = 'langchain_templates/chat_prompts/initial_template.txt'
29
+ REFINEMENT_PROMPT_TEMPLATE = 'langchain_templates/chat_prompts/refinement_template.txt'
30
 
31
  PPTX_TEMPLATE_FILES = {
32
  'Blank': {
 
42
  'caption': 'Marvel in a monochrome dream'
43
  }
44
  }
45
+
46
+ # This is a long text, so not incorporated as a string in `strings.json`
47
+ CHAT_USAGE_INSTRUCTIONS = (
48
+ 'Briefly describe your topic of presentation in the textbox provided below.'
49
+ ' For example, "Make a slide deck on AI." Subsequently, you can add follow-up'
50
+ ' instructions, e.g., "Can you add a slide on GPUs?" You can also ask it to refine any'
51
+ ' particular slide, e.g., "Make the slide with title \'Examples of AI\' a bit more'
52
+ ' descriptive." See this [demo video](https://youtu.be/QvAKzNKtk9k).'
53
+ ' As another example, sometimes the formatting of generated Python code can be a bit weird.'
54
+ ' You can try it telling, "Split multi-line codes into multiple lines," and hope for a fix.'
55
+ '\n\n'
56
+ 'SlideDeck AI generates only text content. It does not have access to the Web.'
57
+ '\n\n'
58
+ 'If you like SlideDeck AI, please consider leaving a heart ❤️ on the'
59
+ ' [Hugging Face Space](https://huggingface.co/spaces/barunsaha/slide-deck-ai/) or'
60
+ ' a star ⭐ on [GitHub](https://github.com/barun-saha/slide-deck-ai).'
61
+ )
62
+
63
+
64
+ logging.basicConfig(
65
+ level=GlobalConfig.LOG_LEVEL,
66
+ format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
67
+ datefmt='%Y-%m-%d %H:%M:%S'
68
+ )
helpers/__init__.py ADDED
File without changes
llm_helper.py → helpers/llm_helper.py RENAMED
@@ -1,5 +1,10 @@
1
  import logging
2
  import requests
 
 
 
 
 
3
 
4
  from global_config import GlobalConfig
5
 
@@ -7,28 +12,58 @@ from global_config import GlobalConfig
7
  HF_API_URL = f"https://api-inference.huggingface.co/models/{GlobalConfig.HF_LLM_MODEL_NAME}"
8
  HF_API_HEADERS = {"Authorization": f"Bearer {GlobalConfig.HUGGINGFACEHUB_API_TOKEN}"}
9
 
10
- logging.basicConfig(
11
- level=GlobalConfig.LOG_LEVEL,
12
- format='%(asctime)s - %(message)s',
 
 
 
 
 
13
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- # llm = None
 
 
 
 
 
 
 
 
 
 
 
16
 
17
 
18
- def hf_api_query(payload: dict):
19
  """
20
  Invoke HF inference end-point API.
21
 
22
- :param payload: The prompt for the LLM and related parameters
23
- :return: The output from the LLM
24
  """
25
 
26
  try:
27
- response = requests.post(HF_API_URL, headers=HF_API_HEADERS, json=payload, timeout=15)
28
  result = response.json()
29
  except requests.exceptions.Timeout as te:
30
- logging.error('*** Error: hf_api_query timeout! %s', str(te))
31
- result = {}
32
 
33
  return result
34
 
@@ -37,8 +72,8 @@ def generate_slides_content(topic: str) -> str:
37
  """
38
  Generate the outline/contents of slides for a presentation on a given topic.
39
 
40
- :param topic: Topic on which slides are to be generated
41
- :return: The content in JSON format
42
  """
43
 
44
  with open(GlobalConfig.SLIDES_TEMPLATE_FILE, 'r', encoding='utf-8') as in_file:
@@ -46,8 +81,8 @@ def generate_slides_content(topic: str) -> str:
46
  template_txt = template_txt.replace('<REPLACE_PLACEHOLDER>', topic)
47
 
48
  output = hf_api_query({
49
- "inputs": template_txt,
50
- "parameters": {
51
  'temperature': GlobalConfig.LLM_MODEL_TEMPERATURE,
52
  'min_length': GlobalConfig.LLM_MODEL_MIN_OUTPUT_LENGTH,
53
  'max_length': GlobalConfig.LLM_MODEL_MAX_OUTPUT_LENGTH,
@@ -56,7 +91,7 @@ def generate_slides_content(topic: str) -> str:
56
  'return_full_text': False,
57
  # "repetition_penalty": 0.0001
58
  },
59
- "options": {
60
  'wait_for_model': True,
61
  'use_cache': True
62
  }
@@ -70,7 +105,7 @@ def generate_slides_content(topic: str) -> str:
70
  # logging.debug(f'{json_end_idx=}')
71
  output = output[:json_end_idx]
72
 
73
- logging.debug('generate_slides_content: output: %s', output)
74
 
75
  return output
76
 
 
1
  import logging
2
  import requests
3
+ from requests.adapters import HTTPAdapter
4
+ from urllib3.util import Retry
5
+
6
+ from langchain_community.llms.huggingface_endpoint import HuggingFaceEndpoint
7
+ from langchain_core.language_models import LLM
8
 
9
  from global_config import GlobalConfig
10
 
 
12
  HF_API_URL = f"https://api-inference.huggingface.co/models/{GlobalConfig.HF_LLM_MODEL_NAME}"
13
  HF_API_HEADERS = {"Authorization": f"Bearer {GlobalConfig.HUGGINGFACEHUB_API_TOKEN}"}
14
 
15
+ logger = logging.getLogger(__name__)
16
+
17
+ retries = Retry(
18
+ total=5,
19
+ backoff_factor=0.25,
20
+ backoff_jitter=0.3,
21
+ status_forcelist=[502, 503, 504],
22
+ allowed_methods={'POST'},
23
  )
24
+ adapter = HTTPAdapter(max_retries=retries)
25
+ http_session = requests.Session()
26
+ http_session.mount('https://', adapter)
27
+ http_session.mount('http://', adapter)
28
+
29
+
30
+ def get_hf_endpoint() -> LLM:
31
+ """
32
+ Get an LLM via the HuggingFaceEndpoint of LangChain.
33
+
34
+ :return: The LLM.
35
+ """
36
+
37
+ logger.debug('Getting LLM via HF endpoint')
38
 
39
+ return HuggingFaceEndpoint(
40
+ repo_id=GlobalConfig.HF_LLM_MODEL_NAME,
41
+ max_new_tokens=GlobalConfig.LLM_MODEL_MAX_OUTPUT_LENGTH,
42
+ top_k=40,
43
+ top_p=0.95,
44
+ temperature=GlobalConfig.LLM_MODEL_TEMPERATURE,
45
+ repetition_penalty=1.03,
46
+ streaming=True,
47
+ huggingfacehub_api_token=GlobalConfig.HUGGINGFACEHUB_API_TOKEN,
48
+ return_full_text=False,
49
+ stop_sequences=['</s>'],
50
+ )
51
 
52
 
53
+ def hf_api_query(payload: dict) -> dict:
54
  """
55
  Invoke HF inference end-point API.
56
 
57
+ :param payload: The prompt for the LLM and related parameters.
58
+ :return: The output from the LLM.
59
  """
60
 
61
  try:
62
+ response = http_session.post(HF_API_URL, headers=HF_API_HEADERS, json=payload, timeout=15)
63
  result = response.json()
64
  except requests.exceptions.Timeout as te:
65
+ logger.error('*** Error: hf_api_query timeout! %s', str(te))
66
+ result = []
67
 
68
  return result
69
 
 
72
  """
73
  Generate the outline/contents of slides for a presentation on a given topic.
74
 
75
+ :param topic: Topic on which slides are to be generated.
76
+ :return: The content in JSON format.
77
  """
78
 
79
  with open(GlobalConfig.SLIDES_TEMPLATE_FILE, 'r', encoding='utf-8') as in_file:
 
81
  template_txt = template_txt.replace('<REPLACE_PLACEHOLDER>', topic)
82
 
83
  output = hf_api_query({
84
+ 'inputs': template_txt,
85
+ 'parameters': {
86
  'temperature': GlobalConfig.LLM_MODEL_TEMPERATURE,
87
  'min_length': GlobalConfig.LLM_MODEL_MIN_OUTPUT_LENGTH,
88
  'max_length': GlobalConfig.LLM_MODEL_MAX_OUTPUT_LENGTH,
 
91
  'return_full_text': False,
92
  # "repetition_penalty": 0.0001
93
  },
94
+ 'options': {
95
  'wait_for_model': True,
96
  'use_cache': True
97
  }
 
105
  # logging.debug(f'{json_end_idx=}')
106
  output = output[:json_end_idx]
107
 
108
+ logger.debug('generate_slides_content: output: %s', output)
109
 
110
  return output
111
 
pptx_helper.py → helpers/pptx_helper.py RENAMED
@@ -2,6 +2,7 @@ import logging
2
  import pathlib
3
  import re
4
  import tempfile
 
5
  from typing import List, Tuple
6
 
7
  import json5
@@ -28,17 +29,14 @@ SAMPLE_JSON_FOR_PPTX = '''
28
  }
29
  '''
30
 
31
- logging.basicConfig(
32
- level=GlobalConfig.LOG_LEVEL,
33
- format='%(asctime)s - %(message)s',
34
- )
35
 
36
 
37
  def remove_slide_number_from_heading(header: str) -> str:
38
  """
39
  Remove the slide number from a given slide header.
40
 
41
- :param header: The header of a slide
42
  """
43
 
44
  if PATTERN.match(header):
@@ -56,17 +54,17 @@ def generate_powerpoint_presentation(
56
  """
57
  Create and save a PowerPoint presentation file containing the content in JSON format.
58
 
59
- :param structured_data: The presentation contents as "JSON" (may contain trailing commas)
60
- :param slides_template: The PPTX template to use
61
- :param output_file_path: The path of the PPTX file to save as
62
- :return A list of presentation title and slides headers
63
  """
64
 
65
  # The structured "JSON" might contain trailing commas, so using json5
66
  parsed_data = json5.loads(structured_data)
67
 
68
- logging.debug(
69
- "*** Using PPTX template: %s",
70
  GlobalConfig.PPTX_TEMPLATE_FILES[slides_template]['file']
71
  )
72
  presentation = pptx.Presentation(GlobalConfig.PPTX_TEMPLATE_FILES[slides_template]['file'])
@@ -77,7 +75,10 @@ def generate_powerpoint_presentation(
77
  title = slide.shapes.title
78
  subtitle = slide.placeholders[1]
79
  title.text = parsed_data['title']
80
- logging.debug('Presentation title is: %s', title.text)
 
 
 
81
  subtitle.text = 'by Myself and SlideDeck AI :)'
82
  all_headers = [title.text, ]
83
 
@@ -125,9 +126,9 @@ def get_flat_list_of_contents(items: list, level: int) -> List[Tuple]:
125
  Flatten a (hierarchical) list of bullet points to a single list containing each item and
126
  its level.
127
 
128
- :param items: A bullet point (string or list)
129
- :param level: The current level of hierarchy
130
- :return: A list of (bullet item text, hierarchical level) tuples
131
  """
132
 
133
  flat_list = []
@@ -240,3 +241,5 @@ if __name__ == '__main__':
240
  output_file_path=path,
241
  slides_template='Blank'
242
  )
 
 
 
2
  import pathlib
3
  import re
4
  import tempfile
5
+
6
  from typing import List, Tuple
7
 
8
  import json5
 
29
  }
30
  '''
31
 
32
+ logger = logging.getLogger(__name__)
 
 
 
33
 
34
 
35
  def remove_slide_number_from_heading(header: str) -> str:
36
  """
37
  Remove the slide number from a given slide header.
38
 
39
+ :param header: The header of a slide.
40
  """
41
 
42
  if PATTERN.match(header):
 
54
  """
55
  Create and save a PowerPoint presentation file containing the content in JSON format.
56
 
57
+ :param structured_data: The presentation contents as "JSON" (may contain trailing commas).
58
+ :param slides_template: The PPTX template to use.
59
+ :param output_file_path: The path of the PPTX file to save as.
60
+ :return A list of presentation title and slides headers.
61
  """
62
 
63
  # The structured "JSON" might contain trailing commas, so using json5
64
  parsed_data = json5.loads(structured_data)
65
 
66
+ logger.debug(
67
+ '*** Using PPTX template: %s',
68
  GlobalConfig.PPTX_TEMPLATE_FILES[slides_template]['file']
69
  )
70
  presentation = pptx.Presentation(GlobalConfig.PPTX_TEMPLATE_FILES[slides_template]['file'])
 
75
  title = slide.shapes.title
76
  subtitle = slide.placeholders[1]
77
  title.text = parsed_data['title']
78
+ logger.info(
79
+ 'PPT title: %s | #slides: %d',
80
+ title.text, len(parsed_data['slides'])
81
+ )
82
  subtitle.text = 'by Myself and SlideDeck AI :)'
83
  all_headers = [title.text, ]
84
 
 
126
  Flatten a (hierarchical) list of bullet points to a single list containing each item and
127
  its level.
128
 
129
+ :param items: A bullet point (string or list).
130
+ :param level: The current level of hierarchy.
131
+ :return: A list of (bullet item text, hierarchical level) tuples.
132
  """
133
 
134
  flat_list = []
 
241
  output_file_path=path,
242
  slides_template='Blank'
243
  )
244
+
245
+ temp.close()
helpers/text_helper.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def is_valid_prompt(prompt: str) -> bool:
2
+ """
3
+ Verify whether user input satisfies the concerned constraints.
4
+
5
+ :param prompt: The user input text.
6
+ :return: True if all criteria are satisfied; False otherwise.
7
+ """
8
+
9
+ if len(prompt) < 7 or ' ' not in prompt:
10
+ return False
11
+
12
+ return True
13
+
14
+
15
+ def get_clean_json(json_str: str) -> str:
16
+ """
17
+ Attempt to clean a JSON response string from the LLM by removing the trailing ```
18
+ and any text beyond that.
19
+ CAUTION: May not be always accurate.
20
+
21
+ :param json_str: The input string in JSON format.
22
+ :return: The "cleaned" JSON string.
23
+ """
24
+
25
+ # An example of response containing JSON and other text:
26
+ # {
27
+ # "title": "AI and the Future: A Transformative Journey",
28
+ # "slides": [
29
+ # ...
30
+ # ]
31
+ # } <<---- This is end of valid JSON content
32
+ # ```
33
+ #
34
+ # ```vbnet
35
+ # Please note that the JSON output is in valid format but the content of the "Role of GPUs in AI" slide is just an example and may not be factually accurate. For accurate information, you should consult relevant resources and update the content accordingly.
36
+ # ```
37
+ response_cleaned = json_str
38
+
39
+ while True:
40
+ idx = json_str.rfind('```') # -1 on failure
41
+
42
+ if idx <= 0:
43
+ break
44
+
45
+ # In the ideal scenario, the character before the last ``` should be
46
+ # a new line or a closing bracket }
47
+ prev_char = json_str[idx - 1]
48
+
49
+ if prev_char == '}':
50
+ response_cleaned = json_str[:idx]
51
+ elif prev_char == '\n' and json_str[idx - 2] == '}':
52
+ response_cleaned = json_str[:idx]
53
+
54
+ json_str = json_str[:idx]
55
+
56
+ return response_cleaned
langchain_templates/chat_prompts/initial_template.txt ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ You are a helpful, intelligent chatbot. Create the slides for a presentation on the given topic.
2
+ Include main headings for each slide, detailed bullet points for each slide.
3
+ Add relevant content to each slide.
4
+ The content of each slide should be verbose, descriptive, and very detailed.
5
+ If relevant, add one or two examples to illustrate the concept.
6
+ Unless explicitly specified with the topic, create about 10 slides.
7
+
8
+
9
+ ### Topic:
10
+ {question}
11
+
12
+
13
+ The output must be only a valid and syntactically correct JSON adhering to the following schema:
14
+ {{
15
+ "title": "Presentation Title",
16
+ "slides": [
17
+ {{
18
+ "heading": "Heading for the First Slide",
19
+ "bullet_points": [
20
+ "First bullet point",
21
+ [
22
+ "Sub-bullet point 1",
23
+ "Sub-bullet point 2"
24
+ ],
25
+ "Second bullet point"
26
+ ]
27
+ }},
28
+ {{
29
+ "heading": "Heading for the Second Slide",
30
+ "bullet_points": [
31
+ "First bullet point",
32
+ "Second bullet item",
33
+ "Third bullet point"
34
+ ]
35
+ }}
36
+ ]
37
+ }}
38
+
39
+
40
+ ### Output:
41
+ ```json
langchain_templates/chat_prompts/refinement_template.txt ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ You are a helpful, intelligent chatbot. You follow instructions to refine an existing slide deck.
2
+ A list of user instructions is provided below in sequential order -- from the oldest to the latest.
3
+ The previously generated content of the slide deck in JSON format is also provided.
4
+ Follow the instructions to revise the content of the previously generated slides of the presentation on the given topic.
5
+ Include main headings for each slide, detailed bullet points for each slide.
6
+ Add relevant content to each slide.
7
+ The content of the slides should be descriptive, verbose, and detailed.
8
+ If relevant, add one or two examples to illustrate the concept.
9
+ Unless explicitly specified with the topic, create about 10 slides.
10
+ You also fix any syntax error that may be present in the JSON-formatted content.
11
+
12
+
13
+ ### List of instructions:
14
+ {instructions}
15
+
16
+
17
+ ### Previously generated slide deck content as JSON:
18
+ {previous_content}
19
+
20
+
21
+ The output must be only a valid and syntactically correct JSON adhering to the following schema:
22
+ {{
23
+ "title": "Presentation Title",
24
+ "slides": [
25
+ {{
26
+ "heading": "Heading for the First Slide",
27
+ "bullet_points": [
28
+ "First bullet point",
29
+ [
30
+ "Sub-bullet point 1",
31
+ "Sub-bullet point 2"
32
+ ],
33
+ "Second bullet point"
34
+ ]
35
+ }},
36
+ {{
37
+ "heading": "Heading for the Second Slide",
38
+ "bullet_points": [
39
+ "First bullet point",
40
+ "Second bullet item",
41
+ "Third bullet point"
42
+ ]
43
+ }}
44
+ ]
45
+ }}
46
+
47
+
48
+ ### Output:
49
+ ```json
legacy_app.py ADDED
@@ -0,0 +1,294 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pathlib
2
+ import logging
3
+ import tempfile
4
+ from typing import List, Tuple
5
+
6
+ import json5
7
+ import metaphor_python as metaphor
8
+ import streamlit as st
9
+
10
+ from helpers import llm_helper, pptx_helper
11
+ from global_config import GlobalConfig
12
+
13
+
14
+ APP_TEXT = json5.loads(open(GlobalConfig.APP_STRINGS_FILE, 'r', encoding='utf-8').read())
15
+ GB_CONVERTER = 2 ** 30
16
+
17
+
18
+ logger = logging.getLogger(__name__)
19
+
20
+
21
+ @st.cache_data
22
+ def get_contents_wrapper(text: str) -> str:
23
+ """
24
+ Fetch and cache the slide deck contents on a topic by calling an external API.
25
+
26
+ :param text: The presentation topic.
27
+ :return: The slide deck contents or outline in JSON format.
28
+ """
29
+
30
+ logger.info('LLM call because of cache miss...')
31
+ return llm_helper.generate_slides_content(text).strip()
32
+
33
+
34
+ @st.cache_resource
35
+ def get_metaphor_client_wrapper() -> metaphor.Metaphor:
36
+ """
37
+ Create a Metaphor client for semantic Web search.
38
+
39
+ :return: Metaphor instance.
40
+ """
41
+
42
+ return metaphor.Metaphor(api_key=GlobalConfig.METAPHOR_API_KEY)
43
+
44
+
45
+ @st.cache_data
46
+ def get_web_search_results_wrapper(text: str) -> List[Tuple[str, str]]:
47
+ """
48
+ Fetch and cache the Web search results on a given topic.
49
+
50
+ :param text: The topic.
51
+ :return: A list of (title, link) tuples.
52
+ """
53
+
54
+ results = []
55
+ search_results = get_metaphor_client_wrapper().search(
56
+ text,
57
+ use_autoprompt=True,
58
+ num_results=5
59
+ )
60
+
61
+ for a_result in search_results.results:
62
+ results.append((a_result.title, a_result.url))
63
+
64
+ return results
65
+
66
+
67
+ def build_ui():
68
+ """
69
+ Display the input elements for content generation. Only covers the first step.
70
+ """
71
+
72
+ # get_disk_used_percentage()
73
+
74
+ st.title(APP_TEXT['app_name'])
75
+ st.subheader(APP_TEXT['caption'])
76
+ st.markdown(
77
+ 'Powered by'
78
+ ' [Mistral-7B-Instruct-v0.2](https://huggingface.co/mistralai/Mistral-7B-Instruct-v0.2).'
79
+ )
80
+ st.markdown(
81
+ '*If the JSON is generated or parsed incorrectly, try again later by making minor changes'
82
+ ' to the input text.*'
83
+ )
84
+
85
+ with st.form('my_form'):
86
+ # Topic input
87
+ try:
88
+ with open(GlobalConfig.PRELOAD_DATA_FILE, 'r', encoding='utf-8') as in_file:
89
+ preload_data = json5.loads(in_file.read())
90
+ except (FileExistsError, FileNotFoundError):
91
+ preload_data = {'topic': '', 'audience': ''}
92
+
93
+ topic = st.text_area(
94
+ APP_TEXT['input_labels'][0],
95
+ value=preload_data['topic']
96
+ )
97
+
98
+ texts = list(GlobalConfig.PPTX_TEMPLATE_FILES.keys())
99
+ captions = [GlobalConfig.PPTX_TEMPLATE_FILES[x]['caption'] for x in texts]
100
+
101
+ pptx_template = st.radio(
102
+ 'Select a presentation template:',
103
+ texts,
104
+ captions=captions,
105
+ horizontal=True
106
+ )
107
+
108
+ st.divider()
109
+ submit = st.form_submit_button('Generate slide deck')
110
+
111
+ if submit:
112
+ # st.write(f'Clicked {time.time()}')
113
+ st.session_state.submitted = True
114
+
115
+ # https://github.com/streamlit/streamlit/issues/3832#issuecomment-1138994421
116
+ if 'submitted' in st.session_state:
117
+ progress_text = 'Generating the slides...give it a moment'
118
+ progress_bar = st.progress(0, text=progress_text)
119
+
120
+ topic_txt = topic.strip()
121
+ generate_presentation(topic_txt, pptx_template, progress_bar)
122
+
123
+ st.divider()
124
+ st.text(APP_TEXT['tos'])
125
+ st.text(APP_TEXT['tos2'])
126
+
127
+ st.markdown(
128
+ '![Visitors]'
129
+ '(https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Fhuggingface.co%2Fspaces%2Fbarunsaha%2Fslide-deck-ai&countColor=%23263759)'
130
+ )
131
+
132
+
133
+ def generate_presentation(topic: str, pptx_template: str, progress_bar):
134
+ """
135
+ Process the inputs to generate the slides.
136
+
137
+ :param topic: The presentation topic based on which contents are to be generated.
138
+ :param pptx_template: The PowerPoint template name to be used.
139
+ :param progress_bar: Progress bar from the page.
140
+ """
141
+
142
+ topic_length = len(topic)
143
+ logger.debug('Input length:: topic: %s', topic_length)
144
+
145
+ if topic_length >= 10:
146
+ logger.debug('Topic: %s', topic)
147
+ target_length = min(topic_length, GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH)
148
+
149
+ try:
150
+ # Step 1: Generate the contents in JSON format using an LLM
151
+ json_str = process_slides_contents(topic[:target_length], progress_bar)
152
+ logger.debug('Truncated topic: %s', topic[:target_length])
153
+ logger.debug('Length of JSON: %d', len(json_str))
154
+
155
+ # Step 2: Generate the slide deck based on the template specified
156
+ if len(json_str) > 0:
157
+ st.info(
158
+ 'Tip: The generated content doesn\'t look so great?'
159
+ ' Need alternatives? Just change your description text and try again.',
160
+ icon="💡️"
161
+ )
162
+ else:
163
+ st.error(
164
+ 'Unfortunately, JSON generation failed, so the next steps would lead'
165
+ ' to nowhere. Try again or come back later.'
166
+ )
167
+ return
168
+
169
+ all_headers = generate_slide_deck(json_str, pptx_template, progress_bar)
170
+
171
+ # Step 3: Bonus stuff: Web references and AI art
172
+ show_bonus_stuff(all_headers)
173
+
174
+ except ValueError as ve:
175
+ st.error(f'Unfortunately, an error occurred: {ve}! '
176
+ f'Please change the text, try again later, or report it, sharing your inputs.')
177
+
178
+ else:
179
+ st.error('Not enough information provided! Please be little more descriptive :)')
180
+
181
+
182
+ def process_slides_contents(text: str, progress_bar: st.progress) -> str:
183
+ """
184
+ Convert given text into structured data and display. Update the UI.
185
+
186
+ :param text: The topic description for the presentation.
187
+ :param progress_bar: Progress bar for this step.
188
+ :return: The contents as a JSON-formatted string.
189
+ """
190
+
191
+ json_str = ''
192
+
193
+ try:
194
+ logger.info('Calling LLM for content generation on the topic: %s', text)
195
+ json_str = get_contents_wrapper(text)
196
+ except Exception as ex:
197
+ st.error(
198
+ f'An exception occurred while trying to convert to JSON. It could be because of heavy'
199
+ f' traffic or something else. Try doing it again or try again later.'
200
+ f'\nError message: {ex}'
201
+ )
202
+
203
+ progress_bar.progress(50, text='Contents generated')
204
+
205
+ with st.expander('The generated contents (in JSON format)'):
206
+ st.code(json_str, language='json')
207
+
208
+ return json_str
209
+
210
+
211
+ def generate_slide_deck(json_str: str, pptx_template: str, progress_bar) -> List:
212
+ """
213
+ Create a slide deck.
214
+
215
+ :param json_str: The contents in JSON format.
216
+ :param pptx_template: The PPTX template name.
217
+ :param progress_bar: Progress bar.
218
+ :return: A list of all slide headers and the title.
219
+ """
220
+
221
+ progress_text = 'Creating the slide deck...give it a moment'
222
+ progress_bar.progress(75, text=progress_text)
223
+
224
+ # # Get a unique name for the file to save -- use the session ID
225
+ # ctx = st_sr.get_script_run_ctx()
226
+ # session_id = ctx.session_id
227
+ # timestamp = time.time()
228
+ # output_file_name = f'{session_id}_{timestamp}.pptx'
229
+
230
+ temp = tempfile.NamedTemporaryFile(delete=False, suffix='.pptx')
231
+ path = pathlib.Path(temp.name)
232
+
233
+ logger.info('Creating PPTX file...')
234
+ all_headers = pptx_helper.generate_powerpoint_presentation(
235
+ json_str,
236
+ slides_template=pptx_template,
237
+ output_file_path=path
238
+ )
239
+ progress_bar.progress(100, text='Done!')
240
+
241
+ with open(path, 'rb') as f:
242
+ st.download_button('Download PPTX file', f, file_name='Presentation.pptx')
243
+
244
+ if temp:
245
+ temp.close()
246
+
247
+ return all_headers
248
+
249
+
250
+ def show_bonus_stuff(ppt_headers: List[str]):
251
+ """
252
+ Show bonus stuff for the presentation.
253
+
254
+ :param ppt_headers: A list of the slide headings.
255
+ """
256
+
257
+ # Use the presentation title and the slide headers to find relevant info online
258
+ logger.info('Calling Metaphor search...')
259
+ ppt_text = ' '.join(ppt_headers)
260
+ search_results = get_web_search_results_wrapper(ppt_text)
261
+ md_text_items = []
262
+
263
+ for (title, link) in search_results:
264
+ md_text_items.append(f'[{title}]({link})')
265
+
266
+ with st.expander('Related Web references'):
267
+ st.markdown('\n\n'.join(md_text_items))
268
+
269
+ logger.info('Done!')
270
+
271
+ # # Avoid image generation. It costs time and an API call, so just limit to the text generation.
272
+ # with st.expander('AI-generated image on the presentation topic'):
273
+ # logger.info('Calling SDXL for image generation...')
274
+ # # img_empty.write('')
275
+ # # img_text.write(APP_TEXT['image_info'])
276
+ # image = get_ai_image_wrapper(ppt_text)
277
+ #
278
+ # if len(image) > 0:
279
+ # image = base64.b64decode(image)
280
+ # st.image(image, caption=ppt_text)
281
+ # st.info('Tip: Right-click on the image to save it.', icon="💡️")
282
+ # logger.info('Image added')
283
+
284
+
285
+ def main():
286
+ """
287
+ Trigger application run.
288
+ """
289
+
290
+ build_ui()
291
+
292
+
293
+ if __name__ == '__main__':
294
+ main()
requirements.txt CHANGED
@@ -6,4 +6,9 @@ streamlit~=1.32.2
6
  python-pptx
7
  metaphor-python
8
  json5~=0.9.14
9
- requests~=2.31.0
 
 
 
 
 
 
6
  python-pptx
7
  metaphor-python
8
  json5~=0.9.14
9
+ requests~=2.31.0
10
+
11
+ transformers~=4.39.2
12
+ langchain-community
13
+
14
+ urllib3~=2.2.1
strings.json CHANGED
@@ -1,6 +1,6 @@
1
  {
2
- "app_name": "SlideDeck AI",
3
- "caption": "*:green[Co-create your next PowerPoint slide deck with AI]*",
4
  "section_headers": [
5
  "Step 1: Generate your content",
6
  "Step 2: Make it structured",
@@ -23,7 +23,15 @@
23
  ],
24
  "urls_info": "Here is a list of some online resources that you can consult for further information on this topic:",
25
  "image_info": "Got some more minutes? We are also trying to deliver an AI-generated art on the presentation topic, fresh off the studio, just for you!",
26
- "content_generation_failure_error": "Unfortunately, SlideDeck AI failed to generate any content for you! Please try again later.",
 
27
  "tos": "SlideDeck AI is an experimental prototype, and it has its limitations.\nPlease carefully review any and all AI-generated content.",
28
- "tos2": "By using SlideDeck AI, you agree to fair and responsible usage.\nNo liability assumed by any party."
 
 
 
 
 
 
 
29
  }
 
1
  {
2
+ "app_name": ":green[SlideDeck AI $^{[Reloaded]}$]",
3
+ "caption": "*Converse, create, and improve your next PowerPoint slide deck*",
4
  "section_headers": [
5
  "Step 1: Generate your content",
6
  "Step 2: Make it structured",
 
23
  ],
24
  "urls_info": "Here is a list of some online resources that you can consult for further information on this topic:",
25
  "image_info": "Got some more minutes? We are also trying to deliver an AI-generated art on the presentation topic, fresh off the studio, just for you!",
26
+ "content_generation_error": "Unfortunately, SlideDeck AI failed to generate any content for you! Please try again later.",
27
+ "json_parsing_error": "Unfortunately, SlideDeck AI failed to parse the response from LLM! Please try again by rephrasing the query or refreshing the page.",
28
  "tos": "SlideDeck AI is an experimental prototype, and it has its limitations.\nPlease carefully review any and all AI-generated content.",
29
+ "tos2": "By using SlideDeck AI, you agree to fair and responsible usage.\nNo liability assumed by any party.",
30
+ "ai_greetings": [
31
+ "How may I help you today?",
32
+ "Stuck with creating your presentation? Let me help you.",
33
+ "Looks like you have a looming deadline. Can I help you get started with your slide deck?",
34
+ "Hello! What topic do you have on your mind today?"
35
+ ],
36
+ "chat_placeholder": "Write the topic or instructions here"
37
  }