|
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) |
|
|
|
|
|
st.markdown(""" |
|
<style> |
|
.main { |
|
background-color: #f8f9fa; |
|
} |
|
.stTextArea textarea { |
|
border: 2px solid #4a4e69; |
|
border-radius: 10px; |
|
padding: 15px !important; |
|
} |
|
.stButton button { |
|
background-color: #4a4e69; |
|
color: white; |
|
border: none; |
|
padding: 12px 30px; |
|
border-radius: 8px; |
|
font-size: 16px; |
|
font-weight: bold; |
|
transition: all 0.3s ease; |
|
width: 100%; |
|
} |
|
.stButton button:hover { |
|
background-color: #3a3e59; |
|
transform: scale(1.02); |
|
} |
|
.report-title { |
|
color: #4a4e69; |
|
font-size: 2.5em; |
|
text-align: center; |
|
padding: 20px; |
|
border-bottom: 3px solid #4a4e69; |
|
margin-bottom: 30px; |
|
} |
|
.requirement-card { |
|
background: white; |
|
border-radius: 15px; |
|
padding: 25px; |
|
margin: 15px 0; |
|
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); |
|
border-left: 5px solid #4a4e69; |
|
} |
|
.sidebar .sidebar-content { |
|
background-color: #4a4e69; |
|
color: white; |
|
} |
|
.metric-box { |
|
background: white; |
|
padding: 15px; |
|
border-radius: 10px; |
|
margin: 10px 0; |
|
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05); |
|
} |
|
.stSpinner > div { |
|
display: flex; |
|
justify-content: center; |
|
align-items: center; |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
def call_mistral_api(prompt): |
|
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() |
|
return response.json()['choices'][0]['message']['content'] |
|
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}" |
|
except Exception as err: |
|
return f"Error: {err}" |
|
|
|
|
|
def call_groq_api(prompt): |
|
try: |
|
response = groq_client.chat.completions.create( |
|
model="llama-3.3-70b-versatile", |
|
messages=[ |
|
{"role": "user", "content": prompt} |
|
] |
|
) |
|
return response.choices[0].message.content |
|
except Exception as err: |
|
st.error(f"Error: {err}") |
|
return f"Error: {err}" |
|
|
|
|
|
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 = call_mistral_api(type_prompt).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 = call_mistral_api(domain_prompt).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 = call_groq_api(defects_prompt).strip() |
|
|
|
rewritten_prompt = f"""Rewrite the following requirement in 1-2 sentences to address the defects:\n\n{requirement}\n\nRewritten:""" |
|
rewritten = call_groq_api(rewritten_prompt).strip() |
|
|
|
return { |
|
"Requirement": requirement, |
|
"Type": req_type, |
|
"Domain": domain, |
|
"Defects": defects, |
|
"Rewritten": rewritten |
|
} |
|
|
|
|
|
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="-" * 50, align='L') |
|
pdf.ln(5) |
|
|
|
pdf_output = "requirements_report.pdf" |
|
pdf.output(pdf_output) |
|
return pdf_output |
|
|
|
|
|
def main(): |
|
|
|
with st.sidebar: |
|
st.markdown("<h1 style='color: white; text-align: center;'>Team Details</h1>", unsafe_allow_html=True) |
|
st.markdown(""" |
|
<div style='background: rgba(255,255,255,0.1); padding: 15px; border-radius: 10px; margin: 15px 0;'> |
|
<h3 style='color: white; margin-bottom: 10px;'>π©π» Team Members</h3> |
|
<p style='color: white;'>Sadia<br>Areeba<br>Rabbia<br>Tesmia</p> |
|
</div> |
|
""", unsafe_allow_html=True) |
|
st.markdown(""" |
|
<div style='background: rgba(255,255,255,0.1); padding: 15px; border-radius: 10px;'> |
|
<h3 style='color: white; margin-bottom: 10px;'>π€ AI Models</h3> |
|
<p style='color: white;'>Mistral (Classification)<br>Groq (Defect Analysis)</p> |
|
</div> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
col1, col2, col3 = st.columns([1,6,1]) |
|
with col2: |
|
st.markdown("<h1 class='report-title'>AI Requirement Analyzer</h1>", unsafe_allow_html=True) |
|
|
|
|
|
with st.container(): |
|
st.markdown("### π Enter Requirements") |
|
input_text = st.text_area( |
|
"Enter your requirements (one per line or separated by periods):", |
|
height=200, |
|
label_visibility="collapsed" |
|
) |
|
|
|
|
|
if st.button("π Analyze Requirements", key="analyze_btn"): |
|
if not input_text.strip(): |
|
st.warning("β οΈ Please enter requirements before analyzing") |
|
else: |
|
with st.spinner("π Analyzing requirements..."): |
|
requirements = [req.strip() for req in input_text.replace("\n", ".").split(".") if req.strip()] |
|
results = [] |
|
progress_bar = st.progress(0) |
|
for i, req in enumerate(requirements): |
|
results.append(analyze_requirement(req.strip())) |
|
progress_bar.progress((i+1)/len(requirements)) |
|
|
|
|
|
st.success("β
Analysis Complete!") |
|
st.markdown("---") |
|
st.markdown("## π Analysis Results") |
|
|
|
|
|
col1, col2, col3 = st.columns(3) |
|
with col1: |
|
st.markdown(f""" |
|
<div class='metric-box'> |
|
<h3>π Total Requirements</h3> |
|
<h2>{len(results)}</h2> |
|
</div> |
|
""", unsafe_allow_html=True) |
|
with col2: |
|
types = [res['Type'] for res in results] |
|
st.markdown(f""" |
|
<div class='metric-box'> |
|
<h3>π Functional/Non-Functional</h3> |
|
<h2>{types.count('Functional')}/{types.count('Non-Functional')}</h2> |
|
</div> |
|
""", unsafe_allow_html=True) |
|
with col3: |
|
domains = len(set([res['Domain'] for res in results])) |
|
st.markdown(f""" |
|
<div class='metric-box'> |
|
<h3>π Unique Domains</h3> |
|
<h2>{domains}</h2> |
|
</div> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
st.markdown("---") |
|
for i, result in enumerate(results, start=1): |
|
with st.container(): |
|
st.markdown(f""" |
|
<div class='requirement-card'> |
|
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 15px;"> |
|
<h3>π Requirement R{i}</h3> |
|
<span style="font-size: 0.9em; color: #666;">{result['Domain']} Domain</span> |
|
</div> |
|
<p style="color: #444; margin-bottom: 15px;">{result['Requirement']}</p> |
|
<div style="display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px;"> |
|
<div style="background: #f8f9fa; padding: 10px; border-radius: 8px;"> |
|
<h4>π Type</h4> |
|
<p>{result['Type']}</p> |
|
</div> |
|
<div style="background: #f8f9fa; padding: 10px; border-radius: 8px;"> |
|
<h4>β οΈ Defects</h4> |
|
<p>{result['Defects']}</p> |
|
</div> |
|
<div style="background: #f8f9fa; padding: 10px; border-radius: 8px;"> |
|
<h4>βοΈ Improved Version</h4> |
|
<p>{result['Rewritten']}</p> |
|
</div> |
|
</div> |
|
</div> |
|
""", unsafe_allow_html=True) |
|
|
|
|
|
st.markdown("---") |
|
st.markdown("## π€ Export Results") |
|
pdf_report = generate_pdf_report(results) |
|
with open(pdf_report, "rb") as f: |
|
st.download_button( |
|
label="π₯ Download Full Report (PDF)", |
|
data=f, |
|
file_name="requirements_report.pdf", |
|
mime="application/pdf", |
|
help="Download comprehensive PDF report with all analysis details" |
|
) |
|
|
|
if __name__ == "__main__": |
|
main() |