├── README.md ├── app.py ├── requirements.txt └── templates.py /README.md: -------------------------------------------------------------------------------- 1 | # 📝 Article Generator App 2 | 3 | ## Overview 4 | The Article Generator App is a Streamlit application that generates SEO-friendly blog articles using transcripts from YouTube videos. It leverages the OpenAI GPT-3.5-turbo model to create unique, engaging, and informative content based on provided guidelines. 5 | 6 | ## Setting Up 7 | 8 | 9 | ### Installation 10 | To get started, install the necessary Python packages: 11 | 12 | ```sh 13 | pip install -r requirements.txt 14 | ``` 15 | 16 | ## Usage 17 | 18 | 1. Open the Streamlit app in a Python environment. 19 | 2. Enter your OpenAI API key in the sidebar. 20 | 3. Paste the YouTube URL you want to use to generate the article in the provided text input field. 21 | 4. Click "Submit" to generate the blog post. 22 | # yt-to-blog 23 | -------------------------------------------------------------------------------- /app.py: -------------------------------------------------------------------------------- 1 | import streamlit as st 2 | from langchain import PromptTemplate 3 | from langchain.document_loaders import YoutubeLoader 4 | from langchain.chat_models import ChatOpenAI 5 | from langchain.output_parsers import StructuredOutputParser, ResponseSchema 6 | from huggingface_hub import InferenceClient 7 | from templates import BLOG_TEMPLATE, IMAGE_TEMPLATE 8 | 9 | CLIENT = InferenceClient() 10 | 11 | # Initialize Streamlit 12 | st.set_page_config(page_title ="📝 Article Generator App") 13 | st.title('📝 Article Generator App') 14 | 15 | # Getting the OpenAI API key from the sidebar 16 | openai_api_key = st.sidebar.text_input('OpenAI API Key', type='password') 17 | chat_model = None 18 | if openai_api_key.startswith('sk-'): 19 | chat_model = ChatOpenAI(model_name='gpt-3.5-turbo-16k', openai_api_key=openai_api_key) 20 | else: 21 | st.warning('Please enter a valid OpenAI API key!', icon='⚠') 22 | 23 | @st.cache_data () 24 | def generate_blog(yt_url): 25 | """Generate a blog article from a YouTube URL.""" 26 | loader = YoutubeLoader.from_youtube_url(yt_url, add_video_info=True) 27 | transcript = loader.load() 28 | 29 | """Create a response schema for structured output.""" 30 | schema = [ 31 | ResponseSchema(name="title", description="Article title"), 32 | ResponseSchema(name="meta_description", description="Article Meta Description"), 33 | ResponseSchema(name="content", description="Article content in markdown"), 34 | ] 35 | output_parser = StructuredOutputParser.from_response_schemas(schema) 36 | format_instructions = output_parser.get_format_instructions() 37 | 38 | prompt = PromptTemplate( 39 | input_variables=['transcript'], 40 | template=BLOG_TEMPLATE, 41 | partial_variables={"format_instructions": format_instructions} 42 | ) 43 | prompt_query = prompt.format(transcript=transcript[0].page_content) 44 | 45 | response = chat_model.predict(prompt_query) 46 | 47 | return output_parser.parse(response), transcript[0].metadata["thumbnail_url"] 48 | 49 | @st.cache_data () 50 | def generate_image(title): 51 | """Generate an image based on the title.""" 52 | prompt = PromptTemplate( 53 | input_variables=['title'], 54 | template=IMAGE_TEMPLATE, 55 | ) 56 | prompt_query = prompt.format(title=title) 57 | 58 | stb_prompt = chat_model.predict(prompt_query) 59 | 60 | tags = [ 61 | stb_prompt, 62 | 'award winning', 63 | 'high resolution', 64 | 'photo realistic', 65 | 'intricate details', 66 | 'beautiful', 67 | '[trending on artstation]' 68 | ] 69 | result = ', '.join(tags) 70 | response = CLIENT.post(json={ 71 | "inputs": result, 72 | "parameters": { "negative_prompt": 'blurry, artificial, cropped, low quality, ugly'} 73 | }, model="stabilityai/stable-diffusion-2-1") 74 | 75 | return response 76 | 77 | # Creating a form to get the YouTube URL 78 | with st.form('myform'): 79 | yt_url = st.text_input('Enter youtube url:', '') 80 | generate_image_option = st.checkbox('Generate Image Instead of Thumbnail') 81 | submitted = st.form_submit_button('Submit') 82 | 83 | if submitted and chat_model and yt_url: 84 | with st.spinner("Generating blog... This may take a while⏳"): 85 | blog, thumbnail = generate_blog(yt_url) 86 | if generate_image_option: 87 | with st.spinner("Generating image... This may take a while⏳"): 88 | image = generate_image(blog['title']) 89 | st.image(image) 90 | else: 91 | st.image(thumbnail) 92 | st.markdown(blog['content']) -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | streamlit 2 | langchain 3 | openai 4 | youtube-transcript-api 5 | pytube 6 | langchainhub 7 | huggingface_hub -------------------------------------------------------------------------------- /templates.py: -------------------------------------------------------------------------------- 1 | BLOG_TEMPLATE = """Act as an expert copywriter specializing in content optimization for SEO. Your task is to take a given YouTube transcript and transform it into a well-structured and engaging article. Your objectives are as follows: 2 | 3 | Content Transformation: Begin by thoroughly reading the provided YouTube transcript. Understand the main ideas, key points, and the overall message conveyed. 4 | 5 | Sentence Structure: While rephrasing the content, pay careful attention to sentence structure. Ensure that the article flows logically and coherently. 6 | 7 | Keyword Identification: Identify the main keyword or phrase from the transcript. It's crucial to determine the primary topic that the YouTube video discusses. 8 | 9 | Keyword Integration: Incorporate the identified keyword naturally throughout the article. Use it in headings, subheadings, and within the body text. However, avoid overuse or keyword stuffing, as this can negatively affect SEO. 10 | 11 | Unique Content: Your goal is to make the article 100% unique. Avoid copying sentences directly from the transcript. Rewrite the content in your own words while retaining the original message and meaning. 12 | 13 | SEO Friendliness: Craft the article with SEO best practices in mind. This includes optimizing meta tags (title and meta description), using header tags appropriately, and maintaining an appropriate keyword density. 14 | 15 | Engaging and Informative: Ensure that the article is engaging and informative for the reader. It should provide value and insight on the topic discussed in the YouTube video. 16 | 17 | Proofreading: Proofread the article for grammar, spelling, and punctuation errors. Ensure it is free of any mistakes that could detract from its quality. 18 | 19 | By following these guidelines, create a well-optimized, unique, and informative article that would rank well in search engine results and engage readers effectively. 20 | 21 | \n{format_instructions} 22 | 23 | Transcript:{transcript}""" 24 | 25 | 26 | IMAGE_TEMPLATE= """Write a Stable Diffusion prompts using the below formula with the title: {title} 27 | \n 28 | Here’s a formula for a Stable Diffusion image prompt: 29 | An image of [adjective] [subject] [doing action], [creative lighting style], detailed, realistic, trending on artstation, 30 | in style of [famous artist 1], [famous artist 2], [famous artist 3] 31 | \n 32 | Output with no introduction, only the prompt 33 | PROMPT: 34 | """ --------------------------------------------------------------------------------