import streamlit as st from swarm import Swarm, Agent from bs4 import BeautifulSoup import requests import os import json from io import BytesIO from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas # Function to fetch OpenAI API key def fetch_openai_api_key(): """Fetch the OpenAI API key from Streamlit secrets.""" try: secret_key = st.secrets.get("OPENAI_API_KEY", "") if secret_key: os.environ['OPENAI_API_KEY'] = secret_key else: st.warning("⚠️ OpenAI API Key is missing! Please check your secrets configuration.") except Exception as e: st.error(f"Error retrieving OpenAI API Key: {str(e)}") # Initialize the Swarm client def initialize_swarm_client(): return Swarm() # Define the scraping function def scrape_website(url): """Scrapes the content of the website.""" try: response = requests.get(url) response.raise_for_status() soup = BeautifulSoup(response.text, 'html.parser') # Extract metadata metadata = { "title": soup.title.string if soup.title else "N/A", "description": soup.find("meta", {"name": "description"})["content"] if soup.find("meta", {"name": "description"}) else "N/A", "keywords": soup.find("meta", {"name": "keywords"})["content"] if soup.find("meta", {"name": "keywords"}) else "N/A", } text_content = soup.get_text() # Extract text content return {"text": text_content, "metadata": metadata} except requests.exceptions.RequestException as e: return f"Error during scraping: {str(e)}" # Enhanced summarization function def analyze_content(content): """Analyzes the scraped content for key points.""" summary = f"Summary of content: {content[:500]}..." return summary # Define the writing function def write_summary(context_variables): """Writes a summary based on the analysis.""" analysis = context_variables.get('analysis', '') metadata = context_variables.get('metadata', {}) detailed_report = ( f"### Metadata:\n" f"**Title:** {metadata.get('title')}\n" f"**Description:** {metadata.get('description')}\n" f"**Keywords:** {metadata.get('keywords')}\n\n" f"### Content Summary:\n{analysis}" ) return detailed_report # Generate PDF report def generate_pdf(report): """Generate a PDF file from the report.""" buffer = BytesIO() pdf_canvas = canvas.Canvas(buffer, pagesize=letter) pdf_canvas.drawString(100, 750, "Web Content Analyzer Report") pdf_canvas.drawString(100, 735, "-" * 50) lines = report.split('\n') y = 700 # Start position for the text for line in lines: if y < 50: # Create a new page if content exceeds one page pdf_canvas.showPage() y = 750 pdf_canvas.drawString(100, y, line) y -= 15 pdf_canvas.save() buffer.seek(0) return buffer # Streamlit App UI st.markdown( """ """, unsafe_allow_html=True, ) st.markdown('
🔎 Multi-Agent Web Content Analyzer
', unsafe_allow_html=True) st.markdown('
Extract, analyze, and summarize web content with advanced capabilities.
', unsafe_allow_html=True) fetch_openai_api_key() if 'OPENAI_API_KEY' in os.environ and os.environ['OPENAI_API_KEY']: client = initialize_swarm_client() # Input field for the website URL st.subheader("🌍 Enter the Website URL") url = st.text_input("Enter the URL of the website you want to scrape", placeholder="https://example.com") # Run Workflow button if st.button("Run Workflow"): if url: with st.spinner("Running the multi-agent workflow... This may take a moment."): scrape_result = scrape_website(url) if isinstance(scrape_result, str): # Error handling st.error(scrape_result) else: content = scrape_result["text"] metadata = scrape_result["metadata"] # Analysis and writing analysis_summary = analyze_content(content) final_summary = write_summary({ "analysis": analysis_summary, "metadata": metadata, }) st.success("✅ Workflow complete!") st.write("### 📜 Final Report:") st.markdown(final_summary, unsafe_allow_html=True) # Prepare downloadable content report_file_json = json.dumps({ "metadata": metadata, "summary": analysis_summary }, indent=4) report_file_txt = final_summary report_file_pdf = generate_pdf(final_summary) # Download options st.download_button( label="Download Report as JSON", data=report_file_json, file_name="report.json", mime="application/json" ) st.download_button( label="Download Report as TXT", data=report_file_txt, file_name="report.txt", mime="text/plain" ) st.download_button( label="Download Report as PDF", data=report_file_pdf, file_name="report.pdf", mime="application/pdf" ) else: st.error("❌ Please enter a valid URL.") else: st.sidebar.warning("⚠️ OpenAI API Key not set. Please check your secrets configuration.")