Spaces:
Runtime error
Runtime error
File size: 2,241 Bytes
1503dc9 e30adea 1503dc9 4b957b8 1503dc9 cb7401f 1503dc9 cb7401f 1503dc9 cb7401f 1503dc9 cb7401f 1503dc9 cb7401f 1503dc9 ef529cc 1503dc9 cb7401f 1503dc9 cb7401f 1503dc9 cb7401f 1503dc9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
import gradio as gr
import textstat
from langchain_huggingface import HuggingFaceEndpoint
import os
# Set up Hugging Face API token and model endpoint
HF_TOKEN = os.getenv("HF_TOKEN") # Ensure you have your token set in your environment
llm = HuggingFaceEndpoint(
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
huggingfacehub_api_token=HF_TOKEN,
temperature=0.7,
max_new_tokens=500
)
def generate_keywords(content):
prompt = f"Generate a list of most appropriate 10 SEO keywords for the following content:\n\n{content}"
response = llm(prompt)
keywords = response.split(",") # Assuming the model returns a comma-separated list
return [keyword.strip() for keyword in keywords]
def check_and_improve_seo(content):
# Define basic SEO criteria
keywords = generate_keywords(content)
keyword_found = any(keyword.lower() in content.lower() for keyword in keywords)
# Check readability score
readability_score = textstat.flesch_reading_ease(content)
# Prepare a prompt for the LLM to improve content
prompt = (
"Optimize the following content for SEO. Ensure it includes appropriate keywords in text, "
"is easy to read, and meets SEO best practices.\n\n"
"Content:\n" + content
)
# Generate SEO-optimized content using the Hugging Face model
response = llm(prompt)
optimized_content = response
# Format the output as plain text
output = (
# f"**Generated Keywords:**\n\n"
#f"Relevant SEO keywords: {', '.join(keywords)}\n\n"
f"**Keywords Present:** {keyword_found}\n\n"
f"**Readability Score (Flesch):** {readability_score}\n\n"
f"**Optimized Content:**\n{optimized_content}"
)
return output
# Define Gradio interface
interface = gr.Interface(
fn=check_and_improve_seo,
inputs=gr.Textbox(lines=10, placeholder="Enter your content here..."),
outputs="text", # Change output to 'text' to return plain text
title="SEO Compatibility Checker and Optimizer",
description="Check if the given content is SEO compatible and get an improved version based on SEO best practices."
)
# Launch the app
if __name__ == "__main__":
interface.launch()
|