Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,67 +53,37 @@ def call_groq_api(prompt):
|
|
| 53 |
st.error(f"Error: {err}")
|
| 54 |
return f"Error: {err}"
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
# Function to analyze a single requirement
|
| 57 |
def analyze_requirement(requirement):
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
defects
|
| 79 |
-
|
| 80 |
-
|
| 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,30 +226,39 @@ def main():
|
|
| 256 |
time.sleep(0.5)
|
| 257 |
st.session_state.results = results
|
| 258 |
|
| 259 |
-
#
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
st.
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
|
| 284 |
# PDF Report Section
|
| 285 |
st.subheader("π€ Generate Report")
|
|
|
|
| 53 |
st.error(f"Error: {err}")
|
| 54 |
return f"Error: {err}"
|
| 55 |
|
| 56 |
+
def clean_text(text):
|
| 57 |
+
# Remove markdown code fences and any trailing newlines/spaces
|
| 58 |
+
cleaned = text.replace("```", "").strip()
|
| 59 |
+
return cleaned
|
| 60 |
+
|
| 61 |
# Function to analyze a single requirement
|
| 62 |
def analyze_requirement(requirement):
|
| 63 |
+
# Use Mistral for classification and domain identification
|
| 64 |
+
type_prompt = f"Classify the following requirement as Functional or Non-Functional in one word:\n\n{requirement}\n\nType:"
|
| 65 |
+
req_type = call_mistral_api(type_prompt).strip()
|
| 66 |
+
|
| 67 |
+
domain_prompt = f"Classify the domain for the following requirement in one word (e.g., E-commerce, Education, etc.):\n\n{requirement}\n\nDomain:"
|
| 68 |
+
domain = call_mistral_api(domain_prompt).strip()
|
| 69 |
+
|
| 70 |
+
# Use Groq for defect analysis and rewriting
|
| 71 |
+
defects_prompt = f"""List ONLY the major defects in the following requirement (e.g., Ambiguity, Incompleteness, etc.) in 1-2 words each:\n\n{requirement}\n\nDefects:"""
|
| 72 |
+
defects_raw = call_groq_api(defects_prompt)
|
| 73 |
+
defects = clean_text(defects_raw)
|
| 74 |
+
|
| 75 |
+
rewritten_prompt = f"""Rewrite the following requirement in 1-2 sentences to address the defects:\n\n{requirement}\n\nRewritten:"""
|
| 76 |
+
rewritten_raw = call_groq_api(rewritten_prompt)
|
| 77 |
+
rewritten = clean_text(rewritten_raw)
|
| 78 |
+
|
| 79 |
+
return {
|
| 80 |
+
"Requirement": requirement,
|
| 81 |
+
"Type": req_type,
|
| 82 |
+
"Domain": domain,
|
| 83 |
+
"Defects": defects,
|
| 84 |
+
"Rewritten": rewritten
|
| 85 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
|
|
|
| 87 |
# Function to generate a PDF report
|
| 88 |
def generate_pdf_report(results):
|
| 89 |
pdf = FPDF()
|
|
|
|
| 226 |
time.sleep(0.5)
|
| 227 |
st.session_state.results = results
|
| 228 |
|
| 229 |
+
# Display Results
|
| 230 |
+
if 'results' in st.session_state:
|
| 231 |
+
st.subheader("π Analysis Results")
|
| 232 |
+
with st.container():
|
| 233 |
+
for i, result in enumerate(st.session_state.results, 1):
|
| 234 |
+
with st.expander(f"Requirement #{i}: {result['Requirement'][:50]}...", expanded=True):
|
| 235 |
+
st.markdown(f"""
|
| 236 |
+
<div class="requirement-card">
|
| 237 |
+
<div style="margin-bottom: 1.5rem;">
|
| 238 |
+
<div class="analysis-badge { 'type-functional' if 'functional' in result['Type'].lower() else 'type-non-functional' }">
|
| 239 |
+
π Type: {result['Type']}
|
| 240 |
+
</div>
|
| 241 |
+
<div class="analysis-badge" style="background: #e8eaf6; color: #303f9f;">
|
| 242 |
+
π·οΈ Domain: {result['Domain']}
|
| 243 |
+
</div>
|
| 244 |
+
</div>
|
| 245 |
+
|
| 246 |
+
<div style="margin: 1rem 0;">
|
| 247 |
+
<h4>π Identified Issues</h4>
|
| 248 |
+
<div style="display: flex; gap: 0.5rem; flex-wrap: wrap;">
|
| 249 |
+
{''.join([f'<div class="analysis-badge defect-badge">β οΈ {d}</div>'
|
| 250 |
+
for d in result['Defects'].split(', ')])}
|
| 251 |
+
</div>
|
| 252 |
+
</div>
|
| 253 |
+
|
| 254 |
+
<div style="margin: 1rem 0;">
|
| 255 |
+
<h4>β¨ Improved Version</h4>
|
| 256 |
+
<div class="analysis-badge improved-badge">
|
| 257 |
+
π {result['Rewritten']}
|
| 258 |
+
</div>
|
| 259 |
+
</div>
|
| 260 |
+
</div>
|
| 261 |
+
""", unsafe_allow_html=True)
|
| 262 |
|
| 263 |
# PDF Report Section
|
| 264 |
st.subheader("π€ Generate Report")
|