ivmpfa commited on
Commit
101cd9b
·
verified ·
1 Parent(s): 2a3cf84

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -12
app.py CHANGED
@@ -3,7 +3,7 @@ from transformers import pipeline
3
  import json
4
  import signal
5
 
6
- # Load a small, instruction-following model
7
  generator = pipeline("text2text-generation", model="google/flan-t5-large")
8
 
9
  # Timeout handling
@@ -19,23 +19,37 @@ def generate_test_cases(user_story):
19
  try:
20
  signal.alarm(180) # Set a 3-minute timeout
21
 
22
- # Improved structured prompt
23
  prompt = (
24
- f"Given the user story below, generate software test cases in JSON format.\n"
25
  f"User Story: {user_story}\n"
26
- f"Format:\n"
27
- f'{{"test_cases": [{{"id": 1, "title": "Test case title", "steps": ["Step 1", "Step 2"], "expected_result": "Expected outcome"}}]}}\n'
28
- f"Ensure the output follows the JSON format strictly."
29
  )
30
 
31
  output = generator(prompt, max_length=512, do_sample=False)[0]["generated_text"]
32
 
33
- # Attempt to parse JSON
34
- try:
35
- json_output = json.loads(output)
36
- formatted_output = json.dumps(json_output, indent=4)
37
- except json.JSONDecodeError:
38
- formatted_output = "Error: Model did not return valid JSON. Try re-running."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  signal.alarm(0) # Disable timeout if successful
41
  return formatted_output
 
3
  import json
4
  import signal
5
 
6
+ # Load a small, free, instruction-following model
7
  generator = pipeline("text2text-generation", model="google/flan-t5-large")
8
 
9
  # Timeout handling
 
19
  try:
20
  signal.alarm(180) # Set a 3-minute timeout
21
 
22
+ # Structured prompt for better response
23
  prompt = (
24
+ f"Generate structured test cases for the following user story:\n"
25
  f"User Story: {user_story}\n"
26
+ f"Provide output in a clear, structured way with a test case title, steps, and expected result."
 
 
27
  )
28
 
29
  output = generator(prompt, max_length=512, do_sample=False)[0]["generated_text"]
30
 
31
+ # Simple manual post-processing to force JSON format
32
+ test_cases = []
33
+ cases = output.split("\n\n") # Split into test cases
34
+
35
+ for i, case in enumerate(cases, start=1):
36
+ lines = case.split("\n")
37
+ if len(lines) >= 3:
38
+ title = lines[0].strip()
39
+ steps = [line.strip() for line in lines[1:-1]]
40
+ expected_result = lines[-1].strip()
41
+
42
+ test_cases.append({
43
+ "id": i,
44
+ "title": title,
45
+ "steps": steps,
46
+ "expected_result": expected_result
47
+ })
48
+
49
+ if not test_cases:
50
+ return "Error: Model did not return structured test cases. Try again."
51
+
52
+ formatted_output = json.dumps({"test_cases": test_cases}, indent=4)
53
 
54
  signal.alarm(0) # Disable timeout if successful
55
  return formatted_output