Spaces:
Running
Running
Cache external API calls
Browse files- app.py +48 -6
- global_config.py +1 -0
- llm_helper.py +20 -43
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from typing import List
|
| 2 |
|
| 3 |
import json5
|
| 4 |
import time
|
|
@@ -13,6 +13,48 @@ from global_config import GlobalConfig
|
|
| 13 |
APP_TEXT = json5.loads(open(GlobalConfig.APP_STRINGS_FILE, 'r').read())
|
| 14 |
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
def build_ui():
|
| 17 |
"""
|
| 18 |
Display the input elements for content generation. Only covers the first step.
|
|
@@ -75,7 +117,7 @@ def process_topic_inputs(topic: str, progress_bar):
|
|
| 75 |
target_length = min(topic_length, GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH)
|
| 76 |
|
| 77 |
try:
|
| 78 |
-
slides_content =
|
| 79 |
content_length = len(slides_content)
|
| 80 |
|
| 81 |
print('=' * 20)
|
|
@@ -132,7 +174,7 @@ def process_slides_contents(text: str, progress_bar: st.progress):
|
|
| 132 |
"""
|
| 133 |
|
| 134 |
print('JSON button clicked')
|
| 135 |
-
json_str =
|
| 136 |
# yaml_str = llm_helper.text_to_yaml(text)
|
| 137 |
print('=' * 20)
|
| 138 |
print(f'JSON:\n{json_str}')
|
|
@@ -206,10 +248,10 @@ def show_bonus_stuff(ppt_headers: List):
|
|
| 206 |
|
| 207 |
# Use the presentation title and the slides headers to find relevant info online
|
| 208 |
ppt_text = ' '.join(ppt_headers)
|
| 209 |
-
search_results =
|
| 210 |
|
| 211 |
-
for
|
| 212 |
-
st.markdown(f'[{
|
| 213 |
|
| 214 |
|
| 215 |
def button_clicked(button):
|
|
|
|
| 1 |
+
from typing import List, Tuple
|
| 2 |
|
| 3 |
import json5
|
| 4 |
import time
|
|
|
|
| 13 |
APP_TEXT = json5.loads(open(GlobalConfig.APP_STRINGS_FILE, 'r').read())
|
| 14 |
|
| 15 |
|
| 16 |
+
@st.cache_data
|
| 17 |
+
def get_contents_wrapper(text: str) -> str:
|
| 18 |
+
"""
|
| 19 |
+
Fetch and cache the slide deck contents on a topic by calling an external API.
|
| 20 |
+
|
| 21 |
+
:param text: The presentation topic
|
| 22 |
+
:return: The slide deck contents or outline
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
return llm_helper.generate_slides_content(text).strip()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
@st.cache_data
|
| 29 |
+
def get_json_wrapper(text: str) -> str:
|
| 30 |
+
"""
|
| 31 |
+
Fetch and cache the JSON-formatted slide deck contents by calling an external API.
|
| 32 |
+
|
| 33 |
+
:param text: The slide deck contents or outline
|
| 34 |
+
:return: The JSON-formatted contents
|
| 35 |
+
"""
|
| 36 |
+
|
| 37 |
+
return llm_helper.text_to_json(text)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
@st.cache_data
|
| 41 |
+
def get_web_search_results_wrapper(text: str) -> List[Tuple[str, str]]:
|
| 42 |
+
"""
|
| 43 |
+
Fetch and cache the Web search results on a given topic.
|
| 44 |
+
|
| 45 |
+
:param text: The topic
|
| 46 |
+
:return: A list of (title, link) tuples
|
| 47 |
+
"""
|
| 48 |
+
|
| 49 |
+
results = []
|
| 50 |
+
search_results = llm_helper.get_related_websites(text)
|
| 51 |
+
|
| 52 |
+
for a_result in search_results.results:
|
| 53 |
+
results.append((a_result.title, a_result.url))
|
| 54 |
+
|
| 55 |
+
return results
|
| 56 |
+
|
| 57 |
+
|
| 58 |
def build_ui():
|
| 59 |
"""
|
| 60 |
Display the input elements for content generation. Only covers the first step.
|
|
|
|
| 117 |
target_length = min(topic_length, GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH)
|
| 118 |
|
| 119 |
try:
|
| 120 |
+
slides_content = get_contents_wrapper(topic[:target_length])
|
| 121 |
content_length = len(slides_content)
|
| 122 |
|
| 123 |
print('=' * 20)
|
|
|
|
| 174 |
"""
|
| 175 |
|
| 176 |
print('JSON button clicked')
|
| 177 |
+
json_str = get_json_wrapper(text)
|
| 178 |
# yaml_str = llm_helper.text_to_yaml(text)
|
| 179 |
print('=' * 20)
|
| 180 |
print(f'JSON:\n{json_str}')
|
|
|
|
| 248 |
|
| 249 |
# Use the presentation title and the slides headers to find relevant info online
|
| 250 |
ppt_text = ' '.join(ppt_headers)
|
| 251 |
+
search_results = get_web_search_results_wrapper(ppt_text)
|
| 252 |
|
| 253 |
+
for (title, link) in search_results:
|
| 254 |
+
st.markdown(f'[{title}]({link})')
|
| 255 |
|
| 256 |
|
| 257 |
def button_clicked(button):
|
global_config.py
CHANGED
|
@@ -27,6 +27,7 @@ class GlobalConfig:
|
|
| 27 |
APP_STRINGS_FILE = 'strings.json'
|
| 28 |
PRELOAD_DATA_FILE = 'examples/example_02.json'
|
| 29 |
SLIDES_TEMPLATE_FILE = 'langchain_templates/template_07.txt'
|
|
|
|
| 30 |
|
| 31 |
PPTX_TEMPLATE_FILES = {
|
| 32 |
'Blank': {
|
|
|
|
| 27 |
APP_STRINGS_FILE = 'strings.json'
|
| 28 |
PRELOAD_DATA_FILE = 'examples/example_02.json'
|
| 29 |
SLIDES_TEMPLATE_FILE = 'langchain_templates/template_07.txt'
|
| 30 |
+
JSON_TEMPLATE_FILE = 'langchain_templates/text_to_json_template.txt'
|
| 31 |
|
| 32 |
PPTX_TEMPLATE_FILES = {
|
| 33 |
'Blank': {
|
llm_helper.py
CHANGED
|
@@ -7,7 +7,7 @@ from global_config import GlobalConfig
|
|
| 7 |
|
| 8 |
prompt = None
|
| 9 |
llm_contents = None
|
| 10 |
-
|
| 11 |
metaphor_client = None
|
| 12 |
|
| 13 |
|
|
@@ -77,53 +77,23 @@ def text_to_json(content: str) -> str:
|
|
| 77 |
:return: JSON string
|
| 78 |
"""
|
| 79 |
|
| 80 |
-
global
|
| 81 |
|
| 82 |
content = content.replace('```', '')
|
| 83 |
|
| 84 |
# f-string is not used in order to prevent interpreting the brackets
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
{
|
| 91 |
-
"title": "...",
|
| 92 |
-
"slides": [
|
| 93 |
-
{
|
| 94 |
-
"heading": "...",
|
| 95 |
-
"bullet_points": [
|
| 96 |
-
"...",
|
| 97 |
-
[
|
| 98 |
-
"...",
|
| 99 |
-
"..."
|
| 100 |
-
]
|
| 101 |
-
]
|
| 102 |
-
},
|
| 103 |
-
{
|
| 104 |
-
...
|
| 105 |
-
},
|
| 106 |
-
]
|
| 107 |
-
}
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
Text:
|
| 111 |
-
'''
|
| 112 |
-
text += content
|
| 113 |
-
text += '''
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
Output:
|
| 117 |
-
```json
|
| 118 |
-
'''
|
| 119 |
|
| 120 |
text = text.strip()
|
| 121 |
print(text)
|
| 122 |
|
| 123 |
-
if
|
| 124 |
-
|
| 125 |
|
| 126 |
-
output =
|
| 127 |
output = output.strip()
|
| 128 |
|
| 129 |
first_index = max(0, output.find('{'))
|
|
@@ -141,7 +111,7 @@ def text_to_yaml(content: str) -> str:
|
|
| 141 |
:return: JSON string
|
| 142 |
"""
|
| 143 |
|
| 144 |
-
global
|
| 145 |
|
| 146 |
content = content.replace('```', '')
|
| 147 |
|
|
@@ -181,10 +151,10 @@ Output:
|
|
| 181 |
text = text.strip()
|
| 182 |
print(text)
|
| 183 |
|
| 184 |
-
if
|
| 185 |
-
|
| 186 |
|
| 187 |
-
output =
|
| 188 |
output = output.strip()
|
| 189 |
|
| 190 |
# first_index = max(0, output.find('{'))
|
|
@@ -195,6 +165,13 @@ Output:
|
|
| 195 |
|
| 196 |
|
| 197 |
def get_related_websites(query: str) -> metaphor.api.SearchResponse:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
global metaphor_client
|
| 199 |
|
| 200 |
if not metaphor_client:
|
|
|
|
| 7 |
|
| 8 |
prompt = None
|
| 9 |
llm_contents = None
|
| 10 |
+
llm_json = None
|
| 11 |
metaphor_client = None
|
| 12 |
|
| 13 |
|
|
|
|
| 77 |
:return: JSON string
|
| 78 |
"""
|
| 79 |
|
| 80 |
+
global llm_json
|
| 81 |
|
| 82 |
content = content.replace('```', '')
|
| 83 |
|
| 84 |
# f-string is not used in order to prevent interpreting the brackets
|
| 85 |
+
with open(GlobalConfig.JSON_TEMPLATE_FILE, 'r') as in_file:
|
| 86 |
+
text = in_file.read()
|
| 87 |
+
# Insert the actual text contents
|
| 88 |
+
text = text.replace('<REPLACE_PLACEHOLDER>', content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
text = text.strip()
|
| 91 |
print(text)
|
| 92 |
|
| 93 |
+
if llm_json is None:
|
| 94 |
+
llm_json = get_llm(use_gpt=True)
|
| 95 |
|
| 96 |
+
output = llm_json(text, verbose=True)
|
| 97 |
output = output.strip()
|
| 98 |
|
| 99 |
first_index = max(0, output.find('{'))
|
|
|
|
| 111 |
:return: JSON string
|
| 112 |
"""
|
| 113 |
|
| 114 |
+
global llm_json
|
| 115 |
|
| 116 |
content = content.replace('```', '')
|
| 117 |
|
|
|
|
| 151 |
text = text.strip()
|
| 152 |
print(text)
|
| 153 |
|
| 154 |
+
if llm_json is None:
|
| 155 |
+
llm_json = get_llm(use_gpt=True)
|
| 156 |
|
| 157 |
+
output = llm_json(text, verbose=True)
|
| 158 |
output = output.strip()
|
| 159 |
|
| 160 |
# first_index = max(0, output.find('{'))
|
|
|
|
| 165 |
|
| 166 |
|
| 167 |
def get_related_websites(query: str) -> metaphor.api.SearchResponse:
|
| 168 |
+
"""
|
| 169 |
+
Fetch Web search results for a given query.
|
| 170 |
+
|
| 171 |
+
:param query: The query text
|
| 172 |
+
:return: The search results object
|
| 173 |
+
"""
|
| 174 |
+
|
| 175 |
global metaphor_client
|
| 176 |
|
| 177 |
if not metaphor_client:
|