From 4d887c87ebca7ad50b9f4fe998edf510d5cbb8ce Mon Sep 17 00:00:00 2001 From: ajaysi Date: Mon, 5 Aug 2024 10:17:30 +0530 Subject: [PATCH] AI SEO alt text generator --- README.md | 7 +- lib/ai_seo_tools/image_alt_text_generator.py | 77 ++++++++++++++++++ .../plagiarism_checker_from_known_sources.py | 80 ------------------- lib/utils/alwrity_utils.py | 4 + 4 files changed, 86 insertions(+), 82 deletions(-) create mode 100644 lib/ai_seo_tools/image_alt_text_generator.py delete mode 100644 lib/ai_seo_tools/plagiarism_checker_from_known_sources.py diff --git a/README.md b/README.md index 3f8c34f8..0bd066d3 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ Alwrity gives hyper content personalization, factual web researched & SEO optimi AI will help achieve Content Hyper-Personalization. ![](https://github.com/AJaySi/AI-Writer/blob/main/lib/workspace/keyword_blog.gif) --- + 1). [Visit alwrity.com](https://www.alwrity.com/ai-writing-tools), You will find AI content writing tools, which are Free & No-Signup. **Note:** Although, this is limited, as is our wallet & Resources. @@ -19,15 +20,17 @@ If you have πŸ’» Laptop + πŸ›œ Internet + 10 minutes, you will be generating blo ### [Step-By-Step: Getting Started for Absolute Begginers](https://www.alwrity.com/post/getting-started-with-alwrity-ai-writer) --- -### [Getting started for Developers](https://github.com/AJaySi/AI-Writer/wiki/Alwrity--%E2%80%90-Get-started) + +### [Getting started for Developers](https://github.com/AJaySi/AI-Writer/wiki/Getting-started-with-ALwrity-AI-writer) ``` 1). git clone https://github.com/AJaySi/AI-Writer.git -2). pip install -r -U requirements.txt +2). pip install -r requirements.txt 3). streamlit run alwrity.py 4). Visit Alwrity UI in a Browser & Start generation AI personalized content. ``` --- + ### Updating to latest Code: (Existing users) ``` 1). Git pull diff --git a/lib/ai_seo_tools/image_alt_text_generator.py b/lib/ai_seo_tools/image_alt_text_generator.py new file mode 100644 index 00000000..fbe97fe0 --- /dev/null +++ b/lib/ai_seo_tools/image_alt_text_generator.py @@ -0,0 +1,77 @@ +import streamlit as st +import base64 +import requests +from PIL import Image +import os + + +# Function to encode the image +def encode_image(image_path): + with open(image_path, "rb") as image_file: + return base64.b64encode(image_file.read()).decode('utf-8') + +def get_image_description(image_path): + base64_image = encode_image(image_path) + + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {os.getenv('OPENAI_API_KEY')}" + } + + payload = { + "model": "gpt-4o-mini", + "messages": [ + { + "role": "user", + "content": [ + { + "type": "text", + "text": """You are an SEO expert specializing in writing optimized Alt text for images. + Your goal is to create clear, descriptive, and concise Alt text that accurately represents + the content and context of the given image. Make sure your response is optimized for search engines and accessibility.""" + }, + { + "type": "image_url", + "image_url": { + "url": f"data:image/jpeg;base64,{base64_image}" + } + } + ] + } + ], + "max_tokens": 300 + } + + response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload) + response_data = response.json() + + # Extract the content field from the response + content = response_data['choices'][0]['message']['content'] + return content + + +def alt_text_gen(): + st.title("Image Description Generator") + + image_path = st.text_input("Enter the full path of the image file") + + if image_path: + if os.path.exists(image_path) and image_path.lower().endswith(('jpg', 'jpeg', 'png')): + try: + image = Image.open(image_path) + st.image(image, caption='Uploaded Image', use_column_width=True) + + if st.button("Get Image Alt Text"): + with st.spinner("Generating Alt Text..."): + try: + description = get_image_description(image_path) + st.success("Alt Text generated successfully!") + st.write("ALt Text:", description) + except Exception as e: + st.error(f"Error generating description: {e}") + except Exception as e: + st.error(f"Error processing image: {e}") + else: + st.error("Please enter a valid image file path ending with .jpg, .jpeg, or .png") + else: + st.info("Please enter the full path of an image file.") diff --git a/lib/ai_seo_tools/plagiarism_checker_from_known_sources.py b/lib/ai_seo_tools/plagiarism_checker_from_known_sources.py deleted file mode 100644 index 1bcc9ca1..00000000 --- a/lib/ai_seo_tools/plagiarism_checker_from_known_sources.py +++ /dev/null @@ -1,80 +0,0 @@ -import re - -####################################################################### -# -# I want you to act as a plagiarism checker. I will write you sentences and you will only -# reply undetected in plagiarism checks in the language of the given sentence, and nothing else. -# Do not write explanations on replies. My first sentence is {} -# -###################################################################### - - -class PlagiarismChecker: - def __init__(self, known_sources): - self.known_sources = known_sources - - def check_plagiarism(self, html_content): - try: - # Preprocess the HTML content by removing HTML tags and extra spaces - text = re.sub(r'<[^>]+>', ' ', html_content) - text = re.sub(r'\s+', ' ', text).strip().lower() - - # Check for exact matches with known sources - for source in self.known_sources: - source_text = re.sub(r'<[^>]+>', ' ', source) - source_text = re.sub(r'\s+', ' ', source_text).strip().lower() - if text == source_text: - return f"Plagiarism detected: Matches known source - {source}" - - # If no exact matches are found - return "No plagiarism detected. Content is original." - - except Exception as e: - return str(e) - -# Example usage: -if __name__ == "__main__": - # List of known sources - known_sources = [ - """ - - - Sample Page 1 - - -

Hello, World!

-

This is sample content from known source 1.

- - - """, - """ - - - Sample Page 2 - - -

Welcome to Known Source 2

-

This is some content from another known source.

- - - """ - ] - - # HTML content to check for plagiarism - html_content = """ - - - Sample Page - - -

Hello, World!

-

This is sample content.

- - - """ - - plagiarism_checker = PlagiarismChecker(known_sources) - result = plagiarism_checker.check_plagiarism(html_content) - - print(result) - diff --git a/lib/utils/alwrity_utils.py b/lib/utils/alwrity_utils.py index 001bb909..43caa00a 100644 --- a/lib/utils/alwrity_utils.py +++ b/lib/utils/alwrity_utils.py @@ -41,6 +41,7 @@ from lib.ai_writers.ai_essay_writer import ai_essay_generator from lib.ai_seo_tools.seo_structured_data import ai_structured_data from lib.ai_seo_tools.content_title_generator import ai_title_generator from lib.ai_seo_tools.meta_desc_generator import metadesc_generator_main +from lib.ai_seo_tools.image_alt_text_generator import alt_text_gen from lib.gpt_providers.text_to_image_generation.main_generate_image_from_prompt import generate_image from lib.content_planning_calender.content_planning_agents_alwrity_crew import ai_agents_planner from ..gpt_providers.text_generation.main_text_generation import llm_text_gen @@ -124,6 +125,7 @@ def ai_seo_tools(): "Generate Structured Data - Rich Snippet", "Generate SEO optimized Blog Titles", "Generate Meta Description for SEO", + "Generate Image Alt Text", "Quit" ] choice = st.selectbox("**πŸ‘‡Select AI SEO Tool:**", options, index=0, format_func=lambda x: f"πŸ“ {x}") @@ -134,6 +136,8 @@ def ai_seo_tools(): metadesc_generator_main() elif choice == "Generate SEO optimized Blog Titles": ai_title_generator() + elif choice == "Generate Image Alt Text": + alt_text_gen()