Spaces:
Runtime error
Runtime error
Use named tempfile to save the presentation
Browse filesFiles changed (2)
- app.py+13-8
- pptx_helper.py+10-4
12342 unmodified lines789101112235 unmodified lines248249250251252253254255256257258259260261262263264265266267268269270import osimport json5import loggingimport shutil2 unmodified linesimport streamlit.runtime.scriptrunner as st_srfrom typing import List, Tupleimport metaphor_python as metaphorimport llm_helperimport pptx_helper235 unmodified linesprogress_text = 'Creating the slide deck...give it a moment'progress_bar.progress(75, text=progress_text)# Get a unique name for the file to save -- use the session IDctx = st_sr.get_script_run_ctx()session_id = ctx.session_idtimestamp = time.time()output_file_name = f'{session_id}_{timestamp}.pptx'logging.info('Creating PPTX file...')all_headers = pptx_helper.generate_powerpoint_presentation(json_str,as_yaml=False,slides_template=pptx_template,output_file_name=output_file_name)progress_bar.progress(100, text='Done!')with open(output_file_name, 'rb') as f:st.download_button('Download PPTX file', f, file_name=output_file_name)return all_headers123452 unmodified lines891011121314235 unmodified lines250251252253254255256257258259260261262263264265266267268269270271272273274275import osimport pathlibimport json5import loggingimport shutil2 unmodified linesimport streamlit.runtime.scriptrunner as st_srfrom typing import List, Tupleimport metaphor_python as metaphorimport tempfileimport llm_helperimport pptx_helper235 unmodified linesprogress_text = 'Creating the slide deck...give it a moment'progress_bar.progress(75, text=progress_text)# # Get a unique name for the file to save -- use the session ID# ctx = st_sr.get_script_run_ctx()# session_id = ctx.session_id# timestamp = time.time()# output_file_name = f'{session_id}_{timestamp}.pptx'temp = tempfile.NamedTemporaryFile(delete=False, suffix='.pptx')path = pathlib.Path(temp.name)logging.info('Creating PPTX file...')all_headers = pptx_helper.generate_powerpoint_presentation(json_str,as_yaml=False,slides_template=pptx_template,output_file_path=path)progress_bar.progress(100, text='Done!')with open(path, 'rb') as f:st.download_button('Download PPTX file', f, file_name='Presentation.pptx')return all_headers
12346 unmodified lines505152535455561 unmodified line5859606162636455 unmodified lines120121122123124125126108 unmodified lines235236237238239240241242243from typing import List, Tupleimport json5import logging46 unmodified linesstructured_data: str,as_yaml: bool,slides_template: str,output_file_name: str) -> List:"""Create and save a PowerPoint presentation file containing the contents in JSON or YAML format.1 unmodified line:param structured_data: The presentation contents as "JSON" (may contain trailing commas) or YAML:param as_yaml: True if the input data is in YAML format; False if it is in JSON format:param slides_template: The PPTX template to use:param output_file_name: The name of the PPTX file to save as:return A list of presentation title and slides headers"""55 unmodified linestitle = slide.shapes.titletitle.text = 'Thank you!'presentation.save(output_file_name)return all_headers108 unmodified lines}]}'''generate_powerpoint_presentation(json5.loads(json_data),as_yaml=False,output_file_name='test.pptx',slides_template='Blank')1234546 unmodified lines525354555657581 unmodified line6061626364656655 unmodified lines122123124125126127128108 unmodified lines237238239240241242243244245246247248249import pathlibimport tempfilefrom typing import List, Tupleimport json5import logging46 unmodified linesstructured_data: str,as_yaml: bool,slides_template: str,output_file_path: pathlib.Path) -> List:"""Create and save a PowerPoint presentation file containing the contents in JSON or YAML format.1 unmodified line:param structured_data: The presentation contents as "JSON" (may contain trailing commas) or YAML:param as_yaml: True if the input data is in YAML format; False if it is in JSON format: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"""55 unmodified linestitle = slide.shapes.titletitle.text = 'Thank you!'presentation.save(output_file_path)return all_headers108 unmodified lines}]}'''temp = tempfile.NamedTemporaryFile(delete=False, suffix='.pptx')path = pathlib.Path(temp.name)generate_powerpoint_presentation(json5.loads(json_data),as_yaml=False,output_file_path=path,slides_template='Blank')
app.py+13-8
pptx_helper.py+10-4