File size: 4,440 Bytes
9434c21 3768f3b 9434c21 51b14ac dd6b41c f6d0b11 dd6b41c f6d0b11 22ea7cc 9434c21 f6d0b11 dd6b41c f6d0b11 9434c21 3768f3b 51b14ac f6d0b11 3768f3b 9434c21 22ea7cc 9434c21 f6d0b11 3768f3b f6d0b11 9434c21 3768f3b 51b14ac f6d0b11 3768f3b 9434c21 22ea7cc 9434c21 22ea7cc 9434c21 22ea7cc 9434c21 22ea7cc 9434c21 22ea7cc 9434c21 22ea7cc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
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 or irrelevant sentences in generated text
def clean_output(text):
sentences = text.split(". ")
seen = set()
cleaned_sentences = []
for sentence in sentences:
if sentence.strip() not in seen:
seen.add(sentence.strip())
cleaned_sentences.append(sentence.strip())
return ". ".join(cleaned_sentences)
# Function to validate and refine generated text
def validate_output(output, prompt):
if len(output.split()) < 10 or output.lower().startswith("the company") or output.count("The company") > 2:
return "The response was unclear. Please adjust the input or try again with a refined question."
return clean_output(output)
# Function to generate a business strategy
def generate_strategy(industry, challenge, goals):
prompt = f"""
You are an experienced business consultant specializing in the {industry} industry.
The company faces the following challenge: {challenge}.
The company's goal is to achieve: {goals}.
Provide a strategy with:
1. Three to five actionable steps.
2. Measurable outcomes within the next 6 months to 1 year.
Example:
1. Improve marketing through targeted ads.
2. Streamline operations for better efficiency.
3. Partner with local businesses to reach new customers.
"""
try:
response = strategy_generator(prompt, max_length=300, num_return_sequences=1, temperature=0.7, top_p=0.9)
validated_response = validate_output(response[0]['generated_text'], prompt)
return validated_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 an experienced business consultant performing a SWOT analysis for a company.
- Strengths: {strengths}
- Weaknesses: {weaknesses}
- Opportunities: {opportunities}
- Threats: {threats}
Provide detailed insights for each category and specific recommendations for improvement.
"""
try:
response = strategy_generator(prompt, max_length=300, num_return_sequences=1, temperature=0.7, top_p=0.9)
validated_response = validate_output(response[0]['generated_text'], prompt)
return validated_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()
|