Update backend.py
Browse files- backend.py +13 -24
backend.py
CHANGED
|
@@ -33,7 +33,6 @@ def generate_testcases(user_story):
|
|
| 33 |
|
| 34 |
# Few-shot learning examples to guide the model
|
| 35 |
few_shot_examples = """
|
| 36 |
-
|
| 37 |
"if its not a DropBury or ODAC Portal User Story, then we perform testing in Tech360 iOS App"
|
| 38 |
"Generate as many as testcases possible minimum 6 ,maximum it can be anything"
|
| 39 |
"Understand the story thoroughly"
|
|
@@ -41,34 +40,33 @@ def generate_testcases(user_story):
|
|
| 41 |
"""
|
| 42 |
|
| 43 |
# Combine the few-shot examples with the user story for the model to process
|
| 44 |
-
prompt = few_shot_examples + f"\nUser Story: {user_story}\n"
|
| 45 |
|
| 46 |
try:
|
| 47 |
# Call the Nvidia llama API with the refined prompt
|
| 48 |
completion = client.chat.completions.create(
|
| 49 |
-
model="meta/llama-3.1-405b-instruct",
|
| 50 |
messages=[
|
| 51 |
{"role": "user", "content": prompt}
|
| 52 |
],
|
| 53 |
-
temperature=0.03,
|
| 54 |
-
top_p=0.7,
|
| 55 |
-
max_tokens=4096,
|
| 56 |
-
stream=True
|
| 57 |
)
|
| 58 |
|
| 59 |
# Initialize an empty string to accumulate the response
|
| 60 |
test_cases_text = ""
|
| 61 |
-
|
| 62 |
# Accumulate the response from the streaming chunks
|
| 63 |
for chunk in completion:
|
| 64 |
if chunk.choices[0].delta.content is not None:
|
| 65 |
test_cases_text += chunk.choices[0].delta.content
|
| 66 |
|
| 67 |
-
|
| 68 |
# Ensure the entire response is captured before cleaning
|
| 69 |
if test_cases_text.strip() == "":
|
| 70 |
return [{"test_case": "No test cases generated or output was empty."}]
|
| 71 |
-
|
| 72 |
# Clean the output by unescaping HTML entities and replacing <br> tags
|
| 73 |
test_cases_text = clean_test_case_output(test_cases_text)
|
| 74 |
|
|
@@ -77,14 +75,13 @@ def generate_testcases(user_story):
|
|
| 77 |
test_cases = json.loads(test_cases_text)
|
| 78 |
if isinstance(test_cases, list):
|
| 79 |
return test_cases # Return structured test cases
|
| 80 |
-
|
| 81 |
else:
|
| 82 |
return [{"test_case": test_cases_text}] # Return as a list with the text wrapped in a dict
|
| 83 |
|
| 84 |
except json.JSONDecodeError:
|
| 85 |
# Fallback: return the raw text if JSON parsing fails
|
| 86 |
return [{"test_case": test_cases_text}]
|
| 87 |
-
|
| 88 |
except requests.exceptions.RequestException as e:
|
| 89 |
print(f"API request failed: {str(e)}")
|
| 90 |
return []
|
|
@@ -94,17 +91,9 @@ def export_test_cases(test_cases):
|
|
| 94 |
return "No test cases to export."
|
| 95 |
|
| 96 |
# Use pandas to export the test cases to Excel
|
| 97 |
-
df = pd.DataFrame(test_cases)
|
| 98 |
output = io.BytesIO()
|
| 99 |
-
df.to_excel(output, index=False)
|
| 100 |
-
output.seek(0)
|
| 101 |
-
return output.getvalue()
|
| 102 |
-
|
| 103 |
-
def save_test_cases_as_file(test_cases):
|
| 104 |
-
if not test_cases:
|
| 105 |
-
return "No test cases to save."
|
| 106 |
-
|
| 107 |
-
# Use pandas to save the test cases to an Excel file
|
| 108 |
df = pd.DataFrame(test_cases)
|
| 109 |
-
df.to_excel(
|
| 110 |
-
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# Few-shot learning examples to guide the model
|
| 35 |
few_shot_examples = """
|
|
|
|
| 36 |
"if its not a DropBury or ODAC Portal User Story, then we perform testing in Tech360 iOS App"
|
| 37 |
"Generate as many as testcases possible minimum 6 ,maximum it can be anything"
|
| 38 |
"Understand the story thoroughly"
|
|
|
|
| 40 |
"""
|
| 41 |
|
| 42 |
# Combine the few-shot examples with the user story for the model to process
|
| 43 |
+
prompt = few_shot_examples + f"\nUser Story: {user_story}\n"
|
| 44 |
|
| 45 |
try:
|
| 46 |
# Call the Nvidia llama API with the refined prompt
|
| 47 |
completion = client.chat.completions.create(
|
| 48 |
+
model="meta/llama-3.1-405b-instruct",
|
| 49 |
messages=[
|
| 50 |
{"role": "user", "content": prompt}
|
| 51 |
],
|
| 52 |
+
temperature=0.03,
|
| 53 |
+
top_p=0.7,
|
| 54 |
+
max_tokens=4096,
|
| 55 |
+
stream=True
|
| 56 |
)
|
| 57 |
|
| 58 |
# Initialize an empty string to accumulate the response
|
| 59 |
test_cases_text = ""
|
| 60 |
+
|
| 61 |
# Accumulate the response from the streaming chunks
|
| 62 |
for chunk in completion:
|
| 63 |
if chunk.choices[0].delta.content is not None:
|
| 64 |
test_cases_text += chunk.choices[0].delta.content
|
| 65 |
|
|
|
|
| 66 |
# Ensure the entire response is captured before cleaning
|
| 67 |
if test_cases_text.strip() == "":
|
| 68 |
return [{"test_case": "No test cases generated or output was empty."}]
|
| 69 |
+
|
| 70 |
# Clean the output by unescaping HTML entities and replacing <br> tags
|
| 71 |
test_cases_text = clean_test_case_output(test_cases_text)
|
| 72 |
|
|
|
|
| 75 |
test_cases = json.loads(test_cases_text)
|
| 76 |
if isinstance(test_cases, list):
|
| 77 |
return test_cases # Return structured test cases
|
|
|
|
| 78 |
else:
|
| 79 |
return [{"test_case": test_cases_text}] # Return as a list with the text wrapped in a dict
|
| 80 |
|
| 81 |
except json.JSONDecodeError:
|
| 82 |
# Fallback: return the raw text if JSON parsing fails
|
| 83 |
return [{"test_case": test_cases_text}]
|
| 84 |
+
|
| 85 |
except requests.exceptions.RequestException as e:
|
| 86 |
print(f"API request failed: {str(e)}")
|
| 87 |
return []
|
|
|
|
| 91 |
return "No test cases to export."
|
| 92 |
|
| 93 |
# Use pandas to export the test cases to Excel
|
|
|
|
| 94 |
output = io.BytesIO()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
df = pd.DataFrame(test_cases)
|
| 96 |
+
df.to_excel(output, index=False, engine='openpyxl') # Use 'openpyxl' engine
|
| 97 |
+
output.seek(0) # Rewind the buffer
|
| 98 |
+
|
| 99 |
+
return output.getvalue()
|