AI_BizGen / app.py
mgbam's picture
Update app.py
dd6b41c verified
raw
history blame
3.88 kB
import gradio as gr
from transformers import pipeline
# Load a more capable open-source model
strategy_generator = pipeline("text2text-generation", model="google/flan-t5-large")
# Function to clean up repetitive sentences in generated text
def clean_output(text):
sentences = text.split(". ")
seen = set()
cleaned_sentences = []
for sentence in sentences:
if sentence not in seen:
seen.add(sentence)
cleaned_sentences.append(sentence)
return ". ".join(cleaned_sentences)
# Function to generate a business strategy
def generate_strategy(industry, challenge, goals):
prompt = f"""
You are a business consultant helping a company in the {industry} industry.
The company faces the following challenge: {challenge}.
The company aims to achieve the following goal: {goals}.
Provide a detailed and actionable business strategy with specific steps, measurable outcomes, and innovative ideas.
"""
try:
response = strategy_generator(prompt, max_length=300, num_return_sequences=1, temperature=0.7)
cleaned_response = clean_output(response[0]['generated_text'])
return cleaned_response
except Exception as e:
return f"Error generating strategy: {e}"
# Function to perform a SWOT analysis
def swot_analysis(strengths, weaknesses, opportunities, threats):
prompt = f"""
You are a business consultant performing a SWOT analysis for a company.
- Strengths: {strengths}
- Weaknesses: {weaknesses}
- Opportunities: {opportunities}
- Threats: {threats}
Provide actionable insights for each category and suggestions on how to maximize strengths and opportunities while addressing weaknesses and threats.
"""
try:
response = strategy_generator(prompt, max_length=300, num_return_sequences=1, temperature=0.7)
cleaned_response = clean_output(response[0]['generated_text'])
return cleaned_response
except Exception as e:
return f"Error generating SWOT analysis: {e}"
# Gradio interface
with gr.Blocks() as demo:
gr.Markdown("# AI Business Strategy Generator")
gr.Markdown("Generate actionable business strategies and SWOT analyses using AI.")
# Tab 1: Generate Business Strategy
with gr.Tab("Generate Strategy"):
gr.Markdown("### Input Information to Generate a Business Strategy")
industry_input = gr.Textbox(label="Industry", placeholder="E.g., E-commerce, Healthcare")
challenge_input = gr.Textbox(label="Key Challenge", placeholder="E.g., Low customer retention")
goals_input = gr.Textbox(label="Goals", placeholder="E.g., Increase sales by 20% in 6 months")
strategy_button = gr.Button("Generate Strategy")
strategy_output = gr.Textbox(label="Generated Strategy", lines=10)
strategy_button.click(
generate_strategy,
inputs=[industry_input, challenge_input, goals_input],
outputs=[strategy_output]
)
# Tab 2: SWOT Analysis
with gr.Tab("SWOT Analysis"):
gr.Markdown("### Input Information to Perform a SWOT Analysis")
strengths_input = gr.Textbox(label="Strengths", placeholder="E.g., Strong brand presence")
weaknesses_input = gr.Textbox(label="Weaknesses", placeholder="E.g., Limited market reach")
opportunities_input = gr.Textbox(label="Opportunities", placeholder="E.g., Emerging markets")
threats_input = gr.Textbox(label="Threats", placeholder="E.g., Rising competition")
swot_button = gr.Button("Perform SWOT Analysis")
swot_output = gr.Textbox(label="Generated SWOT Analysis", lines=10)
swot_button.click(
swot_analysis,
inputs=[strengths_input, weaknesses_input, opportunities_input, threats_input],
outputs=[swot_output]
)
# Launch the Gradio app
demo.launch()