forked from crvernon/paige
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
999 lines (840 loc) · 37.3 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
import io
import os
import importlib
from docxtpl import DocxTemplate
from pptx import Presentation
from pptx.util import Pt
from pptx.enum.text import PP_ALIGN
from openai import OpenAI
import streamlit as st
import highlight as hlt
if "client" not in st.session_state:
key = os.getenv("OPENAI_API_KEY", default=None)
if key is None:
raise KeyError((
"No key found for 'OPENAI_API_KEY' system variable. " +
"Obtain your OpenAI API key from the OpenAI website: https://platform.openai.com/api-keys"
))
else:
st.session_state.client = OpenAI(api_key=key)
if "reduce_document" not in st.session_state:
st.session_state.reduce_document = False
if "content_dict" not in st.session_state:
st.session_state.content_dict = {}
# parameters for word document
if "title_response" not in st.session_state:
st.session_state.title_response = None
if "subtitle_response" not in st.session_state:
st.session_state.subtitle_response = None
if "photo" not in st.session_state:
st.session_state.photo = None
if "photo_link" not in st.session_state:
st.session_state.photo_link = None
if "photo_site_name" not in st.session_state:
st.session_state.photo_site_name = None
if "image_caption" not in st.session_state:
st.session_state.image_caption = None
if "science_response" not in st.session_state:
st.session_state.science_response = None
if "impact_response" not in st.session_state:
st.session_state.impact_response = None
if "summary_response" not in st.session_state:
st.session_state.summary_response = None
if "funding" not in st.session_state:
st.session_state.funding = None
if "citation" not in st.session_state:
st.session_state.citation = None
if "related_links" not in st.session_state:
st.session_state.related_links = None
# additional word doc content that is not in the template
if "figure_response" not in st.session_state:
st.session_state.figure_response = None
if "figure_caption" not in st.session_state:
st.session_state.figure_caption = None
if "caption_response" not in st.session_state:
st.session_state.caption_response = None
if "output_file" not in st.session_state:
st.session_state.output_file = None
# parameters for the ppt slide
if "objective_response" not in st.session_state:
st.session_state.objective_response = None
if "approach_response" not in st.session_state:
st.session_state.approach_response = None
if "ppt_impact_response" not in st.session_state:
st.session_state.ppt_impact_response = None
if "figure_recommendation" not in st.session_state:
st.session_state.figure_recommendation = None
if "citation" not in st.session_state:
st.session_state.citation = None
if "search_phrase" not in st.session_state:
st.session_state.search_phrase = None
if "point_of_contact" not in st.session_state:
st.session_state.point_of_contact = None
if "project_dict" not in st.session_state:
st.session_state.project_dict = {
"IM3": "Jennie Rice\nIM3 Principal Investigator\[email protected]",
"GCIMS": "Marshall Wise\nGCIMS Principal Investigator\[email protected]",
"COMPASS-GLM": "Robert Hetland\nCOMPASS-GLM Principal Investigator\[email protected]",
"ICoM": "Ian Kraucunas\nICoM Principal Investigator\[email protected]",
"Puget Sound": "Ning Sun\nPuget Sound Scoping and Pilot Study Principal Investigator\[email protected]",
"Other": "First and Last Name\nCorresponding Project Name with POC Credentials\nEmail Address",
}
# Force responsive layout for columns also on mobile
st.write(
"""<style>
[data-testid="column"] {
width: calc(50% - 1rem);
flex: 1 1 calc(50% - 1rem);
min-width: calc(50% - 1rem);
}
</style>""",
unsafe_allow_html=True,
)
# Render streamlit page
st.title("Research Highlight Generator")
st.markdown((
"This app uses a Large Language Model (LLM) of your choosing to generate " +
" formatted research highlight content from an input file."
))
st.session_state.model = st.selectbox(
label="Select your model:",
options=("gpt-4o", "gpt-4", "gpt-3.5-turbo-16k", "gpt-3.5-turbo")
)
if st.session_state.model == "gpt-4-32k":
st.session_state.max_allowable_tokens = 32768
elif st.session_state.model == "gpt-4":
st.session_state.max_allowable_tokens = 8192
elif st.session_state.model == "gpt-3.5-turbo-16k":
st.session_state.max_allowable_tokens = 16384
elif st.session_state.model == "gpt-3.5-turbo":
st.session_state.max_allowable_tokens = 4096
elif st.session_state.model == "gpt-4o":
st.session_state.max_allowable_tokens = 150000
# set api key
st.markdown("### Upload file to process:")
uploaded_file = st.file_uploader(
label="### Select PDF or text file to upload",
type=["pdf", "txt"],
help="Select PDF or text file to upload",
)
if uploaded_file is not None:
if uploaded_file.type == "text/plain":
content_dict = hlt.read_text(uploaded_file)
elif uploaded_file.type == "application/pdf":
content_dict = hlt.read_pdf(uploaded_file)
st.session_state.output_file = uploaded_file.name
st.code(f"""File specs:\n
- Number of pages: {content_dict['n_pages']}
- Number of characters: {content_dict['n_characters']}
- Number of words: {content_dict['n_words']}
- Number of tokens: {content_dict['n_tokens']}
""")
if content_dict['n_tokens'] > st.session_state.max_allowable_tokens:
msg = f"""
The number of tokens in your document exceeds the maximum allowable tokens.
This will cause your queries to fail.
The queries account for the number of tokens in a prompt + the number of tokens in your document.
Maximum allowable token count: {st.session_state.max_allowable_tokens}
Your documents token count: {content_dict['n_tokens']}
Token deficit: {content_dict['n_tokens'] - st.session_state.max_allowable_tokens}
"""
st.error(msg, icon="🚨")
st.session_state.reduce_document = st.radio(
"""Would you like me to attempt to reduce the size of
your document by keeping only relevant information?
If so, I will give you a file to download with the content
so you only have to do this once.
If you choose to go through with this, it may take a while
to process, usually on the order of 15 minutes for a 20K token
document.
Alternatively, you can copy and paste the contents that you
know are of interest into a text file and upload that
instead.
""",
("Yes", "No"),
)
# word document content
st.markdown("### Content to fill in Word document template:")
# title section
title_container = st.container()
title_container.markdown("##### Generate title from text content")
# title criteria
title_container.markdown("""
The title should meet the following criteria:
- No colons are allowed in the output.
- Should pique the interest of the reader while still being somewhat descriptive.
- Be understandable to a general audience.
- Should be only once sentence.
- Should have a maximum length of 10 words.
""")
title_container.markdown("Set desired temperature:")
# title slider
title_temperature = title_container.slider(
"Title Temperature",
0.0,
1.0,
0.2,
label_visibility="collapsed"
)
# build container content
if title_container.button('Generate Title'):
st.session_state.title_response = hlt.generate_content(
client=st.session_state.client,
container=title_container,
content=content_dict["content"],
prompt_name="title",
result_title="Title Result:",
max_tokens=50,
temperature=title_temperature,
box_height=50,
max_allowable_tokens=st.session_state.max_allowable_tokens,
model=st.session_state.model
)
else:
if st.session_state.title_response is not None:
title_container.markdown("Title Result:")
title_container.text_area(
label="Title Result:",
value=st.session_state.title_response,
label_visibility="collapsed",
height=50
)
# subtitle section
subtitle_container = st.container()
subtitle_container.markdown("##### Generate subtitle from text content")
# subtitle criteria
subtitle_container.markdown("""
The subtitle should meet the following criteria:
- Be an extension of and related to, but not directly quote, the title.
- Provide information that will make the audience want to find out more about the research.
- Do not use more than 155 characters including spaces.
""")
subtitle_container.markdown("Set desired temperature:")
# subtitle slider
subtitle_temperature = subtitle_container.slider(
"Subtitle Temperature",
0.0,
1.0,
0.5,
label_visibility="collapsed"
)
# build container content
if subtitle_container.button('Generate Subtitle'):
if st.session_state.title_response is None:
st.write("Please generate a Title first. Subtitle generation considers the title response.")
else:
st.session_state.subtitle_response = hlt.generate_content(
client=st.session_state.client,
container=subtitle_container,
content=content_dict["content"],
prompt_name="subtitle",
result_title="Subtitle Result:",
max_tokens=100,
temperature=subtitle_temperature,
box_height=50,
additional_content=st.session_state.title_response,
max_word_count=100,
min_word_count=75,
max_allowable_tokens=st.session_state.max_allowable_tokens,
model=st.session_state.model
)
else:
if st.session_state.subtitle_response is not None:
subtitle_container.markdown("Subtitle Result:")
subtitle_container.text_area(
label="Subtitle Result:",
value=st.session_state.subtitle_response,
label_visibility="collapsed",
height=50
)
# science section
science_container = st.container()
science_container.markdown("##### Generate science summary from text content")
# science criteria
science_container.markdown("""
**GOAL**: Describe the scientific results for a non-expert, non-scientist audience.
The description should meet the following criteria:
- Answer what the big challenge in this field of science is that the research addresses.
- State what the key finding is.
- Explain the science, not the process.
- Be understandable to a high school senior or college freshman.
- Use short sentences and succinct words.
- Avoid technical terms if possible. If technical terms are necessary, define them.
- Provide the necessary context so someone can have a very basic understanding of what you did.
- Start with topics that the reader already may know and move on to more complex ideas.
- Use present tense.
- In general, the description should speak about the research or researchers in first person.
- Use a minimum of 75 words and a maximum of 100 words.
""")
science_container.markdown("Set desired temperature:")
# slider
science_temperature = science_container.slider(
"Science Summary Temperature",
0.0,
1.0,
0.3,
label_visibility="collapsed"
)
# build container content
if science_container.button('Generate Science Summary'):
st.session_state.science_response = hlt.generate_content(
client=st.session_state.client,
container=science_container,
content=content_dict["content"],
prompt_name="science",
result_title="Science Summary Result:",
max_tokens=200,
temperature=science_temperature,
box_height=250,
max_word_count=100,
min_word_count=75,
max_allowable_tokens=st.session_state.max_allowable_tokens,
model=st.session_state.model
)
else:
if st.session_state.science_response is not None:
science_container.markdown("Science Summary Result:")
science_container.text_area(
label="Science Summary Result:",
value=st.session_state.science_response,
label_visibility="collapsed",
height=250
)
# impact section
impact_container = st.container()
impact_container.markdown("##### Generate impact summary from text content")
impact_container.markdown("""
**GOAL**: Describe the impact of the research to a non-expert, non-scientist audience.
The description should meet the following criteria:
- Answer why the findings presented are important, i.e., what problem the research is trying to solve.
- Answer if the finding is the first of its kind.
- Answer what was innovative or distinct about the research.
- Answer what the research enables other scientists in your field to do next.
- Include other scientific fields potentially impacted.
- Be understandable to a high school senior or college freshman.
- Use short sentences and succinct words.
- Avoid technical terms if possible. If technical terms are necessary, define them.
- Use present tense.
- In general, the description should speak about the research or researchers in first person.
- Use a minimum of 75 words and a maximum of 100 words.
""")
impact_container.markdown("Set desired temperature:")
# slider
impact_temperature = impact_container.slider(
"Impact Summary Temperature",
0.0,
1.0,
0.0,
label_visibility="collapsed"
)
# build container content
if impact_container.button('Generate Impact Summary'):
st.session_state.impact_response = hlt.generate_content(
client=st.session_state.client,
container=impact_container,
content=content_dict["content"],
prompt_name="impact",
result_title="Impact Summary Result:",
max_tokens=700,
temperature=impact_temperature,
box_height=250,
max_word_count=100,
min_word_count=75,
max_allowable_tokens=st.session_state.max_allowable_tokens,
model=st.session_state.model
)
else:
if st.session_state.impact_response is not None:
impact_container.markdown("Impact Summary Result:")
impact_container.text_area(
label="Impact Summary Result:",
value=st.session_state.impact_response,
label_visibility="collapsed",
height=250
)
# general summary section
summary_container = st.container()
summary_container.markdown("##### Generate general summary from text content")
summary_container.markdown("""
**GOAL**: Generate a general summary of the current research.
The summary should meet the following criteria:
- Should relay key findings and value.
- The summary should be still accessible to the non-specialist but may be more technical if necessary.
- Do not mention the names of institutions.
- If there is a United States Department of Energy Office of Science user facility involved, such as NERSC, you can mention the user facility.
- Should be 1 or 2 paragraphs detailing the research.
- Use present tense.
- In general, the description should speak about the research or researchers in first person.
- Use no more than 200 words.
""")
summary_container.markdown("Set desired temperature:")
# slider
summary_temperature = summary_container.slider(
"General Summary Temperature",
0.0,
1.0,
0.3,
label_visibility="collapsed"
)
# build container content
if summary_container.button('Generate General Summary'):
st.session_state.summary_response = hlt.generate_content(
client=st.session_state.client,
container=summary_container,
content=content_dict["content"],
prompt_name="summary",
result_title="General Summary Result:",
max_tokens=700,
temperature=summary_temperature,
box_height=400,
max_word_count=200,
min_word_count=100,
max_allowable_tokens=st.session_state.max_allowable_tokens,
model=st.session_state.model
)
else:
if st.session_state.summary_response is not None:
summary_container.markdown("General Summary Result:")
summary_container.text_area(
label="General Summary Result:",
value=st.session_state.summary_response,
label_visibility="collapsed",
height=400
)
# figure recommendations section
figure_container = st.container()
figure_container.markdown("##### Generate figure search string recommendations from the general summary")
figure_container.markdown("Set desired temperature:")
# slider
figure_temperature = figure_container.slider(
"Figure Recommendations Temperature",
0.0,
1.0,
0.9,
label_visibility="collapsed"
)
# build container content
if figure_container.button('Generate Figure Recommendations'):
if st.session_state.summary_response is None:
st.write("Please generate a general summary first.")
else:
st.session_state.figure_response = hlt.generate_content(
client=st.session_state.client,
container=figure_container,
content=st.session_state.summary_response,
prompt_name="figure",
result_title="Figure Recommendations Result:",
max_tokens=200,
temperature=figure_temperature,
box_height=200,
max_allowable_tokens=st.session_state.max_allowable_tokens,
model=st.session_state.model
)
else:
if st.session_state.figure_response is not None:
figure_container.markdown("Figure Recommendations Result:")
figure_container.text_area(
label="Figure Recommendations Result:",
value=st.session_state.figure_response,
label_visibility="collapsed",
height=200
)
figure_summary_container = st.container()
figure_summary_container.markdown(
"##### Generate a figure caption that summarizes the work generally to use with the artistic photo above"
)
# slider
figure_summary_container.markdown("Set desired temperature:")
figure_summary_temperature = figure_summary_container.slider(
"Figure Caption Temperature",
0.0,
1.0,
0.1,
label_visibility="collapsed"
)
# build container content
if figure_summary_container.button('Generate Figure Caption'):
if st.session_state.summary_response is None:
st.write("Please generate a general summary first.")
else:
st.session_state.figure_caption = hlt.generate_content(
client=st.session_state.client,
container=figure_summary_container,
content=st.session_state.summary_response,
prompt_name="figure_caption",
result_title="Figure Caption Result:",
max_tokens=300,
temperature=figure_temperature,
box_height=200,
max_allowable_tokens=st.session_state.max_allowable_tokens,
model=st.session_state.model
).replace('"', "")
else:
if st.session_state.figure_caption is not None:
figure_container.markdown("Figure Caption Result:")
figure_container.text_area(
label="Figure Caption Result:",
value=st.session_state.figure_caption,
label_visibility="collapsed",
height=200
)
# citation recommendations section
citation_container = st.container()
citation_container.markdown("##### Citation for the paper in Chicago style")
if citation_container.button('Generate Citation'):
st.session_state.citation = hlt.generate_content(
client=st.session_state.client,
container=citation_container,
content=content_dict["content"],
prompt_name="citation",
result_title="",
max_tokens=300,
temperature=0.0,
box_height=200,
max_allowable_tokens=st.session_state.max_allowable_tokens,
model=st.session_state.model
).replace('"', "")
else:
if st.session_state.citation is not None:
citation_container.text_area(
label="Citation",
value=st.session_state.citation,
label_visibility="collapsed",
height=200
)
# funding recommendations section
funding_container = st.container()
funding_container.markdown("##### Funding statement from the paper")
if funding_container.button('Generate funding statement'):
st.session_state.funding = hlt.generate_content(
client=st.session_state.client,
container=funding_container,
content=content_dict["content"],
prompt_name="funding",
result_title="",
max_tokens=300,
temperature=0.0,
box_height=200,
max_allowable_tokens=st.session_state.max_allowable_tokens,
model=st.session_state.model
).replace('"', "")
else:
if st.session_state.funding is not None:
funding_container.text_area(
label="Funding statement",
value=st.session_state.funding,
label_visibility="collapsed",
height=200
)
# point of contact box
poc_container = st.container()
poc_container.markdown("##### Point of contact for the research by project")
# select the POC information from the dropdown
st.session_state.point_of_contact = st.session_state.project_dict[
poc_container.selectbox(
label="Select the project who funded the work:",
options=[
"COMPASS-GLM",
"GCIMS",
"ICoM",
"IM3",
"Puget Sound",
"Other",
]
)
]
poc_container.write("What will be written to the document as the point of contact:")
poc_parts = st.session_state.point_of_contact.split("\n")
poc_container.success(
f"""
{poc_parts[0]}\n
{poc_parts[1]}\n
{poc_parts[2]}\n
"""
)
export_container = st.container()
export_container.markdown("##### Export Word document with new content when ready")
# template parameters
word_parameters = {
'title': st.session_state.title_response,
'subtitle': st.session_state.subtitle_response,
'photo': st.session_state.photo,
'photo_link': st.session_state.photo_link,
'photo_site_name': st.session_state.photo_site_name,
'image_caption': st.session_state.figure_caption,
'science': st.session_state.science_response,
'impact': st.session_state.impact_response,
'summary': st.session_state.summary_response,
'funding': st.session_state.funding,
'citation': st.session_state.citation,
'related_links': st.session_state.related_links,
'point_of_contact': st.session_state.point_of_contact,
}
# template word document
word_template_file = importlib.resources.files('highlight.data').joinpath('highlight_template.docx')
template = DocxTemplate(word_template_file)
template.render(word_parameters)
bio = io.BytesIO()
template.save(bio)
if template:
export_container.download_button(
label="Export Word Document",
data=bio.getvalue(),
file_name="modified_template.docx",
mime="docx"
)
# power point slide content
st.markdown("### Content to fill in PowerPoint template:")
# objective section
objective_container = st.container()
objective_container.markdown("##### Generate objective summary from text content")
objective_container.markdown("""
**GOAL**: Generate one sentence stating the core purpose of the study.
The sentence should meet the following criteria:
- Use active verbs for the start of each point.
- Use present tense.
- Do not include methodology related to statistical, technological, and theory based
""")
objective_container.markdown("Set desired temperature:")
# slider
objective_temperature = objective_container.slider(
"Objective Temperature",
0.0,
1.0,
0.3,
label_visibility="collapsed"
)
# build container content
if objective_container.button('Generate Objective'):
st.session_state.objective_response = hlt.generate_content(
client=st.session_state.client,
container=objective_container,
content=content_dict["content"],
prompt_name="objective",
result_title="Objective Result:",
max_tokens=300,
temperature=objective_temperature,
box_height=250,
max_allowable_tokens=st.session_state.max_allowable_tokens,
model=st.session_state.model
)
else:
if st.session_state.objective_response is not None:
objective_container.markdown("Objective Result:")
objective_container.text_area(
label="Objective Result:",
value=st.session_state.objective_response,
label_visibility="collapsed",
height=250
)
# approach section
approach_container = st.container()
approach_container.markdown("##### Generate approach summary from text content")
approach_container.markdown("""
**GOAL**: Clearly and concisely state in 2-3 short points how this work accomplished the stated objective from a methodolgocial perspecive.
- Based off of the objective summary
- Only include methodology including but not limited to: statistical, technological, and theory based approaches.
- Use a different action verb to start sentences than what is used to begin the objective statement.
- Use active verbs for the start of each point.
- Use present tense.
""")
approach_container.markdown("Set desired temperature:")
# slider
approach_temperature = approach_container.slider(
"Approach Temperature",
0.0,
1.0,
0.1,
label_visibility="collapsed"
)
# build container content
if approach_container.button('Generate Approach'):
st.session_state.approach_response = hlt.generate_content(
client=st.session_state.client,
container=approach_container,
content=content_dict["content"],
prompt_name="approach",
result_title="Approach Result:",
max_tokens=300,
temperature=approach_temperature,
box_height=250,
additional_content=st.session_state.objective_response,
max_allowable_tokens=st.session_state.max_allowable_tokens,
model=st.session_state.model
)
else:
if st.session_state.approach_response is not None:
approach_container.markdown("Approach Result:")
approach_container.text_area(
label="Approach Result:",
value=st.session_state.approach_response,
label_visibility="collapsed",
height=250
)
# power point impact section
ppt_impact_container = st.container()
ppt_impact_container.markdown("##### Generate impact points from text content")
ppt_impact_container.markdown("""
**GOAL**: Clearly and concisely state in 3 points the key results and outcomes from this research.
- State what the results indicate.
- Include results that may be considered profound or surprising.
- Each point should be 1 concise sentence.
- Use present tense.
"""
)
ppt_impact_container.markdown("Set desired temperature:")
# slider
ppt_impact_temperature = ppt_impact_container.slider(
"Impact Points Temperature",
0.0,
1.0,
0.1,
label_visibility="collapsed"
)
# build container content
if ppt_impact_container.button('Generate Impact Points'):
st.session_state.ppt_impact_response = hlt.generate_content(
client=st.session_state.client,
container=ppt_impact_container,
content=content_dict["content"],
prompt_name="ppt_impact",
result_title="Impact Points Result:",
max_tokens=300,
temperature=ppt_impact_temperature,
box_height=250,
max_allowable_tokens=st.session_state.max_allowable_tokens,
model=st.session_state.model
)
else:
if st.session_state.ppt_impact_response is not None:
ppt_impact_container.markdown("Impact Points Result:")
ppt_impact_container.text_area(
label="Impact Points Result:",
value=st.session_state.ppt_impact_response,
label_visibility="collapsed",
height=250
)
# power point figure selection section
ppt_figure_selection = st.container()
ppt_figure_selection.markdown("##### Select a representative figure from the paper")
ppt_figure_selection.markdown("""
**GOAL**: What figure best represents the high impact content that can be easily understood by a non-technical, non-scientifc audience.
Limit the response to:
1. The figure name as it is written in the text,
2. An explanation of why it was chosen,
3. And what the figure is about in less than 50 words.
""")
ppt_figure_selection.markdown("Set desired temperature:")
# slider
ppt_figure_selection_temperature = ppt_figure_selection.slider(
"Figure recommendation Temperature",
0.0,
1.0,
0.2,
label_visibility="collapsed"
)
# build container content
if ppt_figure_selection.button('Generate Figure Recommendation'):
st.session_state.figure_recommendation = hlt.generate_content(
client=st.session_state.client,
container=ppt_figure_selection,
content=content_dict["content"],
prompt_name="figure_choice",
result_title="Figure Recommendation Result:",
max_tokens=300,
temperature=ppt_figure_selection_temperature,
box_height=250,
max_allowable_tokens=st.session_state.max_allowable_tokens,
model=st.session_state.model
)
else:
if st.session_state.figure_recommendation is not None:
ppt_figure_selection.markdown("Figure Recommendation Result:")
ppt_figure_selection.text_area(
label="Figure Recommendation Result:",
value=st.session_state.figure_recommendation,
label_visibility="collapsed",
height=250
)
# Add PowerPoint export container at the end
export_ppt_container = st.container()
export_ppt_container.markdown("##### Export PowerPoint Presentation with New Content")
if ("title_response" in st.session_state and
"objective_response" in st.session_state and
"ppt_impact_response" in st.session_state and
"approach_response" in st.session_state):
if export_ppt_container.button('Export PowerPoint'):
try:
# Load the PowerPoint template
ppt_template_file = importlib.resources.files('highlight.data').joinpath('highlight_template.pptx')
prs = Presentation(ppt_template_file)
# Split the impact and approach responses into bullet points (assuming they are separated by newlines)
impact_points = st.session_state.ppt_impact_response.split("\n")
approach_points = st.session_state.approach_response.split("\n")
# Iterate over all slides to find the text boxes labeled "impact_0", "impact_1", "impact_2"
for slide in prs.slides:
for shape in slide.shapes:
if shape.has_text_frame:
# Handle title insertion and maintain font size and bold
if "title" in shape.text_frame.text:
shape.text_frame.text = st.session_state.title_response
# Ensure font size and bold settings are maintained for each paragraph
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(24) # Example size, adjust as needed
run.font.bold = True # Maintain bold
run.alignment = PP_ALIGN.LEFT # Align title
# Handle citation insertion and maintain font size and bold
if "citation" in shape.text_frame.text:
shape.text_frame.text = st.session_state.citation
# Ensure font size and bold settings are maintained for each paragraph
for paragraph in shape.text_frame.paragraphs:
for run in paragraph.runs:
run.font.size = Pt(11) # Example size for citation, adjust as needed
run.font.bold = False # Citation typically isn't bold
run.alignment = PP_ALIGN.LEFT # Align citation
if shape.text_frame.text == "objective_0":
# Set the text of the text box to the objective response
shape.text_frame.text = st.session_state.objective_response
# Optional: Adjust font size and alignment for the objective
for paragraph in shape.text_frame.paragraphs:
paragraph.font.size = Pt(13) # Set font size
paragraph.alignment = PP_ALIGN.LEFT # Set alignment
# Handle approach bullet points
if "approach_0" in shape.text_frame.text:
# Clear existing paragraphs in the text frame
shape.text_frame.clear()
# Add bullet points for approach
for i, approach_point in enumerate(approach_points[:3]): # Only take the first 3 approach points
p = shape.text_frame.add_paragraph()
p.text = approach_point
p.level = 0 # This sets it as a bullet point
p.font.size = Pt(13) # Adjust bullet point font size
p.alignment = PP_ALIGN.LEFT # Align bullet points
# Handle the impact bullet points in the same text box
if "impact_0" in shape.text_frame.text:
# Clear the existing paragraphs
shape.text_frame.clear()
# Add bullet points for impact
for i, impact_point in enumerate(impact_points[:3]): # Only take the first 3 impact points
p = shape.text_frame.add_paragraph()
p.text = impact_point
p.level = 0 # This sets it as a bullet point
p.font.size = Pt(13) # Adjust bullet point font size
p.alignment = PP_ALIGN.LEFT # Align bullet points
# Save the modified presentation to a BytesIO object
ppt_io = io.BytesIO()
prs.save(ppt_io)
ppt_io.seek(0)
# Provide a download button for the user
export_ppt_container.download_button(
label="Export PowerPoint Presentation",
data=ppt_io,
file_name="modified_highlight_template.pptx",
mime="application/vnd.openxmlformats-officedocument.presentationml.presentation"
)
export_ppt_container.success("PowerPoint presentation generated successfully!", icon="✅")
except Exception as e:
export_ppt_container.error(f"An error occurred while generating the PowerPoint: {e}", icon="🚨")
else:
export_ppt_container.error("Please generate the objective and impact responses before exporting.", icon="⚠️")