Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,71 +1,142 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
from
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
#
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
requirement_type = classify_requirement_type(requirement)
|
51 |
-
stakeholder = identify_stakeholders(requirement)
|
52 |
-
domain = classify_domain(requirement)
|
53 |
-
defects = detect_defects(requirement)
|
54 |
-
rewritten_requirement = rewrite_requirement(requirement)
|
55 |
-
|
56 |
return {
|
57 |
-
"Requirement
|
58 |
-
"
|
|
|
59 |
"Domain": domain,
|
60 |
"Defects": defects,
|
61 |
-
"Rewritten
|
62 |
}
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from fpdf import FPDF
|
4 |
+
import pdfplumber
|
5 |
+
|
6 |
+
# Mistral API key (replace with your key)
|
7 |
+
API_KEY = "gz6lDXokxgR6cLY72oomALWcm7vhjzQ"
|
8 |
+
MISTRAL_API_URL = "https://api.mistral.ai/v1/chat/completions"
|
9 |
+
|
10 |
+
# Function to call Mistral API
|
11 |
+
def call_mistral_api(prompt):
|
12 |
+
headers = {
|
13 |
+
"Authorization": f"Bearer {API_KEY}",
|
14 |
+
"Content-Type": "application/json"
|
15 |
+
}
|
16 |
+
payload = {
|
17 |
+
"model": "mistral-medium",
|
18 |
+
"messages": [
|
19 |
+
{"role": "user", "content": prompt}
|
20 |
+
]
|
21 |
+
}
|
22 |
+
response = requests.post(MISTRAL_API_URL, headers=headers, json=payload)
|
23 |
+
if response.status_code == 200:
|
24 |
+
return response.json()['choices'][0]['message']['content']
|
25 |
+
else:
|
26 |
+
return f"Error: {response.status_code}, {response.text}"
|
27 |
+
|
28 |
+
# Function to analyze a single requirement
|
29 |
+
def analyze_requirement(requirement):
|
30 |
+
# Detect requirement type
|
31 |
+
type_prompt = f"Classify the following requirement as Functional or Non-Functional:\n\n{requirement}\n\nType:"
|
32 |
+
req_type = call_mistral_api(type_prompt)
|
33 |
+
|
34 |
+
# Identify stakeholders
|
35 |
+
stakeholders_prompt = f"Identify the stakeholders for the following requirement:\n\n{requirement}\n\nStakeholders:"
|
36 |
+
stakeholders = call_mistral_api(stakeholders_prompt)
|
37 |
+
|
38 |
+
# Classify domain
|
39 |
+
domain_prompt = f"Classify the domain for the following requirement (e.g., Bank, Healthcare, etc.):\n\n{requirement}\n\nDomain:"
|
40 |
+
domain = call_mistral_api(domain_prompt)
|
41 |
+
|
42 |
+
# Detect defects
|
43 |
+
defects_prompt = f"Detect any defects in the following requirement (e.g., Ambiguity, Incompleteness, etc.):\n\n{requirement}\n\nDefects:"
|
44 |
+
defects = call_mistral_api(defects_prompt)
|
45 |
+
|
46 |
+
# Rewrite requirement
|
47 |
+
rewrite_prompt = f"Rewrite the following requirement in a simpler form:\n\n{requirement}\n\nSimplified Requirement:"
|
48 |
+
rewritten = call_mistral_api(rewrite_prompt)
|
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 process multiple requirements
|
60 |
+
def process_requirements(input_text, uploaded_file):
|
61 |
+
requirements = []
|
62 |
+
|
63 |
+
# If a file is uploaded, extract text from the PDF
|
64 |
+
if uploaded_file:
|
65 |
+
with pdfplumber.open(uploaded_file) as pdf:
|
66 |
+
for page in pdf.pages:
|
67 |
+
requirements.extend(page.extract_text().split("\n"))
|
68 |
+
|
69 |
+
# If text input is provided, split into individual requirements
|
70 |
+
if input_text:
|
71 |
+
requirements.extend(input_text.split("\n"))
|
72 |
+
|
73 |
+
# Analyze each requirement
|
74 |
+
results = []
|
75 |
+
for req in requirements:
|
76 |
+
if req.strip(): # Ignore empty lines
|
77 |
+
results.append(analyze_requirement(req.strip()))
|
78 |
+
|
79 |
+
return results
|
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 |
+
for result in results:
|
88 |
+
pdf.cell(200, 10, txt=f"Requirement: {result['Requirement']}", ln=True)
|
89 |
+
pdf.cell(200, 10, txt=f"Type: {result['Type']}", ln=True)
|
90 |
+
pdf.cell(200, 10, txt=f"Stakeholders: {result['Stakeholders']}", ln=True)
|
91 |
+
pdf.cell(200, 10, txt=f"Domain: {result['Domain']}", ln=True)
|
92 |
+
pdf.cell(200, 10, txt=f"Defects: {result['Defects']}", ln=True)
|
93 |
+
pdf.cell(200, 10, txt=f"Rewritten: {result['Rewritten']}", ln=True)
|
94 |
+
pdf.cell(200, 10, txt="-" * 50, ln=True)
|
95 |
+
|
96 |
+
pdf_output = "requirements_report.pdf"
|
97 |
+
pdf.output(pdf_output)
|
98 |
+
return pdf_output
|
99 |
+
|
100 |
+
# Gradio interface
|
101 |
+
def app_interface(input_text, uploaded_file):
|
102 |
+
results = process_requirements(input_text, uploaded_file)
|
103 |
+
pdf_report = generate_pdf_report(results)
|
104 |
+
|
105 |
+
# Display results in a clean format
|
106 |
+
output_text = ""
|
107 |
+
for result in results:
|
108 |
+
output_text += f"Requirement: {result['Requirement']}\n"
|
109 |
+
output_text += f"Type: {result['Type']}\n"
|
110 |
+
output_text += f"Stakeholders: {result['Stakeholders']}\n"
|
111 |
+
output_text += f"Domain: {result['Domain']}\n"
|
112 |
+
output_text += f"Defects: {result['Defects']}\n"
|
113 |
+
output_text += f"Rewritten: {result['Rewritten']}\n"
|
114 |
+
output_text += "-" * 50 + "\n\n"
|
115 |
+
|
116 |
+
return output_text, pdf_report
|
117 |
+
|
118 |
+
# Gradio app
|
119 |
+
title = "Requirement Analysis App"
|
120 |
+
description = """
|
121 |
+
This app analyzes software requirements using the Mistral model.
|
122 |
+
You can either enter requirements manually or upload a PDF file containing requirements.
|
123 |
+
"""
|
124 |
+
team_name = "Team Innovators"
|
125 |
+
|
126 |
+
interface = gr.Interface(
|
127 |
+
fn=app_interface,
|
128 |
+
inputs=[
|
129 |
+
gr.Textbox(label="Enter Requirements (one per line)"),
|
130 |
+
gr.File(label="Upload PDF File (optional)")
|
131 |
+
],
|
132 |
+
outputs=[
|
133 |
+
gr.Textbox(label="Analysis Results"),
|
134 |
+
gr.File(label="Download PDF Report")
|
135 |
+
],
|
136 |
+
title=title,
|
137 |
+
description=description,
|
138 |
+
theme="soft"
|
139 |
+
)
|
140 |
+
|
141 |
+
# Add team name to the interface
|
142 |
+
interface.launch()
|