SoDa12321's picture
Update app.py
1223aeb verified
# app.py
import streamlit as st
import os
from dotenv import load_dotenv
from retrying import retry
# from functions import * # (
# extract_content_from_url,
# summarize_text,
# clean_text,
# generate_questions,
# create_document,
# add_section_to_doc,
# get_docx_bytes,
#)
from business_plan_functions import *
from groq import Groq
from exa_py import Exa
try:
nlp = spacy.load("en_core_web_lg")
except:
script = "python -m spacy download en_core_web_lg"
os.system("bash -c '%s'" % script)
nlp = spacy.load("en_core_web_lg")
# Load environment variables from .env file
load_dotenv()
# Initialize APIs
exa = Exa(api_key=os.getenv("EXA_API_KEY"))
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
utilized_model = "llama3-70b-8192"
# Exa Search configurations
highlights_options = {
"num_sentences": 7,
"highlights_per_url": 1,
}
@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.strip()
def collect_basic_info():
st.title("Business Proposal Generator")
# Option to input URL for automatic extraction
use_url = st.checkbox("Extract information from a URL")
if use_url:
url = st.text_input("Enter the URL")
if st.button("Extract and Generate Proposal"):
with st.spinner("Extracting content from URL..."):
content = extract_content_from_url(url)
if content['text']:
summary = summarize_text(content['text'])
st.subheader("Extracted Title")
st.write(content['title'])
st.subheader("Summarized Content")
st.write(summary)
# Generate questions based on summary
st.subheader("Generated Questions")
questions = generate_questions(summary)
for idx, q in enumerate(questions):
st.write(f"{idx+1}. {q['question']}")
# Prepare data for proposal generation
data = {
"company_name": content['title'],
"industry": "",
"location": "",
"mission": summary,
"vision": "",
"products_services": "",
"target_market": "",
"value_proposition": "",
"current_revenue": 0.0,
"current_expenses": 0.0,
"funding_requirements": "",
"management_team": "",
"company_structure": "",
"goals_objectives": "",
"operational_strategy": "",
"market_overview": "",
"promotional_strategy": ""
}
# Create document
doc = create_document()
# Define sections to generate
sections_to_process = [
("Executive Summary", generate_executive_summary),
("Mission Statement", generate_mission),
("Vision Statement", generate_vision),
("Products", generate_products),
("Market Analysis", analyze_market),
("SWOT Analysis", generate_swot_analysis),
# Add more sections as needed
]
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
doc = add_section_to_doc(doc, section_name, section_content)
# Download full proposal
doc_bytes = get_docx_bytes(doc)
st.download_button(
label="Download Full Business Proposal",
data=doc_bytes,
file_name="business_proposal.docx",
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
else:
st.error("Failed to extract content from the provided URL.")
else:
# Manual input fields
st.header("Manual Input")
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("Target Market")
value_proposition = st.text_area("Unique Value Proposition")
promotional_strategy = st.text_area("Promotional Strategy")
current_revenue = st.number_input("Current Revenue", min_value=0.0, format="%f")
current_expenses = st.number_input("Current Expenses", min_value=0.0, format="%f")
funding_requirements = st.text_area("Funding Requirements")
management_team = st.text_area("Management Team")
company_structure = st.text_area("Company Structure")
goals_objectives = st.text_area("Goals and Objectives")
operational_strategy = st.text_area("Operational Strategy")
market_overview = st.text_area("Market Overview")
if st.button("Generate Business Proposal"):
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
}
doc = create_document()
sections_to_process = [
("Executive Summary", generate_executive_summary),
("Mission Statement", generate_mission),
("Vision Statement", generate_vision),
("Products and Services", generate_products),
("Market Analysis", analyze_market),
("SWOT Analysis", generate_swot_analysis),
("Marketing Strategy", generate_marketing_strategy),
("Financial Plan", generate_financial_plan),
("Operational Plan", generate_operational_plan),
("Management Team", generate_management_team),
# Add more sections as needed
]
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
doc = add_section_to_doc(doc, section_name, section_content)
# Download full proposal
doc_bytes = get_docx_bytes(doc)
st.download_button(
label="Download Full Business Proposal",
data=doc_bytes,
file_name="business_proposal.docx",
mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document"
)
if __name__ == "__main__":
collect_basic_info()