DrishtiSharma commited on
Commit
457c5ae
Β·
verified Β·
1 Parent(s): cc8eee1

Create test.py

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