Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,62 +18,32 @@ def extract_text_from_block(pdf_path, block_pages):
|
|
| 18 |
return text
|
| 19 |
|
| 20 |
def generate_test_cases(text, prompt_template):
|
| 21 |
-
|
| 22 |
-
termination_phrase = "### END OF TEST CASES ###"
|
| 23 |
-
|
| 24 |
-
# Append termination instruction to the prompt template
|
| 25 |
-
full_prompt_template = prompt_template + (
|
| 26 |
-
"\n\nPlease ensure that your response ends with '"
|
| 27 |
-
+ termination_phrase
|
| 28 |
-
+ "' to indicate that no further test cases are needed."
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
prompt = full_prompt_template.format(text=text)
|
| 32 |
messages = [
|
| 33 |
{"role": "system", "content": "You are a helpful assistant that generates test cases based on given text."},
|
| 34 |
{"role": "user", "content": prompt}
|
| 35 |
]
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
part = response.choices[0].message['content'].strip()
|
| 57 |
-
full_content += "\n" + part
|
| 58 |
-
|
| 59 |
-
# Check if the termination phrase is in the output
|
| 60 |
-
if termination_phrase in part:
|
| 61 |
-
# Optionally remove the termination phrase before returning
|
| 62 |
-
full_content = full_content.replace(termination_phrase, "")
|
| 63 |
-
break
|
| 64 |
-
|
| 65 |
-
finish_reason = response.choices[0].get("finish_reason", None)
|
| 66 |
-
# If the response appears truncated, ask the model to continue
|
| 67 |
-
if finish_reason == "length" or part.endswith("..."):
|
| 68 |
-
messages.append({"role": "assistant", "content": part})
|
| 69 |
-
messages.append({"role": "user", "content": "Please continue with the remaining test cases."})
|
| 70 |
-
else:
|
| 71 |
-
break
|
| 72 |
-
|
| 73 |
-
if iteration == max_iterations:
|
| 74 |
-
full_content += "\n[WARNING: Maximum iterations reached. The output may be incomplete.]"
|
| 75 |
|
| 76 |
-
return
|
| 77 |
|
| 78 |
|
| 79 |
|
|
|
|
| 18 |
return text
|
| 19 |
|
| 20 |
def generate_test_cases(text, prompt_template):
|
| 21 |
+
prompt = prompt_template.format(text=text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
messages = [
|
| 23 |
{"role": "system", "content": "You are a helpful assistant that generates test cases based on given text."},
|
| 24 |
{"role": "user", "content": prompt}
|
| 25 |
]
|
| 26 |
|
| 27 |
+
try:
|
| 28 |
+
response = openai.ChatCompletion.create(
|
| 29 |
+
model="gpt-4o-mini",
|
| 30 |
+
messages=messages,
|
| 31 |
+
max_tokens=3000,
|
| 32 |
+
n=1,
|
| 33 |
+
temperature=0.7,
|
| 34 |
+
timeout=30 # Timeout after 30 seconds
|
| 35 |
+
)
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"[Error during API call: {str(e)}]"
|
| 38 |
+
|
| 39 |
+
content = response.choices[0].message['content'].strip()
|
| 40 |
+
finish_reason = response.choices[0].get("finish_reason", None)
|
| 41 |
+
|
| 42 |
+
# Check if the response appears truncated; if so, warn the user.
|
| 43 |
+
if finish_reason == "length" or content.endswith("..."):
|
| 44 |
+
content += "\n[WARNING: The output may be truncated due to token limits.]"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
return content.strip()
|
| 47 |
|
| 48 |
|
| 49 |
|