|
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 |
|
|
|
|
|
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)}") |
|
|
|
|
|
def initialize_swarm_client(): |
|
return Swarm() |
|
|
|
|
|
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') |
|
|
|
|
|
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() |
|
return {"text": text_content, "metadata": metadata} |
|
except requests.exceptions.RequestException as e: |
|
return f"Error during scraping: {str(e)}" |
|
|
|
|
|
def analyze_content(content): |
|
"""Analyzes the scraped content for key points.""" |
|
summary = f"Summary of content: {content[:500]}..." |
|
return summary |
|
|
|
|
|
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 |
|
|
|
|
|
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 |
|
for line in lines: |
|
if y < 50: |
|
pdf_canvas.showPage() |
|
y = 750 |
|
pdf_canvas.drawString(100, y, line) |
|
y -= 15 |
|
|
|
pdf_canvas.save() |
|
buffer.seek(0) |
|
return buffer |
|
|
|
|
|
st.markdown( |
|
""" |
|
<style> |
|
.title { text-align: center; font-size: 2.5rem; font-weight: bold; } |
|
.description { text-align: center; font-size: 1.1rem; color: #555; } |
|
.button-container { text-align: center; } |
|
.ack { font-size: 0.95rem; color: #888; text-align: center; } |
|
</style> |
|
""", |
|
unsafe_allow_html=True, |
|
) |
|
|
|
st.markdown('<div class="title">π Multi-Agent Web Content Analyzer</div>', unsafe_allow_html=True) |
|
st.markdown('<div class="description">Extract, analyze, and summarize web content with advanced capabilities.</div>', 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() |
|
|
|
|
|
st.subheader("π Enter the Website URL") |
|
url = st.text_input("Enter the URL of the website you want to scrape", placeholder="https://example.com") |
|
|
|
|
|
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): |
|
st.error(scrape_result) |
|
else: |
|
content = scrape_result["text"] |
|
metadata = scrape_result["metadata"] |
|
|
|
|
|
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) |
|
|
|
|
|
report_file_json = json.dumps({ |
|
"metadata": metadata, |
|
"summary": analysis_summary |
|
}, indent=4) |
|
report_file_txt = final_summary |
|
report_file_pdf = generate_pdf(final_summary) |
|
|
|
|
|
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.") |
|
|