barunsaha commited on
Commit
a2e3454
·
unverified ·
2 Parent(s): fd9232d 5e41fab

Merge pull request #93 from barun-saha/visual

Browse files
.streamlit/config.toml CHANGED
@@ -1,7 +1,7 @@
1
  [server]
2
  runOnSave = true
3
  headless = false
4
- maxUploadSize = 0
5
 
6
  [browser]
7
  gatherUsageStats = false
 
1
  [server]
2
  runOnSave = true
3
  headless = false
4
+ maxUploadSize = 2
5
 
6
  [browser]
7
  gatherUsageStats = false
app.py CHANGED
@@ -21,6 +21,7 @@ from langchain_core.messages import HumanMessage
21
  from langchain_core.prompts import ChatPromptTemplate
22
 
23
  import global_config as gcfg
 
24
  from global_config import GlobalConfig
25
  from helpers import llm_helper, pptx_helper, text_helper
26
 
@@ -141,6 +142,7 @@ APP_TEXT = _load_strings()
141
  CHAT_MESSAGES = 'chat_messages'
142
  DOWNLOAD_FILE_KEY = 'download_file_name'
143
  IS_IT_REFINEMENT = 'is_it_refinement'
 
144
 
145
 
146
  logger = logging.getLogger(__name__)
@@ -266,8 +268,17 @@ def set_up_chat_ui():
266
 
267
  if prompt := st.chat_input(
268
  placeholder=APP_TEXT['chat_placeholder'],
269
- max_chars=GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH
 
 
270
  ):
 
 
 
 
 
 
 
271
  provider, llm_name = llm_helper.get_provider_model(
272
  llm_provider_to_use,
273
  use_ollama=RUN_IN_OFFLINE_MODE
@@ -279,20 +290,20 @@ def set_up_chat_ui():
279
  api_ver = api_version.strip()
280
 
281
  if not are_all_inputs_valid(
282
- prompt, provider, llm_name, user_key,
283
  az_deployment, az_endpoint, api_ver
284
  ):
285
  return
286
 
287
  logger.info(
288
  'User input: %s | #characters: %d | LLM: %s',
289
- prompt, len(prompt), llm_name
290
  )
291
- st.chat_message('user').write(prompt)
292
 
293
  if _is_it_refinement():
294
  user_messages = _get_user_messages()
295
- user_messages.append(prompt)
296
  list_of_msgs = [
297
  f'{idx + 1}. {msg}' for idx, msg in enumerate(user_messages)
298
  ]
@@ -300,10 +311,16 @@ def set_up_chat_ui():
300
  **{
301
  'instructions': '\n'.join(list_of_msgs),
302
  'previous_content': _get_last_response(),
 
303
  }
304
  )
305
  else:
306
- formatted_template = prompt_template.format(**{'question': prompt})
 
 
 
 
 
307
 
308
  progress_bar = st.progress(0, 'Preparing to call LLM...')
309
  response = ''
@@ -392,7 +409,7 @@ def set_up_chat_ui():
392
  )
393
  return
394
 
395
- history.add_user_message(prompt)
396
  history.add_ai_message(response)
397
 
398
  # The content has been generated as JSON
 
21
  from langchain_core.prompts import ChatPromptTemplate
22
 
23
  import global_config as gcfg
24
+ import helpers.file_manager as filem
25
  from global_config import GlobalConfig
26
  from helpers import llm_helper, pptx_helper, text_helper
27
 
 
142
  CHAT_MESSAGES = 'chat_messages'
143
  DOWNLOAD_FILE_KEY = 'download_file_name'
144
  IS_IT_REFINEMENT = 'is_it_refinement'
145
+ ADDITIONAL_INFO = 'additional_info'
146
 
147
 
148
  logger = logging.getLogger(__name__)
 
268
 
269
  if prompt := st.chat_input(
270
  placeholder=APP_TEXT['chat_placeholder'],
271
+ max_chars=GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH,
272
+ accept_file=True,
273
+ file_type=['pdf', ],
274
  ):
