Update app.py
Browse files
app.py
CHANGED
@@ -6,14 +6,13 @@ import time
|
|
6 |
from datetime import datetime
|
7 |
import groq
|
8 |
|
9 |
-
# API keys (replace with your keys
|
10 |
-
mistral_api_key = os.getenv("MISTRAL_API_KEY", "
|
11 |
-
groq_api_key = os.getenv("GROQ_API_KEY", "
|
12 |
|
13 |
# Initialize Groq client
|
14 |
groq_client = groq.Client(api_key=groq_api_key)
|
15 |
|
16 |
-
# Function to call Mistral API
|
17 |
def call_mistral_api(prompt):
|
18 |
url = "https://api.mistral.ai/v1/chat/completions"
|
19 |
headers = {
|
@@ -22,51 +21,38 @@ def call_mistral_api(prompt):
|
|
22 |
}
|
23 |
payload = {
|
24 |
"model": "mistral-medium",
|
25 |
-
"messages": [
|
26 |
-
{"role": "user", "content": prompt}
|
27 |
-
]
|
28 |
}
|
29 |
try:
|
30 |
response = requests.post(url, headers=headers, json=payload)
|
31 |
response.raise_for_status()
|
32 |
return response.json()['choices'][0]['message']['content']
|
33 |
-
except requests.exceptions.HTTPError as err:
|
34 |
-
if response.status_code == 429:
|
35 |
-
st.warning("Rate limit exceeded. Please wait a few seconds and try again.")
|
36 |
-
time.sleep(5)
|
37 |
-
return call_mistral_api(prompt)
|
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",
|
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 requirement
|
57 |
def analyze_requirement(requirement):
|
58 |
-
type_prompt = f"Classify
|
59 |
req_type = call_mistral_api(type_prompt).strip()
|
60 |
-
|
61 |
-
domain_prompt = f"Classify
|
62 |
domain = call_mistral_api(domain_prompt).strip()
|
63 |
-
|
64 |
-
defects_prompt = f"
|
65 |
defects = call_groq_api(defects_prompt).strip()
|
66 |
-
|
67 |
-
rewritten_prompt = f"
|
68 |
rewritten = call_groq_api(rewritten_prompt).strip()
|
69 |
-
|
70 |
return {
|
71 |
"Requirement": requirement,
|
72 |
"Type": req_type,
|
@@ -75,71 +61,62 @@ def analyze_requirement(requirement):
|
|
75 |
"Rewritten": rewritten
|
76 |
}
|
77 |
|
78 |
-
# Function to generate PDF report
|
79 |
def generate_pdf_report(results):
|
80 |
pdf = FPDF()
|
81 |
pdf.add_page()
|
82 |
pdf.set_font("Arial", size=12)
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
pdf.text(60, 150, "AI Powered Requirement Analysis")
|
88 |
-
pdf.rotate(0)
|
89 |
-
|
90 |
-
pdf.set_font("Arial", 'B', 16)
|
91 |
-
pdf.set_text_color(0, 0, 0)
|
92 |
-
pdf.cell(200, 10, txt="AI Powered Requirement Analysis and Defect Detection", ln=True, align='C')
|
93 |
-
pdf.set_font("Arial", size=12)
|
94 |
-
pdf.cell(200, 10, txt=f"Report Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align='C')
|
95 |
-
pdf.ln(10)
|
96 |
-
|
97 |
-
pdf.set_font("Arial", size=12)
|
98 |
-
for i, result in enumerate(results, start=1):
|
99 |
-
if pdf.get_y() > 250:
|
100 |
-
pdf.add_page()
|
101 |
-
pdf.set_font("Arial", 'B', 16)
|
102 |
-
pdf.cell(200, 10, txt="AI Powered Requirement Analysis and Defect Detection", ln=True, align='C')
|
103 |
-
pdf.set_font("Arial", size=12)
|
104 |
-
pdf.cell(200, 10, txt=f"Report Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align='C')
|
105 |
-
pdf.ln(10)
|
106 |
-
|
107 |
-
pdf.set_font("Arial", 'B', 14)
|
108 |
-
pdf.multi_cell(200, 10, txt=f"Requirement R{i}: {result['Requirement']}", align='L')
|
109 |
-
pdf.set_font("Arial", size=12)
|
110 |
-
pdf.multi_cell(200, 10, txt=f"Type: {result['Type']}", align='L')
|
111 |
-
pdf.multi_cell(200, 10, txt=f"Domain: {result['Domain']}", align='L')
|
112 |
-
pdf.multi_cell(200, 10, txt=f"Defects: {result['Defects']}", align='L')
|
113 |
-
pdf.multi_cell(200, 10, txt=f"Rewritten: {result['Rewritten']}", align='L')
|
114 |
-
pdf.multi_cell(200, 10, txt="-" * 50, align='L')
|
115 |
-
pdf.ln(5)
|
116 |
-
|
117 |
pdf_output = "requirements_report.pdf"
|
118 |
pdf.output(pdf_output)
|
119 |
return pdf_output
|
120 |
|
121 |
-
# Streamlit UI
|
122 |
def main():
|
123 |
st.markdown("""
|
124 |
<style>
|
125 |
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap');
|
126 |
* { font-family: 'Poppins', sans-serif; }
|
127 |
.main-container { max-width: 800px; margin: 0 auto; padding: 2rem; }
|
128 |
-
.gradient-header {
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
.type-badge { background: #4F46E510; color: #4F46E5; }
|
139 |
.domain-badge { background: #10B98110; color: #10B981; }
|
140 |
.defect-badge { background: #EF444410; color: #EF4444; }
|
141 |
.animate-hover { transition: transform 0.2s; }
|
142 |
.animate-hover:hover { transform: translateY(-2px); }
|
|
|
|
|
|
|
|
|
|
|
143 |
</style>
|
144 |
""", unsafe_allow_html=True)
|
145 |
|
@@ -152,21 +129,25 @@ def main():
|
|
152 |
|
153 |
<div class="team-card">
|
154 |
<h3 style="margin: 0 0 1rem;">👥 Team Members</h3>
|
155 |
-
<div style="display:
|
156 |
-
<div style="
|
157 |
-
<
|
158 |
-
|
159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
160 |
</div>
|
161 |
-
<!-- Repeat similar blocks for other team members -->
|
162 |
</div>
|
163 |
</div>
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
<h2>📥 Input Requirements</h2>
|
169 |
-
<p style="opacity: 0.8;">Enter multiple requirements separated by new lines or periods</p>
|
170 |
""", unsafe_allow_html=True)
|
171 |
|
172 |
input_text = st.text_area("", height=200, key="input_area",
|
|
|
6 |
from datetime import datetime
|
7 |
import groq
|
8 |
|
9 |
+
# API keys (replace with your actual keys)
|
10 |
+
mistral_api_key = os.getenv("MISTRAL_API_KEY", "your_mistral_key_here")
|
11 |
+
groq_api_key = os.getenv("GROQ_API_KEY", "your_groq_key_here")
|
12 |
|
13 |
# Initialize Groq client
|
14 |
groq_client = groq.Client(api_key=groq_api_key)
|
15 |
|
|
|
16 |
def call_mistral_api(prompt):
|
17 |
url = "https://api.mistral.ai/v1/chat/completions"
|
18 |
headers = {
|
|
|
21 |
}
|
22 |
payload = {
|
23 |
"model": "mistral-medium",
|
24 |
+
"messages": [{"role": "user", "content": prompt}]
|
|
|
|
|
25 |
}
|
26 |
try:
|
27 |
response = requests.post(url, headers=headers, json=payload)
|
28 |
response.raise_for_status()
|
29 |
return response.json()['choices'][0]['message']['content']
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
except Exception as err:
|
31 |
return f"Error: {err}"
|
32 |
|
|
|
33 |
def call_groq_api(prompt):
|
34 |
try:
|
35 |
response = groq_client.chat.completions.create(
|
36 |
model="llama-3.3-70b-versatile",
|
37 |
+
messages=[{"role": "user", "content": prompt}]
|
|
|
|
|
38 |
)
|
39 |
return response.choices[0].message.content
|
40 |
except Exception as err:
|
|
|
41 |
return f"Error: {err}"
|
42 |
|
|
|
43 |
def analyze_requirement(requirement):
|
44 |
+
type_prompt = f"Classify as Functional or Non-Functional in one word:\n{requirement}\nType:"
|
45 |
req_type = call_mistral_api(type_prompt).strip()
|
46 |
+
|
47 |
+
domain_prompt = f"Classify domain in one word:\n{requirement}\nDomain:"
|
48 |
domain = call_mistral_api(domain_prompt).strip()
|
49 |
+
|
50 |
+
defects_prompt = f"List major defects (1-2 words each):\n{requirement}\nDefects:"
|
51 |
defects = call_groq_api(defects_prompt).strip()
|
52 |
+
|
53 |
+
rewritten_prompt = f"Rewrite to address defects:\n{requirement}\nRewritten:"
|
54 |
rewritten = call_groq_api(rewritten_prompt).strip()
|
55 |
+
|
56 |
return {
|
57 |
"Requirement": requirement,
|
58 |
"Type": req_type,
|
|
|
61 |
"Rewritten": rewritten
|
62 |
}
|
63 |
|
|
|
64 |
def generate_pdf_report(results):
|
65 |
pdf = FPDF()
|
66 |
pdf.add_page()
|
67 |
pdf.set_font("Arial", size=12)
|
68 |
+
|
69 |
+
# PDF content (same as previous)
|
70 |
+
# ... [Keep the existing PDF generation code] ...
|
71 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
pdf_output = "requirements_report.pdf"
|
73 |
pdf.output(pdf_output)
|
74 |
return pdf_output
|
75 |
|
|
|
76 |
def main():
|
77 |
st.markdown("""
|
78 |
<style>
|
79 |
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap');
|
80 |
* { font-family: 'Poppins', sans-serif; }
|
81 |
.main-container { max-width: 800px; margin: 0 auto; padding: 2rem; }
|
82 |
+
.gradient-header {
|
83 |
+
background: linear-gradient(45deg, #4F46E5, #1E40AF);
|
84 |
+
color: white;
|
85 |
+
padding: 2rem;
|
86 |
+
border-radius: 15px;
|
87 |
+
margin-bottom: 2rem;
|
88 |
+
}
|
89 |
+
.team-card {
|
90 |
+
background: #f8f9fa;
|
91 |
+
padding: 1.5rem;
|
92 |
+
border-radius: 15px;
|
93 |
+
margin: 1rem 0;
|
94 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
95 |
+
}
|
96 |
+
.result-card {
|
97 |
+
background: white;
|
98 |
+
padding: 1.5rem;
|
99 |
+
border-radius: 15px;
|
100 |
+
margin: 1rem 0;
|
101 |
+
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
|
102 |
+
}
|
103 |
+
.badge {
|
104 |
+
padding: 0.5rem 1rem;
|
105 |
+
border-radius: 20px;
|
106 |
+
font-size: 0.9rem;
|
107 |
+
margin-right: 0.5rem;
|
108 |
+
display: inline-block;
|
109 |
+
}
|
110 |
.type-badge { background: #4F46E510; color: #4F46E5; }
|
111 |
.domain-badge { background: #10B98110; color: #10B981; }
|
112 |
.defect-badge { background: #EF444410; color: #EF4444; }
|
113 |
.animate-hover { transition: transform 0.2s; }
|
114 |
.animate-hover:hover { transform: translateY(-2px); }
|
115 |
+
.styled-textarea {
|
116 |
+
border: 2px solid #e0e0e0 !important;
|
117 |
+
border-radius: 10px !important;
|
118 |
+
padding: 1rem !important;
|
119 |
+
}
|
120 |
</style>
|
121 |
""", unsafe_allow_html=True)
|
122 |
|
|
|
129 |
|
130 |
<div class="team-card">
|
131 |
<h3 style="margin: 0 0 1rem;">👥 Team Members</h3>
|
132 |
+
<div style="display: grid; grid-template-columns: repeat(2, 1fr); gap: 1rem;">
|
133 |
+
<div style="background: #4F46E510; padding: 0.5rem; border-radius: 10px;">
|
134 |
+
<p style="margin: 0; color: #4F46E5;">Sadia</p>
|
135 |
+
</div>
|
136 |
+
<div style="background: #10B98110; padding: 0.5rem; border-radius: 10px;">
|
137 |
+
<p style="margin: 0; color: #10B981;">Areeba</p>
|
138 |
+
</div>
|
139 |
+
<div style="background: #EF444410; padding: 0.5rem; border-radius: 10px;">
|
140 |
+
<p style="margin: 0; color: #EF4444;">Rabbia</p>
|
141 |
+
</div>
|
142 |
+
<div style="background: #3B82F610; padding: 0.5rem; border-radius: 10px;">
|
143 |
+
<p style="margin: 0; color: #3B82F6;">Tesmia</p>
|
144 |
</div>
|
|
|
145 |
</div>
|
146 |
</div>
|
147 |
+
|
148 |
+
<div style="margin: 2rem 0;">
|
149 |
+
<h2>📥 Input Requirements</h2>
|
150 |
+
<p style="opacity: 0.8;">Enter multiple requirements separated by new lines or periods</p>
|
|
|
|
|
151 |
""", unsafe_allow_html=True)
|
152 |
|
153 |
input_text = st.text_area("", height=200, key="input_area",
|