ivmpfa commited on
Commit
fe16918
·
verified ·
1 Parent(s): 19108a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -18
app.py CHANGED
@@ -1,36 +1,65 @@
1
- from transformers import pipeline
2
  import torch
 
 
3
 
4
- # Initialize the smaller GPT-2 model (distilgpt2)
 
 
 
5
  print("Initializing distilgpt2 model pipeline...")
6
- try:
7
- model = pipeline(
8
- "text-generation",
9
- model="distilgpt2", # Use the smaller, efficient distilgpt2 model
10
- torch_dtype=torch.float32 # Use float32 for CPU compatibility
11
- )
12
- print("distilgpt2 model pipeline initialized successfully.")
13
- except Exception as e:
14
- print(f"Error initializing model: {e}")
15
- raise
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  def generate_test_cases(requirement):
18
- # Formulate the prompt to ask the model to generate test cases in JSON format
19
- prompt = f"Generate test cases for the following software requirement in JSON format: '{requirement}'. Only provide the JSON array of test cases."
20
 
21
  try:
22
  print("Generating test cases...")
23
- # Generate text (test cases) with a max length of 300 characters
24
- result = model(prompt, max_length=300, num_return_sequences=1)[0]["generated_text"]
25
  print("Test cases generated successfully.")
26
- return result.strip()
 
 
 
 
 
 
27
  except Exception as e:
28
  print(f"Error during generation: {e}")
29
  return None
30
 
31
  # Example usage
32
  if __name__ == "__main__":
33
- # Sample requirements for testing
34
  requirements = [
35
  "User login functionality with email and password",
36
  "Search functionality on an e-commerce website",
 
 
1
  import torch
2
+ from transformers import pipeline, set_seed
3
+ import time
4
 
5
+ # Set random seed for reproducibility
6
+ set_seed(42)
7
+
8
+ # Initialize model pipeline with better error handling
9
  print("Initializing distilgpt2 model pipeline...")
 
 
 
 
 
 
 
 
 
 
10
 
11
+ def initialize_model():
12
+ try:
13
+ # Set up the model pipeline with distilgpt2
14
+ model = pipeline(
15
+ "text-generation",
16
+ model="distilgpt2", # Load the distilgpt2 model
17
+ tokenizer="distilgpt2", # Ensure tokenizer is loaded as well
18
+ torch_dtype=torch.float32 # Ensures compatibility with CPU
19
+ )
20
+ print("Model pipeline initialized successfully.")
21
+ return model
22
+ except Exception as e:
23
+ print(f"Error during model initialization: {e}")
24
+ return None
25
+
26
+ # Retry logic to handle initialization issues
27
+ model = None
28
+ attempts = 0
29
+ max_attempts = 3
30
+ while model is None and attempts < max_attempts:
31
+ print(f"Attempt {attempts + 1}/{max_attempts} to initialize the model...")
32
+ model = initialize_model()
33
+ attempts += 1
34
+ if model is None:
35
+ print(f"Retrying after a delay...")
36
+ time.sleep(10) # Wait 10 seconds before retrying
37
+
38
+ if model is None:
39
+ print("Failed to initialize the model after several attempts. Exiting.")
40
+ exit(1) # Exit if model is not initialized successfully
41
+
42
+ # Generate test cases function
43
  def generate_test_cases(requirement):
44
+ prompt = f"Generate detailed test cases in JSON format for the following software requirement: '{requirement}'. Please return only the JSON array with test cases."
 
45
 
46
  try:
47
  print("Generating test cases...")
48
+ result = model(prompt, max_length=500, num_return_sequences=1)[0]["generated_text"]
 
49
  print("Test cases generated successfully.")
50
+
51
+ # Clean up output to only return JSON array
52
+ clean_result = result.strip().split("\n")[0]
53
+ if clean_result.startswith('[') and clean_result.endswith(']'):
54
+ return clean_result
55
+ else:
56
+ return f"Error: Unexpected output format.\n{result.strip()}"
57
  except Exception as e:
58
  print(f"Error during generation: {e}")
59
  return None
60
 
61
  # Example usage
62
  if __name__ == "__main__":
 
63
  requirements = [
64
  "User login functionality with email and password",
65
  "Search functionality on an e-commerce website",