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,8 +6,6 @@ import os
|
|
| 5 |
import time
|
| 6 |
from datetime import datetime
|
| 7 |
import groq
|
| 8 |
-
import pandas as pd
|
| 9 |
-
import matplotlib.pyplot as plt
|
| 10 |
|
| 11 |
# API keys (replace with your keys or use environment variables)
|
| 12 |
mistral_api_key = os.getenv("MISTRAL_API_KEY", "gz6lDXokxgR6cLY72oomALWcm7vhjRzQ")
|
|
@@ -17,7 +16,6 @@ groq_client = groq.Client(api_key=groq_api_key)
|
|
| 17 |
|
| 18 |
# Function to call Mistral API
|
| 19 |
def call_mistral_api(prompt):
|
| 20 |
-
start_time = time.time()
|
| 21 |
url = "https://api.mistral.ai/v1/chat/completions"
|
| 22 |
headers = {
|
| 23 |
"Authorization": f"Bearer {mistral_api_key}",
|
|
@@ -32,22 +30,18 @@ def call_mistral_api(prompt):
|
|
| 32 |
try:
|
| 33 |
response = requests.post(url, headers=headers, json=payload)
|
| 34 |
response.raise_for_status() # Raise an error for bad status codes
|
| 35 |
-
|
| 36 |
-
speed = end_time - start_time
|
| 37 |
-
content = response.json()['choices'][0]['message']['content']
|
| 38 |
-
return content, speed
|
| 39 |
except requests.exceptions.HTTPError as err:
|
| 40 |
if response.status_code == 429: # Rate limit exceeded
|
| 41 |
st.warning("Rate limit exceeded. Please wait a few seconds and try again.")
|
| 42 |
time.sleep(5) # Wait for 5 seconds before retrying
|
| 43 |
return call_mistral_api(prompt) # Retry the request
|
| 44 |
-
return f"HTTP Error: {err}"
|
| 45 |
except Exception as err:
|
| 46 |
-
return f"Error: {err}"
|
| 47 |
|
| 48 |
# Function to call Groq API
|
| 49 |
def call_groq_api(prompt):
|
| 50 |
-
start_time = time.time()
|
| 51 |
try:
|
| 52 |
response = groq_client.chat.completions.create(
|
| 53 |
model="llama-3.3-70b-versatile", # Correct model name
|
|
@@ -55,52 +49,35 @@ def call_groq_api(prompt):
|
|
| 55 |
{"role": "user", "content": prompt}
|
| 56 |
]
|
| 57 |
)
|
| 58 |
-
|
| 59 |
-
speed = end_time - start_time
|
| 60 |
-
content = response.choices[0].message.content
|
| 61 |
-
return content, speed
|
| 62 |
except Exception as err:
|
| 63 |
st.error(f"Error: {err}")
|
| 64 |
-
return f"Error: {err}"
|
| 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
|
| 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
|
| 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
|
| 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
|
| 84 |
-
rewritten = rewritten.strip()
|
| 85 |
-
|
| 86 |
-
# Calculate total speed for this requirement
|
| 87 |
-
total_speed = type_speed + domain_speed + defects_speed + rewritten_speed
|
| 88 |
|
| 89 |
return {
|
| 90 |
"Requirement": requirement,
|
| 91 |
"Type": req_type,
|
| 92 |
"Domain": domain,
|
| 93 |
"Defects": defects,
|
| 94 |
-
"Rewritten": rewritten
|
| 95 |
-
"Total_Speed": total_speed
|
| 96 |
}
|
| 97 |
|
| 98 |
-
# Function to calculate accuracy (placeholder logic)
|
| 99 |
-
def calculate_accuracy(results):
|
| 100 |
-
# Placeholder logic: Assume 80% accuracy for demonstration
|
| 101 |
-
# Replace this with actual logic if you have correct answers
|
| 102 |
-
return 80.0 # Return accuracy as a percentage
|
| 103 |
-
|
| 104 |
# Function to generate a PDF report
|
| 105 |
def generate_pdf_report(results):
|
| 106 |
pdf = FPDF()
|
|
@@ -141,7 +118,6 @@ def generate_pdf_report(results):
|
|
| 141 |
pdf.multi_cell(200, 10, txt=f"Domain: {result['Domain']}", align='L')
|
| 142 |
pdf.multi_cell(200, 10, txt=f"Defects: {result['Defects']}", align='L')
|
| 143 |
pdf.multi_cell(200, 10, txt=f"Rewritten: {result['Rewritten']}", align='L')
|
| 144 |
-
pdf.multi_cell(200, 10, txt=f"Total Speed: {result['Total_Speed']:.2f}s", align='L')
|
| 145 |
pdf.multi_cell(200, 10, txt="-" * 50, align='L')
|
| 146 |
pdf.ln(5) # Add some space between requirements
|
| 147 |
|
|
@@ -167,41 +143,19 @@ def main():
|
|
| 167 |
st.warning("Please enter requirements.")
|
| 168 |
else:
|
| 169 |
results = []
|
| 170 |
-
total_speed = 0
|
| 171 |
for req in requirements:
|
| 172 |
if req.strip(): # Ignore empty lines
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
st.metric("Total Speed", f"{total_speed:.2f} seconds")
|
| 185 |
-
with col2:
|
| 186 |
-
st.metric("Accuracy", f"{accuracy:.2f}%")
|
| 187 |
-
|
| 188 |
-
# Visualize metrics
|
| 189 |
-
st.subheader("Visualization")
|
| 190 |
-
metrics_data = {
|
| 191 |
-
"Metric": ["Total Speed", "Accuracy"],
|
| 192 |
-
"Value": [total_speed, accuracy]
|
| 193 |
-
}
|
| 194 |
-
df = pd.DataFrame(metrics_data)
|
| 195 |
-
|
| 196 |
-
# Bar chart
|
| 197 |
-
fig, ax = plt.subplots()
|
| 198 |
-
ax.bar(df["Metric"], df["Value"], color=["blue", "green"])
|
| 199 |
-
ax.set_ylabel("Value")
|
| 200 |
-
ax.set_title("Performance Metrics")
|
| 201 |
-
st.pyplot(fig)
|
| 202 |
-
|
| 203 |
-
# Table
|
| 204 |
-
st.table(df)
|
| 205 |
|
| 206 |
# Generate and download PDF report
|
| 207 |
pdf_report = generate_pdf_report(results)
|
|
@@ -215,4 +169,4 @@ def main():
|
|
| 215 |
|
| 216 |
# Run the app
|
| 217 |
if __name__ == "__main__":
|
| 218 |
-
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")
|
|
|
|
| 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 |
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
|
|
|
|
| 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()
|
|
|
|
| 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 |
|
|
|
|
| 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)
|
|
|
|
| 169 |
|
| 170 |
# Run the app
|
| 171 |
if __name__ == "__main__":
|
| 172 |
+
main()
|