Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from fpdf import FPDF
|
|
| 4 |
import os
|
| 5 |
import time
|
| 6 |
from datetime import datetime
|
|
|
|
| 7 |
|
| 8 |
# Mistral API key (replace with your key or use environment variable)
|
| 9 |
api_key = os.getenv("MISTRAL_API_KEY", "gz6lDXokxgR6cLY72oomALWcm7vhjRzQ")
|
|
@@ -22,9 +23,7 @@ def call_mistral_api(prompt):
|
|
| 22 |
]
|
| 23 |
}
|
| 24 |
try:
|
| 25 |
-
start_time = time.time()
|
| 26 |
response = requests.post(url, headers=headers, json=payload)
|
| 27 |
-
latency = round(time.time() - start_time, 2) # in seconds
|
| 28 |
response.raise_for_status() # Raise an error for bad status codes
|
| 29 |
return response.json()['choices'][0]['message']['content']
|
| 30 |
except requests.exceptions.HTTPError as err:
|
|
@@ -38,30 +37,27 @@ def call_mistral_api(prompt):
|
|
| 38 |
|
| 39 |
# Function to analyze a single requirement
|
| 40 |
def analyze_requirement(requirement):
|
| 41 |
-
latencies = {}
|
| 42 |
# Detect requirement type
|
| 43 |
type_prompt = f"Classify the following requirement as Functional or Non-Functional:\n\n{requirement}\n\nType:"
|
| 44 |
-
req_type
|
| 45 |
|
| 46 |
# Identify stakeholders
|
| 47 |
stakeholders_prompt = f"Identify the stakeholders for the following requirement:\n\n{requirement}\n\nStakeholders:"
|
| 48 |
-
stakeholders
|
| 49 |
|
| 50 |
# Classify domain
|
| 51 |
domain_prompt = f"Classify the domain for the following requirement (e.g., Bank, Healthcare, etc.):\n\n{requirement}\n\nDomain:"
|
| 52 |
-
domain
|
| 53 |
|
| 54 |
# Detect defects
|
| 55 |
defects_prompt = f"""Analyze the following requirement and identify ONLY MAJOR defects (e.g., Ambiguity, Incompleteness, etc.).
|
| 56 |
If the requirement is clear and complete, respond with 'No defects.'
|
| 57 |
Requirement: {requirement}
|
| 58 |
Defects:"""
|
| 59 |
-
|
| 60 |
|
| 61 |
# Rewrite requirement
|
| 62 |
-
rewritten
|
| 63 |
-
|
| 64 |
-
total_latency = sum(latencies.values())
|
| 65 |
|
| 66 |
return {
|
| 67 |
"Requirement": requirement,
|
|
@@ -69,8 +65,7 @@ def analyze_requirement(requirement):
|
|
| 69 |
"Stakeholders": stakeholders,
|
| 70 |
"Domain": domain,
|
| 71 |
"Defects": defects,
|
| 72 |
-
"Rewritten": rewritten
|
| 73 |
-
"Latency (s)": round(total_latency, 2)
|
| 74 |
}
|
| 75 |
|
| 76 |
# Function to rewrite requirement concisely
|
|
@@ -86,7 +81,7 @@ def rewrite_requirement(requirement, defects):
|
|
| 86 |
Defects: {defects}
|
| 87 |
|
| 88 |
Rewritten Requirement:"""
|
| 89 |
-
response
|
| 90 |
return response.strip()
|
| 91 |
|
| 92 |
# Function to generate a PDF report with professional formatting
|
|
@@ -138,6 +133,32 @@ def generate_pdf_report(results):
|
|
| 138 |
pdf.output(pdf_output)
|
| 139 |
return pdf_output
|
| 140 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
# Streamlit app
|
| 142 |
def main():
|
| 143 |
st.title("AI Powered Requirement Analysis and Defect Detection using Large Language Model Mistral")
|
|
@@ -170,7 +191,6 @@ def main():
|
|
| 170 |
st.write(f"**Domain:** {result['Domain']}")
|
| 171 |
st.write(f"**Defects:** {result['Defects']}")
|
| 172 |
st.write(f"**Rewritten:** {result['Rewritten']}")
|
| 173 |
-
st.write(f"**Total Latency:** {result['Latency (s)']} seconds")
|
| 174 |
st.write("---")
|
| 175 |
|
| 176 |
# Generate and download PDF report
|
|
@@ -183,6 +203,12 @@ def main():
|
|
| 183 |
mime="application/pdf"
|
| 184 |
)
|
| 185 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
# Run the app
|
| 187 |
if __name__ == "__main__":
|
| 188 |
-
main()
|
|
|
|
| 4 |
import os
|
| 5 |
import time
|
| 6 |
from datetime import datetime
|
| 7 |
+
import unittest
|
| 8 |
|
| 9 |
# Mistral API key (replace with your key or use environment variable)
|
| 10 |
api_key = os.getenv("MISTRAL_API_KEY", "gz6lDXokxgR6cLY72oomALWcm7vhjRzQ")
|
|
|
|
| 23 |
]
|
| 24 |
}
|
| 25 |
try:
|
|
|
|
| 26 |
response = requests.post(url, headers=headers, json=payload)
|
|
|
|
| 27 |
response.raise_for_status() # Raise an error for bad status codes
|
| 28 |
return response.json()['choices'][0]['message']['content']
|
| 29 |
except requests.exceptions.HTTPError as err:
|
|
|
|
| 37 |
|
| 38 |
# Function to analyze a single requirement
|
| 39 |
def analyze_requirement(requirement):
|
|
|
|
| 40 |
# Detect requirement type
|
| 41 |
type_prompt = f"Classify the following requirement as Functional or Non-Functional:\n\n{requirement}\n\nType:"
|
| 42 |
+
req_type = call_mistral_api(type_prompt)
|
| 43 |
|
| 44 |
# Identify stakeholders
|
| 45 |
stakeholders_prompt = f"Identify the stakeholders for the following requirement:\n\n{requirement}\n\nStakeholders:"
|
| 46 |
+
stakeholders = call_mistral_api(stakeholders_prompt)
|
| 47 |
|
| 48 |
# Classify domain
|
| 49 |
domain_prompt = f"Classify the domain for the following requirement (e.g., Bank, Healthcare, etc.):\n\n{requirement}\n\nDomain:"
|
| 50 |
+
domain = call_mistral_api(domain_prompt)
|
| 51 |
|
| 52 |
# Detect defects
|
| 53 |
defects_prompt = f"""Analyze the following requirement and identify ONLY MAJOR defects (e.g., Ambiguity, Incompleteness, etc.).
|
| 54 |
If the requirement is clear and complete, respond with 'No defects.'
|
| 55 |
Requirement: {requirement}
|
| 56 |
Defects:"""
|
| 57 |
+
defects = call_mistral_api(defects_prompt)
|
| 58 |
|
| 59 |
# Rewrite requirement
|
| 60 |
+
rewritten = rewrite_requirement(requirement, defects)
|
|
|
|
|
|
|
| 61 |
|
| 62 |
return {
|
| 63 |
"Requirement": requirement,
|
|
|
|
| 65 |
"Stakeholders": stakeholders,
|
| 66 |
"Domain": domain,
|
| 67 |
"Defects": defects,
|
| 68 |
+
"Rewritten": rewritten
|
|
|
|
| 69 |
}
|
| 70 |
|
| 71 |
# Function to rewrite requirement concisely
|
|
|
|
| 81 |
Defects: {defects}
|
| 82 |
|
| 83 |
Rewritten Requirement:"""
|
| 84 |
+
response = call_mistral_api(prompt)
|
| 85 |
return response.strip()
|
| 86 |
|
| 87 |
# Function to generate a PDF report with professional formatting
|
|
|
|
| 133 |
pdf.output(pdf_output)
|
| 134 |
return pdf_output
|
| 135 |
|
| 136 |
+
# Unit testing section
|
| 137 |
+
class TestRequirementAnalysis(unittest.TestCase):
|
| 138 |
+
|
| 139 |
+
def test_analyze_requirement(self):
|
| 140 |
+
requirement = "The system must allow users to log in using email and password."
|
| 141 |
+
result = analyze_requirement(requirement)
|
| 142 |
+
self.assertIn("Requirement", result)
|
| 143 |
+
self.assertIn("Type", result)
|
| 144 |
+
self.assertIn("Stakeholders", result)
|
| 145 |
+
self.assertIn("Domain", result)
|
| 146 |
+
self.assertIn("Defects", result)
|
| 147 |
+
self.assertIn("Rewritten", result)
|
| 148 |
+
|
| 149 |
+
def test_rewrite_requirement_no_defects(self):
|
| 150 |
+
requirement = "The system should send an email notification when a user signs up."
|
| 151 |
+
defects = "No defects."
|
| 152 |
+
result = rewrite_requirement(requirement, defects)
|
| 153 |
+
self.assertEqual(result, "No modification needed.")
|
| 154 |
+
|
| 155 |
+
def test_generate_pdf_report(self):
|
| 156 |
+
results = [
|
| 157 |
+
{"Requirement": "The system should be scalable.", "Type": "Functional", "Stakeholders": "Developers", "Domain": "IT", "Defects": "No defects", "Rewritten": "No modification needed."}
|
| 158 |
+
]
|
| 159 |
+
report = generate_pdf_report(results)
|
| 160 |
+
self.assertTrue(os.path.exists(report))
|
| 161 |
+
|
| 162 |
# Streamlit app
|
| 163 |
def main():
|
| 164 |
st.title("AI Powered Requirement Analysis and Defect Detection using Large Language Model Mistral")
|
|
|
|
| 191 |
st.write(f"**Domain:** {result['Domain']}")
|
| 192 |
st.write(f"**Defects:** {result['Defects']}")
|
| 193 |
st.write(f"**Rewritten:** {result['Rewritten']}")
|
|
|
|
| 194 |
st.write("---")
|
| 195 |
|
| 196 |
# Generate and download PDF report
|
|
|
|
| 203 |
mime="application/pdf"
|
| 204 |
)
|
| 205 |
|
| 206 |
+
# Display test results
|
| 207 |
+
st.subheader("Unit Test Results")
|
| 208 |
+
test_suite = unittest.TextTestRunner()
|
| 209 |
+
test_results = test_suite.run(unittest.TestLoader().loadTestsFromTestCase(TestRequirementAnalysis))
|
| 210 |
+
st.write(test_results)
|
| 211 |
+
|
| 212 |
# Run the app
|
| 213 |
if __name__ == "__main__":
|
| 214 |
+
main()
|