DrishtiSharma commited on
Commit
6ece58e
Β·
verified Β·
1 Parent(s): 152414b

Create mylab/app.py

Browse files
Files changed (1) hide show
  1. mylab/app.py +209 -0
mylab/app.py ADDED
@@ -0,0 +1,209 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from crewai import Agent, Task, Crew
3
+ import os
4
+ from langchain_groq import ChatGroq
5
+ from fpdf import FPDF
6
+ import pandas as pd
7
+ import plotly.express as px
8
+ import time
9
+
10
+ # Title and Sidebar
11
+ st.title("Multi-Agent Business Consultant")
12
+
13
+ st.sidebar.write(
14
+ "This Business Consultant is built using Multi-Agent system. "
15
+ "Use this application to generate actionable business insights and data-driven analysis!"
16
+ )
17
+
18
+ # User Inputs
19
+ business = st.text_input('Enter The Required Business Search Area', value="Artificial Intelligence")
20
+ stakeholder = st.text_input('Enter The Stakeholder Team', value="Executives")
21
+
22
+ # Optional Customization
23
+ st.sidebar.subheader("Agent Customization")
24
+
25
+ # Display customization section in a collapsible expander
26
+ with st.sidebar.expander("Customize Agent Goals", expanded=False):
27
+ enable_customization = st.checkbox("Enable Custom Goals")
28
+ if enable_customization:
29
+ planner_goal = st.text_area(
30
+ "Planner Goal",
31
+ value="Develop a comprehensive plan focusing on market trends and strategies."
32
+ )
33
+ writer_goal = st.text_area(
34
+ "Writer Goal",
35
+ value="Craft engaging and actionable content based on analysis."
36
+ )
37
+ analyst_goal = st.text_area(
38
+ "Analyst Goal",
39
+ value="Perform advanced statistical analysis and generate key insights."
40
+ )
41
+ else:
42
+ planner_goal = "Develop a comprehensive plan focusing on market trends and strategies."
43
+ writer_goal = "Craft engaging and actionable content based on analysis."
44
+ analyst_goal = "Perform advanced statistical analysis and generate key insights."
45
+
46
+
47
+ #=================
48
+ # LLM Object
49
+ #=================
50
+ llm = ChatGroq(groq_api_key=os.getenv("GROQ_API_KEY"), model="groq/llama-3.3-70b-versatile")
51
+
52
+ #=================
53
+ # Crew Agents
54
+ #=================
55
+
56
+ planner = Agent(
57
+ role="Business Consultant",
58
+ goal=planner_goal,
59
+ backstory=(
60
+ "You're tasked with providing insights about {topic} to the stakeholder: {stakeholder}. "
61
+ "Your work will form the foundation for the Business Writer and Data Analyst."
62
+ ),
63
+ allow_delegation=False,
64
+ verbose=True,
65
+ llm=llm
66
+ )
67
+
68
+ writer = Agent(
69
+ role="Business Writer",
70
+ goal=writer_goal,
71
+ backstory=(
72
+ "You will write a professional insights document about {topic}, "
73
+ "based on the Business Consultant's plan and the Data Analyst's results."
74
+ ),
75
+ allow_delegation=False,
76
+ verbose=True,
77
+ llm=llm
78
+ )
79
+
80
+ analyst = Agent(
81
+ role="Data Analyst",
82
+ goal=analyst_goal,
83
+ backstory=(
84
+ "You will perform statistical analysis on {topic}, based on the Business Consultant's plan. "
85
+ "Your analysis will support the Business Writer's final document for {stakeholder}."
86
+ ),
87
+ allow_delegation=False,
88
+ verbose=True,
89
+ llm=llm
90
+ )
91
+
92
+ #=================
93
+ # Crew Tasks
94
+ #=================
95
+
96
+ plan = Task(
97
+ description=(
98
+ "1. Research trends, key players, and noteworthy news for {topic}.\n"
99
+ "2. Provide structured insights and actionable recommendations.\n"
100
+ "3. Suggest strategies for dealing with international operators.\n"
101
+ "4. Limit content to 500 words."
102
+ ),
103
+ expected_output="A comprehensive consultancy document with insights and recommendations.",
104
+ agent=planner
105
+ )
106
+
107
+ write = Task(
108
+ description=(
109
+ "1. Use the Business Consultant's plan to write a professional document for {topic}.\n"
110
+ "2. Structure the content with engaging sections and visuals.\n"
111
+ "3. Ensure alignment with the stakeholder's goals.\n"
112
+ "4. Limit the document to 200 words."
113
+ ),
114
+ expected_output="A professional document tailored for {stakeholder}.",
115
+ agent=writer
116
+ )
117
+
118
+ analyse = Task(
119
+ description=(
120
+ "1. Perform statistical analysis to provide actionable insights for {topic}.\n"
121
+ "2. Collaborate with the Business Consultant and Writer to align on key metrics.\n"
122
+ "3. Present findings in a format suitable for inclusion in the final document."
123
+ ),
124
+ expected_output="A data-driven analysis tailored for {stakeholder}.",
125
+ agent=analyst
126
+ )
127
+
128
+ #=================
129
+ # Execution
130
+ #=================
131
+
132
+ crew = Crew(
133
+ agents=[planner, analyst, writer],
134
+ tasks=[plan, analyse, write],
135
+ verbose=True
136
+ )
137
+
138
+ def generate_pdf_report(result):
139
+ """Generate a professional PDF report from the Crew output."""
140
+ pdf = FPDF()
141
+ pdf.add_page()
142
+ pdf.set_font("Arial", size=12)
143
+ pdf.set_auto_page_break(auto=True, margin=15)
144
+
145
+ # Title
146
+ pdf.set_font("Arial", size=16, style="B")
147
+ pdf.cell(200, 10, txt="AI Business Consultant Report", ln=True, align="C")
148
+ pdf.ln(10)
149
+
150
+ # Content
151
+ pdf.set_font("Arial", size=12)
152
+ pdf.multi_cell(0, 10, txt=result)
153
+
154
+ # Save PDF
155
+ report_path = "Business_Insights_Report.pdf"
156
+ pdf.output(report_path)
157
+ return report_path
158
+
159
+
160
+ if st.button("Generate Insights"):
161
+ with st.spinner('Processing...'):
162
+ try:
163
+ start_time = time.time()
164
+ results = crew.kickoff(inputs={"topic": business, "stakeholder": stakeholder})
165
+ elapsed_time = time.time() - start_time
166
+
167
+ # Parse and Display Results
168
+ st.markdown("### Insights and Analysis")
169
+
170
+ # Handle Raw Output
171
+ raw_output = getattr(results, "raw", None)
172
+ if raw_output:
173
+ st.markdown("#### Executive Summary")
174
+ st.write(raw_output)
175
+ else:
176
+ st.warning("No executive summary available.")
177
+
178
+ # Handle Task Outputs
179
+ tasks_output = getattr(results, "tasks_output", [])
180
+ if tasks_output:
181
+ st.markdown("#### Task Outputs")
182
+ for idx, task in enumerate(tasks_output):
183
+ task_description = getattr(task,"description", "No description available.")
184
+ task_raw = getattr(task, "raw", "No details available.")
185
+ st.subheader(f"Task {idx + 1}")
186
+ st.write(f"**Description:** {task_description}")
187
+ st.write(task_raw)
188
+ else:
189
+ st.warning("No task outputs available.")
190
+
191
+ # Display Token Usage
192
+ token_usage = getattr(results, "token_usage", None)
193
+ if token_usage:
194
+ st.markdown("#### Token Usage")
195
+ st.json(token_usage)
196
+ else:
197
+ st.warning("No token usage information available.")
198
+
199
+ # Display Execution Time
200
+ st.success(f"Analysis completed in {elapsed_time:.2f} seconds.")
201
+
202
+ # Generate PDF Report
203
+ if raw_output:
204
+ report_path = generate_pdf_report(raw_output)
205
+ with open(report_path, "rb") as report_file:
206
+ st.download_button("Download Report", data=report_file, file_name="Business_Report.pdf")
207
+
208
+ except Exception as e:
209
+ st.error(f"An error occurred during execution: {e}")