Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,19 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
-
# Load a
|
| 5 |
-
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
seen.add(sentence.strip())
|
| 15 |
-
cleaned_sentences.append(sentence.strip())
|
| 16 |
-
return ". ".join(cleaned_sentences)
|
| 17 |
|
| 18 |
# Function to generate actionable steps
|
| 19 |
def generate_steps(industry, challenge, goals):
|
|
@@ -25,13 +25,12 @@ def generate_steps(industry, challenge, goals):
|
|
| 25 |
Focus on specific, realistic, and innovative strategies relevant to the industry.
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
-
response = strategy_generator(prompt, max_length=
|
| 29 |
-
|
| 30 |
-
return cleaned_response
|
| 31 |
except Exception as e:
|
| 32 |
return f"Error generating steps: {e}"
|
| 33 |
|
| 34 |
-
# Function to
|
| 35 |
def expand_step(step):
|
| 36 |
prompt = f"""
|
| 37 |
You are a business consultant. For the following strategy:
|
|
@@ -41,25 +40,25 @@ def expand_step(step):
|
|
| 41 |
- How to implement this step effectively.
|
| 42 |
"""
|
| 43 |
try:
|
| 44 |
-
response = strategy_generator(prompt, max_length=
|
| 45 |
-
|
| 46 |
-
return cleaned_response
|
| 47 |
except Exception as e:
|
| 48 |
return f"Error expanding step: {e}"
|
| 49 |
|
| 50 |
-
# Combined function to generate strategy
|
| 51 |
def generate_strategy(industry, challenge, goals):
|
| 52 |
# Generate initial steps
|
| 53 |
steps = generate_steps(industry, challenge, goals)
|
| 54 |
if "Error" in steps:
|
| 55 |
return steps
|
| 56 |
|
| 57 |
-
#
|
| 58 |
steps_list = steps.split("\n")
|
| 59 |
detailed_steps = []
|
| 60 |
for step in steps_list:
|
| 61 |
if step.strip():
|
| 62 |
-
|
|
|
|
| 63 |
|
| 64 |
return "\n\n".join(detailed_steps)
|
| 65 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline, AutoModelForSeq2SeqLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
+
# Load a smaller, optimized model
|
| 6 |
+
model_name = "google/flan-t5-base" # Switch to a smaller model for faster inference
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 9 |
|
| 10 |
+
# Load model onto CPU with optimization
|
| 11 |
+
strategy_generator = pipeline(
|
| 12 |
+
"text2text-generation",
|
| 13 |
+
model=model,
|
| 14 |
+
tokenizer=tokenizer,
|
| 15 |
+
device=0 if torch.cuda.is_available() else -1, # Use GPU if available
|
| 16 |
+
)
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Function to generate actionable steps
|
| 19 |
def generate_steps(industry, challenge, goals):
|
|
|
|
| 25 |
Focus on specific, realistic, and innovative strategies relevant to the industry.
|
| 26 |
"""
|
| 27 |
try:
|
| 28 |
+
response = strategy_generator(prompt, max_length=200, num_return_sequences=1, temperature=0.7, top_p=0.9)
|
| 29 |
+
return response[0]['generated_text']
|
|
|
|
| 30 |
except Exception as e:
|
| 31 |
return f"Error generating steps: {e}"
|
| 32 |
|
| 33 |
+
# Function to combine rationale ("why") and implementation ("how")
|
| 34 |
def expand_step(step):
|
| 35 |
prompt = f"""
|
| 36 |
You are a business consultant. For the following strategy:
|
|
|
|
| 40 |
- How to implement this step effectively.
|
| 41 |
"""
|
| 42 |
try:
|
| 43 |
+
response = strategy_generator(prompt, max_length=150, num_return_sequences=1, temperature=0.7, top_p=0.9)
|
| 44 |
+
return response[0]['generated_text']
|
|
|
|
| 45 |
except Exception as e:
|
| 46 |
return f"Error expanding step: {e}"
|
| 47 |
|
| 48 |
+
# Combined function to generate detailed strategy
|
| 49 |
def generate_strategy(industry, challenge, goals):
|
| 50 |
# Generate initial steps
|
| 51 |
steps = generate_steps(industry, challenge, goals)
|
| 52 |
if "Error" in steps:
|
| 53 |
return steps
|
| 54 |
|
| 55 |
+
# Split steps and expand each
|
| 56 |
steps_list = steps.split("\n")
|
| 57 |
detailed_steps = []
|
| 58 |
for step in steps_list:
|
| 59 |
if step.strip():
|
| 60 |
+
expanded = expand_step(step)
|
| 61 |
+
detailed_steps.append(f"{step}\n{expanded}")
|
| 62 |
|
| 63 |
return "\n\n".join(detailed_steps)
|
| 64 |
|