Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
| 2 |
+
import random
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Load the Hugging Face model and tokenizer
|
| 6 |
+
model_name = "EleutherAI/gpt-neo-1.3B" # Replace with your preferred model
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 9 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0)
|
| 10 |
+
|
| 11 |
+
# Predefined random options
|
| 12 |
+
names = ["John Doe", "Jane Smith", "Ali Khan"]
|
| 13 |
+
work_types = ["Pump Maintenance", "Chemical Handling", "Valve Inspection"]
|
| 14 |
+
locations = ["North Sector Valve House", "Central Processing Unit"]
|
| 15 |
+
conclusions = [
|
| 16 |
+
"Spill identified and cleaned; training on PPE compliance conducted.",
|
| 17 |
+
"No major issues observed; team adherence to safety protocols noted.",
|
| 18 |
+
"Minor deviation in housekeeping; corrective action implemented."
|
| 19 |
+
]
|
| 20 |
+
observations = [
|
| 21 |
+
"- Energy sources controlled (Good Practice)",
|
| 22 |
+
"- Leaks contained (Deviation: Minor leakage in Valve 12; action taken to tighten seals).",
|
| 23 |
+
"- Housekeeping standards maintained (Good Practice)."
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
# Random SOC generator
|
| 27 |
+
def generate_random_soc():
|
| 28 |
+
report = f"""
|
| 29 |
+
SAFETY OBSERVATION AND CONVERSATION (SOC) REPORT
|
| 30 |
+
|
| 31 |
+
1. Key Safety Conclusions/Comments/Agreements Made:
|
| 32 |
+
{random.choice(conclusions)}
|
| 33 |
+
|
| 34 |
+
2. Observer's Name:
|
| 35 |
+
{random.choice(names)}
|
| 36 |
+
|
| 37 |
+
3. KOC ID No.:
|
| 38 |
+
{random.randint(100000, 999999)}
|
| 39 |
+
|
| 40 |
+
4. Type of Work Observed:
|
| 41 |
+
{random.choice(work_types)}
|
| 42 |
+
|
| 43 |
+
5. Location:
|
| 44 |
+
{random.choice(locations)}
|
| 45 |
+
|
| 46 |
+
6. Duration:
|
| 47 |
+
{random.randint(30, 120)} minutes
|
| 48 |
+
|
| 49 |
+
7. Plant Observations:
|
| 50 |
+
{random.choice(observations)}
|
| 51 |
+
|
| 52 |
+
8. People Observations:
|
| 53 |
+
- Personnel followed PPE standards.
|
| 54 |
+
- One deviation: Operator missed wearing gloves during chemical handling (Corrective action: Operator was instructed and gloves provided).
|
| 55 |
+
|
| 56 |
+
9. Process Observations:
|
| 57 |
+
- Job safety analysis conducted properly (Good Practice).
|
| 58 |
+
- Improvement needed: Ensure pre-task meetings cover all hazards.
|
| 59 |
+
|
| 60 |
+
10. Performance Observations:
|
| 61 |
+
- Pace of work was efficient and safety prioritized.
|
| 62 |
+
"""
|
| 63 |
+
return report
|
| 64 |
+
|
| 65 |
+
# AI refinement function
|
| 66 |
+
def refine_report(report, instructions):
|
| 67 |
+
prompt = f"Refine the following SOC report based on the instructions provided:\n\nSOC Report:\n{report}\n\nInstructions: {instructions}\n\nRefined Report:"
|
| 68 |
+
outputs = generator(prompt, max_length=512, num_return_sequences=1)
|
| 69 |
+
return outputs[0]["generated_text"]
|
| 70 |
+
|
| 71 |
+
# Gradio Interface
|
| 72 |
+
def app_interface(instructions=None):
|
| 73 |
+
# Generate random SOC report
|
| 74 |
+
random_report = generate_random_soc()
|
| 75 |
+
|
| 76 |
+
if instructions:
|
| 77 |
+
# Refine the report using the Hugging Face model
|
| 78 |
+
refined_report = refine_report(random_report, instructions)
|
| 79 |
+
return refined_report
|
| 80 |
+
else:
|
| 81 |
+
return random_report
|
| 82 |
+
|
| 83 |
+
# Gradio Layout
|
| 84 |
+
with gr.Blocks() as app:
|
| 85 |
+
gr.Markdown("# Safety Observation and Conversation (SOC) Generator")
|
| 86 |
+
gr.Markdown(
|
| 87 |
+
"""
|
| 88 |
+
This app generates random SOC reports for a water injection plant. You can also provide specific instructions to refine the report using AI assistance.
|
| 89 |
+
"""
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
generate_btn = gr.Button("Create Random SOC")
|
| 94 |
+
instructions_box = gr.Textbox(
|
| 95 |
+
label="Refinement Instructions (Optional)",
|
| 96 |
+
placeholder="E.g., Make the report more concise or focus on PPE compliance...",
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
output_box = gr.Textbox(
|
| 100 |
+
label="Generated SOC Report",
|
| 101 |
+
placeholder="Your SOC report will appear here...",
|
| 102 |
+
lines=15
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
with gr.Row():
|
| 106 |
+
refine_btn = gr.Button("Refine Report with AI")
|
| 107 |
+
copy_btn = gr.Button("Copy to Clipboard")
|
| 108 |
+
|
| 109 |
+
# Button Actions
|
| 110 |
+
generate_btn.click(app_interface, inputs=None, outputs=output_box)
|
| 111 |
+
refine_btn.click(app_interface, inputs=instructions_box, outputs=output_box)
|
| 112 |
+
copy_btn.click(lambda text: text, inputs=output_box, outputs=None)
|
| 113 |
+
|
| 114 |
+
# Launch the app
|
| 115 |
+
app.launch()
|