Spaces:
Runtime error
Runtime error
Fix problems with prior commits
Browse filesFiles changed (7)
276 unmodified lines277278279280281282283284285286287288289290291292293294295296276 unmodified linesslides_template=pptx_template,output_file_path=path)_display_download_button(path)except ValueError as ve:st.error(f"{APP_TEXT['json_parsing_error']}"f"\n\nAdditional error info: {ve}"f"\n\nHere are some sample instructions that you could try to possibly fix this error;"f" if these don't work, try rephrasing or refreshing:"f"\n\n""- Regenerate content and fix the JSON error.""\n- Regenerate content and fix the JSON error. Quotes inside quotes should be escaped.")logger.error('%s', APP_TEXT['json_parsing_error'])logger.error('Additional error info: %s', str(ve))except Exception as ex:st.error(APP_TEXT['content_generation_error'])logger.error('Caught a generic exception: %s', str(ex))276 unmodified lines277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308276 unmodified linesslides_template=pptx_template,output_file_path=path)_display_download_button(path)except ValueError:# st.error(# f"{APP_TEXT['json_parsing_error']}"# f"\n\nAdditional error info: {ve}"# f"\n\nHere are some sample instructions that you could try to possibly fix this error;"# f" if these don't work, try rephrasing or refreshing:"# f"\n\n"# "- Regenerate content and fix the JSON error."# "\n- Regenerate content and fix the JSON error. Quotes inside quotes should be escaped."# )# logger.error('%s', APP_TEXT['json_parsing_error'])# logger.error('Additional error info: %s', str(ve))st.info('Encountered error while parsing JSON...will fix it and retry')logger.debug('Caught ValueError: trying again after repairing JSON...')pptx_helper.generate_powerpoint_presentation(text_helper.fix_malformed_json(json_str),slides_template=pptx_template,output_file_path=path)_display_download_button(path)except Exception as ex:st.error(APP_TEXT['content_generation_error'])logger.error('Caught a generic exception: %s', str(ex))
23 unmodified lines24252627282930313233343523 unmodified linesAPP_STRINGS_FILE = 'strings.json'PRELOAD_DATA_FILE = 'examples/example_02.json'SLIDES_TEMPLATE_FILE = 'langchain_templates/template_combined.txt'JSON_TEMPLATE_FILE = 'langchain_templates/text_to_json_template_02.txt'INITIAL_PROMPT_TEMPLATE = 'langchain_templates/chat_prompts/initial_template.txt'REFINEMENT_PROMPT_TEMPLATE = 'langchain_templates/chat_prompts/refinement_template.txt'PPTX_TEMPLATE_FILES = {'Blank': {'file': 'pptx_templates/Blank.pptx','caption': 'A good start'},23 unmodified lines24252627282930313233343523 unmodified linesAPP_STRINGS_FILE = 'strings.json'PRELOAD_DATA_FILE = 'examples/example_02.json'SLIDES_TEMPLATE_FILE = 'langchain_templates/template_combined.txt'# JSON_TEMPLATE_FILE = 'langchain_templates/text_to_json_template_02.txt'INITIAL_PROMPT_TEMPLATE = 'langchain_templates/chat_prompts/initial_template_v2_steps.txt'REFINEMENT_PROMPT_TEMPLATE = 'langchain_templates/chat_prompts/refinement_template_v2_steps.txt'PPTX_TEMPLATE_FILES = {'Basic': {'file': 'pptx_templates/Blank.pptx','caption': 'A good start'},
6 unmodified lines7891011121314151652 unmodified lines69707172737416 unmodified lines91929394959697989910010110210310410510610710810911011111211311427 unmodified lines1421431441451461471481491501511521531541551561571581591601611621631641651661671681694 unmodified lines1741751761771781791807 unmodified lines1881891901911921931948 unmodified lines2032042052062072082092 unmodified lines2122132142152162172182 unmodified lines2212222232242252262272282292302312322332342 unmodified lines2372382392402412422432442456 unmodified linesimport json5import pptxfrom global_config import GlobalConfigPATTERN = re.compile(r"^slide[ ]+\d+:", re.IGNORECASE)SAMPLE_JSON_FOR_PPTX = '''{52 unmodified lines)presentation = pptx.Presentation(GlobalConfig.PPTX_TEMPLATE_FILES[slides_template]['file'])# The title slidetitle_slide_layout = presentation.slide_layouts[0]slide = presentation.slides.add_slide(title_slide_layout)16 unmodified linesfor a_slide in parsed_data['slides']:bullet_slide_layout = presentation.slide_layouts[1]slide = presentation.slides.add_slide(bullet_slide_layout)shapes = slide.shapestitle_shape = shapes.titlebody_shape = shapes.placeholders[1]title_shape.text = remove_slide_number_from_heading(a_slide['heading'])all_headers.append(title_shape.text)text_frame = body_shape.text_frame# The bullet_points may contain a nested hierarchy of JSON arrays# In some scenarios, it may contain objects (dictionaries) because the LLM generated so# ^ The second scenario is not coveredflat_items_list = get_flat_list_of_contents(a_slide['bullet_points'], level=0)for an_item in flat_items_list:paragraph = text_frame.add_paragraph()paragraph.text = an_item[0]paragraph.level = an_item[1]# The thank-you slidelast_slide_layout = presentation.slide_layouts[0]27 unmodified linesreturn flat_listif __name__ == '__main__':# bullets = [# 'Description',# 'Types',# [# 'Type A',# 'Type B'# ],# 'Grand parent',# [# 'Parent',# [# 'Grand child'# ]# ]# ]# output = get_flat_list_of_contents(bullets, level=0)# for x in output:# print(x)json_data = '''{"title": "Understanding AI","slides": [4 unmodified lines["Importance of understanding AI"]]},{"heading": "What is AI?",7 unmodified lines]],"Differences between AI and machine learning"]},{"heading": "How AI Works",8 unmodified lines]],"How AI processes data"]},{"heading": "Pros of AI",2 unmodified lines"Improved accuracy and precision","Enhanced decision-making capabilities","Personalized experiences"]},{"heading": "Cons of AI",2 unmodified lines"Bias and discrimination","Privacy and security concerns","Dependence on technology"]},{"heading": "Future Prospects of AI","bullet_points": ["Advancements in fields such as healthcare and finance","Increased use"]}]}'''2 unmodified linespath = pathlib.Path(temp.name)generate_powerpoint_presentation(json5.loads(json_data),output_file_path=path,slides_template='Blank')temp.close()6 unmodified lines78910111213141516171819202122232425262752 unmodified lines8081828384858687888916 unmodified lines10610710810911011111211311411511611711811912012112212312412512627 unmodified lines1541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802814 unmodified lines2862872882892902912922937 unmodified lines3013023033043053063073088 unmodified lines3173183193203213223233243253263273283293303313323333343352 unmodified lines3383393403413423433443452 unmodified lines3483493503513523533543553563573583593603613623632 unmodified lines3663673683693703713723733743756 unmodified linesimport json5import pptxfrom pptx.enum.shapes import MSO_AUTO_SHAPE_TYPEfrom global_config import GlobalConfig# English Metric Unit (used by PowerPoint) to inchesEMU_TO_INCH_SCALING_FACTOR = 1.0 / 914400INCHES_1_5 = pptx.util.Inches(1.5)INCHES_1 = pptx.util.Inches(1)INCHES_0_5 = pptx.util.Inches(0.5)INCHES_0_4 = pptx.util.Inches(0.4)INCHES_0_3 = pptx.util.Inches(0.3)STEP_BY_STEP_PROCESS_MARKER = '>> 'PATTERN = re.compile(r"^slide[ ]+\d+:", re.IGNORECASE)SAMPLE_JSON_FOR_PPTX = '''{52 unmodified lines)presentation = pptx.Presentation(GlobalConfig.PPTX_TEMPLATE_FILES[slides_template]['file'])slide_width_inch = EMU_TO_INCH_SCALING_FACTOR * presentation.slide_widthslide_height_inch = EMU_TO_INCH_SCALING_FACTOR * presentation.slide_heightlogger.debug('Slide width: %f, height: %f', slide_width_inch, slide_height_inch)# The title slidetitle_slide_layout = presentation.slide_layouts[0]slide = presentation.slides.add_slide(title_slide_layout)16 unmodified linesfor a_slide in parsed_data['slides']:bullet_slide_layout = presentation.slide_layouts[1]slide = presentation.slides.add_slide(bullet_slide_layout)if not _handle_step_by_step_process(slide=slide,slide_json=a_slide,slide_width_inch=slide_width_inch,slide_height_inch=slide_height_inch,):_handle_default_display(slide, a_slide)_handle_key_message(slide=slide,slide_json=a_slide,slide_width_inch=slide_width_inch,slide_height_inch=slide_height_inch,)# The thank-you slidelast_slide_layout = presentation.slide_layouts[0]27 unmodified linesreturn flat_listdef _handle_default_display(slide: pptx.slide.Slide,slide_json: dict,):"""Display a list of text in a slide.:param slide: The slide to be processed.:param slide_json: The content of the slide as JSON data."""shapes = slide.shapestitle_shape = shapes.titlebody_shape = shapes.placeholders[1]title_shape.text = remove_slide_number_from_heading(slide_json['heading'])text_frame = body_shape.text_frame# The bullet_points may contain a nested hierarchy of JSON arrays# In some scenarios, it may contain objects (dictionaries) because the LLM generated so# ^ The second scenario is not coveredflat_items_list = get_flat_list_of_contents(slide_json['bullet_points'], level=0)for an_item in flat_items_list:paragraph = text_frame.add_paragraph()paragraph.text = an_item[0].removeprefix(STEP_BY_STEP_PROCESS_MARKER)paragraph.level = an_item[1]def _handle_step_by_step_process(slide: pptx.slide.Slide,slide_json: dict,slide_width_inch: float,slide_height_inch: float) -> bool:"""Add shapes to display a step-by-step process in the slide, if available.:param slide: The slide to be processed.:param slide_json: The content of the slide as JSON data.:param slide_width_inch: The width of the slide in inches.:param slide_height_inch: The height of the slide in inches.:return True is this slide has a step-by-step process depiction; False otherwise."""if 'bullet_points' in slide_json and slide_json['bullet_points']:steps = slide_json['bullet_points']# Ensure that it is a single list of strings without any sub-listfor step in steps:if not isinstance(step, str):return Falseif not step.startswith(STEP_BY_STEP_PROCESS_MARKER):return Falseshapes = slide.shapesshapes.title.text = remove_slide_number_from_heading(slide_json['heading'])n_steps = len(steps)if 3 <= n_steps <= 4:# Horizontal displayheight = INCHES_1_5width = pptx.util.Inches(slide_width_inch / n_steps - 0.01)top = pptx.util.Inches(slide_height_inch / 2)left = pptx.util.Inches((slide_width_inch - width.inches * n_steps) / 2 + 0.05)for step in steps:shape = shapes.add_shape(MSO_AUTO_SHAPE_TYPE.CHEVRON, left, top, width, height)shape.text = step.removeprefix(STEP_BY_STEP_PROCESS_MARKER)left += width - INCHES_0_4elif n_steps > 4:# Vertical displayheight = pptx.util.Inches(0.65)width = pptx.util.Inches(slide_width_inch * 2/ 3)top = pptx.util.Inches(slide_height_inch / 4)left = INCHES_1 # slide_width_inch - width.inches)for step in steps:shape = shapes.add_shape(MSO_AUTO_SHAPE_TYPE.PENTAGON, left, top, width, height)shape.text = step.removeprefix(STEP_BY_STEP_PROCESS_MARKER)top += height + INCHES_0_3left += INCHES_0_5else:# Two steps -- probably not a processreturn Falsereturn Truedef _handle_key_message(slide: pptx.slide.Slide,slide_json: dict,slide_width_inch: float,slide_height_inch: float):"""Add a shape to display the key message in the slide, if available.:param slide: The slide to be processed.:param slide_json: The content of the slide as JSON data.:param slide_width_inch: The width of the slide in inches.:param slide_height_inch: The height of the slide in inches."""if 'key_message' in slide_json and slide_json['key_message']:height = pptx.util.Inches(1.6)width = pptx.util.Inches(slide_width_inch / 2.3)top = pptx.util.Inches(slide_height_inch - height.inches - 0.1)left = pptx.util.Inches((slide_width_inch - width.inches) / 2)shape = slide.shapes.add_shape(MSO_AUTO_SHAPE_TYPE.ROUNDED_RECTANGLE,left=left,top=top,width=width,height=height)shape.text = slide_json['key_message']if __name__ == '__main__':_JSON_DATA = '''{"title": "Understanding AI","slides": [4 unmodified lines["Importance of understanding AI"]],"key_message": ""},{"heading": "What is AI?",7 unmodified lines]],"Differences between AI and machine learning"],"key_message": ""},{"heading": "How AI Works",8 unmodified lines]],"How AI processes data"],"key_message": ""},{"heading": "Building AI Models","bullet_points": [">> Collect data",">> Select model or architecture to use",">> Set appropriate parameters",">> Train model with data",">> Run inference",],"key_message": ""},{"heading": "Pros of AI",2 unmodified lines"Improved accuracy and precision","Enhanced decision-making capabilities","Personalized experiences"],"key_message": "AI can be used for many different purposes"},{"heading": "Cons of AI",2 unmodified lines"Bias and discrimination","Privacy and security concerns","Dependence on technology"],"key_message": ""},{"heading": "Future Prospects of AI","bullet_points": ["Advancements in fields such as healthcare and finance","Increased use"],"key_message": ""}]}'''2 unmodified linespath = pathlib.Path(temp.name)generate_powerpoint_presentation(json5.loads(_JSON_DATA),output_file_path=path,slides_template='Basic')print(f'File path: {path}')temp.close()
12342 unmodified lines4647484950515253545556def is_valid_prompt(prompt: str) -> bool:"""Verify whether user input satisfies the concerned constraints.42 unmodified lines# a new line or a closing bracket }prev_char = json_str[idx - 1]if prev_char == '}':response_cleaned = json_str[:idx]elif prev_char == '\n' and json_str[idx - 2] == '}':response_cleaned = json_str[:idx]json_str = json_str[:idx]return response_cleaned12345642 unmodified lines4950515253545556575859606162636465666768697071727374757677787980818283848586878889import json_repair as jrdef is_valid_prompt(prompt: str) -> bool:"""Verify whether user input satisfies the concerned constraints.42 unmodified lines# a new line or a closing bracket }prev_char = json_str[idx - 1]if (prev_char == '}') or (prev_char == '\n' and json_str[idx - 2] == '}'):response_cleaned = json_str[:idx]json_str = json_str[:idx]return response_cleaneddef fix_malformed_json(json_str: str) -> str:"""Try and fix the syntax error(s) in a JSON string.:param json_str: The input JSON string.:return: The fixed JSOn string."""return jr.repair_json(json_str, skip_json_loads=True)if __name__ == '__main__':json1 = '''{"key": "value"}'''json2 = '''["Reason": "Regular updates help protect against known vulnerabilities."]'''json3 = '''["Reason" Regular updates help protect against known vulnerabilities."]'''json4 = '''{"bullet_points": [">> Write without stopping or editing",>> Set daily writing goals and stick to them,">> Allow yourself to make mistakes"],}'''print(fix_malformed_json(json1))print(fix_malformed_json(json2))print(fix_malformed_json(json3))print(fix_malformed_json(json4))
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859You 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.The content of each slide should be verbose, descriptive, and very detailed.If relevant, add one or two examples to illustrate the concept.Unless explicitly specified with the topic, create about 10 slides.
### Topic:{question}
The output must be only a valid and syntactically correct JSON adhering to the following schema:{{ "title": "Presentation Title", "slides": [ {{ "heading": "Heading for the First Slide", "bullet_points": [ "First bullet point", [ "Sub-bullet point 1", "Sub-bullet point 2" ], "Second bullet point" ], "key_message": "" }}, {{ "heading": "Heading for the Second Slide", "bullet_points": [ "First bullet point", "Second bullet item", "Third bullet point" ], "key_message": "The key message conveyed in this slide" }}, {{ "heading": "A slide that describes a step-by-step/sequential process", "bullet_points": [ ">> The first step of the process (begins with special marker >>)", ">> A second step (begins with >>)", ">> Third step", ], "key_message": "" }} ]}}
### Some more hints on the slide content and JSON output format:- For two or three important slides, generate the key message that those slides convey and assignthem to the `key_message` elements of JSON output.- Identify if a slide describes a step-by-step/sequential process, then begin the bullet pointswith a special marker >>. Limit this to max two or three slides.
### Output:```json12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970You are a helpful, intelligent chatbot. You follow instructions to refine an existing slide deck.A list of user instructions is provided below in sequential order -- from the oldest to the latest.The previously generated content of the slide deck in JSON format is also provided.Follow the instructions to revise the content of the previously generated slides of the presentation on the given topic.Include main headings for each slide, detailed bullet points for each slide.Add relevant content to each slide.The content of the slides should be descriptive, verbose, and detailed.If relevant, add one or two examples to illustrate the concept.Unless explicitly specified with the topic, create about 10 slides.You also fix any syntax error that may be present in the JSON-formatted content.
A slide that describes a step-by-step/sequential process begins the bullet pointswith a special marker >>
### List of instructions:{instructions}
### Previously generated slide deck content as JSON:{previous_content}
The output must be only a valid and syntactically correct JSON adhering to the following schema:{{ "title": "Presentation Title", "slides": [ {{ "heading": "Heading for the First Slide", "bullet_points": [ "First bullet point", [ "Sub-bullet point 1", "Sub-bullet point 2" ], "Second bullet point" ], "key_message": "" }}, {{ "heading": "Heading for the Second Slide", "bullet_points": [ "First bullet point", "Second bullet item", "Third bullet point" ], "key_message": "The key message conveyed in this slide" }}, {{ "heading": "A slide that describes a step-by-step/sequential process", "bullet_points": [ ">> The first step of the process (begins with special marker >>)", ">> A second step (begins with >>)", ">> Third step", ], "key_message": "" }} ]}}
### Some more hints on the slide content and JSON output format:- For two or three important slides, generate the key message that those slides convey and assignthem to the `key_message` elements of JSON output.- Identify if a slide describes a step-by-step/sequential process, then begin the bullet pointswith a special marker >>. Limit this to max two or three slides.
### Output:```json123456python-dotenv[cli]~=1.0.0langchain~=0.1.13# huggingface_hubstreamlit~=1.32.2python-pptx1234567891011121314aiohttp==3.9.5python-dotenv[cli]~=1.0.0gitpython==3.1.43json_repair==0.15.3idna==3.7jinja2==3.1.3Pillow==10.3.0pyarrow~=16.0.0pydantic==2.4.0langchain~=0.1.16langchain-core~=0.1.46streamlit~=1.32.2python-pptx
app.py+24-12
global_config.py+4-4
helpers/pptx_helper.py+176-46
helpers/text_helper.py+36-3
langchain_templates/chat_prompts/initial_template_v2_steps.txtAdded+59
langchain_templates/chat_prompts/refinement_template_v2_steps.txtAdded+70
requirements.txt+10-2