Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,12 @@
|
|
1 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
2 |
-
import random
|
3 |
import gradio as gr
|
|
|
4 |
|
5 |
-
#
|
6 |
model_options = {
|
7 |
"distilgpt2": "distilgpt2",
|
8 |
"GPT-Neo 125M": "EleutherAI/gpt-neo-125M",
|
|
|
9 |
}
|
10 |
|
11 |
# Load default model
|
@@ -14,31 +15,22 @@ tokenizer = AutoTokenizer.from_pretrained(default_model_name)
|
|
14 |
model = AutoModelForCausalLM.from_pretrained(default_model_name)
|
15 |
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=-1) # Use CPU
|
16 |
|
17 |
-
#
|
18 |
-
names = ["
|
19 |
-
locations = ["Pump House 1", "Main Valve Station", "Chemical Storage Area"]
|
20 |
work_types = ["Routine pump maintenance", "Valve inspection", "Chemical handling"]
|
|
|
21 |
durations = [30, 45, 60]
|
22 |
-
good_practices = ["Good Practice"]
|
23 |
-
deviations = ["Deviation"]
|
24 |
-
|
25 |
-
plant_observations = [
|
26 |
-
("Energy sources controlled", "Good Practice", "Lockout/tagout procedures were followed."),
|
27 |
-
("Leaks/spills contained", "Deviation", "Oil spill near a pump flagged for cleanup."),
|
28 |
-
("Housekeeping standard high", "Deviation", "Scattered tools were organized after reminder."),
|
29 |
-
]
|
30 |
|
31 |
# Function to set seed
|
32 |
def set_seed(seed_value):
|
33 |
random.seed(seed_value)
|
34 |
|
35 |
# AI-based SOC report generation
|
36 |
-
def generate_soc(model_choice, seed=None):
|
37 |
-
# Set seed if provided
|
38 |
if seed:
|
39 |
set_seed(seed)
|
40 |
|
41 |
-
# Update
|
42 |
global generator
|
43 |
model_name = model_options[model_choice]
|
44 |
if generator.tokenizer.name_or_path != model_name:
|
@@ -46,49 +38,49 @@ def generate_soc(model_choice, seed=None):
|
|
46 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
47 |
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=-1)
|
48 |
|
49 |
-
#
|
50 |
observer_name = random.choice(names)
|
51 |
-
location = random.choice(locations)
|
52 |
work_type = random.choice(work_types)
|
|
|
53 |
duration = random.choice(durations)
|
54 |
|
55 |
-
#
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
|
60 |
|
61 |
-
# AI
|
62 |
prompt = f"""
|
63 |
-
Write a detailed Safety Observation and Conversation (SOC) report for a water injection plant
|
64 |
-
|
65 |
-
Key Safety Conclusions/Comments/Agreements Made:
|
66 |
-
Briefly summarize safety observations, key concerns, and corrective actions.
|
67 |
|
68 |
Observer's Name: {observer_name}
|
69 |
KOC ID No.: [Insert KOC ID here]
|
70 |
Type of Work Observed: {work_type}
|
71 |
Location: {location}
|
72 |
-
Duration
|
73 |
-
|
74 |
-
--- Plant Observations:
|
75 |
-
{observations}
|
76 |
|
77 |
-
|
78 |
-
Include details on PPE compliance, hazard understanding, and good practices or deviations.
|
79 |
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
82 |
|
83 |
-
|
84 |
-
Evaluate the overall safety performance, including work pace and supervision.
|
85 |
"""
|
86 |
-
|
|
|
|
|
87 |
return result
|
88 |
|
89 |
# Gradio Interface
|
90 |
-
def app_interface(model_choice, seed):
|
91 |
-
return generate_soc(model_choice, seed)
|
92 |
|
93 |
# Gradio Layout
|
94 |
with gr.Blocks() as app:
|
@@ -96,17 +88,24 @@ with gr.Blocks() as app:
|
|
96 |
gr.Markdown(
|
97 |
"""
|
98 |
Generate detailed SOC reports for a water injection plant using AI assistance.
|
99 |
-
Customize your report with multiple models,
|
100 |
"""
|
101 |
)
|
102 |
|
103 |
with gr.Row():
|
104 |
model_choice = gr.Dropdown(
|
105 |
-
label="Select Model",
|
106 |
choices=list(model_options.keys()),
|
107 |
value="GPT-Neo 125M",
|
108 |
)
|
109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
|
111 |
output_box = gr.Textbox(
|
112 |
label="Generated SOC Report",
|
@@ -118,7 +117,7 @@ with gr.Blocks() as app:
|
|
118 |
generate_btn = gr.Button("Generate SOC Report")
|
119 |
copy_btn = gr.Button("Copy to Clipboard")
|
120 |
|
121 |
-
generate_btn.click(app_interface, inputs=[model_choice,
|
122 |
copy_btn.click(lambda text: text, inputs=output_box, outputs=None)
|
123 |
|
124 |
# Launch the app
|
|
|
1 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
|
|
2 |
import gradio as gr
|
3 |
+
import random
|
4 |
|
5 |
+
# Predefined model options
|
6 |
model_options = {
|
7 |
"distilgpt2": "distilgpt2",
|
8 |
"GPT-Neo 125M": "EleutherAI/gpt-neo-125M",
|
9 |
+
"GPT-Neo 1.3B": "EleutherAI/gpt-neo-1.3B",
|
10 |
}
|
11 |
|
12 |
# Load default model
|
|
|
15 |
model = AutoModelForCausalLM.from_pretrained(default_model_name)
|
16 |
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=-1) # Use CPU
|
17 |
|
18 |
+
# Random options for observations
|
19 |
+
names = ["WPMPOperator 1", "John Doe", "Ali Khan"]
|
|
|
20 |
work_types = ["Routine pump maintenance", "Valve inspection", "Chemical handling"]
|
21 |
+
locations = ["Water Injection Plant - Pump House 2", "Main Valve Station", "Chemical Storage Area"]
|
22 |
durations = [30, 45, 60]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
# Function to set seed
|
25 |
def set_seed(seed_value):
|
26 |
random.seed(seed_value)
|
27 |
|
28 |
# AI-based SOC report generation
|
29 |
+
def generate_soc(model_choice, severity, seed=None):
|
|
|
30 |
if seed:
|
31 |
set_seed(seed)
|
32 |
|
33 |
+
# Update generator if model choice changes
|
34 |
global generator
|
35 |
model_name = model_options[model_choice]
|
36 |
if generator.tokenizer.name_or_path != model_name:
|
|
|
38 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
39 |
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=-1)
|
40 |
|
41 |
+
# Random selections for the fields
|
42 |
observer_name = random.choice(names)
|
|
|
43 |
work_type = random.choice(work_types)
|
44 |
+
location = random.choice(locations)
|
45 |
duration = random.choice(durations)
|
46 |
|
47 |
+
# Adjust tone based on severity slider
|
48 |
+
severity_description = {
|
49 |
+
1: "minor concerns and deviations were observed.",
|
50 |
+
2: "moderate concerns requiring immediate attention were identified.",
|
51 |
+
3: "serious safety concerns were flagged for urgent corrective action."
|
52 |
+
}
|
53 |
+
severity_text = severity_description.get(severity, "moderate concerns requiring attention.")
|
54 |
|
55 |
+
# AI prompt
|
56 |
prompt = f"""
|
57 |
+
Write a detailed Safety Observation and Conversation (SOC) report for a water injection plant with the following details:
|
|
|
|
|
|
|
58 |
|
59 |
Observer's Name: {observer_name}
|
60 |
KOC ID No.: [Insert KOC ID here]
|
61 |
Type of Work Observed: {work_type}
|
62 |
Location: {location}
|
63 |
+
Duration: {duration} minutes
|
|
|
|
|
|
|
64 |
|
65 |
+
Severity Level: {severity_text}
|
|
|
66 |
|
67 |
+
The report should include:
|
68 |
+
- Key safety conclusions, concerns, and corrective actions taken.
|
69 |
+
- Plant observations (e.g., energy control, housekeeping) marked as Good Practice or Deviation with comments.
|
70 |
+
- People observations (e.g., PPE compliance, hazard understanding).
|
71 |
+
- Process observations (e.g., job safety analysis, procedures).
|
72 |
+
- Performance observations (e.g., pace, supervision, and safety prioritization).
|
73 |
|
74 |
+
Format the output neatly in sections, and ensure it is professional and actionable.
|
|
|
75 |
"""
|
76 |
+
|
77 |
+
# Generate report using the selected model
|
78 |
+
result = generator(prompt, max_length=1024, num_return_sequences=1)[0]["generated_text"]
|
79 |
return result
|
80 |
|
81 |
# Gradio Interface
|
82 |
+
def app_interface(model_choice, severity, seed=None):
|
83 |
+
return generate_soc(model_choice, severity, seed)
|
84 |
|
85 |
# Gradio Layout
|
86 |
with gr.Blocks() as app:
|
|
|
88 |
gr.Markdown(
|
89 |
"""
|
90 |
Generate detailed SOC reports for a water injection plant using AI assistance.
|
91 |
+
Customize your report with multiple AI models, severity levels, and reproducibility using seeds.
|
92 |
"""
|
93 |
)
|
94 |
|
95 |
with gr.Row():
|
96 |
model_choice = gr.Dropdown(
|
97 |
+
label="Select AI Model",
|
98 |
choices=list(model_options.keys()),
|
99 |
value="GPT-Neo 125M",
|
100 |
)
|
101 |
+
severity_slider = gr.Slider(
|
102 |
+
label="Severity of SOC Report",
|
103 |
+
minimum=1,
|
104 |
+
maximum=3,
|
105 |
+
step=1,
|
106 |
+
value=2,
|
107 |
+
)
|
108 |
+
seed_input = gr.Number(label="Seed (Optional)", value=None, precision=0)
|
109 |
|
110 |
output_box = gr.Textbox(
|
111 |
label="Generated SOC Report",
|
|
|
117 |
generate_btn = gr.Button("Generate SOC Report")
|
118 |
copy_btn = gr.Button("Copy to Clipboard")
|
119 |
|
120 |
+
generate_btn.click(app_interface, inputs=[model_choice, severity_slider, seed_input], outputs=output_box)
|
121 |
copy_btn.click(lambda text: text, inputs=output_box, outputs=None)
|
122 |
|
123 |
# Launch the app
|