Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import warnings
|
| 2 |
+
warnings.filterwarnings('ignore')
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
+
import json
|
| 6 |
+
import requests
|
| 7 |
+
from IPython.display import Markdown, display
|
| 8 |
+
import gradio as gr
|
| 9 |
+
|
| 10 |
+
# Attempt to read the OpenAI API key
|
| 11 |
+
try:
|
| 12 |
+
with open('mykey.txt', 'r') as file:
|
| 13 |
+
openai_key = file.read().strip()
|
| 14 |
+
except FileNotFoundError:
|
| 15 |
+
raise FileNotFoundError("The file 'mykey.txt' was not found. Please ensure the file exists and contains your OpenAI key.")
|
| 16 |
+
|
| 17 |
+
if not openai_key:
|
| 18 |
+
raise ValueError("OpenAI API key is empty. Please provide a valid API key in 'mykey.txt'.")
|
| 19 |
+
|
| 20 |
+
# Set environment variables
|
| 21 |
+
os.environ['OPENAI_API_KEY'] = openai_key
|
| 22 |
+
os.environ["OPENAI_MODEL_NAME"] = 'gpt-4'
|
| 23 |
+
|
| 24 |
+
from crewai import Agent, Task, Crew
|
| 25 |
+
import gradio as gr
|
| 26 |
+
|
| 27 |
+
# Define the Agent with improved documentation
|
| 28 |
+
readability_editor = Agent(
|
| 29 |
+
role="Blog language expert",
|
| 30 |
+
goal="Edit a given blog post from the field of AI to improve the readability of the blog.",
|
| 31 |
+
backstory=(
|
| 32 |
+
"""
|
| 33 |
+
You check and edit blog posts which are published on WordPress to improve their readability.
|
| 34 |
+
While editing, do not change quotes by people.
|
| 35 |
+
After editing, ensure that the word count is not less than 80% of the original content.
|
| 36 |
+
The blog post content is provided as: {blog}
|
| 37 |
+
"""
|
| 38 |
+
),
|
| 39 |
+
allow_delegation=False,
|
| 40 |
+
verbose=True
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Define the Task with clearer instructions
|
| 44 |
+
read = Task(
|
| 45 |
+
description=(
|
| 46 |
+
"""
|
| 47 |
+
1. Retain at least 80% of the blog's original content while removing unnecessary repetition.
|
| 48 |
+
2. Ensure over 90% of the article is written in active voice.
|
| 49 |
+
3. Ensure at least 15% of the sentences contain transition words (e.g., but, also, because, however, furthermore, therefore, meanwhile).
|
| 50 |
+
4. Correct language, word usage, and grammar.
|
| 51 |
+
5. If three consecutive sentences start with the same word, revise at least one so they do not all start identically.
|
| 52 |
+
6. Break long sentences so that no sentence is longer than 20 words.
|
| 53 |
+
7. Split long paragraphs so that no paragraph is longer than 80 words.
|
| 54 |
+
8. Adjust headings so no section is longer than 300 words or shorter than 100 words. Add or remove headings if needed.
|
| 55 |
+
9. Each section should have an appropriate heading (without numbers).
|
| 56 |
+
"""
|
| 57 |
+
),
|
| 58 |
+
expected_output="A well-written blog post in markdown format, ready for publication.",
|
| 59 |
+
agent=readability_editor,
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
crew = Crew(
|
| 63 |
+
agents=[readability_editor],
|
| 64 |
+
tasks=[read],
|
| 65 |
+
verbose=True
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
def improve_blog_post(blog_content: str) -> str:
|
| 69 |
+
# Validate the input
|
| 70 |
+
if not blog_content or len(blog_content.strip()) == 0:
|
| 71 |
+
return "Please provide some blog content to process."
|
| 72 |
+
|
| 73 |
+
# Run the pipeline
|
| 74 |
+
result = crew.kickoff(inputs={"blog": blog_content})
|
| 75 |
+
markdown_content = result.raw.strip("```markdown").strip("```").strip()
|
| 76 |
+
return markdown_content
|
| 77 |
+
|
| 78 |
+
# Create a Gradio Interface
|
| 79 |
+
# Input: Text area for the blog post
|
| 80 |
+
# Output: Markdown display of the improved blog
|
| 81 |
+
iface = gr.Interface(
|
| 82 |
+
fn=improve_blog_post,
|
| 83 |
+
inputs=gr.Textbox(lines=20, placeholder="Paste your blog content here..."),
|
| 84 |
+
outputs=gr.Markdown(),
|
| 85 |
+
title="AI Blog Readability Improver",
|
| 86 |
+
description=(
|
| 87 |
+
"Paste your blog content into the text box below and click 'Submit'. "
|
| 88 |
+
"The agent will improve the readability following Yoast-like guidelines, ensuring active voice, proper headings, and more."
|
| 89 |
+
),
|
| 90 |
+
allow_flagging="never"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# Launch the interface (this will be used in Hugging Face Spaces)
|
| 94 |
+
if __name__ == "__main__":
|
| 95 |
+
iface.launch(server_name="0.0.0.0", server_port=7860)
|