Spaces:
Sleeping
Sleeping
Add logging; replace all print statements
Browse files- app.py +28 -15
- global_config.py +1 -0
- llm_helper.py +16 -10
- pptx_helper.py +9 -3
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import base64
|
2 |
import os
|
3 |
import json5
|
|
|
4 |
import shutil
|
5 |
import time
|
6 |
import streamlit as st
|
@@ -16,6 +17,12 @@ APP_TEXT = json5.loads(open(GlobalConfig.APP_STRINGS_FILE, 'r').read())
|
|
16 |
GB_CONVERTER = 2 ** 30
|
17 |
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
@st.cache_data
|
20 |
def get_contents_wrapper(text: str) -> str:
|
21 |
"""
|
@@ -82,11 +89,12 @@ def get_disk_used_percentage() -> float:
|
|
82 |
used = used // GB_CONVERTER
|
83 |
free = free // GB_CONVERTER
|
84 |
used_perc = 100.0 * used / total
|
85 |
-
print(f'Total: {total} GB\n'
|
86 |
-
f'Used: {used} GB\n'
|
87 |
-
f'Free: {free} GB')
|
88 |
|
89 |
-
|
|
|
|
|
|
|
|
|
90 |
|
91 |
return used_perc
|
92 |
|
@@ -147,24 +155,25 @@ def process_topic_inputs(topic: str, progress_bar):
|
|
147 |
"""
|
148 |
|
149 |
topic_length = len(topic)
|
150 |
-
|
151 |
|
152 |
if topic_length >= 10:
|
153 |
-
|
154 |
f'Topic: {topic}\n'
|
155 |
)
|
156 |
-
|
157 |
|
158 |
target_length = min(topic_length, GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH)
|
159 |
|
160 |
try:
|
|
|
161 |
slides_content = get_contents_wrapper(topic[:target_length])
|
162 |
content_length = len(slides_content)
|
163 |
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
st.write(f'''Slides content:\n{slides_content}''')
|
169 |
progress_bar.progress(100, text='Done!')
|
170 |
|
@@ -213,10 +222,11 @@ def process_slides_contents(text: str, progress_bar: st.progress):
|
|
213 |
:param progress_bar: Progress bar for this step
|
214 |
"""
|
215 |
|
216 |
-
|
217 |
json_str = ''
|
218 |
|
219 |
try:
|
|
|
220 |
json_str = get_json_wrapper(text)
|
221 |
except Exception as ex:
|
222 |
st.error(f'An exception occurred while trying to convert to JSON.'
|
@@ -226,9 +236,9 @@ def process_slides_contents(text: str, progress_bar: st.progress):
|
|
226 |
# st.stop()
|
227 |
|
228 |
# yaml_str = llm_helper.text_to_yaml(text)
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
st.code(json_str, language='json')
|
233 |
|
234 |
if len(json_str) > 0:
|
@@ -275,6 +285,7 @@ def process_slides_contents(text: str, progress_bar: st.progress):
|
|
275 |
timestamp = time.time()
|
276 |
output_file_name = f'{session_id}_{timestamp}.pptx'
|
277 |
|
|
|
278 |
all_headers = pptx_helper.generate_powerpoint_presentation(
|
279 |
json_str,
|
280 |
as_yaml=False,
|
@@ -347,6 +358,7 @@ def show_bonus_stuff(
|
|
347 |
urls_text.write(APP_TEXT['urls_info'])
|
348 |
|
349 |
# Use the presentation title and the slides headers to find relevant info online
|
|
|
350 |
ppt_text = ' '.join(ppt_headers)
|
351 |
search_results = get_web_search_results_wrapper(ppt_text)
|
352 |
md_text_items = []
|
@@ -356,6 +368,7 @@ def show_bonus_stuff(
|
|
356 |
|
357 |
urls_list.markdown('\n\n'.join(md_text_items))
|
358 |
|
|
|
359 |
img_empty.write('')
|
360 |
img_text.write(APP_TEXT['image_info'])
|
361 |
image = get_ai_image_wrapper(ppt_text)
|
|
|
1 |
import base64
|
2 |
import os
|
3 |
import json5
|
4 |
+
import logging
|
5 |
import shutil
|
6 |
import time
|
7 |
import streamlit as st
|
|
|
17 |
GB_CONVERTER = 2 ** 30
|
18 |
|
19 |
|
20 |
+
logging.basicConfig(
|
21 |
+
level=GlobalConfig.LOG_LEVEL,
|
22 |
+
format='%(asctime)s - %(message)s',
|
23 |
+
)
|
24 |
+
|
25 |
+
|
26 |
@st.cache_data
|
27 |
def get_contents_wrapper(text: str) -> str:
|
28 |
"""
|
|
|
89 |
used = used // GB_CONVERTER
|
90 |
free = free // GB_CONVERTER
|
91 |
used_perc = 100.0 * used / total
|
|
|
|
|
|
|
92 |
|
93 |
+
logging.debug(f'Total: {total} GB\n'
|
94 |
+
f'Used: {used} GB\n'
|
95 |
+
f'Free: {free} GB')
|
96 |
+
|
97 |
+
logging.debug('\n'.join(os.listdir()))
|
98 |
|
99 |
return used_perc
|
100 |
|
|
|
155 |
"""
|
156 |
|
157 |
topic_length = len(topic)
|
158 |
+
logging.debug(f'Input length:: topic: {topic_length}')
|
159 |
|
160 |
if topic_length >= 10:
|
161 |
+
logging.debug(
|
162 |
f'Topic: {topic}\n'
|
163 |
)
|
164 |
+
logging.debug('=' * 20)
|
165 |
|
166 |
target_length = min(topic_length, GlobalConfig.LLM_MODEL_MAX_INPUT_LENGTH)
|
167 |
|
168 |
try:
|
169 |
+
logging.info('Calling LLM for content generation...')
|
170 |
slides_content = get_contents_wrapper(topic[:target_length])
|
171 |
content_length = len(slides_content)
|
172 |
|
173 |
+
logging.debug('=' * 20)
|
174 |
+
logging.debug(f'Slides content:\n{slides_content}')
|
175 |
+
logging.debug(f'Content length: {content_length}')
|
176 |
+
logging.debug('=' * 20)
|
177 |
st.write(f'''Slides content:\n{slides_content}''')
|
178 |
progress_bar.progress(100, text='Done!')
|
179 |
|
|
|
222 |
:param progress_bar: Progress bar for this step
|
223 |
"""
|
224 |
|
225 |
+
logging.debug('JSON button clicked')
|
226 |
json_str = ''
|
227 |
|
228 |
try:
|
229 |
+
logging.info('Calling LLM for conversion...')
|
230 |
json_str = get_json_wrapper(text)
|
231 |
except Exception as ex:
|
232 |
st.error(f'An exception occurred while trying to convert to JSON.'
|
|
|
236 |
# st.stop()
|
237 |
|
238 |
# yaml_str = llm_helper.text_to_yaml(text)
|
239 |
+
logging.debug('=' * 20)
|
240 |
+
logging.debug(f'JSON:\n{json_str}')
|
241 |
+
logging.debug('=' * 20)
|
242 |
st.code(json_str, language='json')
|
243 |
|
244 |
if len(json_str) > 0:
|
|
|
285 |
timestamp = time.time()
|
286 |
output_file_name = f'{session_id}_{timestamp}.pptx'
|
287 |
|
288 |
+
logging.info('Creating PPTX file...')
|
289 |
all_headers = pptx_helper.generate_powerpoint_presentation(
|
290 |
json_str,
|
291 |
as_yaml=False,
|
|
|
358 |
urls_text.write(APP_TEXT['urls_info'])
|
359 |
|
360 |
# Use the presentation title and the slides headers to find relevant info online
|
361 |
+
logging.info('Using Metaphor search...')
|
362 |
ppt_text = ' '.join(ppt_headers)
|
363 |
search_results = get_web_search_results_wrapper(ppt_text)
|
364 |
md_text_items = []
|
|
|
368 |
|
369 |
urls_list.markdown('\n\n'.join(md_text_items))
|
370 |
|
371 |
+
logging.info('Calling SDXL for image generation...')
|
372 |
img_empty.write('')
|
373 |
img_text.write(APP_TEXT['image_info'])
|
374 |
image = get_ai_image_wrapper(ppt_text)
|
global_config.py
CHANGED
@@ -29,6 +29,7 @@ class GlobalConfig:
|
|
29 |
|
30 |
METAPHOR_API_KEY = os.environ.get('METAPHOR_API_KEY', '')
|
31 |
|
|
|
32 |
APP_STRINGS_FILE = 'strings.json'
|
33 |
PRELOAD_DATA_FILE = 'examples/example_02.json'
|
34 |
SLIDES_TEMPLATE_FILE = 'langchain_templates/template_07.txt'
|
|
|
29 |
|
30 |
METAPHOR_API_KEY = os.environ.get('METAPHOR_API_KEY', '')
|
31 |
|
32 |
+
LOG_LEVEL = 'INFO'
|
33 |
APP_STRINGS_FILE = 'strings.json'
|
34 |
PRELOAD_DATA_FILE = 'examples/example_02.json'
|
35 |
SLIDES_TEMPLATE_FILE = 'langchain_templates/template_07.txt'
|
llm_helper.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import json
|
|
|
2 |
import time
|
3 |
import metaphor_python as metaphor
|
4 |
import requests
|
@@ -8,6 +9,11 @@ from langchain.llms import Clarifai
|
|
8 |
from global_config import GlobalConfig
|
9 |
|
10 |
|
|
|
|
|
|
|
|
|
|
|
11 |
prompt = None
|
12 |
llm_contents = None
|
13 |
llm_json = None
|
@@ -39,7 +45,7 @@ def get_llm(use_gpt: bool) -> Clarifai:
|
|
39 |
verbose=True,
|
40 |
# temperature=0.1,
|
41 |
)
|
42 |
-
print(llm)
|
43 |
|
44 |
return llm
|
45 |
|
@@ -62,7 +68,7 @@ def generate_slides_content(topic: str) -> str:
|
|
62 |
prompt = PromptTemplate.from_template(template_txt)
|
63 |
|
64 |
formatted_prompt = prompt.format(topic=topic)
|
65 |
-
print(f'formatted_prompt:\n{formatted_prompt}')
|
66 |
|
67 |
if llm_contents is None:
|
68 |
llm_contents = get_llm(use_gpt=False)
|
@@ -91,7 +97,7 @@ def text_to_json(content: str) -> str:
|
|
91 |
text = text.replace('<REPLACE_PLACEHOLDER>', content)
|
92 |
|
93 |
text = text.strip()
|
94 |
-
print(text)
|
95 |
|
96 |
if llm_json is None:
|
97 |
llm_json = get_llm(use_gpt=True)
|
@@ -152,7 +158,7 @@ Output:
|
|
152 |
'''
|
153 |
|
154 |
text = text.strip()
|
155 |
-
print(text)
|
156 |
|
157 |
if llm_json is None:
|
158 |
llm_json = get_llm(use_gpt=True)
|
@@ -208,8 +214,8 @@ def get_ai_image(text: str) -> str:
|
|
208 |
]
|
209 |
}
|
210 |
|
211 |
-
print('*** AI image generator...')
|
212 |
-
print(url)
|
213 |
|
214 |
start = time.time()
|
215 |
response = requests.post(
|
@@ -219,16 +225,16 @@ def get_ai_image(text: str) -> str:
|
|
219 |
)
|
220 |
stop = time.time()
|
221 |
|
222 |
-
print('Response:', response, response.status_code)
|
223 |
-
|
224 |
img_data = ''
|
225 |
|
226 |
if response.ok:
|
227 |
-
print('*** Clarifai SDXL request: Response OK')
|
228 |
json_data = json.loads(response.text)
|
229 |
img_data = json_data['outputs'][0]['data']['image']['base64']
|
230 |
else:
|
231 |
-
|
232 |
|
233 |
return img_data
|
234 |
|
|
|
1 |
import json
|
2 |
+
import logging
|
3 |
import time
|
4 |
import metaphor_python as metaphor
|
5 |
import requests
|
|
|
9 |
from global_config import GlobalConfig
|
10 |
|
11 |
|
12 |
+
logging.basicConfig(
|
13 |
+
level=GlobalConfig.LOG_LEVEL,
|
14 |
+
format='%(asctime)s - %(message)s',
|
15 |
+
)
|
16 |
+
|
17 |
prompt = None
|
18 |
llm_contents = None
|
19 |
llm_json = None
|
|
|
45 |
verbose=True,
|
46 |
# temperature=0.1,
|
47 |
)
|
48 |
+
# print(llm)
|
49 |
|
50 |
return llm
|
51 |
|
|
|
68 |
prompt = PromptTemplate.from_template(template_txt)
|
69 |
|
70 |
formatted_prompt = prompt.format(topic=topic)
|
71 |
+
# print(f'formatted_prompt:\n{formatted_prompt}')
|
72 |
|
73 |
if llm_contents is None:
|
74 |
llm_contents = get_llm(use_gpt=False)
|
|
|
97 |
text = text.replace('<REPLACE_PLACEHOLDER>', content)
|
98 |
|
99 |
text = text.strip()
|
100 |
+
# print(text)
|
101 |
|
102 |
if llm_json is None:
|
103 |
llm_json = get_llm(use_gpt=True)
|
|
|
158 |
'''
|
159 |
|
160 |
text = text.strip()
|
161 |
+
# print(text)
|
162 |
|
163 |
if llm_json is None:
|
164 |
llm_json = get_llm(use_gpt=True)
|
|
|
214 |
]
|
215 |
}
|
216 |
|
217 |
+
# print('*** AI image generator...')
|
218 |
+
# print(url)
|
219 |
|
220 |
start = time.time()
|
221 |
response = requests.post(
|
|
|
225 |
)
|
226 |
stop = time.time()
|
227 |
|
228 |
+
# print('Response:', response, response.status_code)
|
229 |
+
logging.debug('Image generation took', stop - start, 'seconds')
|
230 |
img_data = ''
|
231 |
|
232 |
if response.ok:
|
233 |
+
# print('*** Clarifai SDXL request: Response OK')
|
234 |
json_data = json.loads(response.text)
|
235 |
img_data = json_data['outputs'][0]['data']['image']['base64']
|
236 |
else:
|
237 |
+
logging.error('*** Image generation failed:', response.text)
|
238 |
|
239 |
return img_data
|
240 |
|
pptx_helper.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
from typing import List, Tuple
|
2 |
import json5
|
|
|
3 |
import pptx
|
4 |
import re
|
5 |
import yaml
|
@@ -9,6 +10,11 @@ from global_config import GlobalConfig
|
|
9 |
|
10 |
PATTERN = re.compile(r"^slide[ ]+\d+:", re.IGNORECASE)
|
11 |
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
def remove_slide_number_from_heading(header: str) -> str:
|
14 |
"""
|
@@ -45,13 +51,13 @@ def generate_powerpoint_presentation(
|
|
45 |
try:
|
46 |
parsed_data = yaml.safe_load(structured_data)
|
47 |
except yaml.parser.ParserError as ype:
|
48 |
-
|
49 |
parsed_data = {'title': '', 'slides': []}
|
50 |
else:
|
51 |
# The structured "JSON" might contain trailing commas, so using json5
|
52 |
parsed_data = json5.loads(structured_data)
|
53 |
|
54 |
-
|
55 |
presentation = pptx.Presentation(GlobalConfig.PPTX_TEMPLATE_FILES[slides_template]['file'])
|
56 |
|
57 |
# The title slide
|
@@ -60,7 +66,7 @@ def generate_powerpoint_presentation(
|
|
60 |
title = slide.shapes.title
|
61 |
subtitle = slide.placeholders[1]
|
62 |
title.text = parsed_data['title']
|
63 |
-
|
64 |
subtitle.text = 'by Myself and SlideDeck AI :)'
|
65 |
all_headers = [title.text, ]
|
66 |
|
|
|
1 |
from typing import List, Tuple
|
2 |
import json5
|
3 |
+
import logging
|
4 |
import pptx
|
5 |
import re
|
6 |
import yaml
|
|
|
10 |
|
11 |
PATTERN = re.compile(r"^slide[ ]+\d+:", re.IGNORECASE)
|
12 |
|
13 |
+
logging.basicConfig(
|
14 |
+
level=GlobalConfig.LOG_LEVEL,
|
15 |
+
format='%(asctime)s - %(message)s',
|
16 |
+
)
|
17 |
+
|
18 |
|
19 |
def remove_slide_number_from_heading(header: str) -> str:
|
20 |
"""
|
|
|
51 |
try:
|
52 |
parsed_data = yaml.safe_load(structured_data)
|
53 |
except yaml.parser.ParserError as ype:
|
54 |
+
logging.error(f'*** YAML parse error: {ype}')
|
55 |
parsed_data = {'title': '', 'slides': []}
|
56 |
else:
|
57 |
# The structured "JSON" might contain trailing commas, so using json5
|
58 |
parsed_data = json5.loads(structured_data)
|
59 |
|
60 |
+
logging.debug(f"*** Using PPTX template: {GlobalConfig.PPTX_TEMPLATE_FILES[slides_template]['file']}")
|
61 |
presentation = pptx.Presentation(GlobalConfig.PPTX_TEMPLATE_FILES[slides_template]['file'])
|
62 |
|
63 |
# The title slide
|
|
|
66 |
title = slide.shapes.title
|
67 |
subtitle = slide.placeholders[1]
|
68 |
title.text = parsed_data['title']
|
69 |
+
logging.debug(f'Title is: {title.text}')
|
70 |
subtitle.text = 'by Myself and SlideDeck AI :)'
|
71 |
all_headers = [title.text, ]
|
72 |
|