GiantAnalytics commited on
Commit
7bbf2a5
·
verified ·
1 Parent(s): 86e5a6f

Creating app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -0
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import os
12
+ from reportlab.lib.pagesizes import letter
13
+ from reportlab.pdfgen import canvas
14
+
15
+ # Hardcoded OpenAI API Key
16
+ os.environ['OPENAI_API_KEY'] = os.getenv('OPENAI_API_KEY')
17
+
18
+ # Streamlit UI
19
+ st.title("🔍 AI Benefits Analysis for Any Company")
20
+
21
+ # User input: Only Website URL (with placeholder)
22
+ website_url = st.text_input("Enter Website URL", placeholder="e.g., https://www.companywebsite.com")
23
+
24
+ # Fixed question for AI analysis
25
+ fixed_question = (
26
+ "Analyze how Artificial Intelligence (AI) can benefit this company based on its industry, "
27
+ "key operations, and challenges. Provide insights on AI-driven improvements in customer experience, "
28
+ "automation, sales, risk management, decision-making, and innovation. Include an AI implementation roadmap, "
29
+ "challenges, solutions, and future opportunities with real-world examples."
30
+ )
31
+
32
+ # Temporary directory to store FAISS index
33
+ temp_dir = tempfile.gettempdir()
34
+ faiss_db_path = os.path.join(temp_dir, "faiss_index_dir")
35
+
36
+ # Function to fetch and process website data
37
+ def build_embeddings(url):
38
+ st.info("Fetching and processing website data...")
39
+
40
+ # Load website data
41
+ loader = WebBaseLoader(url)
42
+ raw_text = loader.load()
43
+
44
+ # Chunking the fetched text
45
+ text_splitter = CharacterTextSplitter(separator='\n', chunk_size=500, chunk_overlap=50)
46
+ docs = text_splitter.split_documents(raw_text)
47
+
48
+ # Creating embeddings
49
+ embeddings = OpenAIEmbeddings()
50
+ docsearch = FAISS.from_documents(docs, embeddings)
51
+
52
+ # Save FAISS index
53
+ if os.path.exists(faiss_db_path):
54
+ shutil.rmtree(faiss_db_path)
55
+ os.makedirs(faiss_db_path)
56
+ docsearch.save_local(faiss_db_path)
57
+
58
+ return docsearch
59
+
60
+ # Function to save text to a .txt file
61
+ def save_text_to_file(text, file_path):
62
+ with open(file_path, "w") as f:
63
+ f.write(text)
64
+
65
+ # Function to generate PDF from text file
66
+ def generate_pdf_from_txt(txt_file, pdf_file):
67
+ with open(txt_file, "r") as f:
68
+ lines = f.readlines()
69
+
70
+ c = canvas.Canvas(pdf_file, pagesize=letter)
71
+ c.setFont("Helvetica", 12)
72
+ width, height = letter
73
+ y_position = height - 40 # Start from top of the page
74
+
75
+ # Add title to the PDF
76
+ c.setFont("Helvetica-Bold", 16)
77
+ c.drawString(40, y_position, "AI Benefits Analysis Report")
78
+ y_position -= 30
79
+
80
+ # Reset font for text content
81
+ c.setFont("Helvetica", 12)
82
+
83
+ # Write each line of the text to the PDF
84
+ for line in lines:
85
+ if y_position < 40: # If near the bottom, create a new page
86
+ c.showPage()
87
+ c.setFont("Helvetica", 12)
88
+ y_position = height - 40
89
+ c.drawString(40, y_position, line.strip())
90
+ y_position -= 14
91
+
92
+ c.showPage()
93
+ c.save()
94
+
95
+ # Run everything in one click
96
+ if st.button("Get AI Insights") and website_url:
97
+ docsearch = build_embeddings(website_url)
98
+
99
+ # AI Benefits Analysis
100
+ st.subheader("💬 AI Benefits Analysis")
101
+
102
+ chain = load_qa_chain(ChatOpenAI(model="gpt-4o"), chain_type="stuff")
103
+ docs = docsearch.similarity_search(fixed_question)
104
+ response = chain.run(input_documents=docs, question=fixed_question)
105
+
106
+ st.write("**AI Insights:**", response)
107
+
108
+ # Save the AI insights as a .txt file
109
+ txt_file = tempfile.NamedTemporaryFile(delete=False, suffix=".txt")
110
+ save_text_to_file(response, txt_file.name)
111
+
112
+ # Generate PDF from the .txt file
113
+ pdf_file = tempfile.NamedTemporaryFile(delete=False, suffix=".pdf")
114
+ generate_pdf_from_txt(txt_file.name, pdf_file.name)
115
+
116
+ # Provide download link for the generated PDF
117
+ with open(pdf_file.name, "rb") as f:
118
+ st.download_button(
119
+ label="Download AI Insights as PDF",
120
+ data=f,
121
+ file_name="ai_benefits_analysis_report.pdf",
122
+ mime="application/pdf"
123
+ )