|
import streamlit as st |
|
import requests |
|
from fpdf import FPDF |
|
import os |
|
import time |
|
from datetime import datetime |
|
import groq |
|
|
|
|
|
mistral_api_key = os.getenv("MISTRAL_API_KEY", "gz6lDXokxgR6cLY72oomALWcm7vhjRzQ") |
|
groq_api_key = os.getenv("GROQ_API_KEY", "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") |
|
|
|
|
|
groq_client = groq.Client(api_key=groq_api_key) |
|
|
|
|
|
def call_mistral_api(prompt): |
|
start_time = time.time() |
|
url = "https://api.mistral.ai/v1/chat/completions" |
|
headers = { |
|
"Authorization": f"Bearer {mistral_api_key}", |
|
"Content-Type": "application/json" |
|
} |
|
payload = { |
|
"model": "mistral-medium", |
|
"messages": [ |
|
{"role": "user", "content": prompt} |
|
] |
|
} |
|
try: |
|
response = requests.post(url, headers=headers, json=payload) |
|
response.raise_for_status() |
|
end_time = time.time() |
|
speed = end_time - start_time |
|
content = response.json()['choices'][0]['message']['content'] |
|
confidence = len(content.split()) |
|
return content, speed, confidence |
|
except requests.exceptions.HTTPError as err: |
|
if response.status_code == 429: |
|
st.warning("Rate limit exceeded. Please wait a few seconds and try again.") |
|
time.sleep(5) |
|
return call_mistral_api(prompt) |
|
return f"HTTP Error: {err}", 0, 0 |
|
except Exception as err: |
|
return f"Error: {err}", 0, 0 |
|
|
|
|
|
def call_groq_api(prompt): |
|
start_time = time.time() |
|
try: |
|
response = groq_client.chat.completions.create( |
|
model="llama-3.3-70b-versatile", |
|
messages=[ |
|
{"role": "user", "content": prompt} |
|
] |
|
) |
|
end_time = time.time() |
|
speed = end_time - start_time |
|
content = response.choices[0].message.content |
|
confidence = len(content.split()) |
|
return content, speed, confidence |
|
except Exception as err: |
|
st.error(f"Error: {err}") |
|
return f"Error: {err}", 0, 0 |
|
|
|
|
|
def analyze_requirement(requirement): |
|
|
|
type_prompt = f"Classify the following requirement as Functional or Non-Functional in one word:\n\n{requirement}\n\nType:" |
|
req_type, type_speed, type_confidence = call_mistral_api(type_prompt) |
|
req_type = req_type.strip() |
|
|
|
domain_prompt = f"Classify the domain for the following requirement in one word (e.g., E-commerce, Education, etc.):\n\n{requirement}\n\nDomain:" |
|
domain, domain_speed, domain_confidence = call_mistral_api(domain_prompt) |
|
domain = domain.strip() |
|
|
|
|
|
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:""" |
|
defects, defects_speed, defects_confidence = call_groq_api(defects_prompt) |
|
defects = defects.strip() |
|
|
|
rewritten_prompt = f"""Rewrite the following requirement in 1-2 sentences to address the defects:\n\n{requirement}\n\nRewritten:""" |
|
rewritten, rewritten_speed, rewritten_confidence = call_groq_api(rewritten_prompt) |
|
rewritten = rewritten.strip() |
|
|
|
return { |
|
"Requirement": requirement, |
|
"Type": req_type, |
|
"Domain": domain, |
|
"Defects": defects, |
|
"Rewritten": rewritten, |
|
"Type_Speed": type_speed, |
|
"Type_Confidence": type_confidence, |
|
"Domain_Speed": domain_speed, |
|
"Domain_Confidence": domain_confidence, |
|
"Defects_Speed": defects_speed, |
|
"Defects_Confidence": defects_confidence, |
|
"Rewritten_Speed": rewritten_speed, |
|
"Rewritten_Confidence": rewritten_confidence |
|
} |
|
|
|
|
|
def generate_pdf_report(results): |
|
pdf = FPDF() |
|
pdf.add_page() |
|
pdf.set_font("Arial", size=12) |
|
|
|
|
|
pdf.set_font("Arial", 'B', 50) |
|
pdf.set_text_color(230, 230, 230) |
|
pdf.rotate(45) |
|
pdf.text(60, 150, "AI Powered Requirement Analysis") |
|
pdf.rotate(0) |
|
|
|
|
|
pdf.set_font("Arial", 'B', 16) |
|
pdf.set_text_color(0, 0, 0) |
|
pdf.cell(200, 10, txt="AI Powered Requirement Analysis and Defect Detection", ln=True, align='C') |
|
pdf.set_font("Arial", size=12) |
|
pdf.cell(200, 10, txt=f"Report Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align='C') |
|
pdf.ln(10) |
|
|
|
|
|
pdf.set_font("Arial", size=12) |
|
for i, result in enumerate(results, start=1): |
|
if pdf.get_y() > 250: |
|
pdf.add_page() |
|
pdf.set_font("Arial", 'B', 16) |
|
pdf.cell(200, 10, txt="AI Powered Requirement Analysis and Defect Detection", ln=True, align='C') |
|
pdf.set_font("Arial", size=12) |
|
pdf.cell(200, 10, txt=f"Report Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align='C') |
|
pdf.ln(10) |
|
|
|
|
|
pdf.set_font("Arial", 'B', 14) |
|
pdf.multi_cell(200, 10, txt=f"Requirement R{i}: {result['Requirement']}", align='L') |
|
pdf.set_font("Arial", size=12) |
|
pdf.multi_cell(200, 10, txt=f"Type: {result['Type']}", align='L') |
|
pdf.multi_cell(200, 10, txt=f"Domain: {result['Domain']}", align='L') |
|
pdf.multi_cell(200, 10, txt=f"Defects: {result['Defects']}", align='L') |
|
pdf.multi_cell(200, 10, txt=f"Rewritten: {result['Rewritten']}", align='L') |
|
pdf.multi_cell(200, 10, txt=f"Type Speed: {result['Type_Speed']:.2f}s", align='L') |
|
pdf.multi_cell(200, 10, txt=f"Type Confidence: {result['Type_Confidence']}", align='L') |
|
pdf.multi_cell(200, 10, txt=f"Domain Speed: {result['Domain_Speed']:.2f}s", align='L') |
|
pdf.multi_cell(200, 10, txt=f"Domain Confidence: {result['Domain_Confidence']}", align='L') |
|
pdf.multi_cell(200, 10, txt=f"Defects Speed: {result['Defects_Speed']:.2f}s", align='L') |
|
pdf.multi_cell(200, 10, txt=f"Defects Confidence: {result['Defects_Confidence']}", align='L') |
|
pdf.multi_cell(200, 10, txt=f"Rewritten Speed: {result['Rewritten_Speed']:.2f}s", align='L') |
|
pdf.multi_cell(200, 10, txt=f"Rewritten Confidence: {result['Rewritten_Confidence']}", align='L') |
|
pdf.multi_cell(200, 10, txt="-" * 50, align='L') |
|
pdf.ln(5) |
|
|
|
pdf_output = "requirements_report.pdf" |
|
pdf.output(pdf_output) |
|
return pdf_output |
|
|
|
|
|
def main(): |
|
st.title("AI Powered Requirement Analysis and Defect Detection") |
|
st.markdown("**Team Name:** Sadia, Areeba, Rabbia, Tesmia") |
|
st.markdown("**Models:** Mistral (Classification & Domain) + Groq (Defects & Rewriting)") |
|
|
|
|
|
input_text = st.text_area("Enter your requirements (one per line or separated by periods):") |
|
requirements = [] |
|
if input_text: |
|
requirements = [req.strip() for req in input_text.replace("\n", ".").split(".") if req.strip()] |
|
|
|
|
|
if st.button("Analyze Requirements"): |
|
if not requirements: |
|
st.warning("Please enter requirements.") |
|
else: |
|
results = [] |
|
for req in requirements: |
|
if req.strip(): |
|
results.append(analyze_requirement(req.strip())) |
|
|
|
|
|
st.subheader("Analysis Results") |
|
for i, result in enumerate(results, start=1): |
|
st.write(f"### Requirement R{i}: {result['Requirement']}") |
|
st.write(f"**Type:** {result['Type']}") |
|
st.write(f"**Domain:** {result['Domain']}") |
|
st.write(f"**Defects:** {result['Defects']}") |
|
st.write(f"**Rewritten:** {result['Rewritten']}") |
|
st.write(f"**Type Speed:** {result['Type_Speed']:.2f}s") |
|
st.write(f"**Type Confidence:** {result['Type_Confidence']}") |
|
st.write(f"**Domain Speed:** {result['Domain_Speed']:.2f}s") |
|
st.write(f"**Domain Confidence:** {result['Domain_Confidence']}") |
|
st.write(f"**Defects Speed:** {result['Defects_Speed']:.2f}s") |
|
st.write(f"**Defects Confidence:** {result['Defects_Confidence']}") |
|
st.write(f"**Rewritten Speed:** {result['Rewritten_Speed']:.2f}s") |
|
st.write(f"**Rewritten Confidence:** {result['Rewritten_Confidence']}") |
|
st.write("---") |
|
|
|
|
|
pdf_report = generate_pdf_report(results) |
|
with open(pdf_report, "rb") as f: |
|
st.download_button( |
|
label="Download PDF Report", |
|
data=f, |
|
file_name="requirements_report.pdf", |
|
mime="application/pdf" |
|
) |
|
|
|
|
|
if __name__ == "__main__": |
|
main() |