| import streamlit as st |
| from docx import Document |
| import re |
| import io |
| import os |
| import smtplib |
| from email.mime.multipart import MIMEMultipart |
| from email.mime.base import MIMEBase |
| from email import encoders |
| from email.mime.text import MIMEText |
| from fpdf import FPDF |
| from dotenv import load_dotenv |
| from retrying import retry |
| from functions import * |
|
|
| |
| load_dotenv() |
|
|
| |
| exa = Exa(api_key=os.getenv("EXA_API_KEY")) |
|
|
| |
| |
| utilized_model = "llama3-70b-8192" |
|
|
| try: |
| |
| http_client = httpx.Client() |
| client = Groq(api_key=os.getenv("GROQ_API_KEY"), http_client=http_client) |
| print("Groq client initialized successfully!") |
| except TypeError as e: |
| print("Error initializing Groq client:", str(e)) |
| except Exception as ex: |
| print("Unexpected error:", str(ex)) |
| |
| highlights_options = { |
| "num_sentences": 7, |
| "highlights_per_url": 1, |
| } |
| |
| def create_document(): |
| doc = Document() |
| doc.add_heading("Business Proposal", 0) |
| return doc |
|
|
| |
| def add_section_to_doc(doc, section_name, section_content): |
| section_content = strip_md(section_content) |
| section_content = section_content.replace("\\", "") |
| doc.add_heading(section_name, level=1) |
| doc.add_paragraph(section_content) |
| return doc |
|
|
| |
| def get_docx_bytes(doc): |
| doc_io = io.BytesIO() |
| doc.save(doc_io) |
| doc_io.seek(0) |
| return doc_io |
|
|
| |
| def send_email_with_attachment(to_email, subject, body, filename, section_content): |
| from_email = os.getenv("EMAIL_USER") |
| email_password = os.getenv("EMAIL_PASSWORD") |
|
|
| msg = MIMEMultipart() |
| msg['From'] = from_email |
| msg['To'] = to_email |
| msg['Subject'] = subject |
|
|
| |
| msg.attach(MIMEText(body + f"\n\nContent of the section:\n\n{section_content}", 'plain')) |
|
|
| |
| try: |
| with open(filename, 'rb') as attachment: |
| part = MIMEBase('application', 'octet-stream') |
| part.set_payload(attachment.read()) |
| encoders.encode_base64(part) |
| part.add_header('Content-Disposition', f'attachment; filename={filename}') |
| msg.attach(part) |
|
|
| |
| with smtplib.SMTP('smtp.gmail.com', 587) as server: |
| server.starttls() |
| server.login(from_email, email_password) |
| server.send_message(msg) |
|
|
| |
| return f"Email sent successfully to {to_email} for section '{subject}'." |
| |
| except Exception as e: |
| |
| return f"Failed to send email to {to_email}: {str(e)}" |
| finally: |
| server.quit() |
|
|
| |
| def sanitize_filename(filename, max_length=100): |
| """ |
| Remove or replace invalid characters from the filename to make it valid for file systems, |
| and limit the filename length to avoid file system errors. |
| """ |
| |
| sanitized = re.sub(r'[<>:"/\\|?*]', '', filename) |
| |
| |
| return sanitized[:max_length] |
|
|
| |
| @retry(stop_max_attempt_number=3) |
| def extract_text_from_file(file_path): |
| try: |
| if file_path.endswith('.docx'): |
| doc = Document(file_path) |
| return '\n'.join([paragraph.text for paragraph in doc.paragraphs]) |
| elif file_path.endswith('.pdf'): |
| with open(file_path, 'rb') as f: |
| pdf = FPDF() |
| pdf.add_page() |
| pdf.set_font("Arial", size=12) |
| pdf.multi_cell(0, 10, extract_text(f)) |
| return pdf.output().decode() |
| else: |
| raise ValueError("Unsupported file format. Only DOCX and PDF files are supported.") |
| except Exception as e: |
| print(f"Error extracting text from file: {str(e)}") |
| return "" |
|
|
| |
| def collect_basic_info(): |
| st.title("Business Proposal Generator") |
|
|
| |
| company_name = st.text_input("Company Name") |
| industry = st.text_input("Industry") |
| location = st.text_area("Location") |
| mission = st.text_area("Mission Statement") |
| vision = st.text_area("Vision Statement") |
| products_services = st.text_area("Description of Products/Services") |
| target_market = st.text_area("Customer Segments") |
| value_proposition = st.text_area("Competitive Advantage") |
| promotional_strategy = st.text_area("Promotional Strategy") |
| current_revenue = st.number_input("Current Revenue (R)", min_value=0.0, format="%f") |
| current_expenses = st.number_input("Current Expenses (R)", min_value=0.0, format="%f") |
| funding_requirements = st.text_area("Funding Requirements") |
| management_team = st.text_area("Key Personnel") |
| company_structure = st.text_area("Company Structure") |
| goals_objectives = st.text_area("Short-term and Long-term Goals") |
| operational_strategy = st.text_area("Operational Strategy") |
| market_overview = st.text_area("Market Overview") |
|
|
| |
| st.write("## Contact Information") |
| email = st.text_input("Email") |
| whatsapp_number = st.text_input("WhatsApp Number") |
|
|
| if st.button('Submit'): |
| |
| uploaded_file = st.file_uploader("Upload DOCX or PDF file", type=["docx", "pdf"]) |
| |
| if uploaded_file is not None: |
| file_extension = os.path.splitext(uploaded_file.name)[1].lower() |
| if file_extension not in ['.docx', '.pdf']: |
| st.error("Please upload a DOCX or PDF file.") |
| else: |
| |
| extracted_text = extract_text_from_file(uploaded_file.name) |
| |
| |
| data = { |
| "company_name": extract_company_name(extracted_text), |
| "industry": extract_industry(extracted_text), |
| "location": extract_location(extracted_text), |
| "mission": extract_mission(extracted_text), |
| "vision": extract_vision(extracted_text), |
| "products_services": extract_products_services(extracted_text), |
| "target_market": extract_target_market(extracted_text), |
| "value_proposition": extract_value_proposition(extracted_text), |
| "current_revenue": extract_current_revenue(extracted_text), |
| "current_expenses": extract_current_expenses(extracted_text), |
| "funding_requirements": extract_funding_requirements(extracted_text), |
| "management_team": extract_management_team(extracted_text), |
| "company_structure": extract_company_structure(extracted_text), |
| "goals_objectives": extract_goals_objectives(extracted_text), |
| "operational_strategy": extract_operational_strategy(extracted_text), |
| "market_overview": extract_market_overview(extracted_text), |
| "promotional_strategy": extract_promotional_strategy(extracted_text), |
| "email": email, |
| "whatsapp_number": whatsapp_number |
| } |
|
|
| |
| sections_to_process = [ |
| ("Executive Summary", generate_executive_summary), |
| ("Mission Statement", generate_mission), |
| ("Vision Statement", generate_vision), |
| ("Objectives", generate_objectives), |
| ("Core Values", generate_core_values), |
| ("Business Description Analysis", generate_business_description), |
| ("Company Location", generate_company_location), |
| ("Products", generate_products), |
| ("Ownership", generate_ownership), |
| ("Company Structure", generate_company_structure), |
| ("Management Profiles", generate_management_profiles), |
| ("Operational Strategy", generate_operational_strategy), |
| ("Marketing Mix Strategy", generate_marketing_mix), |
| ("Promotional Strategy", generate_promotional_strategy), |
| ("Market Demand Analysis", analyze_demand), |
| ("Market Segment Analysis", segment_market), |
| ("Competitor Analysis", analyze_competitors), |
| ("Porter's Five Forces Analysis", perform_porters_five_forces), |
| ("Industry Analysis", analyze_industry_accommodation), |
| ("Major Player Analysis", list_major_players), |
| ("Business Sub Sector Analysis", analyze_business_sub_sector), |
| ("SWOT Analysis", generate_swot_analysis), |
| ("Funding Request", generate_funding_request), |
| ("Financing & Bank Loan Amortization", create_financing_plan), |
| ("Income Statement Analysis", generate_pro_forma_income_statement), |
| ("Revenue Expense Analysis", predict_revenue_expenses), |
| ("Montly Cash Flow Analysis", generate_monthly_cash_flow), |
| ("Pro Forma Annual Cash Flow Analysis", generate_pro_forma_annual_cash_flow), |
| ("Pro Forma Balance Sheet Analysis", generate_pro_forma_balance_sheet), |
| ("Break-Even Analysis", perform_break_even_analysis), |
| ("Payback Period Analysis", calculate_payback_period), |
| ("Financial Graphs Analysis", generate_financial_graphs), |
| ("Risk Mitigations Analysis", identify_risks_mitigations) |
| ] |
|
|
| |
| sanitized_company_name = sanitize_filename(company_name, max_length=50) |
|
|
| |
| filename = f"Business_Plan_for_{sanitized_company_name}.docx" |
|
|
| |
| doc = create_document() |
| for section_name, generate_prompt_func in sections_to_process: |
| prompt = generate_prompt_func(data) |
| section_content = call_llm(prompt) |
| st.subheader(section_name) |
| st.write(section_content) |
| |
| |
| doc = add_section_to_doc(doc, section_name, section_content) |
| doc_bytes = get_docx_bytes(doc) |
|
|
| st.download_button( |
| label=f"Download {section_name} as DOCX", |
| data=doc_bytes, |
| file_name=f"{section_name.replace(' ', '_').lower()}.docx", |
| mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document" |
| ) |
|
|
| |
| full_filename = f"{sanized_company_name}_{section_name}.docx" |
| |
| |
| with open(filename, 'wb') as f: |
| f.write(doc_bytes.getbuffer()) |
| |
| |
| |
|
|
| collect_basic_info() |
| |