import streamlit as st import tempfile import os import shutil from langchain.embeddings.openai import OpenAIEmbeddings from langchain.text_splitter import CharacterTextSplitter from langchain.vectorstores import FAISS from langchain_community.document_loaders import WebBaseLoader from langchain.chains.question_answering import load_qa_chain from langchain_openai import ChatOpenAI import os from reportlab.lib.pagesizes import letter from reportlab.pdfgen import canvas # Hardcoded OpenAI API Key os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY') # Streamlit UI st.title("🔍 AI Benefits Analysis for Any Company") # User input: Only Website URL (with placeholder) website_url = st.text_input("Enter Website URL", placeholder="e.g., https://www.companywebsite.com") # Fixed question for AI analysis fixed_question = ( "Analyze how Artificial Intelligence (AI) can benefit this company based on its industry, " "key operations, and challenges. Provide insights on AI-driven improvements in customer experience, " "automation, sales, risk management, decision-making, and innovation. Include an AI implementation roadmap, " "challenges, solutions, and future opportunities with real-world examples." ) # Temporary directory to store FAISS index temp_dir = tempfile.gettempdir() faiss_db_path = os.path.join(temp_dir, "faiss_index_dir") # Function to fetch and process website data def build_embeddings(url): st.info("Fetching and processing website data...") # Load website data loader = WebBaseLoader(url) raw_text = loader.load() # Chunking the fetched text text_splitter = CharacterTextSplitter(separator='\n', chunk_size=500, chunk_overlap=50) docs = text_splitter.split_documents(raw_text) # Creating embeddings embeddings = OpenAIEmbeddings() docsearch = FAISS.from_documents(docs, embeddings) # Save FAISS index if os.path.exists(faiss_db_path): shutil.rmtree(faiss_db_path) os.makedirs(faiss_db_path) docsearch.save_local(faiss_db_path) return docsearch # Function to save text to a .txt file def save_text_to_file(text, file_path): with open(file_path, "w") as f: f.write(text) # Function to generate PDF from text file def generate_pdf_from_txt(txt_file, pdf_file): with open(txt_file, "r") as f: lines = f.readlines() c = canvas.Canvas(pdf_file, pagesize=letter) c.setFont("Helvetica", 12) width, height = letter y_position = height - 40 # Start from top of the page # Add title to the PDF c.setFont("Helvetica-Bold", 16) c.drawString(40, y_position, "AI Benefits Analysis Report") y_position -= 30 # Reset font for text content c.setFont("Helvetica", 12) # Write each line of the text to the PDF for line in lines: if y_position < 40: # If near the bottom, create a new page c.showPage() c.setFont("Helvetica", 12) y_position = height - 40 c.drawString(40, y_position, line.strip()) y_position -= 14 c.showPage() c.save() # Run everything in one click if st.button("Get AI Insights") and website_url: docsearch = build_embeddings(website_url) # AI Benefits Analysis st.subheader("💬 AI Benefits Analysis") chain = load_qa_chain(ChatOpenAI(model="gpt-4o"), chain_type="stuff") docs = docsearch.similarity_search(fixed_question) response = chain.run(input_documents=docs, question=fixed_question) st.write("**AI Insights:**", response) # Save the AI insights as a .txt file txt_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt") save_text_to_file(response, txt_file.name) # Generate PDF from the .txt file pdf_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") generate_pdf_from_txt(txt_file.name, pdf_file.name) # Provide download link for the generated PDF with open(pdf_file.name, "rb") as f: st.download_button( label="Download AI Insights as PDF", data=f, file_name="ai_benefits_analysis_report.pdf", mime="application/pdf" )