Skip to content

Repurpose Any Piece of Content Into 3 Other ContentTypes

🔑 ID:

53776

👨‍💻

Python

🕒

28/06/2024
Free

Description:

This code is based on a blog post I posted, which will help you repurpose one piece of content into three other types and save them all in a json formatted output.

To learn more about how the code works, check this post.

The resources file imported into the script contains all the prompts used.

Code:

import json
from SimplerLLM.tools.generic_loader import load_content
from SimplerLLM.language.llm import LLM, LLMProvider
from resources import text_to_x_thread, text_to_summary, text_to_newsletter, format_to_json

llm_instance = LLM.create(provider=LLMProvider.OPENAI, model_name="gpt-4o")

#This can take a youtube video link, a blog post link, a csv file, etc... as input too 
file = load_content("https://learnwithhasan.com/create-ai-agents-with-python/") 

#Getting the 3 inputs
x_prompt = text_to_x_thread.format(input = file.content) 
newsletter_prompt = text_to_newsletter.format(input = file.content) 
summary_prompt = text_to_summary.format(input = file.content) 

#Generating the 3 types of social posts
x_thread = llm_instance.generate_response(prompt = x_prompt, max_tokens=1000)
with open("twitter.txt", "w", encoding='utf-8') as f:
    f.write(x_thread)

newsletter_section = llm_instance.generate_response(prompt = newsletter_prompt, max_tokens=1000)
with open("newsletter.txt", "w", encoding='utf-8') as f:
    f.write(newsletter_section)

bullet_point_summary = llm_instance.generate_response(prompt = summary_prompt, max_tokens=1000)
with open("summary.txt", "w", encoding='utf-8') as f:
    f.write(bullet_point_summary)

#Converting them into json format
final_prompt = format_to_json.format(input_1 = x_thread, 
                                     input_2 = newsletter_section, 
                                     input_3 = bullet_point_summary) 

response = llm_instance.generate_response(prompt = final_prompt, max_tokens=3000)
    
# Validate and write JSON with indentation for readability
try:
    json_data = json.loads(response)
    with open("Json_Result.json", "w", encoding='utf-8') as f:
        json.dump(json_data, f, ensure_ascii=False, indent=4)
    print("JSON saved successfully.")
except json.JSONDecodeError as e:
    print("Error in JSON format:", e)
    with open("Json_Result.json", "w", encoding='utf-8') as f:
        f.write(response) 

 

Untitled design (82)

GitHub Link

✖️ Not Available

Untitled design (83)

Download File

✖️ Not Available

If you’re encountering any problems or need further assistance with this code, we’re here to help! Join our community on the forum or Discord for support, tips, and discussion.