barunsaha commited on
Commit
5621ff1
1 Parent(s): 3e24d2f

Add conversion of slides contents to JSON

Browse files
app.py CHANGED
@@ -1,51 +1,83 @@
 
 
1
  import streamlit as st
2
 
3
- import llm_agent
4
  from global_config import GlobalConfig
5
 
6
 
 
 
 
 
 
 
 
7
  def build_ui():
8
- st.write('''
9
- # Slides Wizard
10
-
11
- *Create your next presentation using AI*
12
- ''')
13
-
14
- name = st.text_input(
15
- f'''**Type in your name**''',
16
- value='John Doe'
17
- )
 
 
 
 
 
 
 
 
 
 
 
18
 
19
  topic = st.text_area(
20
  f'''**Describe the topic of the presentation.
21
  Avoid mentioning the count of slides.**''',
22
- value='''Make a presentation about AI. Talk about its pros, cons, and future prospects. '''
23
- '''Add examples of some real-life use cases in engineering and medicine.'''
24
  )
25
 
26
  audience = st.text_input(
27
  f'''**Briefly describe your target audience**''',
28
- value='I am a teacher and want to present these slides to college students'
29
  )
30
 
31
- if st.button('Generate slides'):
 
 
 
32
  progress_text = 'Generating your presentation slides...give it a moment'
33
  progress_bar = st.progress(0, text=progress_text)
34
 
35
- name_txt = name.strip()
36
  topic_txt = topic.strip()
37
  audience_txt = audience.strip()
38
 
39
- process_inputs(name_txt, topic_txt, audience_txt, progress_bar)
 
40
 
 
 
 
41
 
42
- def process_inputs(name: str, topic: str, audience: str, progress_bar):
43
- name_length = len(name)
 
 
 
 
 
 
44
  topic_length = len(topic)
45
  audience_length = len(audience)
46
- print(f'Input lengths:: name: {name_length}, topic: {topic_length}, audience: {audience_length}')
47
 
