Update app.py
Browse files
app.py
CHANGED
@@ -1,73 +1,124 @@
|
|
1 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
|
|
2 |
import gradio as gr
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# AI-based SOC report generation
|
11 |
-
def generate_soc():
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
32 |
|
33 |
--- Plant Observations:
|
34 |
-
|
35 |
|
36 |
--- People Observations:
|
37 |
-
Include details
|
38 |
|
39 |
--- Process Observations:
|
40 |
-
|
41 |
|
42 |
--- Performance Observations:
|
43 |
-
Evaluate the overall safety performance, including work pace
|
44 |
-
|
45 |
-
Format the output neatly with headers for each section and placeholders where needed.
|
46 |
"""
|
47 |
-
result = generator(prompt, max_length=
|
48 |
return result
|
49 |
|
50 |
# Gradio Interface
|
51 |
-
def app_interface():
|
52 |
-
return generate_soc()
|
53 |
|
54 |
# Gradio Layout
|
55 |
with gr.Blocks() as app:
|
56 |
gr.Markdown("# AI-Generated Safety Observation and Conversation (SOC) Reports")
|
57 |
gr.Markdown(
|
58 |
"""
|
59 |
-
|
|
|
60 |
"""
|
61 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
output_box = gr.Textbox(
|
63 |
label="Generated SOC Report",
|
64 |
placeholder="Your SOC report will appear here...",
|
65 |
lines=30,
|
66 |
)
|
67 |
-
generate_btn = gr.Button("Generate SOC Report")
|
68 |
-
copy_btn = gr.Button("Copy to Clipboard")
|
69 |
|
70 |
-
|
|
|
|
|
|
|
|
|
71 |
copy_btn.click(lambda text: text, inputs=output_box, outputs=None)
|
72 |
|
73 |
# Launch the app
|
|
|
1 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
2 |
+
import random
|
3 |
import gradio as gr
|
4 |
|
5 |
+
# Model options
|
6 |
+
model_options = {
|
7 |
+
"distilgpt2": "distilgpt2",
|
8 |
+
"GPT-Neo 125M": "EleutherAI/gpt-neo-125M",
|
9 |
+
}
|
10 |
+
|
11 |
+
# Load default model
|
12 |
+
default_model_name = model_options["GPT-Neo 125M"]
|
13 |
+
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 |
+
# Predefined options for randomization
|
18 |
+
names = ["John Doe", "Jane Smith", "Ali Khan"]
|
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 the generator if model_choice changes
|
42 |
+
global generator
|
43 |
+
model_name = model_options[model_choice]
|
44 |
+
if generator.tokenizer.name_or_path != model_name:
|
45 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
46 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
47 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=-1)
|
48 |
+
|
49 |
+
# Randomized fields
|
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 |
+
# Generate random plant observations
|
56 |
+
observations = "\n".join(
|
57 |
+
f"{i+1}. {obs[0]}\n{obs[1]}\n{obs[2]}"
|
58 |
+
for i, obs in enumerate(random.sample(plant_observations, len(plant_observations)))
|
59 |
+
)
|
60 |
|
61 |
+
# AI Prompt
|
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 (in mins): {duration}
|
73 |
|
74 |
--- Plant Observations:
|
75 |
+
{observations}
|
76 |
|
77 |
--- People Observations:
|
78 |
+
Include details on PPE compliance, hazard understanding, and good practices or deviations.
|
79 |
|
80 |
--- Process Observations:
|
81 |
+
Summarize job safety analysis, procedures followed, and improvements needed.
|
82 |
|
83 |
--- Performance Observations:
|
84 |
+
Evaluate the overall safety performance, including work pace and supervision.
|
|
|
|
|
85 |
"""
|
86 |
+
result = generator(prompt, max_length=512, num_return_sequences=1)[0]["generated_text"]
|
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:
|
95 |
gr.Markdown("# AI-Generated Safety Observation and Conversation (SOC) Reports")
|
96 |
gr.Markdown(
|
97 |
"""
|
98 |
+
Generate detailed SOC reports for a water injection plant using AI assistance.
|
99 |
+
Customize your report with multiple models, randomization, and reproducibility through seeds.
|
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 |
+
seed = gr.Number(label="Seed (Optional)", value=None, precision=0)
|
110 |
+
|
111 |
output_box = gr.Textbox(
|
112 |
label="Generated SOC Report",
|
113 |
placeholder="Your SOC report will appear here...",
|
114 |
lines=30,
|
115 |
)
|
|
|
|
|
116 |
|
117 |
+
with gr.Row():
|
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, seed], outputs=output_box)
|
122 |
copy_btn.click(lambda text: text, inputs=output_box, outputs=None)
|
123 |
|
124 |
# Launch the app
|