SoDa12321's picture
Rename app.py to app_1.py
35d6012 verified
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 environment variables from .env file
load_dotenv()
# Declare the exa search API
exa = Exa(api_key=os.getenv("EXA_API_KEY"))
# Define your API Model and key
#client = Groq(api_key=os.getenv("GROQ_API_KEY"))
utilized_model = "llama3-70b-8192"
try:
# Use a custom HTTP client
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,
}
# Function to create a new document
def create_document():
doc = Document()
doc.add_heading("Business Proposal", 0)
return doc
# Function to add a section to the document
def add_section_to_doc(doc, section_name, section_content):
section_content = strip_md(section_content)
section_content = section_content.replace("\\", "") # Remove backslashes
doc.add_heading(section_name, level=1)
doc.add_paragraph(section_content)
return doc
# Function to get DOCX bytes
def get_docx_bytes(doc):
doc_io = io.BytesIO()
doc.save(doc_io)
doc_io.seek(0)
return doc_io
# Function to send email with attachment
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
# Attach the body of the email
msg.attach(MIMEText(body + f"\n\nContent of the section:\n\n{section_content}", 'plain'))
# Attach the DOCX file
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)
# Send the email
with smtplib.SMTP('smtp.gmail.com', 587) as server:
server.starttls()
server.login(from_email, email_password)
server.send_message(msg)
# Return success message
return f"Email sent successfully to {to_email} for section '{subject}'."
except Exception as e:
# Return error message
return f"Failed to send email to {to_email}: {str(e)}"
finally:
server.quit()
# Function to sanitize filenames
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.
"""
# Remove invalid characters
sanitized = re.sub(r'[<>:"/\\|?*]', '', filename)
# Ensure the filename is within the max_length
return sanitized[:max_length]
# Function to extract text from DOCX or PDF files
@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 ""
# Function to collect basic information
def collect_basic_info():
st.title("Business Proposal Generator")
# Basic Company Information
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")
# Add your contact information
st.write("## Contact Information")
email = st.text_input("Email")
whatsapp_number = st.text_input("WhatsApp Number")
if st.button('Submit'):
# Collect data from uploaded file
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:
# Extract text from uploaded file
extracted_text = extract_text_from_file(uploaded_file.name)
# Create data dictionary based on extracted text
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
}
# Process and update document with each section
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)
]
# Sanitize the company name to be used in file names
sanitized_company_name = sanitize_filename(company_name, max_length=50)
# Generate final file name and ensure it is not too long
filename = f"Business_Plan_for_{sanitized_company_name}.docx"
# Create a new document
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)
# Update document and create download link
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"
)
# Save section content in a file
full_filename = f"{sanized_company_name}_{section_name}.docx"
#with open(full_filename, 'wb') as f:Y1:0```
with open(filename, 'wb') as f:
f.write(doc_bytes.getbuffer())
# Send email with section content and attachment
#send_email_with_attachment(email, f"{section_name} of Your Business Proposal", "Please find attached the section of your business proposal.", filename, section_content)
collect_basic_info()