48
- if name_length > 0 and topic_length > 10 and audience_length > 5:
49
  print(
50
  f'Name: {name}\n'
51
  f'Topic: {topic}\n'
@@ -56,27 +88,72 @@ def process_inputs(name: str, topic: str, audience: str, progress_bar):
56
  target_length = min(topic_length, GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH)
57
 
58
  try:
59
- slides_content = llm_agent.generate_slides_content(name, topic[:target_length], audience)
60
 
61
  print('=' * 20)
62
  print(f'Slides content:\n{slides_content}')
63
  print('=' * 20)
64
  st.write(f'''Slides content:\n{slides_content}''')
65
  progress_bar.progress(100, text='Done!')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  except ValueError as ve:
67
  st.error(f'Unfortunately, an error occurred: {ve}! '
68
  f'Please change the text, try again later, or report it, sharing your inputs.')
69
 
70
- # image = generate_image_from_text(summary)
71
- # progress_bar.progress(100, text='Done!')
72
- #
73
- # st.image(image, caption=summary)
74
- # st.info('Tip: Right-click on the image to save it')
75
  else:
76
  st.error('Not enough information provided! Please be little more descriptive :)')
77
 
78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  def main():
 
 
 
 
80
  build_ui()
81
 
82
 
 
1
+ import json
2
+
3
  import streamlit as st
4
 
5
+ import llm_helper
6
  from global_config import GlobalConfig
7
 
8
 
9
+ UI_BUTTONS = [
10
+ 'Generate slides content',
11
+ 'Generate JSON',
12
+ 'Make the slides'
13
+ ]
14
+
15
+
16
  def build_ui():
17
+ """
18
+ Display the input elements for content generation. Only covers the first step.
19
+ """
20
+
21
+ st.title('Slides Wizard')
22
+ st.subheader('*:blue[Create your next slide deck using AI]*')
23
+ st.divider()
24
+
25
+ st.header('Step 1: Generate your content')
26
+ st.caption('Let\'s start by generating some contents for your slides')
27
+
28
+ # name = st.text_input(
29
+ # f'''**Type in your name**''',
30
+ # value='John Doe'
31
+ # )
32
+
33
+ try:
34
+ with open(GlobalConfig.PRELOAD_DATA_FILE, 'r') as in_file:
35
+ preload_data = json.loads(in_file.read())
36
+ except (FileExistsError, FileNotFoundError):
37
+ preload_data = {'topic': '', 'audience': ''}
38
 
39
  topic = st.text_area(
40
  f'''**Describe the topic of the presentation.
41
  Avoid mentioning the count of slides.**''',
42
+ value=preload_data['topic']
 
43
  )
44
 
45
  audience = st.text_input(
46
  f'''**Briefly describe your target audience**''',
47
+ value=preload_data['audience']
48
  )
49
 
50
+ # Button with callback function
51
+ st.button(UI_BUTTONS[0], on_click=button_clicked, args=[0])
52
+
53
+ if st.session_state.clicked[0]:
54
  progress_text = 'Generating your presentation slides...give it a moment'
55
  progress_bar = st.progress(0, text=progress_text)
56
 
57
+ # name_txt = name.strip()
58
  topic_txt = topic.strip()
59
  audience_txt = audience.strip()
60
 
61
+ process_topic_inputs('', topic_txt, audience_txt, progress_bar)
62
+
63
 
64
+ def process_topic_inputs(name: str, topic: str, audience: str, progress_bar):
65
+ """
66
+ Process the inputs to generate contents for the slides.
67
 
68
+ :param name: Name of the speaker
69
+ :param topic: The presentation topic
70
+ :param audience: Target audience description
71
+ :param progress_bar: Progress bar from the page
72
+ :return:
73
+ """
74
+
75
+ # name_length = len(name)
76
  topic_length = len(topic)
77
  audience_length = len(audience)
78
+ print(f'Input lengths:: topic: {topic_length}, audience: {audience_length}')
79
 
80
+ if topic_length > 10 and audience_length > 5:
81
  print(
82
  f'Name: {name}\n'
83
  f'Topic: {topic}\n'
 
88
  target_length = min(topic_length, GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH)
89
 
90
  try:
91
+ slides_content = llm_helper.generate_slides_content(name, topic[:target_length], audience)
92
 
93
  print('=' * 20)
94
  print(f'Slides content:\n{slides_content}')
95
  print('=' * 20)
96
  st.write(f'''Slides content:\n{slides_content}''')
97
  progress_bar.progress(100, text='Done!')
98
+
99
+ # Move on to step 2
100
+ st.divider()
101
+ st.header('Step 2: Make it structured')
102
+ st.caption('Let\'s now convert the above generated contents into JSON')
103
+
104
+ # Streamlit multiple buttons work in a weird way!
105
+ # Click on any button, the page just reloads!
106
+ # Buttons are not "stateful"
107
+ # https://blog.streamlit.io/10-most-common-explanations-on-the-streamlit-forum/#1-buttons-aren%E2%80%99t-stateful
108
+ # Apparently, "nested button click" needs to be handled differently
109
+ # https://playground.streamlit.app/?q=triple-button
110
+
111
+ st.button(UI_BUTTONS[1], on_click=button_clicked, args=[1])
112
+
113
+ if st.session_state.clicked[1]:
114
+ progress_text = 'Converting...give it a moment'
115
+ progress_bar = st.progress(0, text=progress_text)
116
+
117
+ process_slides_contents(slides_content, progress_bar)
118
  except ValueError as ve:
119
  st.error(f'Unfortunately, an error occurred: {ve}! '
120
  f'Please change the text, try again later, or report it, sharing your inputs.')
121
 
 
 
 
 
 
122
  else:
123
  st.error('Not enough information provided! Please be little more descriptive :)')
124
 
125
 
126
+ def process_slides_contents(text: str, progress_bar: st.progress):
127
+ """
128
+ Convert given content to JSON and display. Update the UI.
129
+
130
+ :param text: The contents generated for the slides
131
+ :param progress_bar: Progress bar for this step
132
+ """
133
+
134
+ print('JSON button clicked')
135
+ json_str = llm_helper.text_to_json(text)
136
+ print('=' * 20)
137
+ print(f'JSON:\n{json_str}')
138
+ print('=' * 20)
139
+ st.code(json_str, language='json')
140
+
141
+ progress_bar.progress(100, text='Done!')
142
+
143
+
144
+ def button_clicked(button):
145
+ """
146
+ Function to update the value in session state.
147
+ """
148
+
149
+ st.session_state.clicked[button] = True
150
+
151
+
152
  def main():
153
+ # Initialize the key in session state to manage the nested buttons states
154
+ if 'clicked' not in st.session_state:
155
+ st.session_state.clicked = {0: False, 1: False, 2: False}
156
+
157
  build_ui()
158
 
159
 
global_config.py CHANGED
@@ -12,11 +12,16 @@ class GlobalConfig:
12
  # Flan-T5
13
  # LLM_MODEL_NAME: str = 'google/flan-t5-xxl'
14
  LLM_MODEL_NAME = 'tiiuae/falcon-7b-instruct'
 
 
15
  LLM_MODEL_TEMPERATURE: float = 0.5
16
- LLM_MODEL_MIN_OUTPUT_LENGTH: int = 200
17
  LLM_MODEL_MAX_OUTPUT_LENGTH: int = 2000
18
  LLM_MODEL_MAX_INPUT_LENGTH: int = 1000
19
 
20
  # # Stable Diffusion
21
  # DIFFUSION_MODEL_NAME: str = 'stabilityai/stable-diffusion-2-1'
22
  # DIFFUSION_NUM_INFERENCE_STEPS: int = 3
 
 
 
 
12
  # Flan-T5
13
  # LLM_MODEL_NAME: str = 'google/flan-t5-xxl'
14
  LLM_MODEL_NAME = 'tiiuae/falcon-7b-instruct'
15
+ # LLM_MODEL_NAME = 'h2oai/h2ogpt-gm-oasst1-en-2048-falcon-7b-v2'
16
+ # LLM_MODEL_NAME = 'garage-bAInd/Platypus2-70B-instruct'
17
  LLM_MODEL_TEMPERATURE: float = 0.5
18
+ LLM_MODEL_MIN_OUTPUT_LENGTH: int = 50
19
  LLM_MODEL_MAX_OUTPUT_LENGTH: int = 2000
20
  LLM_MODEL_MAX_INPUT_LENGTH: int = 1000
21
 
22
  # # Stable Diffusion
23
  # DIFFUSION_MODEL_NAME: str = 'stabilityai/stable-diffusion-2-1'
24
  # DIFFUSION_NUM_INFERENCE_STEPS: int = 3
25
+
26
+ PRELOAD_DATA_FILE = 'examples/example_02.json'
27
+ SLIDES_TEMPLATE_FILE = 'langchain_templates/template_07.txt'
langchain_templates/template_07.txt CHANGED
@@ -1,4 +1,4 @@
1
- You are a helpful, intelligent chatbot. Create the slides for a presentation on the given topic. Include main headings for each slide, detailed bullet points for each slide. Add relevant content to each slide.
2
 
3
 
4
  Topic:
@@ -7,6 +7,3 @@ Topic:
7
 
8
  Target audience:
9
  ```{audience}```
10
-
11
-
12
- Finally, generate an engaging title for the presentation aimed for the target audience.
 
1
+ You are a helpful, intelligent chatbot. Create the slides for a presentation on the given topic. Include main headings for each slide, detailed bullet points for each slide. Add relevant content to each slide. Also, generate an engaging title for the presentation aimed for the target audience.
2
 
3
 
4
  Topic:
 
7
 
8
  Target audience:
9
  ```{audience}```
 
 
 
llm_agent.py DELETED
@@ -1,89 +0,0 @@
1
- from langchain import HuggingFaceHub, PromptTemplate
2
-
3
- from global_config import GlobalConfig
4
-
5
-
6
- llm = HuggingFaceHub(
7
- repo_id=GlobalConfig.LLM_MODEL_NAME,
8
- task='text-generation',
9
- huggingfacehub_api_token=GlobalConfig.HUGGINGFACEHUB_API_TOKEN,
10
- model_kwargs={
11
- 'temperature': GlobalConfig.LLM_MODEL_TEMPERATURE,
12
- 'min_length': GlobalConfig.LLM_MODEL_MIN_OUTPUT_LENGTH,
13
- 'max_length': GlobalConfig.LLM_MODEL_MAX_OUTPUT_LENGTH,
14
- 'max_new_tokens': GlobalConfig.LLM_MODEL_MAX_OUTPUT_LENGTH,
15
- 'num_return_sequences': 1
16
- }
17
- )
18
- print(llm)
19
-
20
- #
21
- # so that the speaker can deliver an engaging talk to the target audience. You use facts in your slides. When necessary,
22
- # you also look up online for further details
23
-
24
- template = '''
25
- You are an artificial intelligence assistant.
26
- You are experienced in creating slides for a presentation on any given topic.
27
- Generate a slide deck based on the following information:
28
-
29
-
30
- Topic:
31
- ```{topic}```
32
-
33
-
34
- Target audience:
35
- ```{audience}```
36
-
37
-
38
- Generate an engaging title for the presentation and output it as the first line.
39
- The next line should contain the speaker's name.
40
- Add a title and number to each slide on a separate line.
41
- Each slide should have a bulleted list of items to talk about.
42
- '''
43
-
44
-
45
- template2 = '''
46
- Act like a professional speaker and expert in PowerPoint: Create the outline for a PowerPoint presentation on
47
- any given topic. Include main headings for each slide, detailed bullet points for each slide,
48
- ideas for photos for each slide and an impactful intro and closing slide.
49
-
50
-
51
- Speaker's name:
52
- ```{name}```
53
-
54
-
55
- Topic:
56
- ```{topic}```
57
-
58
-
59
- Target audience:
60
- ```{audience}```
61
-
62
-
63
- Generate an engaging title for the presentation and output it in the first line.
64
- The next line should contain the speaker's name.
65
- Add a title and number to each slide on a separate line.
66
- '''
67
-
68
- # The contents of the slides should be in plain-text in the form of bulleted list items.
69
-
70
- with open('langchain_templates/template_07.txt', 'r') as in_file:
71
- template_txt = in_file.read().strip()
72
-
73
- # prompt = PromptTemplate.from_template(template)
74
- prompt = PromptTemplate.from_template(template_txt)
75
-
76
-
77
- def generate_slides_content(name: str, topic: str, audience: str) -> str:
78
- """
79
- Generate the contents of slides for a presentation on a given topic.
80
-
81
- :return: The summary
82
- """
83
-
84
- formatted_prompt = prompt.format(topic=topic, audience=audience)
85
- print(formatted_prompt)
86
-
87
- slides_content = llm(formatted_prompt)
88
-
89
- return slides_content
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
llm_helper.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain import HuggingFaceHub, PromptTemplate
2
+
3
+ from global_config import GlobalConfig
4
+
5
+
6
+ prompt = None
7
+
8
+
9
+ def get_llm() -> HuggingFaceHub:
10
+ llm = HuggingFaceHub(
11
+ repo_id=GlobalConfig.LLM_MODEL_NAME,
12
+ task='text-generation',
13
+ huggingfacehub_api_token=GlobalConfig.HUGGINGFACEHUB_API_TOKEN,
14
+ model_kwargs={
15
+ 'temperature': GlobalConfig.LLM_MODEL_TEMPERATURE,
16
+ 'min_length': GlobalConfig.LLM_MODEL_MIN_OUTPUT_LENGTH,
17
+ 'max_length': GlobalConfig.LLM_MODEL_MAX_OUTPUT_LENGTH,
18
+ 'max_new_tokens': GlobalConfig.LLM_MODEL_MAX_OUTPUT_LENGTH,
19
+ 'num_return_sequences': 1
20
+ }
21
+ )
22
+ print(llm)
23
+
24
+ return llm
25
+
26
+
27
+ def generate_slides_content(name: str, topic: str, audience: str) -> str:
28
+ """
29
+ Generate the outline/contents of slides for a presentation on a given topic.
30
+
31
+ :return: The content
32
+ """
33
+
34
+ global prompt
35
+
36
+ if not prompt:
37
+ with open(GlobalConfig.SLIDES_TEMPLATE_FILE, 'r') as in_file:
38
+ template_txt = in_file.read().strip()
39
+
40
+ prompt = PromptTemplate.from_template(template_txt)
41
+
42
+ formatted_prompt = prompt.format(topic=topic, audience=audience)
43
+ # print(formatted_prompt)
44
+
45
+ llm = get_llm()
46
+ slides_content = llm(formatted_prompt, verbose=True)
47
+
48
+ return slides_content
49
+
50
+
51
+ def text_to_json(content: str) -> str:
52
+ """
53
+ Convert input text into structured JSON representation.
54
+
55
+ :param content: Input text
56
+ :return: JSON string
57
+ """
58
+
59
+ # f-string is not used in order to prevent interpreting the brackets
60
+ text = '''
61
+ Context:
62
+
63
+
64
+ '''
65
+ text += content
66
+ text += '''
67
+
68
+
69
+ Convert the above text into structured JSON output. The JSON structure should be something like this:
70
+ {
71
+ "presentation_title": "...",
72
+ "slides": [
73
+ {
74
+ "slide_number": "...",
75
+ "slide_heading": "...",
76
+ "slide_contents": [
77
+ "...",
78
+ "...",
79
+ ],
80
+ },
81
+ {
82
+ ...
83
+ },
84
+ ]
85
+ }
86
+ '''
87
+
88
+ llm = get_llm()
89
+ output = llm(text, verbose=True)
90
+ output = output.strip()
91
+
92
+ first_index = max(0, output.find('{'))
93
+ last_index = min(output.rfind('}'), len(output))
94
+ output = output[first_index: last_index + 1]
95
+
96
+ return output
requirements.txt CHANGED
@@ -1,4 +1,4 @@
1
- python-dotenv[cli]
2
- langchain
3
  huggingface_hub
4
- streamlit
 
1
+ python-dotenv[cli]~=1.0.0
2
+ langchain~=0.0.268
3
  huggingface_hub
4
+ streamlit~=1.25.0