iisadia commited on
Commit
bee80d0
Β·
verified Β·
1 Parent(s): 62dccbc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -82
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
- 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,30 +226,39 @@ def main():
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")
 
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")