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
112348 unmodified lines13141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596056 unmodified lines1171181197812012112212350 unmodified lines17417517613517717817918067 unmodified lines248249250209251252211212253254255256257from typing import Listfrom typing import List, Tuple
import 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 results
def build_ui(): """ Display the input elements for content generation. Only covers the first step.56 unmodified lines target_length = min(topic_length, GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH)
try: slides_content = llm_helper.generate_slides_content(topic[:target_length]).strip() slides_content = get_contents_wrapper(topic[:target_length]) content_length = len(slides_content)
print('=' * 20)50 unmodified lines """
print('JSON button clicked') json_str = llm_helper.text_to_json(text) 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 online ppt_text = ' '.join(ppt_headers) search_results = llm_helper.get_related_websites(ppt_text) search_results = get_web_search_results_wrapper(ppt_text)
for a_result in search_results.results: st.markdown(f'[{a_result.title}]({a_result.url})') for (title, link) in search_results: st.markdown(f'[{title}]({link})')
def button_clicked(button):26 unmodified lines2728293031323326 unmodified lines APP_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 lines789101011121363 unmodified lines77787980808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711885868788899091921231249394951269697989911 unmodified lines11111211314411411511611733 unmodified lines1511521531841851541551561871571581591604 unmodified lines1651661671681691701711721731741751761776 unmodified lines
prompt = Nonellm_contents = Nonellm_yaml = Nonellm_json = Nonemetaphor_client = None
63 unmodified lines :return: JSON string """
global llm_yaml global llm_json
content = content.replace('```', '')
# f-string is not used in order to prevent interpreting the brackets text = '''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 += content text += '''
Output:```json''' with open(GlobalConfig.JSON_TEMPLATE_FILE, 'r') as in_file: text = in_file.read() # Insert the actual text contents text = text.replace('<REPLACE_PLACEHOLDER>', content)
text = text.strip() print(text)
if llm_yaml is None: llm_yaml = get_llm(use_gpt=True) if llm_json is None: llm_json = get_llm(use_gpt=True)
output = llm_yaml(text, verbose=True) output = llm_json(text, verbose=True) output = output.strip()
first_index = max(0, output.find('{'))11 unmodified lines :return: JSON string """
global llm_yaml global llm_json
content = content.replace('```', '')
33 unmodified lines text = text.strip() print(text)
if llm_yaml is None: llm_yaml = get_llm(use_gpt=True) if llm_json is None: llm_json = get_llm(use_gpt=True)
output = llm_yaml(text, verbose=True) output = llm_json(text, verbose=True) output = output.strip()
# first_index = max(0, output.find('{'))4 unmodified lines
def 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_client
if not metaphor_client:app.py+48-6
global_config.py+1
llm_helper.py+20-43