DrishtiSharma
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#=================
|
2 |
+
# Import Libraries
|
3 |
+
#=================
|
4 |
+
|
5 |
+
import streamlit as st
|
6 |
+
from crewai import Agent, Task, Crew
|
7 |
+
import os
|
8 |
+
|
9 |
+
from fpdf import FPDF
|
10 |
+
import pandas as pd
|
11 |
+
import plotly.express as px
|
12 |
+
import time
|
13 |
+
|
14 |
+
#=================
|
15 |
+
# Add Streamlit Components
|
16 |
+
#=================
|
17 |
+
|
18 |
+
# Background
|
19 |
+
page_bg_img = '''
|
20 |
+
<style>
|
21 |
+
.stApp {
|
22 |
+
background-image: url("https://images.all-free-download.com/images/graphiclarge/abstract_bright_corporate_background_310453.jpg");
|
23 |
+
background-size: cover;
|
24 |
+
}
|
25 |
+
</style>
|
26 |
+
'''
|
27 |
+
st.markdown(page_bg_img, unsafe_allow_html=True)
|
28 |
+
|
29 |
+
# Title and Sidebar
|
30 |
+
st.title("AI Business Consultant")
|
31 |
+
|
32 |
+
image_url = "https://cdn-icons-png.flaticon.com/512/1998/1998614.png"
|
33 |
+
st.sidebar.image(image_url, caption="", use_column_width=True)
|
34 |
+
st.sidebar.write(
|
35 |
+
"This AI Business Consultant is built using an AI Multi-Agent system. "
|
36 |
+
"It provides business insights, statistical analysis, and professional recommendations!"
|
37 |
+
)
|
38 |
+
|
39 |
+
# User Inputs
|
40 |
+
business = st.text_input('Enter The Required Business Search Area', value="Artificial Intelligence")
|
41 |
+
stakeholder = st.text_input('Enter The Stakeholder Team', value="Executives")
|
42 |
+
|
43 |
+
# Optional Customization
|
44 |
+
enable_customization = st.sidebar.checkbox("Enable Advanced Agent Customization")
|
45 |
+
if enable_customization:
|
46 |
+
st.sidebar.markdown("### Customize Agent Goals")
|
47 |
+
planner_goal = st.sidebar.text_area(
|
48 |
+
"Planner Goal",
|
49 |
+
value="Plan engaging and factually accurate content about the topic."
|
50 |
+
)
|
51 |
+
writer_goal = st.sidebar.text_area(
|
52 |
+
"Writer Goal",
|
53 |
+
value="Write insightful and engaging content based on the topic."
|
54 |
+
)
|
55 |
+
analyst_goal = st.sidebar.text_area(
|
56 |
+
"Analyst Goal",
|
57 |
+
value="Perform statistical analysis to extract actionable insights."
|
58 |
+
)
|
59 |
+
else:
|
60 |
+
planner_goal = "Plan engaging and factually accurate content about the topic."
|
61 |
+
writer_goal = "Write insightful and engaging content based on the topic."
|
62 |
+
analyst_goal = "Perform statistical analysis to extract actionable insights."
|
63 |
+
|
64 |
+
#=================
|
65 |
+
# LLM Object and API Key
|
66 |
+
#=================
|
67 |
+
llm = ChatGroq(groq_api_key=os.getenv("GROQ_API_KEY"), model_name="llama-3.2-90b-text-preview")
|
68 |
+
|
69 |
+
#=================
|
70 |
+
# Crew Agents
|
71 |
+
#=================
|
72 |
+
|
73 |
+
planner = Agent(
|
74 |
+
role="Business Consultant",
|
75 |
+
goal=planner_goal,
|
76 |
+
backstory=(
|
77 |
+
"You're tasked with providing insights about {topic} to the stakeholder: {stakeholder}. "
|
78 |
+
"Your work will form the foundation for the Business Writer and Data Analyst."
|
79 |
+
),
|
80 |
+
allow_delegation=False,
|
81 |
+
verbose=True,
|
82 |
+
llm=llm
|
83 |
+
)
|
84 |
+
|
85 |
+
writer = Agent(
|
86 |
+
role="Business Writer",
|
87 |
+
goal=writer_goal,
|
88 |
+
backstory=(
|
89 |
+
"You will write a professional insights document about {topic}, "
|
90 |
+
"based on the Business Consultant's plan and the Data Analyst's results."
|
91 |
+
),
|
92 |
+
allow_delegation=False,
|
93 |
+
verbose=True,
|
94 |
+
llm=llm
|
95 |
+
)
|
96 |
+
|
97 |
+
analyst = Agent(
|
98 |
+
role="Data Analyst",
|
99 |
+
goal=analyst_goal,
|
100 |
+
backstory=(
|
101 |
+
"You will perform statistical analysis on {topic}, based on the Business Consultant's plan. "
|
102 |
+
"Your analysis will support the Business Writer's final document for {stakeholder}."
|
103 |
+
),
|
104 |
+
allow_delegation=False,
|
105 |
+
verbose=True,
|
106 |
+
llm=llm
|
107 |
+
)
|
108 |
+
|
109 |
+
#=================
|
110 |
+
# Crew Tasks
|
111 |
+
#=================
|
112 |
+
|
113 |
+
plan = Task(
|
114 |
+
description=(
|
115 |
+
"1. Research trends, key players, and noteworthy news for {topic}.\n"
|
116 |
+
"2. Provide structured insights and actionable recommendations.\n"
|
117 |
+
"3. Suggest strategies for dealing with international operators.\n"
|
118 |
+
"4. Limit content to 500 words."
|
119 |
+
),
|
120 |
+
expected_output="A comprehensive consultancy document with insights and recommendations.",
|
121 |
+
agent=planner
|
122 |
+
)
|
123 |
+
|
124 |
+
write = Task(
|
125 |
+
description=(
|
126 |
+
"1. Use the Business Consultant's plan to write a professional document for {topic}.\n"
|
127 |
+
"2. Structure the content with engaging sections and visuals.\n"
|
128 |
+
"3. Ensure alignment with the stakeholder's goals.\n"
|
129 |
+
"4. Limit the document to 200 words."
|
130 |
+
),
|
131 |
+
expected_output="A professional document tailored for {stakeholder}.",
|
132 |
+
agent=writer
|
133 |
+
)
|
134 |
+
|
135 |
+
analyse = Task(
|
136 |
+
description=(
|
137 |
+
"1. Perform statistical analysis to provide actionable insights for {topic}.\n"
|
138 |
+
"2. Collaborate with the Business Consultant and Writer to align on key metrics.\n"
|
139 |
+
"3. Present findings in a format suitable for inclusion in the final document."
|
140 |
+
),
|
141 |
+
expected_output="A data-driven analysis tailored for {stakeholder}.",
|
142 |
+
agent=analyst
|
143 |
+
)
|
144 |
+
|
145 |
+
#=================
|
146 |
+
# Execution
|
147 |
+
#=================
|
148 |
+
|
149 |
+
crew = Crew(
|
150 |
+
agents=[planner, analyst, writer],
|
151 |
+
tasks=[plan, analyse, write],
|
152 |
+
verbose=2
|
153 |
+
)
|
154 |
+
|
155 |
+
def generate_pdf_report(result):
|
156 |
+
"""Generate a professional PDF report from the Crew output."""
|
157 |
+
pdf = FPDF()
|
158 |
+
pdf.add_page()
|
159 |
+
pdf.set_font("Arial", size=12)
|
160 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
161 |
+
|
162 |
+
# Title
|
163 |
+
pdf.set_font("Arial", size=16, style="B")
|
164 |
+
pdf.cell(200, 10, txt="AI Business Consultant Report", ln=True, align="C")
|
165 |
+
pdf.ln(10)
|
166 |
+
|
167 |
+
# Content
|
168 |
+
pdf.set_font("Arial", size=12)
|
169 |
+
pdf.multi_cell(0, 10, txt=result)
|
170 |
+
|
171 |
+
# Save PDF
|
172 |
+
report_path = "Business_Insights_Report.pdf"
|
173 |
+
pdf.output(report_path)
|
174 |
+
return report_path
|
175 |
+
|
176 |
+
if st.button("Run Analysis"):
|
177 |
+
with st.spinner('Executing analysis...'):
|
178 |
+
try:
|
179 |
+
start_time = time.time()
|
180 |
+
result = crew.kickoff(inputs={"topic": business, "stakeholder": stakeholder})
|
181 |
+
execution_time = time.time() - start_time
|
182 |
+
|
183 |
+
# Display Results
|
184 |
+
st.markdown("### Insights and Analysis")
|
185 |
+
st.write(result)
|
186 |
+
|
187 |
+
# Display Execution Time
|
188 |
+
st.success(f"Analysis completed in {execution_time:.2f} seconds!")
|
189 |
+
|
190 |
+
# Visualization Example
|
191 |
+
st.markdown("### Data Visualization Example")
|
192 |
+
data = pd.DataFrame({
|
193 |
+
"Metric": ["Trend 1", "Trend 2", "Trend 3"],
|
194 |
+
"Value": [45, 80, 65]
|
195 |
+
})
|
196 |
+
fig = px.bar(data, x="Metric", y="Value", title="Sample Metrics for Analysis")
|
197 |
+
st.plotly_chart(fig)
|
198 |
+
|
199 |
+
# Generate and Provide PDF Report
|
200 |
+
report_path = generate_pdf_report(result)
|
201 |
+
with open(report_path, "rb") as file:
|
202 |
+
st.download_button(
|
203 |
+
label="Download Report as PDF",
|
204 |
+
data=file,
|
205 |
+
file_name="Business_Insights_Report.pdf",
|
206 |
+
mime="application/pdf"
|
207 |
+
)
|
208 |
+
|
209 |
+
except Exception as e:
|
210 |
+
st.error(f"An error occurred during execution: {e}")
|