GiantAnalytics commited on
Commit
10eddfc
·
verified ·
1 Parent(s): 921f62f
Files changed (1) hide show
  1. app.py +108 -0
app.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tempfile
3
+ import os
4
+ import shutil
5
+ from langchain.embeddings.openai import OpenAIEmbeddings
6
+ from langchain.text_splitter import CharacterTextSplitter
7
+ from langchain.vectorstores import FAISS
8
+ from langchain_community.document_loaders import WebBaseLoader
9
+ from langchain.chains.question_answering import load_qa_chain
10
+ from langchain_openai import ChatOpenAI
11
+ from reportlab.lib.pagesizes import letter
12
+ from reportlab.pdfgen import canvas
13
+
14
+ # Hardcoded OpenAI API Key
15
+ os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY')
16
+
17
+ # Streamlit UI
18
+ st.title("🔍 AI Benefits Analysis for Any Company")
19
+
20
+ # User input: Only Website URL (with placeholder)
21
+ website_url = st.text_input("Enter Website URL", placeholder="e.g., https://www.companywebsite.com")
22
+
23
+ # Fixed question for AI analysis
24
+ fixed_question = (
25
+ "Analyze how Artificial Intelligence (AI) can benefit this company based on its industry, "
26
+ "key operations, and challenges. Provide insights on AI-driven improvements in customer experience, "
27
+ "automation, sales, risk management, decision-making, and innovation. Include an AI implementation roadmap, "
28
+ "challenges, solutions, and future opportunities with real-world examples."
29
+ )
30
+
31
+ # Temporary directory to store FAISS index
32
+ temp_dir = tempfile.gettempdir()
33
+ faiss_db_path = os.path.join(temp_dir, "faiss_index_dir")
34
+
35
+ # Function to fetch and process website data
36
+ def build_embeddings(url):
37
+ st.info("Fetching and processing website data...")
38
+
39
+ # Load website data
40
+ loader = WebBaseLoader(url)
41
+ raw_text = loader.load()
42
+
43
+ # Chunking the fetched text
44
+ text_splitter = CharacterTextSplitter(separator='\n', chunk_size=500, chunk_overlap=50)
45
+ docs = text_splitter.split_documents(raw_text)
46
+
47
+ # Creating embeddings
48
+ embeddings = OpenAIEmbeddings()
49
+ docsearch = FAISS.from_documents(docs, embeddings)
50
+
51
+ # Save FAISS index
52
+ if os.path.exists(faiss_db_path):
53
+ shutil.rmtree(faiss_db_path)
54
+ os.makedirs(faiss_db_path)
55
+ docsearch.save_local(faiss_db_path)
56
+
57
+ return docsearch
58
+
59
+ # Function to save text to a PDF file
60
+ def save_text_to_pdf(text, file_path):
61
+ c = canvas.Canvas(file_path, pagesize=letter)
62
+ width, height = letter
63
+
64
+ # Set title
65
+ c.setFont("Helvetica-Bold", 16)
66
+ c.drawString(30, height - 50, "AI Benefits Analysis Report")
67
+
68
+ # Set content font
69
+ c.setFont("Helvetica", 12)
70
+
71
+ # Define starting position for the text
72
+ text_object = c.beginText(30, height - 80)
73
+ text_object.setFont("Helvetica", 12)
74
+ text_object.setTextOrigin(30, height - 80)
75
+
76
+ # Add the AI Insights text line by line
77
+ for line in text.split("\n"):
78
+ text_object.textLine(line)
79
+
80
+ # Draw the text
81
+ c.drawText(text_object)
82
+ c.save()
83
+
84
+ # Run everything in one click
85
+ if st.button("Get AI Insights") and website_url:
86
+ docsearch = build_embeddings(website_url)
87
+
88
+ # AI Benefits Analysis
89
+ st.subheader("💬 AI Benefits Analysis")
90
+
91
+ chain = load_qa_chain(ChatOpenAI(model="gpt-4o"), chain_type="stuff")
92
+ docs = docsearch.similarity_search(fixed_question)
93
+ response = chain.run(input_documents=docs, question=fixed_question)
94
+
95
+ st.write("**AI Insights:**", response)
96
+
97
+ # Save the AI insights as a PDF
98
+ pdf_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
99
+ save_text_to_pdf(response, pdf_file.name)
100
+
101
+ # Provide download link for the generated PDF file
102
+ with open(pdf_file.name, "rb") as f:
103
+ st.download_button(
104
+ label="Download AI Insights as PDF File",
105
+ data=f,
106
+ file_name="ai_benefits_analysis_report.pdf",
107
+ mime="application/pdf"
108
+ )