275
+ prompt_text = prompt.text or ''
276
+ if prompt['files']:
277
+ # Apparently, Streamlit stores uploaded files in memory and clears on browser close
278
+ # https://docs.streamlit.io/knowledge-base/using-streamlit/where-file-uploader-store-when-deleted
279
+ st.session_state[ADDITIONAL_INFO] = filem.get_pdf_contents(prompt['files'][0])
280
+ print(f'{prompt["files"]=}')
281
+
282
  provider, llm_name = llm_helper.get_provider_model(
283
  llm_provider_to_use,
284
  use_ollama=RUN_IN_OFFLINE_MODE
 
290
  api_ver = api_version.strip()
291
 
292
  if not are_all_inputs_valid(
293
+ prompt_text, provider, llm_name, user_key,
294
  az_deployment, az_endpoint, api_ver
295
  ):
296
  return
297
 
298
  logger.info(
299
  'User input: %s | #characters: %d | LLM: %s',
300
+ prompt_text, len(prompt_text), llm_name
301
  )
302
+ st.chat_message('user').write(prompt_text)
303
 
304
  if _is_it_refinement():
305
  user_messages = _get_user_messages()
306
+ user_messages.append(prompt_text)
307
  list_of_msgs = [
308
  f'{idx + 1}. {msg}' for idx, msg in enumerate(user_messages)
309
  ]
 
311
  **{
312
  'instructions': '\n'.join(list_of_msgs),
313
  'previous_content': _get_last_response(),
314
+ 'additional_info': st.session_state.get(ADDITIONAL_INFO, ''),
315
  }
316
  )
317
  else:
318
+ formatted_template = prompt_template.format(
319
+ **{
320
+ 'question': prompt_text,
321
+ 'additional_info': st.session_state.get(ADDITIONAL_INFO, ''),
322
+ }
323
+ )
324
 
325
  progress_bar = st.progress(0, 'Preparing to call LLM...')
326
  response = ''
 
409
  )
410
  return
411
 
412
+ history.add_user_message(prompt_text)
413
  history.add_ai_message(response)
414
 
415
  # The content has been generated as JSON
global_config.py CHANGED
@@ -86,6 +86,7 @@ class GlobalConfig:
86
  LLM_MODEL_TEMPERATURE = 0.2
87
  LLM_MODEL_MIN_OUTPUT_LENGTH = 100
88
  LLM_MODEL_MAX_INPUT_LENGTH = 400 # characters
 
89
 
90
  LOG_LEVEL = 'DEBUG'
91
  COUNT_TOKENS = False
@@ -133,15 +134,23 @@ class GlobalConfig:
133
  '\n\n'
134
  'Finally, click on the download button at the bottom to download the slide deck.'
135
  ' See this [demo video](https://youtu.be/QvAKzNKtk9k) for a brief walkthrough.\n\n'
136
- 'Remember, the conversational interface is meant to (and will) update yor *initial*'
137
- ' slide deck. If you want to create a new slide deck on a different topic,'
138
- ' start a new chat session by reloading this page.\n\n'
139
- 'Currently, paid or *free-to-use* LLMs from five different providers are supported.'
140
- ' If one is not available, choose the other from the dropdown list. A [summary of'
141
- ' the supported LLMs]('
 
 
 
 
 
 
 
 
142
  'https://github.com/barun-saha/slide-deck-ai/blob/main/README.md#summary-of-the-llms)'
143
- ' is available for reference. SlideDeck AI does **NOT** store your API keys.\n\n'
144
- ' SlideDeck AI does not have access to the Web, apart for searching for images relevant'
145
  ' to the slides. Photos are added probabilistically; transparency needs to be changed'
146
  ' manually, if required.\n\n'
147
  '[SlideDeck AI](https://github.com/barun-saha/slide-deck-ai) is an Open-Source project,'
 
86
  LLM_MODEL_TEMPERATURE = 0.2
87
  LLM_MODEL_MIN_OUTPUT_LENGTH = 100
88
  LLM_MODEL_MAX_INPUT_LENGTH = 400 # characters
89
+ MAX_PAGE_COUNT = 50
90
 
