Spaces:
Sleeping
Sleeping
| import os | |
| from phi.agent import Agent | |
| from phi.model.openai import OpenAIChat | |
| import openai | |
| import markdown2 | |
| import pdfkit | |
| from datetime import datetime | |
| from bni_agent import get_bni_benefits | |
| from rag_agent import recommend_bni_connections | |
| # Load environment variables (API keys, etc.) | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| ##################################################################################### | |
| # PHASE 3 # | |
| ##################################################################################### | |
| ############################## | |
| # 1️⃣ Reasoning Agent # | |
| ############################## | |
| reasoning_agent = Agent( | |
| name="Reasoning Agent", | |
| model=OpenAIChat(id="gpt-4o"), | |
| description="Processes all collected data and generates structured AI adoption strategies.", | |
| show_tool_calls=True, | |
| markdown=True, | |
| ) | |
| def generate_ai_strategy(company_data: str, industry_trends: str) -> str: | |
| """Generates a structured AI adoption strategy based on company details and industry insights.""" | |
| query = f""" | |
| You are an AI business strategist analyzing a company's AI adoption potential. Given: | |
| - **Company Overview:** {company_data} | |
| - **Industry Trends:** {industry_trends} | |
| Generate a structured AI strategy including: | |
| 1. Key AI Opportunities | |
| """ | |
| response = reasoning_agent.run(query) | |
| return response.content # Returns the generated AI strategy | |
| ############################## | |
| # 2️⃣ AI Integration Advisor # | |
| ############################## | |
| # ai_integration_agent = Agent( | |
| # name="AI Integration Advisor", | |
| # model=OpenAIChat(id="gpt-4o"), | |
| # description="Suggests AI implementation strategies based on industry insights and company operations.", | |
| # show_tool_calls=True, | |
| # markdown=True, | |
| # ) | |
| # def suggest_ai_integration(company_data: str, ai_strategy: str) -> str: | |
| # """Suggests a structured AI implementation plan.""" | |
| # query = f""" | |
| # Based on the AI adoption strategy: | |
| # - **Company Context:** {company_data} | |
| # - **AI Strategy Summary:** {ai_strategy} | |
| # Provide a structured AI implementation plan including: | |
| # 1. Step-by-step AI adoption process | |
| # 2. Required AI Technologies & Infrastructure | |
| # 3. Workforce training & AI skill development | |
| # 4. Risk considerations (data security, compliance, ethical AI) | |
| # 5. Key performance indicators (KPIs) for AI success. | |
| # """ | |
| # response = ai_integration_agent.run(query) | |
| # return response.content # Returns AI integration plan | |
| ############################## | |
| # 3️⃣ Revenue Growth Agent # | |
| ############################## | |
| revenue_growth_agent = Agent( | |
| name="Revenue Growth Agent", | |
| model=OpenAIChat(id="gpt-4o"), | |
| description="Identifies AI-driven opportunities to enhance revenue and efficiency.", | |
| show_tool_calls=True, | |
| markdown=True, | |
| ) | |
| def identify_revenue_opportunities(company_data: str, ai_strategy: str) -> str: | |
| """Identifies AI-driven revenue generation opportunities for a business.""" | |
| query = f""" | |
| You are an AI business analyst identifying practical AI-driven revenue growth opportunities for: | |
| Company Overview: {company_data} | |
| AI Strategy: {ai_strategy} | |
| Provide a concise, single-paragraph summary covering: | |
| AI-based Monetization Strategies – How can the company create new revenue streams using AI? | |
| Cost Reduction & Operational Efficiency – How can AI streamline operations and reduce costs? | |
| Market Expansion – How can AI open new markets or customer segments? | |
| Competitive Positioning – How can AI help differentiate the company from competitors? | |
| Keep it short, crisp, and actionable, avoiding complex or unnecessary details. | |
| """ | |
| response = revenue_growth_agent.run(query) | |
| return response.content # Returns revenue opportunities | |
| ############################## | |
| # 4️⃣ BNI Agent: Membership Benefits # | |
| ############################## | |
| def get_bni_membership_benefits(company_data: str) -> str: | |
| """Fetches BNI membership benefits tailored to the user's company.""" | |
| return get_bni_benefits(company_data) | |
| ############################## | |
| # 5️⃣ BNI RAG Agent: Pearl Chapter Connections # | |
| ############################## | |
| def get_bni_recommendations(company_data: str) -> str: | |
| """Recommends relevant BNI Pearl Chapter members based on the user's company data.""" | |
| return recommend_bni_connections(company_data) | |
| ############################## | |
| # 6️⃣ Report Generation Agent # | |
| ############################## | |
| # Ensure your OpenAI API key is set in your environment variables | |
| openai.api_key = os.getenv("OPENAI_API_KEY") | |
| def generate_report(company_name: str, company_data: str, | |
| ai_strategy: str, | |
| revenue_opportunities: str, | |
| bni_benefits: str, | |
| bni_connections: str) -> str: | |
| """ | |
| Generates a structured AI strategy report in PDF format using `pdfkit`. | |
| """ | |
| # 📝 **Step 1: Prepare the Report Content** | |
| report_content = f""" | |
| # AI Strategy Report for {company_name} | |
| ## 1️⃣ Company Overview | |
| {company_data} | |
| ## 2️⃣ How BNI Can Help Your Business | |
| {bni_benefits} | |
| ## 3️⃣ Recommended BNI Pearl Chapter Connections | |
| {bni_connections} | |
| ## 4️⃣ AI Adoption Strategy | |
| {ai_strategy} | |
| ## 5️⃣ Revenue Growth Opportunities | |
| {revenue_opportunities} | |
| """ | |
| # 🎯 **Step 2: Use OpenAI GPT-4o-mini to Format Markdown** | |
| response = openai.chat.completions.create( | |
| model="gpt-4o-mini", | |
| messages=[ | |
| {"role": "system", "content": "You are an expert in formatting business reports in Markdown."}, | |
| {"role": "user", "content": f"Format the following report in well-structured Markdown. Use bold headings, bullet points, and clear sections. Ensure the text is concise, professional, and structured for readability. Do not include any emojis or unnecessary text:\n\n{report_content}"} | |
| ], | |
| max_tokens=5000, | |
| temperature=0.5, | |
| ) | |
| enhanced_markdown = response.choices[0].message.content # Extract the formatted response | |
| # 🖼 **Step 3: Convert Enhanced Markdown to HTML** | |
| html_report = markdown2.markdown(enhanced_markdown) | |
| # 📄 **Step 4: Define the HTML Template with Styling** | |
| html_template = f""" | |
| <html> | |
| <head> | |
| <style> | |
| body {{ | |
| font-family: 'Times New Roman', serif; | |
| margin: 50px; | |
| position: relative; | |
| }} | |
| .watermark {{ | |
| position: fixed; | |
| top: 50%; | |
| left: 50%; | |
| transform: translate(-50%, -50%); | |
| font-size: 100px; | |
| color: rgba(200, 200, 200, 0.2); | |
| z-index: -1; | |
| user-select: none; | |
| }} | |
| .outline {{ | |
| border: 2px solid #000; | |
| padding: 20px; | |
| margin-bottom: 20px; | |
| }} | |
| h1, h2, h3, h4, h5, h6 {{ | |
| color: #333; | |
| }} | |
| p {{ | |
| font-size: 14pt; | |
| line-height: 1.6; | |
| color: #000; | |
| }} | |
| .footer {{ | |
| position: fixed; | |
| bottom: 20px; | |
| left: 0; | |
| width: 100%; | |
| text-align: center; | |
| font-size: 12pt; | |
| color: #555; | |
| border-top: 1px solid #ccc; | |
| padding-top: 10px; | |
| }} | |
| </style> | |
| </head> | |
| <body> | |
| <div class="watermark">Giant Analytics</div> | |
| <div class="outline"> | |
| {html_report} | |
| </div> | |
| <div class="footer"> | |
| 🚀 Powered by <strong>Giant Analytics</strong> | Contact Us: (+91) 96533 20500 | |
| </div> | |
| </body> | |
| </html> | |
| """ | |
| # 📌 **Step 5: Generate the PDF** | |
| current_date = datetime.now().strftime("%Y-%m-%d") | |
| pdf_filename = f"{company_name}_AI_Report_{current_date}.pdf" | |
| # Path to wkhtmltopdf executable (Make sure it's installed) | |
| path_to_wkhtmltopdf = '/usr/bin/wkhtmltopdf' # Update this path if different | |
| config = pdfkit.configuration(wkhtmltopdf=path_to_wkhtmltopdf) | |
| # Convert HTML to PDF | |
| pdfkit.from_string(html_template, pdf_filename, configuration=config) | |
| return pdf_filename # Returns the generated PDF filename | |