Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,10 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
import signal
|
| 4 |
|
| 5 |
-
# Load model
|
| 6 |
-
generator = pipeline("
|
| 7 |
|
| 8 |
# Function to handle timeouts
|
| 9 |
class TimeoutException(Exception):
|
|
@@ -17,15 +18,30 @@ signal.signal(signal.SIGALRM, timeout_handler)
|
|
| 17 |
def generate_test_cases(user_story):
|
| 18 |
try:
|
| 19 |
# Set a 3-minute timeout
|
| 20 |
-
signal.alarm(
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
# Disable the alarm if processing is successful
|
| 26 |
signal.alarm(0)
|
| 27 |
|
| 28 |
-
return
|
| 29 |
|
| 30 |
except TimeoutException:
|
| 31 |
return "Processing timed out. Please try again with a simpler input."
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
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):
|
|
|
|
| 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."
|