import streamlit as st import os import warnings from crewai import Crew from utils import get_openai_api_key import io from docx import Document from agents import create_agents from tasks import create_tasks # Suppress warnings warnings.filterwarnings('ignore') # Page configuration st.set_page_config( page_title="Research & Write Article Agent", page_icon="📝", layout="wide", initial_sidebar_state="expanded" ) # Custom CSS for better styling st.markdown(""" """, unsafe_allow_html=True) def setup_openai_api(): """Setup OpenAI API key""" try: api_key = get_openai_api_key() os.environ["OPENAI_API_KEY"] = api_key return True except Exception as e: st.error(f"Error setting up OpenAI API: {str(e)}") return False def generate_article(topic): """Generate article using CrewAI""" try: # Create agents and tasks planner, writer, editor = create_agents() plan, write, edit = create_tasks(planner, writer, editor) # Create crew crew = Crew( agents=[planner, writer, editor], tasks=[plan, write, edit], verbose=True ) # Run the crew result = crew.kickoff(inputs={"topic": topic}) return result except Exception as e: st.error(f"Error generating article: {str(e)}") return None def main(): # Header st.markdown('
Planner - Writer - Editor
', unsafe_allow_html=True) # Sidebar with st.sidebar: st.header("⚙️ Configuration") # API Key setup st.subheader("OpenAI API Key") api_key_input = st.text_input( "Enter your OpenAI API Key", type="password", help="Your OpenAI API key is required to run this application" ) if api_key_input: os.environ["OPENAI_API_KEY"] = api_key_input st.success("✅ API Key configured") # Agent information st.subheader("🤖 AI Agents") st.markdown(""" **Content Planner**: Researches and plans content structure\n **Content Writer**: Creates engaging articles\n **Editor**: Reviews and polishes content """) # Features st.subheader("✨ Features") st.markdown(""" • Comprehensive research\n • SEO optimization\n • Grammar checking\n • Style consistency\n • Markdown formatting """) # Main content area col1, col2, col3 = st.columns([1, 2, 1]) with col2: # Topic input st.subheader("🎯 Enter Your Topic") topic = st.text_input( "What would you like to write about?", placeholder="e.g., AI in Healthcare, Climate Change Solutions, Digital Marketing Trends...", help="Enter a specific topic or subject for your article" ) # Generate button if st.button("🚀 Generate Article", type="primary", use_container_width=True): if not topic: st.warning("Please enter a topic to generate an article.") elif not os.getenv("OPENAI_API_KEY"): st.error("Please configure your OpenAI API key in the sidebar.") else: with st.spinner("🤖 AI agents are working on your article..."): # Progress bar progress_bar = st.progress(0) status_text = st.empty() # Simulate progress for i in range(3): if i == 0: status_text.text("📋 Planning content structure...") elif i == 1: status_text.text("✍️ Writing the article...") else: status_text.text("🔍 Editing and polishing...") progress_bar.progress((i + 1) * 33) # Generate article result = generate_article(topic) if result: progress_bar.progress(100) status_text.text("✅ Article generated successfully!") # --- Display Success Message --- st.success("🎉 Your article is ready!") # --- BLOG-STYLE OUTPUT (Minimalist & Clean) --- st.markdown("## 📰 Blog Article", unsafe_allow_html=True) # Convert Markdown-style headings (#, ##) into bold text def clean_markdown(text): lines = text.split("\n") formatted_lines = [] for line in lines: line = line.strip() if line.startswith("### "): line = f"{line[4:].strip()}" elif line.startswith("## "): line = f"{line[3:].strip()}" elif line.startswith("# "): line = f"{line[2:].strip()}" formatted_lines.append(line) return "Built with CrewAI and Streamlit | Research & Write Article Agent
" "Saad Toor | @saadtoorx
", unsafe_allow_html=True ) if __name__ == "__main__": main()