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('