Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,183 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
from fpdf import FPDF
|
4 |
+
import os
|
5 |
+
import time
|
6 |
+
from datetime import datetime
|
7 |
+
import groq
|
8 |
+
|
9 |
+
# Load custom CSS
|
10 |
+
def local_css(file_name):
|
11 |
+
with open(file_name) as f:
|
12 |
+
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
|
13 |
+
|
14 |
+
# API keys (replace with your keys or use environment variables)
|
15 |
+
mistral_api_key = os.getenv("MISTRAL_API_KEY", "gz6lDXokxgR6cLY72oomALWcm7vhjRzQ")
|
16 |
+
groq_api_key = os.getenv("GROQ_API_KEY", "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")
|
17 |
+
|
18 |
+
# Initialize Groq client
|
19 |
+
groq_client = groq.Client(api_key=groq_api_key)
|
20 |
+
|
21 |
+
# Function to call Mistral API
|
22 |
+
def call_mistral_api(prompt):
|
23 |
+
url = "https://api.mistral.ai/v1/chat/completions"
|
24 |
+
headers = {
|
25 |
+
"Authorization": f"Bearer {mistral_api_key}",
|
26 |
+
"Content-Type": "application/json"
|
27 |
+
}
|
28 |
+
payload = {
|
29 |
+
"model": "mistral-medium",
|
30 |
+
"messages": [
|
31 |
+
{"role": "user", "content": prompt}
|
32 |
+
]
|
33 |
+
}
|
34 |
+
try:
|
35 |
+
response = requests.post(url, headers=headers, json=payload)
|
36 |
+
response.raise_for_status() # Raise an error for bad status codes
|
37 |
+
return response.json()['choices'][0]['message']['content']
|
38 |
+
except requests.exceptions.HTTPError as err:
|
39 |
+
if response.status_code == 429: # Rate limit exceeded
|
40 |
+
st.warning("Rate limit exceeded. Please wait a few seconds and try again.")
|
41 |
+
time.sleep(5) # Wait for 5 seconds before retrying
|
42 |
+
return call_mistral_api(prompt) # Retry the request
|
43 |
+
return f"HTTP Error: {err}"
|
44 |
+
except Exception as err:
|
45 |
+
return f"Error: {err}"
|
46 |
+
|
47 |
+
# Function to call Groq API
|
48 |
+
def call_groq_api(prompt):
|
49 |
+
try:
|
50 |
+
response = groq_client.chat.completions.create(
|
51 |
+
model="llama-3.3-70b-versatile", # Correct model name
|
52 |
+
messages=[
|
53 |
+
{"role": "user", "content": prompt}
|
54 |
+
]
|
55 |
+
)
|
56 |
+
return response.choices[0].message.content
|
57 |
+
except Exception as err:
|
58 |
+
st.error(f"Error: {err}")
|
59 |
+
return f"Error: {err}"
|
60 |
+
|
61 |
+
# Function to analyze a single requirement using both models
|
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 = call_groq_api(defects_prompt).strip()
|
73 |
+
|
74 |
+
rewritten_prompt = f"""Rewrite the following requirement in 1-2 sentences to address the defects:\n\n{requirement}\n\nRewritten:"""
|
75 |
+
rewritten = call_groq_api(rewritten_prompt).strip()
|
76 |
+
|
77 |
+
return {
|
78 |
+
"Requirement": requirement,
|
79 |
+
"Type": req_type,
|
80 |
+
"Domain": domain,
|
81 |
+
"Defects": defects,
|
82 |
+
"Rewritten": rewritten
|
83 |
+
}
|
84 |
+
|
85 |
+
# Function to generate a PDF report
|
86 |
+
def generate_pdf_report(results):
|
87 |
+
pdf = FPDF()
|
88 |
+
pdf.add_page()
|
89 |
+
pdf.set_font("Arial", size=12)
|
90 |
+
|
91 |
+
# Add watermark
|
92 |
+
pdf.set_font("Arial", 'B', 50)
|
93 |
+
pdf.set_text_color(230, 230, 230) # Light gray color for watermark
|
94 |
+
pdf.rotate(45) # Rotate the text for watermark effect
|
95 |
+
pdf.text(60, 150, "AI Powered Requirement Analysis")
|
96 |
+
pdf.rotate(0) # Reset rotation
|
97 |
+
|
98 |
+
# Add title and date/time
|
99 |
+
pdf.set_font("Arial", 'B', 16)
|
100 |
+
pdf.set_text_color(0, 0, 0) # Black color for title
|
101 |
+
pdf.cell(200, 10, txt="AI Powered Requirement Analysis and Defect Detection", ln=True, align='C')
|
102 |
+
pdf.set_font("Arial", size=12)
|
103 |
+
pdf.cell(200, 10, txt=f"Report Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align='C')
|
104 |
+
pdf.ln(10) # Add some space
|
105 |
+
|
106 |
+
# Add requirements analysis
|
107 |
+
pdf.set_font("Arial", size=12)
|
108 |
+
for i, result in enumerate(results, start=1):
|
109 |
+
if pdf.get_y() > 250: # If the content is near the bottom of the page
|
110 |
+
pdf.add_page() # Add a new page
|
111 |
+
pdf.set_font("Arial", 'B', 16)
|
112 |
+
pdf.cell(200, 10, txt="AI Powered Requirement Analysis and Defect Detection", ln=True, align='C')
|
113 |
+
pdf.set_font("Arial", size=12)
|
114 |
+
pdf.cell(200, 10, txt=f"Report Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align='C')
|
115 |
+
pdf.ln(10) # Add some space
|
116 |
+
|
117 |
+
# Add requirement details
|
118 |
+
pdf.set_font("Arial", 'B', 14)
|
119 |
+
pdf.multi_cell(200, 10, txt=f"Requirement R{i}: {result['Requirement']}", align='L')
|
120 |
+
pdf.set_font("Arial", size=12)
|
121 |
+
pdf.multi_cell(200, 10, txt=f"Type: {result['Type']}", align='L')
|
122 |
+
pdf.multi_cell(200, 10, txt=f"Domain: {result['Domain']}", align='L')
|
123 |
+
pdf.multi_cell(200, 10, txt=f"Defects: {result['Defects']}", align='L')
|
124 |
+
pdf.multi_cell(200, 10, txt=f"Rewritten: {result['Rewritten']}", align='L')
|
125 |
+
pdf.multi_cell(200, 10, txt="-" * 50, align='L')
|
126 |
+
pdf.ln(5) # Add some space between requirements
|
127 |
+
|
128 |
+
pdf_output = "requirements_report.pdf"
|
129 |
+
pdf.output(pdf_output)
|
130 |
+
return pdf_output
|
131 |
+
|
132 |
+
# Streamlit app
|
133 |
+
def main():
|
134 |
+
# Load custom CSS
|
135 |
+
local_css("style.css")
|
136 |
+
|
137 |
+
st.title("AI Powered Requirement Analysis and Defect Detection")
|
138 |
+
st.markdown("**Team Name:** Sadia, Areeba, Rabbia, Tesmia")
|
139 |
+
st.markdown("**Models:** Mistral (Classification & Domain) + Groq (Defects & Rewriting)")
|
140 |
+
|
141 |
+
# Input requirements manually
|
142 |
+
with st.container():
|
143 |
+
st.subheader("Enter Requirements")
|
144 |
+
input_text = st.text_area("Enter your requirements (one per line or separated by periods):", height=200)
|
145 |
+
requirements = []
|
146 |
+
if input_text:
|
147 |
+
requirements = [req.strip() for req in input_text.replace("\n", ".").split(".") if req.strip()]
|
148 |
+
|
149 |
+
# Analyze requirements
|
150 |
+
with st.container():
|
151 |
+
col1, col2 = st.columns([2, 1])
|
152 |
+
with col1:
|
153 |
+
if st.button("Analyze Requirements"):
|
154 |
+
if not requirements:
|
155 |
+
st.warning("Please enter requirements.")
|
156 |
+
else:
|
157 |
+
with st.spinner("Analyzing requirements..."):
|
158 |
+
results = []
|
159 |
+
for req in requirements:
|
160 |
+
if req.strip(): # Ignore empty lines
|
161 |
+
results.append(analyze_requirement(req.strip()))
|
162 |
+
|
163 |
+
# Display results
|
164 |
+
st.subheader("Analysis Results")
|
165 |
+
for i, result in enumerate(results, start=1):
|
166 |
+
st.write(f"### Requirement R{i}: {result['Requirement']}")
|
167 |
+
st.write(f"**Type:** {result['Type']}")
|
168 |
+
st.write(f"**Domain:** {result['Domain']}")
|
169 |
+
st.write(f"**Defects:** {result['Defects']}")
|
170 |
+
st.write(f"**Rewritten:** {result['Rewritten']}")
|
171 |
+
st.write("---")
|
172 |
+
|
173 |
+
# Generate and download PDF report
|
174 |
+
pdf_report = generate_pdf_report(results)
|
175 |
+
with open(pdf_report, "rb") as f:
|
176 |
+
st.download_button(
|
177 |
+
label="Download PDF Report",
|
178 |
+
data=f,
|
179 |
+
file_name="requirements_report.pdf",
|
180 |
+
mime="application/pdf"
|
181 |
+
)
|
182 |
+
with col2:
|
183 |
+
st.image("path_to_your_image.jpg", width=20
|