Spaces:
Build error
Build error
Cache external API calls
Browse filesFiles changed (3)
- app.py+48-6
- global_config.py+1
- llm_helper.py+20-43
12348 unmodified lines13141516171856 unmodified lines7576777879808150 unmodified lines13213313413513613713867 unmodified lines206207208209210211212213214215from typing import Listimport json5import time8 unmodified linesAPP_TEXT = json5.loads(open(GlobalConfig.APP_STRINGS_FILE, 'r').read())def build_ui():"""Display the input elements for content generation. Only covers the first step.56 unmodified linestarget_length = min(topic_length, GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH)try:slides_content = llm_helper.generate_slides_content(topic[:target_length]).strip()content_length = len(slides_content)print('=' * 20)50 unmodified lines"""print('JSON button clicked')json_str = llm_helper.text_to_json(text)# yaml_str = llm_helper.text_to_yaml(text)print('=' * 20)print(f'JSON:\n{json_str}')67 unmodified lines# Use the presentation title and the slides headers to find relevant info onlineppt_text = ' '.join(ppt_headers)search_results = llm_helper.get_related_websites(ppt_text)for a_result in search_results.results:st.markdown(f'[{a_result.title}]({a_result.url})')def button_clicked(button):12348 unmodified lines13141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596056 unmodified lines11711811912012112212350 unmodified lines17417517617717817918067 unmodified lines248249250251252253254255256257from typing import List, Tupleimport json5import time8 unmodified linesAPP_TEXT = json5.loads(open(GlobalConfig.APP_STRINGS_FILE, 'r').read())@st.cache_datadef get_contents_wrapper(text: str) -> str:"""Fetch and cache the slide deck contents on a topic by calling an external API.:param text: The presentation topic:return: The slide deck contents or outline"""return llm_helper.generate_slides_content(text).strip()@st.cache_datadef get_json_wrapper(text: str) -> str:"""Fetch and cache the JSON-formatted slide deck contents by calling an external API.:param text: The slide deck contents or outline:return: The JSON-formatted contents"""return llm_helper.text_to_json(text)@st.cache_datadef get_web_search_results_wrapper(text: str) -> List[Tuple[str, str]]:"""Fetch and cache the Web search results on a given topic.:param text: The topic:return: A list of (title, link) tuples"""results = []search_results = llm_helper.get_related_websites(text)for a_result in search_results.results:results.append((a_result.title, a_result.url))return resultsdef build_ui():"""Display the input elements for content generation. Only covers the first step.56 unmodified linestarget_length = min(topic_length, GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH)try:slides_content = get_contents_wrapper(topic[:target_length])content_length = len(slides_content)print('=' * 20)50 unmodified lines"""print('JSON button clicked')json_str = get_json_wrapper(text)# yaml_str = llm_helper.text_to_yaml(text)print('=' * 20)print(f'JSON:\n{json_str}')67 unmodified lines# Use the presentation title and the slides headers to find relevant info onlineppt_text = ' '.join(ppt_headers)search_results = get_web_search_results_wrapper(ppt_text)for (title, link) in search_results:st.markdown(f'[{title}]({link})')def button_clicked(button):
26 unmodified lines27282930313226 unmodified linesAPP_STRINGS_FILE = 'strings.json'PRELOAD_DATA_FILE = 'examples/example_02.json'SLIDES_TEMPLATE_FILE = 'langchain_templates/template_07.txt'PPTX_TEMPLATE_FILES = {'Blank': {26 unmodified lines2728293031323326 unmodified linesAPP_STRINGS_FILE = 'strings.json'PRELOAD_DATA_FILE = 'examples/example_02.json'SLIDES_TEMPLATE_FILE = 'langchain_templates/template_07.txt'JSON_TEMPLATE_FILE = 'langchain_templates/text_to_json_template.txt'PPTX_TEMPLATE_FILES = {'Blank': {
6 unmodified lines7891011121363 unmodified lines777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812911 unmodified lines14114214314414514614733 unmodified lines1811821831841851861871881891904 unmodified lines1951961971981992006 unmodified linesprompt = Nonellm_contents = Nonellm_yaml = Nonemetaphor_client = None63 unmodified lines:return: JSON string"""global llm_yamlcontent = content.replace('```', '')# f-string is not used in order to prevent interpreting the bracketstext = '''Convert the given slide deck text into structured JSON output.Also, generate and add an engaging presentation title.The output should be only correct and valid JSON having the following structure:{"title": "...","slides": [{"heading": "...","bullet_points": ["...",["...","..."]]},{...},]}Text:'''text += contenttext += '''Output:```json'''text = text.strip()print(text)if llm_yaml is None:llm_yaml = get_llm(use_gpt=True)output = llm_yaml(text, verbose=True)output = output.strip()first_index = max(0, output.find('{'))11 unmodified lines:return: JSON string"""global llm_yamlcontent = content.replace('```', '')33 unmodified linestext = text.strip()print(text)if llm_yaml is None:llm_yaml = get_llm(use_gpt=True)output = llm_yaml(text, verbose=True)output = output.strip()# first_index = max(0, output.find('{'))4 unmodified linesdef get_related_websites(query: str) -> metaphor.api.SearchResponse:global metaphor_clientif not metaphor_client:6 unmodified lines7891011121363 unmodified lines777879808182838485868788899091929394959697989911 unmodified lines11111211311411511611733 unmodified lines1511521531541551561571581591604 unmodified lines1651661671681691701711721731741751761776 unmodified linesprompt = Nonellm_contents = Nonellm_json = Nonemetaphor_client = None63 unmodified lines:return: JSON string"""global llm_jsoncontent = content.replace('```', '')# f-string is not used in order to prevent interpreting the bracketswith open(GlobalConfig.JSON_TEMPLATE_FILE, 'r') as in_file:text = in_file.read()# Insert the actual text contentstext = text.replace('<REPLACE_PLACEHOLDER>', content)text = text.strip()print(text)if llm_json is None:llm_json = get_llm(use_gpt=True)output = llm_json(text, verbose=True)output = output.strip()first_index = max(0, output.find('{'))11 unmodified lines:return: JSON string"""global llm_jsoncontent = content.replace('```', '')33 unmodified linestext = text.strip()print(text)if llm_json is None:llm_json = get_llm(use_gpt=True)output = llm_json(text, verbose=True)output = output.strip()# first_index = max(0, output.find('{'))4 unmodified linesdef get_related_websites(query: str) -> metaphor.api.SearchResponse:"""Fetch Web search results for a given query.:param query: The query text:return: The search results object"""global metaphor_clientif not metaphor_client:
app.py+48-6
global_config.py+1
llm_helper.py+20-43