Spaces:
Runtime error
Runtime error
Fix #29: improve JSON handling and keys
Browse filesFiles changed (2)
- app.py+31-20
- helpers/pptx_helper.py+32-24
6 unmodified lines7891010111213226 unmodified lines2402412422432442432452462472481 unmodified line2502512522512532542552562572582572592602612622632642652662672682692702712722732742752762772782792802812822832842852862874 unmodified lines2922932942702712952962972742752762772782792802812822832842852862872982993003016 unmodified linesimport pathlibimport randomimport tempfilefrom typing import Listfrom typing import List, Union
import json5import streamlit as st226 unmodified lines progress_bar.progress(1.0, text='Done!')
st.chat_message('ai').code(response, language='json')
if path: _display_download_button(path) _display_download_button(path)
logger.info( '#messages in history / 2: %d',1 unmodified line )
def generate_slide_deck(json_str: str) -> pathlib.Path:def generate_slide_deck(json_str: str) -> Union[pathlib.Path, None]: """ Create a slide deck and return the file path. In case there is any error creating the slide deck, the path may be to an empty file.
:param json_str: The content in *valid* JSON format. :return: The file of the .pptx file. :return: The path to the .pptx file or `None` in case of error. """
try: parsed_data = json5.loads(json_str) except ValueError: st.error( 'Encountered error while parsing JSON...will fix it and retry' ) logger.error( 'Caught ValueError: trying again after repairing JSON...' ) try: parsed_data = json5.loads(text_helper.fix_malformed_json(json_str)) except ValueError: st.error( 'Encountered an error again while fixing JSON...' 'the slide deck cannot be created, unfortunately ☹' '\nPlease try again later.' ) logger.error( 'Caught ValueError: failed to repair JSON!' )
return None
if DOWNLOAD_FILE_KEY in st.session_state: path = pathlib.Path(st.session_state[DOWNLOAD_FILE_KEY]) else:4 unmodified lines if temp: temp.close()
logger.debug('Creating PPTX file: %s...', st.session_state[DOWNLOAD_FILE_KEY])
try: logger.debug('Creating PPTX file: %s...', st.session_state[DOWNLOAD_FILE_KEY]) pptx_helper.generate_powerpoint_presentation( json_str, slides_template=pptx_template, output_file_path=path ) except ValueError: st.error( 'Encountered error while parsing JSON...will fix it and retry' ) logger.error( 'Caught ValueError: trying again after repairing JSON...' )
pptx_helper.generate_powerpoint_presentation( text_helper.fix_malformed_json(json_str), parsed_data, slides_template=pptx_template, output_file_path=path )81 unmodified lines82838485858687888990919292939495969798999899100134 unmodified lines23523623723824024124224324424524624724824925025125225325423924024124224324424524624724824925025125225325425525643 unmodified lines30030130230430330430530630778 unmodified lines386387388389389390391392393173 unmodified lines567568569570571572573574575576577578579580356 unmodified lines9379389399319409419425 unmodified lines94894995094395195295395427 unmodified lines98298398497798598698798881 unmodified lines
def generate_powerpoint_presentation( structured_data: str, parsed_data: dict, slides_template: str, output_file_path: pathlib.Path) -> List: """ Create and save a PowerPoint presentation file containing the content in JSON format.
:param structured_data: The presentation contents as "JSON" (may contain trailing commas). :param parsed_data: The presentation content as parsed JSON data. :param slides_template: The PPTX template to use. :param output_file_path: The path of the PPTX file to save as. :return: A list of presentation title and slides headers. """
# The structured "JSON" might contain trailing commas, so using json5 parsed_data = json5.loads(structured_data) presentation = pptx.Presentation(GlobalConfig.PPTX_TEMPLATE_FILES[slides_template]['file']) slide_width_inch, slide_height_inch = _get_slide_width_height_inches(presentation)
134 unmodified lines
status = False
if 'img_keywords' in slide_json: if random.random() < IMAGE_DISPLAY_PROBABILITY: if random.random() < FOREGROUND_IMAGE_PROBABILITY: status = _handle_display_image__in_foreground( presentation, slide_json, slide_width_inch, slide_height_inch ) else: status = _handle_display_image__in_background( presentation, slide_json, slide_width_inch, slide_height_inch ) if random.random() < IMAGE_DISPLAY_PROBABILITY: if random.random() < FOREGROUND_IMAGE_PROBABILITY: status = _handle_display_image__in_foreground( presentation, slide_json, slide_width_inch, slide_height_inch ) else: status = _handle_display_image__in_background( presentation, slide_json, slide_width_inch, slide_height_inch )
if status: return43 unmodified lines slide_height_inch: float) -> bool: """ Create a slide with text and image using a picture placeholder layout. Create a slide with text and image using a picture placeholder layout. If not image keyword is available, it will add only text to the slide.
:param presentation: The presentation object. :param slide_json: The content of the slide as JSON data.78 unmodified lines) -> bool: """ Add a slide with text and an image in the background. It works just like `_handle_default_display()` but with a background image added. `_handle_default_display()` but with a background image added. If not image keyword is available, it will add only text to the slide.
:param presentation: The presentation object. :param slide_json: The content of the slide as JSON data.173 unmodified lines for run in paragraph.runs: run.font.color.theme_color = pptx.enum.dml.MSO_THEME_COLOR.TEXT_2
_add_text_at_bottom( slide=slide, slide_width_inch=slide_width_inch, slide_height_inch=slide_height_inch, text='More icons available in the SlideDeck AI repository', hyperlink='https://github.com/barun-saha/slide-deck-ai/tree/main/icons/png128' )
return True
return False356 unmodified lines "AI for predicting student performance and dropout rates" ], "key_message": "AI is personalizing education and improving student outcomes", "img_keywords": "education, personalized learning, intelligent tutoring, student performance" }, { "heading": "Step-by-Step: AI Development Process",5 unmodified lines ">> Deploy and monitor the AI system" ], "key_message": "Developing AI involves a structured process from problem definition to deployment", "img_keywords": "AI development process, problem definition, data collection, model training" "img_keywords": "" }, { "heading": "AI Icons: Key Aspects",27 unmodified lines generate_powerpoint_presentation( json5.loads(_JSON_DATA), output_file_path=path, slides_template='Minimalist Sales Pitch' slides_template='Basic' ) print(f'File path: {path}')
app.py+31-20
helpers/pptx_helper.py+32-24