Spaces:
Build error
Build error
Update test.py
Browse files
test.py
CHANGED
|
@@ -1,160 +1,40 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 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
|
| 158 |
try:
|
| 159 |
start_time = time.time()
|
| 160 |
result = crew.kickoff(inputs={"topic": business, "stakeholder": stakeholder})
|
|
@@ -162,15 +42,12 @@ if st.button("Run Analysis"):
|
|
| 162 |
|
| 163 |
# Display Results
|
| 164 |
st.markdown("### Insights and Analysis")
|
| 165 |
-
|
| 166 |
|
| 167 |
-
#
|
| 168 |
st.success(f"Analysis completed in {execution_time:.2f} seconds!")
|
| 169 |
|
| 170 |
-
#
|
| 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(
|
|
@@ -179,6 +56,5 @@ if st.button("Run Analysis"):
|
|
| 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}")
|
|
|
|
| 1 |
+
def display_analysis_results(result):
|
| 2 |
+
"""
|
| 3 |
+
Process and display analysis results from agents' output.
|
| 4 |
+
"""
|
| 5 |
+
try:
|
| 6 |
+
# Display the result text
|
| 7 |
+
st.markdown("#### Detailed Insights")
|
| 8 |
+
st.text(result)
|
| 9 |
+
|
| 10 |
+
# Parse the result into segments if formatted
|
| 11 |
+
if isinstance(result, str) and "\n" in result:
|
| 12 |
+
lines = result.split("\n")
|
| 13 |
+
insights = [line for line in lines if line.strip()]
|
| 14 |
+
|
| 15 |
+
# Show insights as bullet points
|
| 16 |
+
st.markdown("#### Key Insights (Parsed)")
|
| 17 |
+
for insight in insights:
|
| 18 |
+
st.write(f"- {insight}")
|
| 19 |
+
|
| 20 |
+
# Generate a visualization if result contains numerical data
|
| 21 |
+
st.markdown("#### Example Visualization (If Applicable)")
|
| 22 |
+
if "data:" in result.lower():
|
| 23 |
+
# Example parsing numerical data from result (adapt to your agent's output)
|
| 24 |
+
data_lines = [line for line in result.split("\n") if "data:" in line.lower()]
|
| 25 |
+
data_points = [
|
| 26 |
+
{"Category": f"Point {i+1}", "Value": float(line.split(":")[-1].strip())}
|
| 27 |
+
for i, line in enumerate(data_lines) if line.split(":")[-1].strip().replace('.', '', 1).isdigit()
|
| 28 |
+
]
|
| 29 |
+
if data_points:
|
| 30 |
+
df = pd.DataFrame(data_points)
|
| 31 |
+
fig = px.bar(df, x="Category", y="Value", title="Extracted Data Visualization")
|
| 32 |
+
st.plotly_chart(fig)
|
| 33 |
+
except Exception as e:
|
| 34 |
+
st.error(f"Error processing results: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
if st.button("Run Analysis"):
|
| 37 |
+
with st.spinner('Executing...'):
|
| 38 |
try:
|
| 39 |
start_time = time.time()
|
| 40 |
result = crew.kickoff(inputs={"topic": business, "stakeholder": stakeholder})
|
|
|
|
| 42 |
|
| 43 |
# Display Results
|
| 44 |
st.markdown("### Insights and Analysis")
|
| 45 |
+
display_analysis_results(result)
|
| 46 |
|
| 47 |
+
# Execution Time
|
| 48 |
st.success(f"Analysis completed in {execution_time:.2f} seconds!")
|
| 49 |
|
| 50 |
+
# Generate PDF Report
|
|
|
|
|
|
|
|
|
|
| 51 |
report_path = generate_pdf_report(result)
|
| 52 |
with open(report_path, "rb") as file:
|
| 53 |
st.download_button(
|
|
|
|
| 56 |
file_name="Business_Insights_Report.pdf",
|
| 57 |
mime="application/pdf"
|
| 58 |
)
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
+
st.error(f"An error occurred during execution: {e}")
|