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 print(HF_TOKEN) llm = HuggingFaceEndpoint( repo_id="mistralai/Mistral-7B-Instruct-v0.3", huggingfacehub_api_token=HF_TOKEN.strip(), temperature=0.7, max_new_tokens=200 ) def check_and_improve_seo(content): # Define basic SEO criteria keywords = ["SEO", "content", "optimization", "keywords", "readability"] 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 relevant keywords, " "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 # Define SEO checks seo_checks = { "Keywords Present": keyword_found, "Readability Score (Flesch)": readability_score, "Optimized Content": optimized_content } return seo_checks # Define Gradio interface interface = gr.Interface( fn=check_and_improve_seo, inputs=gr.Textbox(lines=10, placeholder="Enter your content here..."), outputs="json", 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()