mgbam commited on
Commit
8d5049d
·
verified ·
1 Parent(s): 2494531

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -23
app.py CHANGED
@@ -1,19 +1,19 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
3
 
4
- # Load a more capable open-source model
5
- strategy_generator = pipeline("text2text-generation", model="google/flan-t5-large")
 
 
6
 
7
- # Function to clean up repetitive or irrelevant sentences in generated text
8
- def clean_output(text):
9
- sentences = text.split(". ")
10
- seen = set()
11
- cleaned_sentences = []
12
- for sentence in sentences:
13
- if sentence.strip() not in seen:
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=300, num_return_sequences=1, temperature=0.7, top_p=0.9)
29
- cleaned_response = clean_output(response[0]['generated_text'])
30
- return cleaned_response
31
  except Exception as e:
32
  return f"Error generating steps: {e}"
33
 
34
- # Function to generate "why" and "how" for each step
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=200, num_return_sequences=1, temperature=0.7, top_p=0.9)
45
- cleaned_response = clean_output(response[0]['generated_text'])
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
- # Expand each step
58
  steps_list = steps.split("\n")
59
  detailed_steps = []
60
  for step in steps_list:
61
  if step.strip():
62
- detailed_steps.append(f"{step}\n{expand_step(step)}")
 
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