DrishtiSharma commited on
Commit
82686e4
Β·
verified Β·
1 Parent(s): f2c5388

Create interim.py

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