AI SEO alt text generator
This commit is contained in:
77
lib/ai_seo_tools/image_alt_text_generator.py
Normal file
77
lib/ai_seo_tools/image_alt_text_generator.py
Normal file
@@ -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.")
|
||||
@@ -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 = [
|
||||
"""
|
||||
<html>
|
||||
<head>
|
||||
<title>Sample Page 1</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello, World!</h1>
|
||||
<p>This is sample content from known source 1.</p>
|
||||
</body>
|
||||
</html>
|
||||
""",
|
||||
"""
|
||||
<html>
|
||||
<head>
|
||||
<title>Sample Page 2</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Welcome to Known Source 2</h1>
|
||||
<p>This is some content from another known source.</p>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
]
|
||||
|
||||
# HTML content to check for plagiarism
|
||||
html_content = """
|
||||
<html>
|
||||
<head>
|
||||
<title>Sample Page</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Hello, World!</h1>
|
||||
<p>This is sample content.</p>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
plagiarism_checker = PlagiarismChecker(known_sources)
|
||||
result = plagiarism_checker.check_plagiarism(html_content)
|
||||
|
||||
print(result)
|
||||
|
||||
Reference in New Issue
Block a user