ivmpfa commited on
Commit
2a3cf84
·
verified ·
1 Parent(s): 88dbb60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -16
app.py CHANGED
@@ -3,10 +3,10 @@ from transformers import pipeline
3
  import json
4
  import signal
5
 
6
- # Load a small, free, instruction-following model
7
- generator = pipeline("text2text-generation", model="google/flan-t5-base")
8
 
9
- # Function to handle timeouts
10
  class TimeoutException(Exception):
11
  pass
12
 
@@ -17,36 +17,33 @@ signal.signal(signal.SIGALRM, timeout_handler)
17
 
18
  def generate_test_cases(user_story):
19
  try:
20
- # Set a 3-minute timeout
21
- signal.alarm(180)
22
 
23
- # Force JSON output using a structured prompt
24
  prompt = (
25
- f"Convert the following user story into structured JSON test cases.\n"
26
- f"User story: {user_story}\n"
27
- f"Output format:\n"
28
- f'{{"test_cases": [{{"id": 1, "title": "Test case title", "steps": ["Step 1", "Step 2"], "expected_result": "Expected outcome"}}]}}'
 
29
  )
30
 
31
- # Generate output
32
  output = generator(prompt, max_length=512, do_sample=False)[0]["generated_text"]
33
 
34
- # Try parsing as JSON to ensure proper formatting
35
  try:
36
  json_output = json.loads(output)
37
  formatted_output = json.dumps(json_output, indent=4)
38
  except json.JSONDecodeError:
39
  formatted_output = "Error: Model did not return valid JSON. Try re-running."
40
 
41
- # Disable the alarm if processing is successful
42
- signal.alarm(0)
43
-
44
  return formatted_output
45
 
46
  except TimeoutException:
47
  return "Processing timed out. Please try again with a simpler input."
48
 
49
- # Create Gradio interface
50
  iface = gr.Interface(
51
  fn=generate_test_cases,
52
  inputs=gr.Textbox(lines=5, placeholder="Enter your user story here..."),
 
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
10
  class TimeoutException(Exception):
11
  pass
12
 
 
17
 
18
  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
42
 
43
  except TimeoutException:
44
  return "Processing timed out. Please try again with a simpler input."
45
 
46
+ # Gradio UI
47
  iface = gr.Interface(
48
  fn=generate_test_cases,
49
  inputs=gr.Textbox(lines=5, placeholder="Enter your user story here..."),