Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
import streamlit as st
|
| 3 |
import requests
|
| 4 |
from fpdf import FPDF
|
|
@@ -16,6 +15,7 @@ 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}",
|
|
@@ -30,18 +30,23 @@ def call_mistral_api(prompt):
|
|
| 30 |
try:
|
| 31 |
response = requests.post(url, headers=headers, json=payload)
|
| 32 |
response.raise_for_status() # Raise an error for bad status codes
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
|
@@ -49,33 +54,49 @@ def call_groq_api(prompt):
|
|
| 49 |
{"role": "user", "content": prompt}
|
| 50 |
]
|
| 51 |
)
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|
|
|
|
| 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)
|
|
|
|
| 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)
|
|
|
|
| 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)
|
|
|
|
| 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
|
|
@@ -118,6 +139,14 @@ def generate_pdf_report(results):
|
|
| 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 |
|
|
@@ -155,6 +184,14 @@ def main():
|
|
| 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
|
|
@@ -169,5 +206,4 @@ def main():
|
|
| 169 |
|
| 170 |
# Run the app
|
| 171 |
if __name__ == "__main__":
|
| 172 |
-
main()
|
| 173 |
-
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import requests
|
| 3 |
from fpdf import FPDF
|
|
|
|
| 15 |
|
| 16 |
# Function to call Mistral API
|
| 17 |
def call_mistral_api(prompt):
|
| 18 |
+
start_time = time.time()
|
| 19 |
url = "https://api.mistral.ai/v1/chat/completions"
|
| 20 |
headers = {
|
| 21 |
"Authorization": f"Bearer {mistral_api_key}",
|
|
|
|
| 30 |
try:
|
| 31 |
response = requests.post(url, headers=headers, json=payload)
|
| 32 |
response.raise_for_status() # Raise an error for bad status codes
|
| 33 |
+
end_time = time.time()
|
| 34 |
+
speed = end_time - start_time
|
| 35 |
+
content = response.json()['choices'][0]['message']['content']
|
| 36 |
+
confidence = len(content.split()) # Simulate confidence with response length
|
| 37 |
+
return content, speed, confidence
|
| 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}", 0, 0
|
| 44 |
except Exception as err:
|
| 45 |
+
return f"Error: {err}", 0, 0
|
| 46 |
|
| 47 |
# Function to call Groq API
|
| 48 |
def call_groq_api(prompt):
|
| 49 |
+
start_time = time.time()
|
| 50 |
try:
|
| 51 |
response = groq_client.chat.completions.create(
|
| 52 |
model="llama-3.3-70b-versatile", # Correct model name
|
|
|
|
| 54 |
{"role": "user", "content": prompt}
|
| 55 |
]
|
| 56 |
)
|
| 57 |
+
end_time = time.time()
|
| 58 |
+
speed = end_time - start_time
|
| 59 |
+
content = response.choices[0].message.content
|
| 60 |
+
confidence = len(content.split()) # Simulate confidence with response length
|
| 61 |
+
return content, speed, confidence
|
| 62 |
except Exception as err:
|
| 63 |
st.error(f"Error: {err}")
|
| 64 |
+
return f"Error: {err}", 0, 0
|
| 65 |
|
| 66 |
# Function to analyze a single requirement using both models
|
| 67 |
def analyze_requirement(requirement):
|
| 68 |
# Use Mistral for classification and domain identification
|
| 69 |
type_prompt = f"Classify the following requirement as Functional or Non-Functional in one word:\n\n{requirement}\n\nType:"
|
| 70 |
+
req_type, type_speed, type_confidence = call_mistral_api(type_prompt)
|
| 71 |
+
req_type = req_type.strip()
|
| 72 |
|
| 73 |
domain_prompt = f"Classify the domain for the following requirement in one word (e.g., E-commerce, Education, etc.):\n\n{requirement}\n\nDomain:"
|
| 74 |
+
domain, domain_speed, domain_confidence = call_mistral_api(domain_prompt)
|
| 75 |
+
domain = domain.strip()
|
| 76 |
|
| 77 |
# Use Groq for defect analysis and rewriting
|
| 78 |
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:"""
|
| 79 |
+
defects, defects_speed, defects_confidence = call_groq_api(defects_prompt)
|
| 80 |
+
defects = defects.strip()
|
| 81 |
|
| 82 |
rewritten_prompt = f"""Rewrite the following requirement in 1-2 sentences to address the defects:\n\n{requirement}\n\nRewritten:"""
|
| 83 |
+
rewritten, rewritten_speed, rewritten_confidence = call_groq_api(rewritten_prompt)
|
| 84 |
+
rewritten = rewritten.strip()
|
| 85 |
|
| 86 |
return {
|
| 87 |
"Requirement": requirement,
|
| 88 |
"Type": req_type,
|
| 89 |
"Domain": domain,
|
| 90 |
"Defects": defects,
|
| 91 |
+
"Rewritten": rewritten,
|
| 92 |
+
"Type_Speed": type_speed,
|
| 93 |
+
"Type_Confidence": type_confidence,
|
| 94 |
+
"Domain_Speed": domain_speed,
|
| 95 |
+
"Domain_Confidence": domain_confidence,
|
| 96 |
+
"Defects_Speed": defects_speed,
|
| 97 |
+
"Defects_Confidence": defects_confidence,
|
| 98 |
+
"Rewritten_Speed": rewritten_speed,
|
| 99 |
+
"Rewritten_Confidence": rewritten_confidence
|
| 100 |
}
|
| 101 |
|
| 102 |
# Function to generate a PDF report
|
|
|
|
| 139 |
pdf.multi_cell(200, 10, txt=f"Domain: {result['Domain']}", align='L')
|
| 140 |
pdf.multi_cell(200, 10, txt=f"Defects: {result['Defects']}", align='L')
|
| 141 |
pdf.multi_cell(200, 10, txt=f"Rewritten: {result['Rewritten']}", align='L')
|
| 142 |
+
pdf.multi_cell(200, 10, txt=f"Type Speed: {result['Type_Speed']:.2f}s", align='L')
|
| 143 |
+
pdf.multi_cell(200, 10, txt=f"Type Confidence: {result['Type_Confidence']}", align='L')
|
| 144 |
+
pdf.multi_cell(200, 10, txt=f"Domain Speed: {result['Domain_Speed']:.2f}s", align='L')
|
| 145 |
+
pdf.multi_cell(200, 10, txt=f"Domain Confidence: {result['Domain_Confidence']}", align='L')
|
| 146 |
+
pdf.multi_cell(200, 10, txt=f"Defects Speed: {result['Defects_Speed']:.2f}s", align='L')
|
| 147 |
+
pdf.multi_cell(200, 10, txt=f"Defects Confidence: {result['Defects_Confidence']}", align='L')
|
| 148 |
+
pdf.multi_cell(200, 10, txt=f"Rewritten Speed: {result['Rewritten_Speed']:.2f}s", align='L')
|
| 149 |
+
pdf.multi_cell(200, 10, txt=f"Rewritten Confidence: {result['Rewritten_Confidence']}", align='L')
|
| 150 |
pdf.multi_cell(200, 10, txt="-" * 50, align='L')
|
| 151 |
pdf.ln(5) # Add some space between requirements
|
| 152 |
|
|
|
|
| 184 |
st.write(f"**Domain:** {result['Domain']}")
|
| 185 |
st.write(f"**Defects:** {result['Defects']}")
|
| 186 |
st.write(f"**Rewritten:** {result['Rewritten']}")
|
| 187 |
+
st.write(f"**Type Speed:** {result['Type_Speed']:.2f}s")
|
| 188 |
+
st.write(f"**Type Confidence:** {result['Type_Confidence']}")
|
| 189 |
+
st.write(f"**Domain Speed:** {result['Domain_Speed']:.2f}s")
|
| 190 |
+
st.write(f"**Domain Confidence:** {result['Domain_Confidence']}")
|
| 191 |
+
st.write(f"**Defects Speed:** {result['Defects_Speed']:.2f}s")
|
| 192 |
+
st.write(f"**Defects Confidence:** {result['Defects_Confidence']}")
|
| 193 |
+
st.write(f"**Rewritten Speed:** {result['Rewritten_Speed']:.2f}s")
|
| 194 |
+
st.write(f"**Rewritten Confidence:** {result['Rewritten_Confidence']}")
|
| 195 |
st.write("---")
|
| 196 |
|
| 197 |
# Generate and download PDF report
|
|
|
|
| 206 |
|
| 207 |
# Run the app
|
| 208 |
if __name__ == "__main__":
|
| 209 |
+
main()
|
|
|