added AI analysis
This commit is contained in:
@@ -7,6 +7,37 @@
|
|||||||
We will send the document to {{ email }} when it is ready.
|
We will send the document to {{ email }} when it is ready.
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<h2>Top 10 Identified Risks</h2>
|
||||||
|
<table class="table table-striped table-bordered">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th scope="col">Risk ID</th>
|
||||||
|
<th scope="col">Risk Name</th>
|
||||||
|
<th scope="col">Category</th>
|
||||||
|
<th scope="col">Primary Impact</th>
|
||||||
|
<th scope="col">Secondary Impact</th>
|
||||||
|
<th scope="col">Tertiary Impact</th>
|
||||||
|
<th scope="col">Detection Difficulty</th>
|
||||||
|
<th scope="col">Recovery Complexity</th>
|
||||||
|
<th scope="col">Business Impact Severity</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for risk in top_risks %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ risk.risk_id }}</td>
|
||||||
|
<td>{{ risk.risk_name }}</td>
|
||||||
|
<td>{{ risk.category }}</td>
|
||||||
|
<td>{{ risk.primary_impact }}</td>
|
||||||
|
<td>{{ risk.secondary_impact }}</td>
|
||||||
|
<td>{{ risk.tretiary_impact }}</td>
|
||||||
|
<td>{{ risk.detection_difficulty }}</td>
|
||||||
|
<td>{{ risk.recovery_complexity }}</td>
|
||||||
|
<td>{{ risk.businnes_impact_severity }}</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
</div>
|
</div>
|
||||||
{% endblock content %}
|
{% endblock content %}
|
||||||
|
|
||||||
|
|||||||
56
backend/core/utils.py
Normal file
56
backend/core/utils.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
from openai import OpenAI
|
||||||
|
from django.conf import settings
|
||||||
|
from .models import Risk
|
||||||
|
|
||||||
|
def extract_risk_factors(organization):
|
||||||
|
excluded_fields={"name","email"}
|
||||||
|
risk_data = {}
|
||||||
|
|
||||||
|
for field in organization._meta.get_fields():
|
||||||
|
if field.name not in excluded_fields and hasattr(organization, field.name):
|
||||||
|
value = getattr(organization, field.name)
|
||||||
|
if value:
|
||||||
|
risk_data[field.name] = value
|
||||||
|
return risk_data
|
||||||
|
|
||||||
|
from openai import OpenAI
|
||||||
|
from django.conf import settings
|
||||||
|
from .models import Risk
|
||||||
|
|
||||||
|
def get_top_risk(organization):
|
||||||
|
client = OpenAI(api_key=settings.OPENAI_API_KEY)
|
||||||
|
|
||||||
|
all_risks = Risk.objects.all()
|
||||||
|
|
||||||
|
risk_list = []
|
||||||
|
for risk in all_risks:
|
||||||
|
risk_list.append(f"""
|
||||||
|
Risk ID: {risk.risk_id}
|
||||||
|
Category: {risk.category}
|
||||||
|
Name: {risk.risk_name}
|
||||||
|
Primary Impact: {risk.primary_impact}
|
||||||
|
""")
|
||||||
|
|
||||||
|
risk_factors = extract_risk_factors(organization)
|
||||||
|
|
||||||
|
prompt = f"""
|
||||||
|
You are an AI risk assessor. Based on the following company details and list of known risks,
|
||||||
|
identify the 10 most critical risks for this company. Respond only with risk IDs.
|
||||||
|
|
||||||
|
Company Details:
|
||||||
|
{risk_factors}
|
||||||
|
|
||||||
|
List of Risks:
|
||||||
|
{risk_list}
|
||||||
|
|
||||||
|
Provide only the 10 most critical risk IDs in a simple comma-separated format, e.g "1,3,7,12,..."
|
||||||
|
"""
|
||||||
|
|
||||||
|
response = client.chat.completions.create(
|
||||||
|
model="gpt-4",
|
||||||
|
messages=[{"role": "system", "content": prompt}]
|
||||||
|
)
|
||||||
|
|
||||||
|
risk_ids = response.choices[0].message.content.strip().split(",")
|
||||||
|
|
||||||
|
return [int(risk_id) for risk_id in risk_ids if risk_id.isdigit()]
|
||||||
@@ -2,7 +2,8 @@ import logging
|
|||||||
|
|
||||||
from django.shortcuts import render, redirect
|
from django.shortcuts import render, redirect
|
||||||
from .forms import OrganizationForm
|
from .forms import OrganizationForm
|
||||||
|
from .models import Organization,Document,Risk
|
||||||
|
from backend.core.utils import get_top_risk
|
||||||
# @login_required
|
# @login_required
|
||||||
# def index(request):
|
# def index(request):
|
||||||
# return HttpResponse('<h1>Django</h1><p>Página simples.</p>')
|
# return HttpResponse('<h1>Django</h1><p>Página simples.</p>')
|
||||||
@@ -19,8 +20,25 @@ def signup(request):
|
|||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
form = OrganizationForm(request.POST)
|
form = OrganizationForm(request.POST)
|
||||||
if form.is_valid():
|
if form.is_valid():
|
||||||
form.save()
|
organization = form.save()
|
||||||
return render(request, 'thankyou.html', {'email': form.data['email']})
|
print(f"Organization :{organization}")
|
||||||
|
top_risk_ids = get_top_risk(organization)
|
||||||
|
print(f"Top risks: {top_risk_ids}")
|
||||||
|
top_risks = Risk.objects.filter(risk_id__in = top_risk_ids)
|
||||||
|
print(f"Final: {top_risks}")
|
||||||
|
|
||||||
|
document = Document.objects.create(organization=organization)
|
||||||
|
document.add_segment('h1', "Top 10 Risk Identified")
|
||||||
|
|
||||||
|
for risk in top_risks:
|
||||||
|
document.add_segment('h2',f"Risk: {risk.risk_id}:{risk.risk_name}")
|
||||||
|
document.add_segment('body',f"Category: {risk.category} \n Primary Impact: {risk.primary_impact} \n Secondary Impact: {risk.secondary_impact} \n Tertiary Impact: {risk.tretiary_impact} \n Detection Difficulty: {risk.detection_difficulty} \n Recovery Complexity: {risk.recovery_complexity} \n Business Impact Severity: {risk.businnes_impact_severity} ")
|
||||||
|
|
||||||
|
return render(request, 'thankyou.html', {
|
||||||
|
'email': form.data['email'],
|
||||||
|
'top_risks':top_risks,
|
||||||
|
'document':document
|
||||||
|
})
|
||||||
else:
|
else:
|
||||||
logging.error(form.errors)
|
logging.error(form.errors)
|
||||||
return render(request, 'signup.html', {'form': form})
|
return render(request, 'signup.html', {'form': form})
|
||||||
|
|||||||
@@ -14,6 +14,12 @@ from pathlib import Path
|
|||||||
|
|
||||||
from decouple import Csv, config
|
from decouple import Csv, config
|
||||||
from dj_database_url import parse as dburl
|
from dj_database_url import parse as dburl
|
||||||
|
import os
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
#API key
|
||||||
|
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
|
|||||||
Reference in New Issue
Block a user