Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -57,54 +57,63 @@ def call_groq_api(prompt):
|
|
| 57 |
def analyze_requirement(requirement):
|
| 58 |
try:
|
| 59 |
# 1. Classify requirement type
|
| 60 |
-
type_prompt = f"Classify
|
| 61 |
req_type = call_mistral_api(type_prompt).strip()
|
| 62 |
|
| 63 |
# 2. Identify domain
|
| 64 |
-
domain_prompt = f"Identify the domain
|
| 65 |
domain = call_mistral_api(domain_prompt).strip()
|
| 66 |
|
| 67 |
-
# 3. Detect defects
|
| 68 |
-
defects_prompt = f"""
|
| 69 |
-
|
| 70 |
-
-
|
| 71 |
-
-
|
| 72 |
-
- Defect3
|
| 73 |
|
| 74 |
Requirement: {requirement}
|
| 75 |
Defects:"""
|
| 76 |
defects_response = call_groq_api(defects_prompt).strip()
|
| 77 |
|
| 78 |
-
# Process defects
|
| 79 |
defects = []
|
| 80 |
if "API Error" not in defects_response:
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
# 4. Rewrite requirement
|
| 87 |
-
rewrite_prompt = f"""
|
| 88 |
-
|
|
|
|
| 89 |
rewritten = call_groq_api(rewrite_prompt).strip()
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
return {
|
| 92 |
"Requirement": requirement,
|
| 93 |
-
"Type": req_type
|
| 94 |
-
"Domain": domain
|
| 95 |
-
"Defects": defects,
|
| 96 |
-
"Rewritten": rewritten if "
|
| 97 |
}
|
| 98 |
|
| 99 |
except Exception as e:
|
| 100 |
-
st.error(f"Analysis failed: {str(e)}")
|
| 101 |
return {
|
| 102 |
"Requirement": requirement,
|
| 103 |
"Type": "Error",
|
| 104 |
"Domain": "Error",
|
| 105 |
-
"Defects": ["Analysis
|
| 106 |
"Rewritten": requirement
|
| 107 |
}
|
|
|
|
|
|
|
| 108 |
# Function to generate a PDF report
|
| 109 |
def generate_pdf_report(results):
|
| 110 |
pdf = FPDF()
|
|
@@ -247,39 +256,30 @@ def main():
|
|
| 247 |
time.sleep(0.5)
|
| 248 |
st.session_state.results = results
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
<div style="margin: 1rem 0;">
|
| 276 |
-
<h4>β¨ Improved Version</h4>
|
| 277 |
-
<div class="analysis-badge improved-badge">
|
| 278 |
-
π {result["Rewritten"]}
|
| 279 |
-
</div>
|
| 280 |
-
</div>
|
| 281 |
-
</div>
|
| 282 |
-
""", unsafe_allow_html=True)
|
| 283 |
|
| 284 |
# PDF Report Section
|
| 285 |
st.subheader("π€ Generate Report")
|
|
|
|
| 57 |
def analyze_requirement(requirement):
|
| 58 |
try:
|
| 59 |
# 1. Classify requirement type
|
| 60 |
+
type_prompt = f"Classify exactly as either 'Functional' or 'Non-Functional':\n{requirement}"
|
| 61 |
req_type = call_mistral_api(type_prompt).strip()
|
| 62 |
|
| 63 |
# 2. Identify domain
|
| 64 |
+
domain_prompt = f"Identify the business domain in one word (e.g., Healthcare, Finance):\n{requirement}"
|
| 65 |
domain = call_mistral_api(domain_prompt).strip()
|
| 66 |
|
| 67 |
+
# 3. Detect defects - more explicit prompt
|
| 68 |
+
defects_prompt = f"""Analyze this software requirement and list EXACTLY 3 defects using ONLY these formats:
|
| 69 |
+
- Ambiguity: [specific unclear part]
|
| 70 |
+
- Incompleteness: [missing element]
|
| 71 |
+
- Unverifiability: [unmeasurable aspect]
|
|
|
|
| 72 |
|
| 73 |
Requirement: {requirement}
|
| 74 |
Defects:"""
|
| 75 |
defects_response = call_groq_api(defects_prompt).strip()
|
| 76 |
|
| 77 |
+
# Process defects - more robust parsing
|
| 78 |
defects = []
|
| 79 |
if "API Error" not in defects_response:
|
| 80 |
+
# Extract all lines starting with "-"
|
| 81 |
+
defect_lines = [line.strip() for line in defects_response.split("\n") if line.strip().startswith("-")]
|
| 82 |
+
if defect_lines:
|
| 83 |
+
defects = [line[2:].split(":")[0].strip() for line in defect_lines[:3]] # Take first 3 defects
|
| 84 |
+
else:
|
| 85 |
+
defects = ["No defects found"]
|
| 86 |
+
else:
|
| 87 |
+
defects = ["Analysis error"]
|
| 88 |
|
| 89 |
+
# 4. Rewrite requirement - more constrained prompt
|
| 90 |
+
rewrite_prompt = f"""Improve this requirement by fixing defects while keeping it concise (1 sentence):
|
| 91 |
+
Original: {requirement}
|
| 92 |
+
Improved:"""
|
| 93 |
rewritten = call_groq_api(rewrite_prompt).strip()
|
| 94 |
|
| 95 |
+
# Clean rewritten output
|
| 96 |
+
if "Improved:" in rewritten:
|
| 97 |
+
rewritten = rewritten.split("Improved:")[-1].strip()
|
| 98 |
+
|
| 99 |
return {
|
| 100 |
"Requirement": requirement,
|
| 101 |
+
"Type": req_type,
|
| 102 |
+
"Domain": domain,
|
| 103 |
+
"Defects": defects if defects else ["No defects found"],
|
| 104 |
+
"Rewritten": rewritten if rewritten and "API Error" not in rewritten else requirement
|
| 105 |
}
|
| 106 |
|
| 107 |
except Exception as e:
|
|
|
|
| 108 |
return {
|
| 109 |
"Requirement": requirement,
|
| 110 |
"Type": "Error",
|
| 111 |
"Domain": "Error",
|
| 112 |
+
"Defects": ["Analysis failed"],
|
| 113 |
"Rewritten": requirement
|
| 114 |
}
|
| 115 |
+
|
| 116 |
+
|
| 117 |
# Function to generate a PDF report
|
| 118 |
def generate_pdf_report(results):
|
| 119 |
pdf = FPDF()
|
|
|
|
| 256 |
time.sleep(0.5)
|
| 257 |
st.session_state.results = results
|
| 258 |
|
| 259 |
+
# In your results display section:
|
| 260 |
+
for i, result in enumerate(st.session_state.results, 1):
|
| 261 |
+
with st.expander(f"Requirement #{i}: {result['Requirement'][:50]}...", expanded=True):
|
| 262 |
+
st.markdown(f"""
|
| 263 |
+
<div class="requirement-card">
|
| 264 |
+
<!-- Type and Domain sections remain the same -->
|
| 265 |
+
|
| 266 |
+
<div style="margin: 1rem 0;">
|
| 267 |
+
<h4>π Identified Issues</h4>
|
| 268 |
+
<div style="display: flex; gap: 0.5rem; flex-wrap: wrap;">
|
| 269 |
+
{''.join([f'<div class="analysis-badge defect-badge">β οΈ {d}</div>'
|
| 270 |
+
for d in result["Defects"] if d and d != "No defects found"]) or
|
| 271 |
+
'<div class="analysis-badge" style="background:#e8f5e9;color:#2e7d32;">β No major defects found</div>'}
|
| 272 |
+
</div>
|
| 273 |
+
</div>
|
| 274 |
+
|
| 275 |
+
<div style="margin: 1rem 0;">
|
| 276 |
+
<h4>β¨ Improved Version</h4>
|
| 277 |
+
<div class="analysis-badge improved-badge">
|
| 278 |
+
π {result["Rewritten"] if result["Rewritten"] != result["Requirement"] else "No improvements suggested"}
|
| 279 |
+
</div>
|
| 280 |
+
</div>
|
| 281 |
+
</div>
|
| 282 |
+
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
# PDF Report Section
|
| 285 |
st.subheader("π€ Generate Report")
|