Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import os
|
|
| 5 |
import time
|
| 6 |
from datetime import datetime
|
| 7 |
import groq
|
|
|
|
| 8 |
|
| 9 |
# API keys (replace with your keys or use environment variables)
|
| 10 |
mistral_api_key = os.getenv("MISTRAL_API_KEY", "gz6lDXokxgR6cLY72oomALWcm7vhjRzQ")
|
|
@@ -13,159 +14,79 @@ groq_api_key = os.getenv("GROQ_API_KEY", "gsk_x7oGLO1zSgSVYOWDtGYVWGdyb3FYrWBjaz
|
|
| 13 |
# Initialize Groq client
|
| 14 |
groq_client = groq.Client(api_key=groq_api_key)
|
| 15 |
|
| 16 |
-
# Function to
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
return response.json()['choices'][0]['message']['content']
|
| 33 |
-
except requests.exceptions.HTTPError as err:
|
| 34 |
-
if response.status_code == 429: # Rate limit exceeded
|
| 35 |
-
st.warning("Rate limit exceeded. Please wait a few seconds and try again.")
|
| 36 |
-
time.sleep(5) # Wait for 5 seconds before retrying
|
| 37 |
-
return call_mistral_api(prompt) # Retry the request
|
| 38 |
-
return f"HTTP Error: {err}"
|
| 39 |
-
except Exception as err:
|
| 40 |
-
return f"Error: {err}"
|
| 41 |
-
|
| 42 |
-
# Function to call Groq API
|
| 43 |
-
def call_groq_api(prompt):
|
| 44 |
-
try:
|
| 45 |
-
response = groq_client.chat.completions.create(
|
| 46 |
-
model="llama-3.3-70b-versatile", # Correct model name
|
| 47 |
-
messages=[
|
| 48 |
-
{"role": "user", "content": prompt}
|
| 49 |
-
]
|
| 50 |
-
)
|
| 51 |
-
return response.choices[0].message.content
|
| 52 |
-
except Exception as err:
|
| 53 |
-
st.error(f"Error: {err}")
|
| 54 |
-
return f"Error: {err}"
|
| 55 |
|
| 56 |
# Function to analyze a single requirement using both models
|
| 57 |
def analyze_requirement(requirement):
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
rewritten_prompt = f"""Rewrite the following requirement in 1-2 sentences to address the defects:\n\n{requirement}\n\nRewritten:"""
|
| 70 |
-
rewritten = call_groq_api(rewritten_prompt).strip()
|
| 71 |
|
| 72 |
return {
|
| 73 |
"Requirement": requirement,
|
| 74 |
"Type": req_type,
|
| 75 |
"Domain": domain,
|
| 76 |
"Defects": defects,
|
| 77 |
-
"Rewritten": rewritten
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
}
|
| 79 |
|
| 80 |
-
# Function to generate a PDF report
|
| 81 |
-
def generate_pdf_report(results):
|
| 82 |
-
pdf = FPDF()
|
| 83 |
-
pdf.add_page()
|
| 84 |
-
pdf.set_font("Arial", size=12)
|
| 85 |
-
|
| 86 |
-
# Add watermark
|
| 87 |
-
pdf.set_font("Arial", 'B', 50)
|
| 88 |
-
pdf.set_text_color(230, 230, 230) # Light gray color for watermark
|
| 89 |
-
pdf.rotate(45) # Rotate the text for watermark effect
|
| 90 |
-
pdf.text(60, 150, "AI Powered Requirement Analysis")
|
| 91 |
-
pdf.rotate(0) # Reset rotation
|
| 92 |
-
|
| 93 |
-
# Add title and date/time
|
| 94 |
-
pdf.set_font("Arial", 'B', 16)
|
| 95 |
-
pdf.set_text_color(0, 0, 0) # Black color for title
|
| 96 |
-
pdf.cell(200, 10, txt="AI Powered Requirement Analysis and Defect Detection", ln=True, align='C')
|
| 97 |
-
pdf.set_font("Arial", size=12)
|
| 98 |
-
pdf.cell(200, 10, txt=f"Report Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align='C')
|
| 99 |
-
pdf.ln(10) # Add some space
|
| 100 |
-
|
| 101 |
-
# Add requirements analysis
|
| 102 |
-
pdf.set_font("Arial", size=12)
|
| 103 |
-
for i, result in enumerate(results, start=1):
|
| 104 |
-
if pdf.get_y() > 250: # If the content is near the bottom of the page
|
| 105 |
-
pdf.add_page() # Add a new page
|
| 106 |
-
pdf.set_font("Arial", 'B', 16)
|
| 107 |
-
pdf.cell(200, 10, txt="AI Powered Requirement Analysis and Defect Detection", ln=True, align='C')
|
| 108 |
-
pdf.set_font("Arial", size=12)
|
| 109 |
-
pdf.cell(200, 10, txt=f"Report Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align='C')
|
| 110 |
-
pdf.ln(10) # Add some space
|
| 111 |
-
|
| 112 |
-
# Add requirement details
|
| 113 |
-
pdf.set_font("Arial", 'B', 14)
|
| 114 |
-
pdf.multi_cell(200, 10, txt=f"Requirement R{i}: {result['Requirement']}", align='L')
|
| 115 |
-
pdf.set_font("Arial", size=12)
|
| 116 |
-
pdf.multi_cell(200, 10, txt=f"Type: {result['Type']}", align='L')
|
| 117 |
-
pdf.multi_cell(200, 10, txt=f"Domain: {result['Domain']}", align='L')
|
| 118 |
-
pdf.multi_cell(200, 10, txt=f"Defects: {result['Defects']}", align='L')
|
| 119 |
-
pdf.multi_cell(200, 10, txt=f"Rewritten: {result['Rewritten']}", align='L')
|
| 120 |
-
pdf.multi_cell(200, 10, txt="-" * 50, align='L')
|
| 121 |
-
pdf.ln(5) # Add some space between requirements
|
| 122 |
-
|
| 123 |
-
pdf_output = "requirements_report.pdf"
|
| 124 |
-
pdf.output(pdf_output)
|
| 125 |
-
return pdf_output
|
| 126 |
-
|
| 127 |
# Streamlit app
|
| 128 |
def main():
|
| 129 |
st.title("AI Powered Requirement Analysis and Defect Detection")
|
| 130 |
st.markdown("**Team Name:** Sadia, Areeba, Rabbia, Tesmia")
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
input_text = st.text_area("Enter your requirements (one per line or separated by periods):")
|
| 135 |
-
requirements = []
|
| 136 |
-
if input_text:
|
| 137 |
-
requirements = [req.strip() for req in input_text.replace("\n", ".").split(".") if req.strip()]
|
| 138 |
|
| 139 |
-
# Analyze requirements
|
| 140 |
if st.button("Analyze Requirements"):
|
| 141 |
if not requirements:
|
| 142 |
st.warning("Please enter requirements.")
|
| 143 |
else:
|
| 144 |
-
results = []
|
| 145 |
-
for req in requirements:
|
| 146 |
-
if req.strip(): # Ignore empty lines
|
| 147 |
-
results.append(analyze_requirement(req.strip()))
|
| 148 |
-
|
| 149 |
-
# Display results
|
| 150 |
st.subheader("Analysis Results")
|
|
|
|
| 151 |
for i, result in enumerate(results, start=1):
|
| 152 |
st.write(f"### Requirement R{i}: {result['Requirement']}")
|
| 153 |
-
st.write(f"**Type:** {result['Type']}")
|
| 154 |
-
st.write(f"**Domain:** {result['Domain']}")
|
| 155 |
-
st.write(f"**Defects:** {result['Defects']}")
|
| 156 |
-
st.write(f"**Rewritten:** {result['Rewritten']}")
|
| 157 |
st.write("---")
|
| 158 |
|
| 159 |
-
# Generate and download PDF report
|
| 160 |
-
pdf_report = generate_pdf_report(results)
|
| 161 |
-
with open(pdf_report, "rb") as f:
|
| 162 |
-
st.download_button(
|
| 163 |
-
label="Download PDF Report",
|
| 164 |
-
data=f,
|
| 165 |
-
file_name="requirements_report.pdf",
|
| 166 |
-
mime="application/pdf"
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
-
# Run the app
|
| 170 |
if __name__ == "__main__":
|
| 171 |
-
main()
|
|
|
|
| 5 |
import time
|
| 6 |
from datetime import datetime
|
| 7 |
import groq
|
| 8 |
+
import time
|
| 9 |
|
| 10 |
# API keys (replace with your keys or use environment variables)
|
| 11 |
mistral_api_key = os.getenv("MISTRAL_API_KEY", "gz6lDXokxgR6cLY72oomALWcm7vhjRzQ")
|
|
|
|
| 14 |
# Initialize Groq client
|
| 15 |
groq_client = groq.Client(api_key=groq_api_key)
|
| 16 |
|
| 17 |
+
# Function to measure API response time
|
| 18 |
+
def measure_response_time(api_call, *args):
|
| 19 |
+
start_time = time.time()
|
| 20 |
+
result = api_call(*args)
|
| 21 |
+
end_time = time.time()
|
| 22 |
+
response_time = end_time - start_time
|
| 23 |
+
return result, response_time
|
| 24 |
+
|
| 25 |
+
# Function to check confidence level
|
| 26 |
+
def estimate_confidence(response):
|
| 27 |
+
if "high" in response.lower():
|
| 28 |
+
return "High"
|
| 29 |
+
elif "medium" in response.lower():
|
| 30 |
+
return "Medium"
|
| 31 |
+
else:
|
| 32 |
+
return "Low"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
# Function to analyze a single requirement using both models
|
| 35 |
def analyze_requirement(requirement):
|
| 36 |
+
# Measure API response time and get responses
|
| 37 |
+
req_type, time_type = measure_response_time(call_mistral_api, f"Classify as Functional or Non-Functional:\n{requirement}")
|
| 38 |
+
domain, time_domain = measure_response_time(call_mistral_api, f"Classify domain:\n{requirement}")
|
| 39 |
+
defects, time_defects = measure_response_time(call_groq_api, f"List major defects:\n{requirement}")
|
| 40 |
+
rewritten, time_rewritten = measure_response_time(call_groq_api, f"Rewrite requirement:\n{requirement}")
|
| 41 |
+
|
| 42 |
+
# Estimate confidence
|
| 43 |
+
confidence_type = estimate_confidence(req_type)
|
| 44 |
+
confidence_domain = estimate_confidence(domain)
|
| 45 |
+
confidence_defects = estimate_confidence(defects)
|
| 46 |
+
confidence_rewritten = estimate_confidence(rewritten)
|
|
|
|
|
|
|
| 47 |
|
| 48 |
return {
|
| 49 |
"Requirement": requirement,
|
| 50 |
"Type": req_type,
|
| 51 |
"Domain": domain,
|
| 52 |
"Defects": defects,
|
| 53 |
+
"Rewritten": rewritten,
|
| 54 |
+
"Times": {
|
| 55 |
+
"Type": time_type,
|
| 56 |
+
"Domain": time_domain,
|
| 57 |
+
"Defects": time_defects,
|
| 58 |
+
"Rewritten": time_rewritten
|
| 59 |
+
},
|
| 60 |
+
"Confidence": {
|
| 61 |
+
"Type": confidence_type,
|
| 62 |
+
"Domain": confidence_domain,
|
| 63 |
+
"Defects": confidence_defects,
|
| 64 |
+
"Rewritten": confidence_rewritten
|
| 65 |
+
}
|
| 66 |
}
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# Streamlit app
|
| 69 |
def main():
|
| 70 |
st.title("AI Powered Requirement Analysis and Defect Detection")
|
| 71 |
st.markdown("**Team Name:** Sadia, Areeba, Rabbia, Tesmia")
|
| 72 |
+
|
| 73 |
+
input_text = st.text_area("Enter requirements (one per line):")
|
| 74 |
+
requirements = [req.strip() for req in input_text.split("\n") if req.strip()]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
|
|
|
| 76 |
if st.button("Analyze Requirements"):
|
| 77 |
if not requirements:
|
| 78 |
st.warning("Please enter requirements.")
|
| 79 |
else:
|
| 80 |
+
results = [analyze_requirement(req) for req in requirements]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
st.subheader("Analysis Results")
|
| 82 |
+
|
| 83 |
for i, result in enumerate(results, start=1):
|
| 84 |
st.write(f"### Requirement R{i}: {result['Requirement']}")
|
| 85 |
+
st.write(f"**Type:** {result['Type']} ({result['Confidence']['Type']} Confidence, {result['Times']['Type']:.2f}s)")
|
| 86 |
+
st.write(f"**Domain:** {result['Domain']} ({result['Confidence']['Domain']} Confidence, {result['Times']['Domain']:.2f}s)")
|
| 87 |
+
st.write(f"**Defects:** {result['Defects']} ({result['Confidence']['Defects']} Confidence, {result['Times']['Defects']:.2f}s)")
|
| 88 |
+
st.write(f"**Rewritten:** {result['Rewritten']} ({result['Confidence']['Rewritten']} Confidence, {result['Times']['Rewritten']:.2f}s)")
|
| 89 |
st.write("---")
|
| 90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
if __name__ == "__main__":
|
| 92 |
+
main()
|