Spaces:
Build error
Build error
| # add refrence (https://medium.com/@hanan.tabak/user-friendly-open-source-multi-agent-ai-business-consultant-on-crewai-and-streamlit-0f972feb1b74) + improvize | |
| import streamlit as st | |
| from crewai import Agent, Task, Crew | |
| import os | |
| from langchain_groq import ChatGroq | |
| from fpdf import FPDF | |
| import pandas as pd | |
| import plotly.express as px | |
| import time | |
| # Title and Sidebar | |
| st.title("Multi-Agent Business Consultant") | |
| st.sidebar.write( | |
| "This Business Consultant is built using Multi-Agent system. " | |
| "Use this application to generate actionable business insights and data-driven analysis!" | |
| ) | |
| # User Inputs | |
| business = st.text_input('Enter The Required Business Search Area', value="Artificial Intelligence") | |
| stakeholder = st.text_input('Enter The Stakeholder Team', value="Executives") | |
| # Optional Customization | |
| st.sidebar.subheader("Agent Customization") | |
| # Display customization section in a collapsible expander | |
| with st.sidebar.expander("Customize Agent Goals", expanded=False): | |
| enable_customization = st.checkbox("Enable Custom Goals") | |
| if enable_customization: | |
| planner_goal = st.text_area( | |
| "Planner Goal", | |
| value="Develop a comprehensive plan focusing on market trends and strategies." | |
| ) | |
| writer_goal = st.text_area( | |
| "Writer Goal", | |
| value="Craft engaging and actionable content based on analysis." | |
| ) | |
| analyst_goal = st.text_area( | |
| "Analyst Goal", | |
| value="Perform advanced statistical analysis and generate key insights." | |
| ) | |
| else: | |
| planner_goal = "Develop a comprehensive plan focusing on market trends and strategies." | |
| writer_goal = "Craft engaging and actionable content based on analysis." | |
| analyst_goal = "Perform advanced statistical analysis and generate key insights." | |
| #================= | |
| # LLM Object | |
| #================= | |
| llm = ChatGroq(groq_api_key=os.getenv("GROQ_API_KEY"), model="groq/llama-3.3-70b-versatile") | |
| #================= | |
| # Crew Agents | |
| #================= | |
| planner = Agent( | |
| role="Business Consultant", | |
| goal=planner_goal, | |
| backstory=( | |
| "You're tasked with providing insights about {topic} to the stakeholder: {stakeholder}. " | |
| "Your work will form the foundation for the Business Writer and Data Analyst." | |
| ), | |
| allow_delegation=False, | |
| verbose=True, | |
| llm=llm | |
| ) | |
| writer = Agent( | |
| role="Business Writer", | |
| goal=writer_goal, | |
| backstory=( | |
| "You will write a professional insights document about {topic}, " | |
| "based on the Business Consultant's plan and the Data Analyst's results." | |
| ), | |
| allow_delegation=False, | |
| verbose=True, | |
| llm=llm | |
| ) | |
| analyst = Agent( | |
| role="Data Analyst", | |
| goal=analyst_goal, | |
| backstory=( | |
| "You will perform statistical analysis on {topic}, based on the Business Consultant's plan. " | |
| "Your analysis will support the Business Writer's final document for {stakeholder}." | |
| ), | |
| allow_delegation=False, | |
| verbose=True, | |
| llm=llm | |
| ) | |
| #================= | |
| # Crew Tasks | |
| #================= | |
| plan = Task( | |
| description=( | |
| "1. Research trends, key players, and noteworthy news for {topic}.\n" | |
| "2. Provide structured insights and actionable recommendations.\n" | |
| "3. Suggest strategies for dealing with international operators.\n" | |
| "4. Limit content to 500 words." | |
| ), | |
| expected_output="A comprehensive consultancy document with insights and recommendations.", | |
| agent=planner | |
| ) | |
| write = Task( | |
| description=( | |
| "1. Use the Business Consultant's plan to write a professional document for {topic}.\n" | |
| "2. Structure the content with engaging sections and visuals.\n" | |
| "3. Ensure alignment with the stakeholder's goals.\n" | |
| "4. Limit the document to 200 words." | |
| ), | |
| expected_output="A professional document tailored for {stakeholder}.", | |
| agent=writer | |
| ) | |
| analyse = Task( | |
| description=( | |
| "1. Perform statistical analysis to provide actionable insights for {topic}.\n" | |
| "2. Collaborate with the Business Consultant and Writer to align on key metrics.\n" | |
| "3. Present findings in a format suitable for inclusion in the final document." | |
| ), | |
| expected_output="A data-driven analysis tailored for {stakeholder}.", | |
| agent=analyst | |
| ) | |
| #================= | |
| # Execution | |
| #================= | |
| crew = Crew( | |
| agents=[planner, analyst, writer], | |
| tasks=[plan, analyse, write], | |
| verbose=True | |
| ) | |
| def generate_pdf_report(result): | |
| """Generate a professional PDF report from the Crew output.""" | |
| pdf = FPDF() | |
| pdf.add_page() | |
| pdf.set_font("Arial", size=12) | |
| pdf.set_auto_page_break(auto=True, margin=15) | |
| # Title | |
| pdf.set_font("Arial", size=16, style="B") | |
| pdf.cell(200, 10, txt="AI Business Consultant Report", ln=True, align="C") | |
| pdf.ln(10) | |
| # Content | |
| pdf.set_font("Arial", size=12) | |
| pdf.multi_cell(0, 10, txt=result) | |
| # Save PDF | |
| report_path = "Business_Insights_Report.pdf" | |
| pdf.output(report_path) | |
| return report_path | |
| if st.button("Generate Insights"): | |
| with st.spinner('Processing...'): | |
| try: | |
| start_time = time.time() | |
| results = crew.kickoff(inputs={"topic": business, "stakeholder": stakeholder}) | |
| elapsed_time = time.time() - start_time | |
| # Parse and Display Results | |
| st.markdown("### Insights and Analysis") | |
| # Handle Raw Output | |
| raw_output = getattr(results, "raw", None) | |
| if raw_output: | |
| st.markdown("#### Executive Summary") | |
| st.write(raw_output) | |
| else: | |
| st.warning("No executive summary available.") | |
| # Handle Task Outputs | |
| tasks_output = getattr(results, "tasks_output", []) | |
| if tasks_output: | |
| st.markdown("#### Task Outputs") | |
| for idx, task in enumerate(tasks_output): | |
| task_description = getattr(task,"description", "No description available.") | |
| task_raw = getattr(task, "raw", "No details available.") | |
| st.subheader(f"Task {idx + 1}") | |
| st.write(f"**Description:** {task_description}") | |
| st.write(task_raw) | |
| else: | |
| st.warning("No task outputs available.") | |
| # Display Token Usage | |
| token_usage = getattr(results, "token_usage", None) | |
| if token_usage: | |
| st.markdown("#### Token Usage") | |
| st.json(token_usage) | |
| else: | |
| st.warning("No token usage information available.") | |
| # Display Execution Time | |
| st.success(f"Analysis completed in {elapsed_time:.2f} seconds.") | |
| # Generate PDF Report | |
| if raw_output: | |
| report_path = generate_pdf_report(raw_output) | |
| with open(report_path, "rb") as report_file: | |
| st.download_button("Download Report", data=report_file, file_name="Business_Report.pdf") | |
| except Exception as e: | |
| st.error(f"An error occurred during execution: {e}") | |
| # Add reference and credits in the sidebar | |
| st.sidebar.markdown("---") | |
| st.sidebar.markdown("### Reference:") | |
| st.sidebar.markdown("[Multi-Agent Business Consultant - Hanan Tabak](https://medium.com/@hanan.tabak/user-friendly-open-source-multi-agent-ai-business-consultant-on-crewai-and-streamlit-0f972feb1b74)") |