sosa123454321's picture
Update app.py
a38df1c verified
import streamlit as st
from docx import Document
import re
import io
import os
from fpdf import FPDF
from groq import Groq
from exa_py import Exa
from dotenv import load_dotenv
from retrying import retry
from funtions 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"
# Functions for the Exa Search content & Parameters for Highlights search
highlights_options = {
"num_sentences": 7, # Length of highlights
"highlights_per_url": 1, # Get the best highlight for each URL
}
@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000, stop_max_attempt_number=5)
def call_llm(prompt):
search_response = exa.search_and_contents(query=prompt, highlights=highlights_options, num_results=3, use_autoprompt=True)
info = [sr.highlights[0] for sr in search_response.results]
system_prompt = "You are a Business proposal generator. Read the provided contexts and, if relevant, use them to answer the user's question."
user_prompt = f"Sources: {info}\nQuestion: {prompt}"
completion = client.chat.completions.create(
model=utilized_model,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": user_prompt},
]
)
return completion.choices[0].message.content
def strip_md(text):
text = text.replace("**", "").replace("*", "").replace("#", "")
return re.sub(r'([!*_=~-])', r'\\\1', text)
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("\\", "") # Remove backslashes
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 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_input("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")
if st.button('Submit'):
# Collect data
data = {
"company_name": company_name,
"industry": industry,
"location": location,
"mission": mission,
"vision": vision,
"products_services": products_services,
"target_market": target_market,
"value_proposition": value_proposition,
"current_revenue": current_revenue,
"current_expenses": current_expenses,
"funding_requirements": funding_requirements,
"management_team": management_team,
"company_structure": company_structure,
"goals_objectives": goals_objectives,
"operational_strategy": operational_strategy,
"market_overview": market_overview,
"promotional_strategy": promotional_strategy
}
# Create a new document
doc = create_document()
# 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)
]
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"
)
collect_basic_info()