Spaces:
Sleeping
Sleeping
import streamlit as st | |
from transformers import pipeline | |
from fpdf import FPDF | |
import datetime | |
# Risk matrix mapping | |
RISK_MATRIX = { | |
(1, 1): "Low", | |
(1, 2): "Low", | |
(1, 3): "Moderate", | |
(1, 4): "High", | |
(1, 5): "Critical", | |
(2, 1): "Low", | |
(2, 2): "Moderate", | |
(2, 3): "Moderate", | |
(2, 4): "High", | |
(2, 5): "Critical", | |
(3, 1): "Moderate", | |
(3, 2): "Moderate", | |
(3, 3): "High", | |
(3, 4): "High", | |
(3, 5): "Critical", | |
(4, 1): "High", | |
(4, 2): "High", | |
(4, 3): "High", | |
(4, 4): "Critical", | |
(4, 5): "Critical", | |
(5, 1): "Critical", | |
(5, 2): "Critical", | |
(5, 3): "Critical", | |
(5, 4): "Critical", | |
(5, 5): "Critical", | |
} | |
# Load the text classification model | |
def load_model(): | |
return pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion") # Example model | |
hazard_analyzer = load_model() | |
# Function to generate PDF report | |
def generate_pdf(description, result, risk_levels): | |
pdf = FPDF() | |
pdf.add_page() | |
pdf.set_font("Arial", size=12) | |
# Title and date | |
pdf.cell(200, 10, txt="AI Safety Hazard Identifier Report", ln=True, align='C') | |
pdf.ln(10) | |
pdf.cell(200, 10, txt=f"Date: {datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')}", ln=True) | |
pdf.ln(10) | |
# Workplace description | |
pdf.set_font("Arial", size=10) | |
pdf.multi_cell(0, 10, txt=f"Description: {description}") | |
pdf.ln(5) | |
# Analysis results | |
pdf.cell(0, 10, txt="Analysis Results:") | |
for res, risk in zip(result, risk_levels): | |
pdf.ln(5) | |
pdf.cell(0, 10, txt=f"- {res['label']} ({risk['risk_level']} Risk): {res['score']:.2f}") | |
return pdf | |
# Streamlit app layout | |
st.title("AI Safety Hazard Identifier with Risk Matrix") | |
st.write("Identify potential workplace hazards and assess risk levels based on a risk matrix.") | |
# Input section | |
description = st.text_area("Enter a description of the workplace situation:", height=150) | |
if st.button("Analyze"): | |
if not description.strip(): | |
st.error("Please provide a description.") | |
else: | |
st.write("Analyzing the description...") | |
result = hazard_analyzer(description) | |
# Risk Matrix Assessment | |
st.write("### Identified Hazards and Risk Levels:") | |
risk_levels = [] | |
for res in result: | |
st.write(f"**{res['label']}**: {res['score']:.2f}") | |
likelihood = st.slider( | |
f"Likelihood for {res['label']} (1: Rare, 5: Almost Certain):", | |
1, 5, 3 | |
) | |
severity = st.slider( | |
f"Severity for {res['label']} (1: Minor, 5: Catastrophic):", | |
1, 5, 3 | |
) | |
risk_level = RISK_MATRIX[(likelihood, severity)] | |
risk_levels.append({"label": res["label"], "risk_level": risk_level}) | |
st.write(f"**Risk Level**: {risk_level}") | |
# Generate and offer PDF report | |
if st.button("Generate Report"): | |
pdf = generate_pdf(description, result, risk_levels) | |
pdf_file_path = "hazard_report_with_risk.pdf" | |
pdf.output(pdf_file_path) | |
with open(pdf_file_path, "rb") as file: | |
st.download_button(label="Download Report", data=file, file_name="hazard_report_with_risk.pdf") | |
st.write("---") | |
st.write("Developed with Kamran using Streamlit and Hugging Face.") | |