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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from docx import Document
|
| 3 |
+
import re
|
| 4 |
+
import io
|
| 5 |
+
import os
|
| 6 |
+
from fpdf import FPDF
|
| 7 |
+
from groq import Groq
|
| 8 |
+
from exa_py import Exa
|
| 9 |
+
from dotenv import load_dotenv
|
| 10 |
+
from retrying import retry
|
| 11 |
+
from funtions import *
|
| 12 |
+
|
| 13 |
+
# Load environment variables from .env file
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
# Declare the exa search API
|
| 17 |
+
exa = Exa(api_key=os.getenv("EXA_API_KEY"))
|
| 18 |
+
|
| 19 |
+
# Define your API Model and key
|
| 20 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 21 |
+
utilized_model = "llama3-70b-8192"
|
| 22 |
+
|
| 23 |
+
# Functions for the Exa Search content & Parameters for Highlights search
|
| 24 |
+
highlights_options = {
|
| 25 |
+
"num_sentences": 7, # Length of highlights
|
| 26 |
+
"highlights_per_url": 1, # Get the best highlight for each URL
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000, stop_max_attempt_number=5)
|
| 30 |
+
def call_llm(prompt):
|
| 31 |
+
search_response = exa.search_and_contents(query=prompt, highlights=highlights_options, num_results=3, use_autoprompt=True)
|
| 32 |
+
info = [sr.highlights[0] for sr in search_response.results]
|
| 33 |
+
|
| 34 |
+
system_prompt = "You are a Business proposal generator. Read the provided contexts and, if relevant, use them to answer the user's question."
|
| 35 |
+
user_prompt = f"Sources: {info}\nQuestion: {prompt}"
|
| 36 |
+
|
| 37 |
+
completion = client.chat.completions.create(
|
| 38 |
+
model=utilized_model,
|
| 39 |
+
messages=[
|
| 40 |
+
{"role": "system", "content": system_prompt},
|
| 41 |
+
{"role": "user", "content": user_prompt},
|
| 42 |
+
]
|
| 43 |
+
)
|
| 44 |
+
return completion.choices[0].message.content
|
| 45 |
+
|
| 46 |
+
def strip_md(text):
|
| 47 |
+
text = text.replace("**", "").replace("*", "").replace("#", "")
|
| 48 |
+
return re.sub(r'([!*_=~-])', r'\\\1', text)
|
| 49 |
+
|
| 50 |
+
def create_document():
|
| 51 |
+
doc = Document()
|
| 52 |
+
doc.add_heading("Business Proposal", 0)
|
| 53 |
+
return doc
|
| 54 |
+
|
| 55 |
+
def add_section_to_doc(doc, section_name, section_content):
|
| 56 |
+
section_content = strip_md(section_content)
|
| 57 |
+
section_content = section_content.replace("\\", "") # Remove backslashes
|
| 58 |
+
doc.add_heading(section_name, level=1)
|
| 59 |
+
doc.add_paragraph(section_content)
|
| 60 |
+
return doc
|
| 61 |
+
|
| 62 |
+
def get_docx_bytes(doc):
|
| 63 |
+
doc_io = io.BytesIO()
|
| 64 |
+
doc.save(doc_io)
|
| 65 |
+
doc_io.seek(0)
|
| 66 |
+
return doc_io
|
| 67 |
+
|
| 68 |
+
def collect_basic_info():
|
| 69 |
+
st.title("Business Proposal Generator")
|
| 70 |
+
|
| 71 |
+
# Basic Company Information
|
| 72 |
+
company_name = st.text_input("Company Name")
|
| 73 |
+
industry = st.text_input("Industry")
|
| 74 |
+
location = st.text_input("Location")
|
| 75 |
+
mission = st.text_area("Mission Statement")
|
| 76 |
+
vision = st.text_area("Vision Statement")
|
| 77 |
+
products_services = st.text_area("Description of Products/Services")
|
| 78 |
+
target_market = st.text_area("Customer Segments")
|
| 79 |
+
value_proposition = st.text_area("Competitive Advantage")
|
| 80 |
+
promotional_strategy = st.text_area("Promotional Strategy")
|
| 81 |
+
current_revenue = st.number_input("Current Revenue (R)", min_value=0.0, format="%f")
|
| 82 |
+
current_expenses = st.number_input("Current Expenses (R)", min_value=0.0, format="%f")
|
| 83 |
+
funding_requirements = st.text_area("Funding Requirements")
|
| 84 |
+
management_team = st.text_area("Key Personnel")
|
| 85 |
+
company_structure = st.text_area("Company Structure")
|
| 86 |
+
goals_objectives = st.text_area("Short-term and Long-term Goals")
|
| 87 |
+
operational_strategy = st.text_area("Operational Strategy")
|
| 88 |
+
market_overview = st.text_area("Market Overview")
|
| 89 |
+
|
| 90 |
+
if st.button('Submit'):
|
| 91 |
+
# Collect data
|
| 92 |
+
data = {
|
| 93 |
+
"company_name": company_name,
|
| 94 |
+
"industry": industry,
|
| 95 |
+
"location": location,
|
| 96 |
+
"mission": mission,
|
| 97 |
+
"vision": vision,
|
| 98 |
+
"products_services": products_services,
|
| 99 |
+
"target_market": target_market,
|
| 100 |
+
"value_proposition": value_proposition,
|
| 101 |
+
"current_revenue": current_revenue,
|
| 102 |
+
"current_expenses": current_expenses,
|
| 103 |
+
"funding_requirements": funding_requirements,
|
| 104 |
+
"management_team": management_team,
|
| 105 |
+
"company_structure": company_structure,
|
| 106 |
+
"goals_objectives": goals_objectives,
|
| 107 |
+
"operational_strategy": operational_strategy,
|
| 108 |
+
"market_overview": market_overview,
|
| 109 |
+
"promotional_strategy": promotional_strategy
|
| 110 |
+
}
|
| 111 |
+
|
| 112 |
+
# Create a new document
|
| 113 |
+
doc = create_document()
|
| 114 |
+
|
| 115 |
+
# Process and update document with each section
|
| 116 |
+
sections_to_process = [
|
| 117 |
+
("Executive Summary", generate_executive_summary),
|
| 118 |
+
("Mission Statement", generate_mission),
|
| 119 |
+
("Vision Statement", generate_vision),
|
| 120 |
+
("Objectives", generate_objectives),
|
| 121 |
+
("Core Values", generate_core_values),
|
| 122 |
+
("Business Description Analysis", generate_business_description),
|
| 123 |
+
("Company Location", generate_company_location),
|
| 124 |
+
("Products", generate_products),
|
| 125 |
+
("Ownership", generate_ownership),
|
| 126 |
+
("Company Structure", generate_company_structure),
|
| 127 |
+
("Management Profiles", generate_management_profiles),
|
| 128 |
+
("Operational Strategy", generate_operational_strategy),
|
| 129 |
+
("Marketing Mix Strategy", generate_marketing_mix),
|
| 130 |
+
("Promotional Strategy", generate_promotional_strategy),
|
| 131 |
+
("Market Demand Analysis", analyze_demand),
|
| 132 |
+
("Market Segment Analysis", segment_market),
|
| 133 |
+
("Competitor Analysis", analyze_competitors),
|
| 134 |
+
("Porter's Five Forces Analysis", perform_porters_five_forces),
|
| 135 |
+
("Industry Analysis", analyze_industry_accommodation),
|
| 136 |
+
("Major Player Analysis", list_major_players),
|
| 137 |
+
("Business Sub Sector Analysis", analyze_business_sub_sector),
|
| 138 |
+
("SWOT Analysis", generate_swot_analysis),
|
| 139 |
+
("Funding Request", generate_funding_request),
|
| 140 |
+
("Financing & Bank Loan Amortization", create_financing_plan),
|
| 141 |
+
("Income Statement Analysis", generate_pro_forma_income_statement),
|
| 142 |
+
("Revenue Expense Analysis", predict_revenue_expenses),
|
| 143 |
+
("Montly Cash Flow Analysis", generate_monthly_cash_flow),
|
| 144 |
+
("Pro Forma Annual Cash Flow Analysis", generate_pro_forma_annual_cash_flow),
|
| 145 |
+
("Pro Forma Balance Sheet Analysis", generate_pro_forma_balance_sheet),
|
| 146 |
+
("Break-Even Analysis", perform_break_even_analysis),
|
| 147 |
+
("Payback Period Analysis", calculate_payback_period),
|
| 148 |
+
("Financial Graphs Analysis", generate_financial_graphs),
|
| 149 |
+
("Risk Mitigations Analysis", identify_risks_mitigations)
|
| 150 |
+
]
|
| 151 |
+
|
| 152 |
+
for section_name, generate_prompt_func in sections_to_process:
|
| 153 |
+
prompt = generate_prompt_func(data)
|
| 154 |
+
section_content = call_llm(prompt)
|
| 155 |
+
st.subheader(section_name)
|
| 156 |
+
st.write(section_content)
|
| 157 |
+
|
| 158 |
+
# Update document and create download link
|
| 159 |
+
doc = add_section_to_doc(doc, section_name, section_content)
|
| 160 |
+
doc_bytes = get_docx_bytes(doc)
|
| 161 |
+
|
| 162 |
+
st.download_button(
|
| 163 |
+
label=f"Download {section_name} as DOCX",
|
| 164 |
+
data=doc_bytes,
|
| 165 |
+
file_name=f"{section_name.replace(' ', '_').lower()}.docx",
|
| 166 |
+
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
|
| 167 |
+
)
|
| 168 |
+
|
| 169 |
+
collect_basic_info()
|