91
  LOG_LEVEL = 'DEBUG'
92
  COUNT_TOKENS = False
 
134
  '\n\n'
135
  'Finally, click on the download button at the bottom to download the slide deck.'
136
  ' See this [demo video](https://youtu.be/QvAKzNKtk9k) for a brief walkthrough.\n\n'
137
+ 'Remember, the conversational interface is meant to (and will) update yor *initial*/'
138
+ '*previous* slide deck. If you want to create a new slide deck on a different topic,'
139
+ ' start a new chat session by reloading this page.'
140
+ '\n\nSlideDeck AI can algo generate a presentation based on a PDF file. You can upload'
141
+ ' a PDF file using the chat widget. Only a single file and up to max 50 pages will be'
142
+ ' considered. For PDF-based slide deck generation, LLMs with large context windows, such'
143
+ ' as Gemini, GPT, and Mistral-Nemo, are recommended. Note: images from the PDF files will'
144
+ ' not be used.'
145
+ '\n\nAlso, note that the uploaded file might disappear from the page after click.'
146
+ ' You do not need to upload the same file again to continue'
147
+ ' the interaction and refining—the contents of the PDF file will be retained in the'
148
+ ' same interactive session.'
149
+ '\n\nCurrently, paid or *free-to-use* LLMs from five different providers are supported.'
150
+ ' A [summary of the supported LLMs]('
151
  'https://github.com/barun-saha/slide-deck-ai/blob/main/README.md#summary-of-the-llms)'
152
+ ' is available for reference. SlideDeck AI does **NOT** store your API keys.'
153
+ '\n\nSlideDeck AI does not have access to the Web, apart for searching for images relevant'
154
  ' to the slides. Photos are added probabilistically; transparency needs to be changed'
155
  ' manually, if required.\n\n'
156
  '[SlideDeck AI](https://github.com/barun-saha/slide-deck-ai) is an Open-Source project,'
