Create app_groq.py
Browse files- app_groq.py +150 -0
app_groq.py
ADDED
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
8 |
+
# Groq API key (replace with your actual key)
|
9 |
+
groq_api_key = "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"
|
10 |
+
|
11 |
+
# Function to call Groq Llama API
|
12 |
+
def call_groq_api(prompt):
|
13 |
+
url = "https://api.groq.com/v1/chat/completions"
|
14 |
+
headers = {
|
15 |
+
"Authorization": f"Bearer {groq_api_key}",
|
16 |
+
"Content-Type": "application/json"
|
17 |
+
}
|
18 |
+
payload = {
|
19 |
+
"model": "llama-2-13b-chat",
|
20 |
+
"messages": [{"role": "user", "content": prompt}]
|
21 |
+
}
|
22 |
+
try:
|
23 |
+
response = requests.post(url, headers=headers, json=payload)
|
24 |
+
response.raise_for_status()
|
25 |
+
return response.json()['choices'][0]['message']['content']
|
26 |
+
except requests.exceptions.HTTPError as err:
|
27 |
+
return f"HTTP Error: {err}"
|
28 |
+
except Exception as err:
|
29 |
+
return f"Error: {err}"
|
30 |
+
|
31 |
+
# Function to analyze requirements
|
32 |
+
def analyze_requirement_groq(requirement):
|
33 |
+
type_prompt = f"Classify the following requirement as Functional or Non-Functional:\n\n{requirement}\n\nType:"
|
34 |
+
req_type = call_groq_api(type_prompt)
|
35 |
+
|
36 |
+
stakeholders_prompt = f"Identify the stakeholders for the following requirement:\n\n{requirement}\n\nStakeholders:"
|
37 |
+
stakeholders = call_groq_api(stakeholders_prompt)
|
38 |
+
|
39 |
+
domain_prompt = f"Classify the domain for the following requirement (e.g., Bank, Healthcare, etc.):\n\n{requirement}\n\nDomain:"
|
40 |
+
domain = call_groq_api(domain_prompt)
|
41 |
+
|
42 |
+
defects_prompt = f"""Analyze the following requirement and identify ONLY MAJOR defects (e.g., Ambiguity, Incompleteness, etc.).
|
43 |
+
If the requirement is clear and complete, respond with 'No defects.'
|
44 |
+
Requirement: {requirement}
|
45 |
+
Defects:"""
|
46 |
+
defects = call_groq_api(defects_prompt)
|
47 |
+
|
48 |
+
rewritten = rewrite_requirement_groq(requirement, defects)
|
49 |
+
|
50 |
+
return {
|
51 |
+
"Requirement": requirement,
|
52 |
+
"Type": req_type,
|
53 |
+
"Stakeholders": stakeholders,
|
54 |
+
"Domain": domain,
|
55 |
+
"Defects": defects,
|
56 |
+
"Rewritten": rewritten
|
57 |
+
}
|
58 |
+
|
59 |
+
# Function to rewrite requirement concisely
|
60 |
+
def rewrite_requirement_groq(requirement, defects):
|
61 |
+
if "no defects" in defects.lower():
|
62 |
+
return "No modification needed."
|
63 |
+
|
64 |
+
prompt = f"""Rewrite the following requirement to address the defects listed below. Ensure the rewritten requirement is clear, concise, and free of defects. It should be no more than 1-2 sentences.
|
65 |
+
|
66 |
+
Original Requirement: {requirement}
|
67 |
+
|
68 |
+
Defects: {defects}
|
69 |
+
|
70 |
+
Rewritten Requirement:"""
|
71 |
+
response = call_groq_api(prompt)
|
72 |
+
return response.strip()
|
73 |
+
|
74 |
+
# Function to generate a PDF report
|
75 |
+
def generate_pdf_report_groq(results):
|
76 |
+
pdf = FPDF()
|
77 |
+
pdf.add_page()
|
78 |
+
pdf.set_font("Arial", size=12)
|
79 |
+
|
80 |
+
pdf.set_font("Arial", 'B', 16)
|
81 |
+
pdf.cell(200, 10, txt="AI Powered Requirement Analysis - Groq Llama", ln=True, align='C')
|
82 |
+
pdf.cell(200, 10, txt=f"Report Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align='C')
|
83 |
+
pdf.ln(10)
|
84 |
+
|
85 |
+
pdf.set_font("Arial", size=12)
|
86 |
+
for i, result in enumerate(results, start=1):
|
87 |
+
if pdf.get_y() > 250:
|
88 |
+
pdf.add_page()
|
89 |
+
pdf.set_font("Arial", 'B', 16)
|
90 |
+
pdf.cell(200, 10, txt="AI Powered Requirement Analysis - Groq Llama", ln=True, align='C')
|
91 |
+
pdf.set_font("Arial", size=12)
|
92 |
+
pdf.cell(200, 10, txt=f"Report Generated on: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True, align='C')
|
93 |
+
pdf.ln(10)
|
94 |
+
|
95 |
+
pdf.set_font("Arial", 'B', 14)
|
96 |
+
pdf.multi_cell(200, 10, txt=f"Requirement R{i}: {result['Requirement']}", align='L')
|
97 |
+
pdf.set_font("Arial", size=12)
|
98 |
+
pdf.multi_cell(200, 10, txt=f"Type: {result['Type']}", align='L')
|
99 |
+
pdf.multi_cell(200, 10, txt=f"Stakeholders: {result['Stakeholders']}", align='L')
|
100 |
+
pdf.multi_cell(200, 10, txt=f"Domain: {result['Domain']}", align='L')
|
101 |
+
pdf.multi_cell(200, 10, txt=f"Defects: {result['Defects']}", align='L')
|
102 |
+
pdf.multi_cell(200, 10, txt=f"Rewritten: {result['Rewritten']}", align='L')
|
103 |
+
pdf.multi_cell(200, 10, txt="-" * 50, align='L')
|
104 |
+
pdf.ln(5)
|
105 |
+
|
106 |
+
pdf_output = "requirements_report_groq.pdf"
|
107 |
+
pdf.output(pdf_output)
|
108 |
+
return pdf_output
|
109 |
+
|
110 |
+
# Streamlit app
|
111 |
+
def main():
|
112 |
+
st.title("AI Requirement Analysis - Groq Llama")
|
113 |
+
st.markdown("**Team Name:** Sadia, Areeba, Rabbia, Tesmia")
|
114 |
+
st.markdown("**Model:** Groq Llama")
|
115 |
+
|
116 |
+
input_text = st.text_area("Enter your requirements (one per line or separated by periods):")
|
117 |
+
requirements = []
|
118 |
+
if input_text:
|
119 |
+
requirements = [req.strip() for req in input_text.replace("\n", ".").split(".") if req.strip()]
|
120 |
+
|
121 |
+
if st.button("Analyze Requirements"):
|
122 |
+
if not requirements:
|
123 |
+
st.warning("Please enter requirements.")
|
124 |
+
else:
|
125 |
+
results = []
|
126 |
+
for req in requirements:
|
127 |
+
if req.strip():
|
128 |
+
results.append(analyze_requirement_groq(req.strip()))
|
129 |
+
|
130 |
+
st.subheader("Analysis Results")
|
131 |
+
for i, result in enumerate(results, start=1):
|
132 |
+
st.write(f"### Requirement R{i}: {result['Requirement']}")
|
133 |
+
st.write(f"**Type:** {result['Type']}")
|
134 |
+
st.write(f"**Stakeholders:** {result['Stakeholders']}")
|
135 |
+
st.write(f"**Domain:** {result['Domain']}")
|
136 |
+
st.write(f"**Defects:** {result['Defects']}")
|
137 |
+
st.write(f"**Rewritten:** {result['Rewritten']}")
|
138 |
+
st.write("---")
|
139 |
+
|
140 |
+
pdf_report = generate_pdf_report_groq(results)
|
141 |
+
with open(pdf_report, "rb") as f:
|
142 |
+
st.download_button(
|
143 |
+
label="Download PDF Report",
|
144 |
+
data=f,
|
145 |
+
file_name="requirements_report_groq.pdf",
|
146 |
+
mime="application/pdf"
|
147 |
+
)
|
148 |
+
|
149 |
+
if __name__ == "__main__":
|
150 |
+
main()
|