Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,14 +5,17 @@
|
|
| 5 |
import streamlit as st
|
| 6 |
from crewai import Agent, Task, Crew
|
| 7 |
import os
|
| 8 |
-
from
|
| 9 |
-
from
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
#=================
|
| 12 |
# Add Streamlit Components
|
| 13 |
#=================
|
| 14 |
|
| 15 |
-
#
|
| 16 |
page_bg_img = '''
|
| 17 |
<style>
|
| 18 |
.stApp {
|
|
@@ -23,80 +26,81 @@ background-size: cover;
|
|
| 23 |
'''
|
| 24 |
st.markdown(page_bg_img, unsafe_allow_html=True)
|
| 25 |
|
| 26 |
-
#
|
| 27 |
st.title("AI Business Consultant")
|
| 28 |
|
| 29 |
-
# logo
|
| 30 |
image_url = "https://cdn-icons-png.flaticon.com/512/1998/1998614.png"
|
| 31 |
st.sidebar.image(image_url, caption="", use_column_width=True)
|
| 32 |
-
st.sidebar.write(
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
business = st.text_input('Enter The Required Business Search Area')
|
| 36 |
-
stakeholder = st.text_input('Enter The Stakeholder Team')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
#=================
|
| 39 |
-
# LLM
|
| 40 |
#=================
|
| 41 |
llm = ChatGroq(groq_api_key=os.getenv("GROQ_API_KEY"), model_name="llama-3.2-90b-text-preview")
|
| 42 |
|
| 43 |
-
|
| 44 |
#=================
|
| 45 |
# Crew Agents
|
| 46 |
#=================
|
| 47 |
|
| 48 |
planner = Agent(
|
| 49 |
role="Business Consultant",
|
| 50 |
-
goal=
|
| 51 |
-
backstory=
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
"the Business Writer to deliver good insights.",
|
| 56 |
allow_delegation=False,
|
| 57 |
-
|
| 58 |
-
llm
|
| 59 |
)
|
| 60 |
|
| 61 |
-
|
| 62 |
writer = Agent(
|
| 63 |
role="Business Writer",
|
| 64 |
-
goal=
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
"the Business Consultant, who provides an outline "
|
| 70 |
-
"and relevant context about the : {topic}. "
|
| 71 |
-
"and also the data analyst who will provide you with necessary analysis about the : {topic} "
|
| 72 |
-
"You follow the main objectives and "
|
| 73 |
-
"direction of the outline, "
|
| 74 |
-
"as provided by the Business Consultant. "
|
| 75 |
-
"You also provide objective and impartial insights "
|
| 76 |
-
"and back them up with information "
|
| 77 |
-
"provided by the Business Consultant."
|
| 78 |
-
"design your document in a professional way to be presented to : {stakeholder}."
|
| 79 |
-
,
|
| 80 |
allow_delegation=False,
|
| 81 |
verbose=True,
|
| 82 |
llm=llm
|
| 83 |
)
|
| 84 |
|
| 85 |
-
|
| 86 |
analyst = Agent(
|
| 87 |
role="Data Analyst",
|
| 88 |
-
goal=
|
| 89 |
-
backstory=
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
"and relevant context about the : {topic}. "
|
| 94 |
-
"You follow the main objectives and "
|
| 95 |
-
"direction of the outline, "
|
| 96 |
-
"as provided by the Business Consultant. "
|
| 97 |
-
"You also provide comprehensive statistical analysis with numbers to the Business Writer "
|
| 98 |
-
"and back them up with information "
|
| 99 |
-
"provided by the Business Consultant.",
|
| 100 |
allow_delegation=False,
|
| 101 |
verbose=True,
|
| 102 |
llm=llm
|
|
@@ -108,54 +112,36 @@ analyst = Agent(
|
|
| 108 |
|
| 109 |
plan = Task(
|
| 110 |
description=(
|
| 111 |
-
"1.
|
| 112 |
-
|
| 113 |
-
"
|
| 114 |
-
"
|
| 115 |
-
"dealing with International operators.\n"
|
| 116 |
-
"5. Limit the document to only 500 words"
|
| 117 |
),
|
| 118 |
-
expected_output="A comprehensive
|
| 119 |
-
|
| 120 |
-
agent=planner,
|
| 121 |
-
# tools = [tool]
|
| 122 |
-
|
| 123 |
)
|
| 124 |
|
| 125 |
-
|
| 126 |
-
|
| 127 |
write = Task(
|
| 128 |
description=(
|
| 129 |
-
"1. Use the
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
"3. Proofread for grammatical errors and "
|
| 134 |
-
"alignment with the brand's voice.\n"
|
| 135 |
-
"3. Limit the document to only 200 words "
|
| 136 |
-
"4. Use impressive images and charts to reinforce your insights "
|
| 137 |
),
|
| 138 |
-
expected_output="A
|
| 139 |
-
"providing insights for {stakeholder} ",
|
| 140 |
agent=writer
|
| 141 |
)
|
| 142 |
|
| 143 |
-
|
| 144 |
analyse = Task(
|
| 145 |
description=(
|
| 146 |
-
"1.
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
"to align on the best analysis to be provided about {topic}.\n"
|
| 152 |
-
),
|
| 153 |
-
expected_output="A clear comprehensive data analysis "
|
| 154 |
-
"providing insights and statistics with numbers to the Business Writer ",
|
| 155 |
agent=analyst
|
| 156 |
)
|
| 157 |
|
| 158 |
-
|
| 159 |
#=================
|
| 160 |
# Execution
|
| 161 |
#=================
|
|
@@ -163,10 +149,62 @@ analyse = Task(
|
|
| 163 |
crew = Crew(
|
| 164 |
agents=[planner, analyst, writer],
|
| 165 |
tasks=[plan, analyse, write],
|
| 166 |
-
verbose=
|
| 167 |
)
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import streamlit as st
|
| 6 |
from crewai import Agent, Task, Crew
|
| 7 |
import os
|
| 8 |
+
from langchain_groq import ChatGroq
|
| 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 {
|
|
|
|
| 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
|
|
|
|
| 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 |
#=================
|
|
|
|
| 149 |
crew = Crew(
|
| 150 |
agents=[planner, analyst, writer],
|
| 151 |
tasks=[plan, analyse, write],
|
| 152 |
+
verbose=True
|
| 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}")
|