CoolFace
Apppublic

sachinmosambe/Autonomous-Multi-Agent-Blog-Creation-System

sourceHugging Facemitupdated 1y agoView on Hugging Face
0likes
app.py101 linesDownload Raw Back to root
1import gradio as gr
2import os
3import sys
4import warnings
5from pathlib import Path
6from datetime import datetime
7import subprocess
8import tempfile
9
10# Ignore pysbd SyntaxWarning
11warnings.filterwarnings("ignore", category=SyntaxWarning, module="pysbd")
12
13# Add the project directory to the path to import the blog_writer module
14sys.path.append(str(Path(__file__).parent))
15
16# Import the BlogWriter crew    
17from src.blog_writer.crew import BlogWriter
18
19def generate_blog(topic):
20    """
21    Generate a blog post on the given topic using the blog writer crew.
22    
23    Args:
24        topic (str): The topic for the blog post
25    
26    Returns:
27        str: The generated blog post in markdown format
28    """
29    try:
30        print(f"\n=== Generating blog post about: {topic} ===\n")
31        
32        # Prepare inputs with topic and current date
33        inputs = {
34            'topic': topic,
35            "current_date": str(datetime.now())
36        }
37
38        # Create and run the crew
39        result = BlogWriter().crew().kickoff(inputs=inputs)
40        
41        # Check for report.md file first
42        report_path = Path('report.md')
43        if report_path.exists():
44            with open(report_path, 'r') as f:
45                blog_content = f.read()
46                # Clean up the markdown syntax if needed
47                if blog_content.startswith("```markdown"):
48                    blog_content = blog_content.replace("```markdown", "", 1)
49                if blog_content.endswith("```"):
50                    blog_content = blog_content[:-3]
51                return blog_content
52        
53        # If no report.md, use the result directly
54        if hasattr(result, 'raw'):
55            return result.raw
56        else:
57            return result
58            
59    except Exception as e:
60        return f"An error occurred while generating the blog: {str(e)}"
61
62# Create the Gradio interface
63with gr.Blocks(title="AI Blog Writer") as demo:
64    gr.Markdown("# AI Blog Writer")
65    gr.Markdown("Enter a topic and let our AI agents create a professional blog post for you!")
66    
67    with gr.Row():
68        with gr.Column():
69            topic_input = gr.Textbox(
70                label="Blog Topic",
71                placeholder="Enter the topic for your blog post (e.g., 'The Evolution of AI', 'Remote Work Trends', etc.)",
72                lines=2
73            )
74            generate_button = gr.Button("Generate Blog Post", variant="primary")
75        
76    with gr.Row():
77        with gr.Column():
78            output = gr.Markdown(label="Generated Blog Post")
79    
80    # Set up the button click event
81    generate_button.click(
82        fn=generate_blog,
83        inputs=[topic_input],
84        outputs=[output],
85        api_name="generate"
86    )
87    
88    gr.Markdown("## How it works")
89    gr.Markdown("""
90    This application uses a team of AI agents to create your blog post:
91    
92    1. **Planner**: Researches the topic and creates an outline
93    2. **Writer**: Drafts the complete blog post based on the plan
94    3. **Editor**: Reviews and improves the content
95    4. **Reviewer**: Performs a final quality check
96    
97    The process may take a few minutes depending on the complexity of your topic.
98    """)
99
100if __name__ == "__main__":
101    demo.launch()