Oranblock commited on
Commit
2567e33
·
verified ·
1 Parent(s): 8160119

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -75
app.py CHANGED
@@ -1,114 +1,73 @@
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=-1) # Use CPU
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
 
1
  from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
 
2
  import gradio as gr
3
 
4
  # Load the Hugging Face model and tokenizer
5
  model_name = "EleutherAI/gpt-neo-1.3B" # Replace with your preferred model
6
  tokenizer = AutoTokenizer.from_pretrained(model_name)
7
  model = AutoModelForCausalLM.from_pretrained(model_name)
8
+ generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=-1) # Use CPU for Hugging Face Spaces
9
+
10
+ # AI-based SOC report generation
11
+ def generate_soc():
12
+ prompt = """
13
+ Write a detailed Safety Observation and Conversation (SOC) report for a water injection plant. The report should include:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  1. Key Safety Conclusions/Comments/Agreements Made:
16
+ A brief summary of the observations, highlighting the main safety concerns and any corrective actions taken.
17
 
18
  2. Observer's Name:
19
+ Provide a placeholder for the observer's name.
20
 
21
  3. KOC ID No.:
22
+ Include a placeholder for the observer's ID.
23
 
24
  4. Type of Work Observed:
25
+ Specify the activity being observed (e.g., pump maintenance, valve inspection, or chemical handling).
26
 
27
  5. Location:
28
+ Mention the location within the plant where the observation occurred.
29
 
30
+ 6. Duration (in mins):
31
+ Provide a placeholder for the time spent observing.
32
 
33
+ --- Plant Observations:
34
+ Include detailed observations such as energy sources control, housekeeping, and spill containment. Each observation should be marked as Good Practice or Deviation with explanations for deviations.
35
 
36
+ --- People Observations:
37
+ Include details of personnel behavior, PPE compliance, hazard understanding, and examples of Good Practices or Deviations with corrective actions.
 
38
 
39
+ --- Process Observations:
40
+ Include observations on job safety analysis, procedures followed, control of work standards, and improvements needed.
 
41
 
42
+ --- Performance Observations:
43
+ Evaluate the overall safety performance, including work pace, supervision, and prioritization of safety.
 
 
44
 
45
+ Format the output neatly with headers for each section and placeholders where needed.
46
+ """
47
+ result = generator(prompt, max_length=1024, num_return_sequences=1)[0]["generated_text"]
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
+ This app generates detailed SOC reports for a water injection plant. AI assists in creating observations, including conclusions, corrective actions, and key performance evaluations.
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
+ generate_btn.click(app_interface, outputs=output_box)
 
 
 
 
71
  copy_btn.click(lambda text: text, inputs=output_box, outputs=None)
72
 
73
  # Launch the app