Spaces:
Runtime error
Runtime error
Update bf_trigger.py
Browse files- bf_trigger.py +3 -19
bf_trigger.py
CHANGED
|
@@ -277,23 +277,17 @@ def get_seo_tags(image_url, topical_map, new_imgs, attempts=0, max_attempts=5):
|
|
| 277 |
keys = ['topic', 'description', 'caption', 'seo', 'alt_tag', 'filename']
|
| 278 |
json_dict = ast.literal_eval(response.json()['choices'][0]['message']['content'])
|
| 279 |
if json_dict['topic'] not in topic_list:
|
| 280 |
-
st.write(response.json())
|
| 281 |
-
st.write('wrong_topic')
|
| 282 |
return get_seo_tags(image_path, topical_map, new_imgs, attempts=attempts+1)
|
| 283 |
if set(json_dict.keys()) != set(keys):
|
| 284 |
-
st.write(response.json())
|
| 285 |
-
st.write('incorrect json formatting')
|
| 286 |
return get_seo_tags(image_path, topical_map, new_imgs, attempts=attempts+1)
|
| 287 |
|
| 288 |
|
| 289 |
return json_dict
|
| 290 |
else:
|
| 291 |
-
st.write(response.json())
|
| 292 |
print("API call failed or bad data, retrying...")
|
| 293 |
return get_seo_tags(image_url, topical_map, new_imgs, attempts=attempts + 1)
|
| 294 |
except Exception as e:
|
| 295 |
print("Exception during API call:", str(e))
|
| 296 |
-
st.write(response.json())
|
| 297 |
return get_seo_tags(image_url, topical_map, new_imgs, attempts=attempts + 1)
|
| 298 |
|
| 299 |
# creates the asset in the client's brand folder
|
|
@@ -552,10 +546,8 @@ def delete_og_asset(asset_id):
|
|
| 552 |
|
| 553 |
return
|
| 554 |
|
| 555 |
-
def run_preprocess_ai(topical_map, client_name, section_type):
|
| 556 |
section_id = section_key_dict[section_type]
|
| 557 |
-
status_text = st.empty()
|
| 558 |
-
status_text.text("Getting Images...")
|
| 559 |
headers = {
|
| 560 |
'Content-Type': 'application/json',
|
| 561 |
'Authorization': brandfolder_api
|
|
@@ -578,21 +570,13 @@ def run_preprocess_ai(topical_map, client_name, section_type):
|
|
| 578 |
pre_process_ids.append(asset_names)
|
| 579 |
asset_names = sum(pre_process_ids, [])
|
| 580 |
|
| 581 |
-
progress_text = f"Images are being processed."
|
| 582 |
-
percent_complete = 0
|
| 583 |
-
status_text.text(f'{progress_text} 0% completed.')
|
| 584 |
-
my_bar = st.progress(percent_complete)
|
| 585 |
new_imgs = []
|
| 586 |
-
for
|
| 587 |
time.sleep(2)
|
| 588 |
-
asset_id = asset_names[i]
|
| 589 |
asset_dict = get_asset_info(asset_id)
|
| 590 |
new_img = create_ai_asset(asset_dict, topical_map, client_name, new_imgs)
|
| 591 |
new_imgs.append(new_img)
|
| 592 |
if new_img:
|
| 593 |
delete_og_asset(asset_id)
|
| 594 |
-
|
| 595 |
-
status_text.text(f'{progress_text} {round(prog*100,2)}% completed.')
|
| 596 |
-
my_bar.progress(prog)
|
| 597 |
-
status_text.text(f'AI Algorithm successfuly run!')
|
| 598 |
return
|
|
|
|
| 277 |
keys = ['topic', 'description', 'caption', 'seo', 'alt_tag', 'filename']
|
| 278 |
json_dict = ast.literal_eval(response.json()['choices'][0]['message']['content'])
|
| 279 |
if json_dict['topic'] not in topic_list:
|
|
|
|
|
|
|
| 280 |
return get_seo_tags(image_path, topical_map, new_imgs, attempts=attempts+1)
|
| 281 |
if set(json_dict.keys()) != set(keys):
|
|
|
|
|
|
|
| 282 |
return get_seo_tags(image_path, topical_map, new_imgs, attempts=attempts+1)
|
| 283 |
|
| 284 |
|
| 285 |
return json_dict
|
| 286 |
else:
|
|
|
|
| 287 |
print("API call failed or bad data, retrying...")
|
| 288 |
return get_seo_tags(image_url, topical_map, new_imgs, attempts=attempts + 1)
|
| 289 |
except Exception as e:
|
| 290 |
print("Exception during API call:", str(e))
|
|
|
|
| 291 |
return get_seo_tags(image_url, topical_map, new_imgs, attempts=attempts + 1)
|
| 292 |
|
| 293 |
# creates the asset in the client's brand folder
|
|
|
|
| 546 |
|
| 547 |
return
|
| 548 |
|
| 549 |
+
def run_preprocess_ai(topical_map, client_name, section_type, progress=gr.Progress()):
|
| 550 |
section_id = section_key_dict[section_type]
|
|
|
|
|
|
|
| 551 |
headers = {
|
| 552 |
'Content-Type': 'application/json',
|
| 553 |
'Authorization': brandfolder_api
|
|
|
|
| 570 |
pre_process_ids.append(asset_names)
|
| 571 |
asset_names = sum(pre_process_ids, [])
|
| 572 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 573 |
new_imgs = []
|
| 574 |
+
for asset_id in progress.tqdm(asset_names, desc="Uploading..."):
|
| 575 |
time.sleep(2)
|
|
|
|
| 576 |
asset_dict = get_asset_info(asset_id)
|
| 577 |
new_img = create_ai_asset(asset_dict, topical_map, client_name, new_imgs)
|
| 578 |
new_imgs.append(new_img)
|
| 579 |
if new_img:
|
| 580 |
delete_og_asset(asset_id)
|
| 581 |
+
gr.Info('Images have been processed!')
|
|
|
|
|
|
|
|
|
|
| 582 |
return
|