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