Spaces:
Sleeping
Sleeping
Create backend.py
Browse files- backend.py +66 -0
backend.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import csv
|
| 2 |
+
import io
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
from bs4 import BeautifulSoup
|
| 5 |
+
|
| 6 |
+
# Initialize the model (e.g., FLAN-T5)
|
| 7 |
+
model = pipeline(
|
| 8 |
+
"text-generation",
|
| 9 |
+
model="google/flan-t5-xxl", # Replace with your chosen model
|
| 10 |
+
torch_dtype=torch.float16 # Reduce memory usage
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def clean_test_case_output(text):
|
| 14 |
+
text = text.strip()
|
| 15 |
+
soup = BeautifulSoup(text, 'html.parser')
|
| 16 |
+
return soup.get_text(separator="\n").strip()
|
| 17 |
+
|
| 18 |
+
def generate_testcases(user_story):
|
| 19 |
+
few_shot_examples = """
|
| 20 |
+
"Generate as many test cases as possible (minimum 6)"
|
| 21 |
+
"Understand the user story thoroughly"
|
| 22 |
+
"If it's a portal-related user story, test in ODAC Portal; else, test in Tech360 iOS App"
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
prompt = f"{few_shot_examples}\nUser Story: {user_story}"
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
result = model(prompt, max_length=4096)[0]["generated_text"]
|
| 29 |
+
cleaned_text = clean_test_case_output(result)
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
test_cases = json.loads(cleaned_text)
|
| 33 |
+
if isinstance(test_cases, list):
|
| 34 |
+
return test_cases
|
| 35 |
+
else:
|
| 36 |
+
return [{"test_case": cleaned_text}]
|
| 37 |
+
except json.JSONDecodeError:
|
| 38 |
+
return [{"test_case": cleaned_text}]
|
| 39 |
+
|
| 40 |
+
except Exception as e:
|
| 41 |
+
return [{"test_case": f"Error: {str(e)}"}]
|
| 42 |
+
|
| 43 |
+
def export_test_cases(test_cases, format='csv'):
|
| 44 |
+
if not test_cases:
|
| 45 |
+
return "No test cases to export."
|
| 46 |
+
|
| 47 |
+
if format == 'csv':
|
| 48 |
+
output = io.StringIO()
|
| 49 |
+
writer = csv.writer(output)
|
| 50 |
+
writer.writerow(["Test Case"])
|
| 51 |
+
for case in test_cases:
|
| 52 |
+
writer.writerow([case.get("test_case", "N/A")])
|
| 53 |
+
return output.getvalue()
|
| 54 |
+
else:
|
| 55 |
+
raise ValueError("Unsupported format. Only CSV is supported.")
|
| 56 |
+
|
| 57 |
+
def save_test_cases_as_file(test_cases, format='csv'):
|
| 58 |
+
if format == 'csv':
|
| 59 |
+
with open('test_cases.csv', 'w', newline='') as f:
|
| 60 |
+
writer = csv.writer(f)
|
| 61 |
+
writer.writerow(["Test Case"])
|
| 62 |
+
for case in test_cases:
|
| 63 |
+
writer.writerow([case.get("test_case", "N/A")])
|
| 64 |
+
return "test_cases.csv saved"
|
| 65 |
+
else:
|
| 66 |
+
return f"Unsupported format: {format}"
|