AJaySi / AI-Writer

ALwrity - All in One AI Content Platform. Contextual AI Content generation for Website, Social media & Ad copywriting. Prevents AI hallucinations, Web Researched factual, SEO optimized content. Get blog Images. Create your AI Agents Content team. Generate content with RAG, Local documents, web urls, PDFs. Open Source AI writer. (WIP)
https://alwrity.com
236 stars 76 forks source link

AI tool generator for OpenGraph Tags #103

Closed AJaySi closed 2 months ago

AJaySi commented 2 months ago

AI tool generator for OpenGraph Tags. This will help in SEO of the social media posts.

AJaySi commented 2 months ago
import streamlit as st
from bs4 import BeautifulSoup
import requests
import spacy

nlp = spacy.load("en_core_web_sm")  # Load a small English language model

st.title("Open Graph Tag Generator")

url = st.text_input("Enter URL:")
title = st.text_input("Override Title (optional):")
description = st.text_area("Override Description (optional):")
image_url = st.text_input("Override Image URL (optional):")

if st.button("Generate Open Graph Tags"):
    if url:
        try:
            response = requests.get(url)
            soup = BeautifulSoup(response.content, 'html.parser')
            extracted_title = soup.find('title').text
            extracted_description = soup.find('meta', attrs={'name': 'description'})['content'] if soup.find('meta', attrs={'name': 'description'}) else None
            extracted_image_url = soup.find('meta', attrs={'property': 'og:image'})['content'] if soup.find('meta', attrs={'property': 'og:image'}) else None
        except:
            extracted_title = None
            extracted_description = None
            extracted_image_url = None

        og_tags = """
        <meta property="og:title" content="{title}">
        <meta property="og:type" content="website">
        <meta property="og:url" content="{url}">
        <meta property="og:image" content="{image_url}">
        <meta property="og:description" content="{description}">
        """.format(
            title=title or extracted_title,
            url=url,
            image_url=image_url or extracted_image_url,
            description=description or extracted_description
        )

        st.code(og_tags, language='html')
    else:
        st.warning("Please enter a valid URL.")

Explanation:

  1. Import Libraries: Import necessary libraries: streamlit, BeautifulSoup, requests, and spacy.
  2. Set Up Streamlit App:
    • st.title("Open Graph Tag Generator") - Set the title of your app.
  3. Input Fields:
    • st.text_input("Enter URL:") - Create a text input field for the user to enter the URL.
    • st.text_input("Override Title (optional):") - Create a text input field for the user to optionally override the title.
    • st.text_area("Override Description (optional):") - Create a text area for the user to optionally override the description.
    • st.text_input("Override Image URL (optional):") - Create a text input field for the user to optionally override the image URL.
  4. Generate Button:
    • st.button("Generate Open Graph Tags") - Create a button that triggers the tag generation when clicked.
  5. Generate Open Graph Tags (Inside the Button's Logic):
    • if url: - Check if the user has entered a URL.
    • Fetch Content:
      • response = requests.get(url) - Use requests to get the HTML content of the URL.
      • soup = BeautifulSoup(response.content, 'html.parser') - Parse the HTML content with BeautifulSoup.
      • Extract Information:
      • extracted_title = soup.find('title').text - Try to extract the title from the HTML.
      • extracted_description = soup.find('meta', attrs={'name': 'description'})['content'] - Try to extract the description from the HTML.
      • extracted_image_url = soup.find('meta', attrs={'property': 'og:image'})['content'] - Try to extract the og:image URL from the HTML.
    • Handle Errors:
      • except: - If any errors occur while fetching or parsing the content, set the extracted values to None.
    • Format OG Tags:
      • Use f-strings or format() to create the HTML code for the OG tags, using the provided values or the extracted values.
    • Display Results:
      • st.code(og_tags, language='html') - Display the generated HTML code using st.code().
  6. Error Handling:
    • st.warning("Please enter a valid URL.") - Display a warning message if the user has not entered a valid URL.

To run this code:

  1. Install Requirements:
    pip install streamlit beautifulsoup4 requests spacy en_core_web_sm
  2. Save the code as app.py.
  3. Run:
    streamlit run app.py

    This will open the app in your web browser.

Enhancements:

This Streamlit app will give you a good starting point for building your Open Graph tag generator. Feel free to customize and extend it further.

AJaySi commented 2 months ago

Committed changes.