import warnings warnings.filterwarnings('ignore') import os import json import requests from IPython.display import Markdown, display import gradio as gr # Attempt to read the OpenAI API key try: with open('mykey.txt', 'r') as file: openai_key = file.read().strip() except FileNotFoundError: raise FileNotFoundError("The file 'mykey.txt' was not found. Please ensure the file exists and contains your OpenAI key.") if not openai_key: raise ValueError("OpenAI API key is empty. Please provide a valid API key in 'mykey.txt'.") # Set environment variables os.environ['OPENAI_API_KEY'] = openai_key os.environ["OPENAI_MODEL_NAME"] = 'gpt-4' from crewai import Agent, Task, Crew import gradio as gr # Define the Agent with improved documentation readability_editor = Agent( role="Blog language expert", goal="Edit a given blog post from the field of AI to improve the readability of the blog.", backstory=( """ You check and edit blog posts which are published on WordPress to improve their readability. While editing, do not change quotes by people. After editing, ensure that the word count is not less than 80% of the original content. The blog post content is provided as: {blog} """ ), allow_delegation=False, verbose=True ) # Define the Task with clearer instructions read = Task( description=( """ 1. Retain at least 80% of the blog's original content while removing unnecessary repetition. 2. Ensure over 90% of the article is written in active voice. 3. Ensure at least 15% of the sentences contain transition words (e.g., but, also, because, however, furthermore, therefore, meanwhile). 4. Correct language, word usage, and grammar. 5. If three consecutive sentences start with the same word, revise at least one so they do not all start identically. 6. Break long sentences so that no sentence is longer than 20 words. 7. Split long paragraphs so that no paragraph is longer than 80 words. 8. Adjust headings so no section is longer than 300 words or shorter than 100 words. Add or remove headings if needed. 9. Each section should have an appropriate heading (without numbers). """ ), expected_output="A well-written blog post in markdown format, ready for publication.", agent=readability_editor, ) crew = Crew( agents=[readability_editor], tasks=[read], verbose=True ) def improve_blog_post(blog_content: str) -> str: # Validate the input if not blog_content or len(blog_content.strip()) == 0: return "Please provide some blog content to process." # Run the pipeline result = crew.kickoff(inputs={"blog": blog_content}) markdown_content = result.raw.strip("```markdown").strip("```").strip() return markdown_content # Create a Gradio Interface # Input: Text area for the blog post # Output: Markdown display of the improved blog iface = gr.Interface( fn=improve_blog_post, inputs=gr.Textbox(lines=20, placeholder="Paste your blog content here..."), outputs=gr.Markdown(), title="AI Blog Readability Improver", description=( "Paste your blog content into the text box below and click 'Submit'. " "The agent will improve the readability following Yoast-like guidelines, ensuring active voice, proper headings, and more." ), allow_flagging="never" ) # Launch the interface (this will be used in Hugging Face Spaces) if __name__ == "__main__": iface.launch(server_name="0.0.0.0", server_port=7860)