helpers/file_manager.py ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ File manager helper to work with uploaded files.
3
+ """
4
+ import logging
5
+ import os
6
+ import sys
7
+
8
+ import streamlit as st
9
+ from pypdf import PdfReader
10
+
11
+ sys.path.append('..')
12
+ sys.path.append('../..')
13
+
14
+ from global_config import GlobalConfig
15
+
16
+
17
+ logger = logging.getLogger(__name__)
18
+
19
+
20
+ def get_pdf_contents(
21
+ pdf_file: st.runtime.uploaded_file_manager.UploadedFile,
22
+ max_pages: int = GlobalConfig.MAX_PAGE_COUNT
23
+ ) -> str:
24
+ """
25
+ Extract the text contents from a PDF file.
26
+
27
+ :param pdf_file: The uploaded PDF file.
28
+ :param max_pages: The max no. of pages to extract contents from.
29
+ :return: The contents.
30
+ """
31
+
32
+ reader = PdfReader(pdf_file)
33
+ n_pages = min(max_pages, len(reader.pages))
34
+ text = ''
35
+
36
+ for page in range(n_pages):
37
+ page = reader.pages[page]
38
+ text += page.extract_text()
39
+
40
+ return text
langchain_templates/chat_prompts/initial_template_v4_two_cols_img.txt CHANGED
@@ -5,6 +5,13 @@ Include main headings for each slide, detailed bullet points for each slide.
5
  Add relevant, detailed content to each slide. When relevant, add one or two EXAMPLES to illustrate the concept.
6
  For two or three important slides, generate the key message that those slides convey.
7
 
 
 
 
 
 
 
 
8
  Identify if a slide describes a step-by-step/sequential process, then begin the bullet points with a special marker >>.
9
  Limit this to max two or three slides.
10
 
@@ -16,11 +23,12 @@ In addition, create one slide containing 4 TO 6 icons (pictograms) illustrating
16
  In this slide, each line of text will begin with the name of a relevant icon enclosed between [[ and ]], e.g., [[machine-learning]] and [[fairness]].
17
  Insert icons only in this slide.
18
 
19
- Your output, i.e., the content of each slide should be VERBOSE, DESCRIPTIVE, and very DETAILED.
 
20
  Each bullet point should be detailed and explanatory, not just short phrases.
21
 
22
  ALWAYS add a concluding slide at the end, containing a list of the key takeaways and an optional call-to-action if relevant to the context.
23
- Unless explicitly instructed with the topic, create 10 TO 12 SLIDES in total. You must never create more tha 15 slides.
24
 
25
 
26
  ### Topic:
@@ -102,5 +110,10 @@ The output must be only a valid and syntactically correct JSON adhering to the f
102
  }}
103
 
104
 
 
 
 
 
 
105
  ### Output:
106
  ```json
 
5
  Add relevant, detailed content to each slide. When relevant, add one or two EXAMPLES to illustrate the concept.
6
  For two or three important slides, generate the key message that those slides convey.
7
 
8
+ The <ADDITIONAL_INFO> may provide additional information. If available, you should incorporate them while making the slides.
9
+ Read this information carefully. Based on the contents provided, plan the structure of the presentation.
10
+ For example, if it's a paper, you can consider having Problem, Solution, Experiments, and Results, among other sections.
11
+ If it's a product brochure, you can have Features, Changes, Operating Conditions, and likewise relevant sections.
12
+ Similarly, decide for other content types. Then appropriately incorporate the contents into the relevant slides, presenting in a useful way.
13
+ If <ADDITIONAL_INFO> is empty, ignore it.
14
+
15
  Identify if a slide describes a step-by-step/sequential process, then begin the bullet points with a special marker >>.
16
  Limit this to max two or three slides.
17
 
 
23
  In this slide, each line of text will begin with the name of a relevant icon enclosed between [[ and ]], e.g., [[machine-learning]] and [[fairness]].
24
  Insert icons only in this slide.
25
 
26
+ Your output, i.e., the content of each slide should be vert detailed and descriptive but not way too verbose.
27
+ Avoid writing like a report, but also avoid very short bullet points with just 3-4 words.
28
  Each bullet point should be detailed and explanatory, not just short phrases.
29
 
30
  ALWAYS add a concluding slide at the end, containing a list of the key takeaways and an optional call-to-action if relevant to the context.
31
+ Unless explicitly instructed with the topic, create 10 to 12 slides. You must never create more than 15 to 20 slides.
32
 
33
 
34
  ### Topic:
 
110
  }}
111
 
112
 
113
+ <ADDITIONAL_INFO>
114
+ {additional_info}
115
+ </ADDITIONAL_INFO>
116
+
117
+
118
  ### Output:
119
  ```json
langchain_templates/chat_prompts/refinement_template_v4_two_cols_img.txt CHANGED
@@ -8,6 +8,13 @@ Include main headings for each slide, detailed bullet points for each slide.
8
  Add relevant, detailed content to each slide. When relevant, add one or two EXAMPLES to illustrate the concept.
9
  For two or three important slides, generate the key message that those slides convey.
10
 
 
 
 
 
 
 
 
11
  Identify if a slide describes a step-by-step/sequential process, then begin the bullet points with a special marker >>. Limit this to max two or three slides.
12
  Also, add at least one slide with a double column layout by generating appropriate content based on the description in the JSON schema provided below.
13
  In addition, for each slide, add image keywords based on the content of the respective slides.
@@ -18,11 +25,12 @@ In this slide, each line of text will begin with the name of a relevant icon enc
18
  Insert icons only in this slide.
19
  Do not repeat any icons or the icons slide.
20
 
21
- Your output, i.e., the content of each slide should be VERBOSE, DESCRIPTIVE, and very DETAILED.
 
22
  Each bullet point should be detailed and explanatory, not just short phrases.
23
 
24
  ALWAYS add a concluding slide at the end, containing a list of the key takeaways and an optional call-to-action if relevant to the context.
25
- Unless explicitly specified in the instructions below, create 10 TO 12 SLIDES in total. You must never create more tha 15 slides.
26
 
27
 
28
  ### List of instructions:
@@ -108,5 +116,10 @@ The output must be only a valid and syntactically correct JSON adhering to the f
108
  }}
109
 
110
 
 
 
 
 
 
111
  ### Output:
112
  ```json
 
8
  Add relevant, detailed content to each slide. When relevant, add one or two EXAMPLES to illustrate the concept.
9
  For two or three important slides, generate the key message that those slides convey.
10
 
11
+ The <ADDITIONAL_INFO> may provide additional information. If available, you should incorporate them while making the slides.
12
+ Read this information carefully. Based on the contents provided, plan the structure of the presentation.
13
+ For example, if it's a paper, you can consider having Problem, Solution, Experiments, and Results, among other sections.
14
+ If it's a product brochure, you can have Features, Changes, Operating Conditions, and likewise relevant sections.
15
+ Similarly, decide for other content types. Then appropriately incorporate the contents into the relevant slides, presenting in a useful way.
16
+ If <ADDITIONAL_INFO> is empty, ignore it.
17
+
18
  Identify if a slide describes a step-by-step/sequential process, then begin the bullet points with a special marker >>. Limit this to max two or three slides.
19
  Also, add at least one slide with a double column layout by generating appropriate content based on the description in the JSON schema provided below.
20
  In addition, for each slide, add image keywords based on the content of the respective slides.
 
25
  Insert icons only in this slide.
26
  Do not repeat any icons or the icons slide.
27
 
28
+ Your output, i.e., the content of each slide should be vert detailed and descriptive but not way too verbose.
29
+ Avoid writing like a report, but also avoid very short bullet points with just 3-4 words.
30
  Each bullet point should be detailed and explanatory, not just short phrases.
31
 
32
  ALWAYS add a concluding slide at the end, containing a list of the key takeaways and an optional call-to-action if relevant to the context.
33
+ Unless explicitly instructed with the topic, create 10 to 12 slides. You must never create more than 15 to 20 slides.
34
 
35
 
36
  ### List of instructions:
 
116
  }}
117
 
118
 
119
+ <ADDITIONAL_INFO>
120
+ {additional_info}
121
+ </ADDITIONAL_INFO>
122
+
123
+
124
  ### Output:
125
  ```json
requirements.txt CHANGED
@@ -15,14 +15,15 @@ langchain-cohere==0.3.3
15
  langchain-together==0.3.0
16
  langchain-ollama==0.2.1
17
  langchain-openai==0.3.3
18
- streamlit~=1.38.0
19
 
20
  python-pptx~=1.0.2
21
  json5~=0.9.14
22
  requests~=2.32.3
 
23
 
24
  transformers>=4.48.0
25
- torch==2.4.0
26
 
27
  lxml~=4.9.3
28
  tqdm~=4.66.5
 
15
  langchain-together==0.3.0
16
  langchain-ollama==0.2.1
17
  langchain-openai==0.3.3
18
+ streamlit~=1.44.0
19
 
20
  python-pptx~=1.0.2
21
  json5~=0.9.14
22
  requests~=2.32.3
23
+ pypdf~=5.4.0
24
 
25
  transformers>=4.48.0
26
+ torch>=2.6.0
27
 
28
  lxml~=4.9.3
29
  tqdm~=4.66.5
strings.json CHANGED
@@ -25,15 +25,16 @@
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
- "Did you know that SlideDeck AI supports eight LLMs that are free to use and generate contents in different styles? Try them out from the dropdown list."
 
36
  ],
37
- "chat_placeholder": "Write the topic or instructions here",
38
  "like_feedback": "If you like SlideDeck AI, please consider leaving a heart ❤\uFE0F 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."
39
  }
 
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.\nAI-generated content may be incorrect. Please carefully review and verify the contents.",
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
+ "Did you know that SlideDeck AI supports eight LLMs that generate contents in different styles?",
36
+ "Did you know that SlideDeck AI can create a presentation based on any uploaded PDF file?"
37
  ],
38
+ "chat_placeholder": "Write the topic or instructions here. You can also upload a PDF file.",
39
  "like_feedback": "If you like SlideDeck AI, please consider leaving a heart ❤\uFE0F 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."
40
  }