Download app.py from andydumbell/Notify_Technology_Safety_Copilot_Chat_POC: direct link, hf CLI and curl.
- Browser
- Download file 9.75 kB
-
https://huggingface.co/spaces/andydumbell/Notify_Technology_Safety_Copilot_Chat_POC/resolve/main/app.py
- Command line
-
hf download hf://spaces/andydumbell/Notify_Technology_Safety_Copilot_Chat_POC/app.py
-
curl -L -o app.py https://huggingface.co/spaces/andydumbell/Notify_Technology_Safety_Copilot_Chat_POC/resolve/main/app.py
9.75 kB
| from typing import Iterable | |
| from dotenv import load_dotenv | |
| from openai import OpenAI | |
| import json | |
| import os | |
| import requests | |
| from pypdf import PdfReader | |
| import gradio as gr | |
| from gradio.themes.base import Base | |
| from gradio.themes.utils import colors, fonts, sizes | |
| from agents import Agent, Runner, trace, function_tool | |
| import asyncio | |
| # Help me create a risk assessment for a FLT | |
| load_dotenv(override=True) | |
| def push(text): | |
| requests.post( | |
| "https://api.pushover.net/1/messages.json", | |
| data={ | |
| "token": os.getenv("PUSHOVER_TOKEN"), | |
| "user": os.getenv("PUSHOVER_USER"), | |
| "message": text, | |
| } | |
| ) | |
| def record_prospect_details(email: str, name: str="Name not provided", notes: str="not provided"): | |
| """Record a prospects interest is hearing more about Notify Technology and its products and services""" | |
| push(f"Recording {name} with email {email} and notes {notes}") | |
| return {"recorded": "ok"} | |
| def record_unknown_question(question: str): | |
| """Always use this tool to record any question that couldn't be answered as you didn't know the answer""" | |
| push(f"Recording {question}") | |
| return {"recorded": "ok"} | |
| class SafetyCopilotChat: | |
| def __init__(self): | |
| # self.openai = OpenAI() | |
| # self.name = "Safety Copilot by Notify Technology" | |
| self.system_prompt = """## System Prompt: Health and Safety Assistant (with Notify Integration) | |
| You are a helpful, knowledgeable Health and Safety Assistant. | |
| You support users by answering questions and providing guidance related to workplace health and safety. | |
| ### Your responses must: | |
| - Be accurate, practical, and based on best practice (UK legislation and guidance unless otherwise specified). | |
| - Use plain English and avoid legal jargon unless necessary. | |
| - Be clear if the answer is advisory, best practice, or a legal requirement. | |
| - Signpost to relevant legislation, HSE guidance, or company policies when helpful. | |
| - Be cautious not to give legal advice, only general information. | |
| - Where appropriate to give a better example or guidance, you need to ask one or more follow up questions to get the information you need. | |
| - Anticipate the user's next likely questions or needs, and weave this into your response to prompt them to continue the conversation. | |
| - Whenever relevant, recommend a specific **Notify Technology** tool or solution that matches the user’s need. | |
| - If no specific tool matches, direct them to: [https://www.notifytechnology.com](https://www.notifytechnology.com) | |
| ### Tone of voice: | |
| - Friendly and professional | |
| - Reassuring and supportive | |
| - Confident but not overly formal | |
| Assume the user is a worker, team leader, or H&S rep who wants quick, reliable help. | |
| --- | |
| ### Common Topics + Notify Solutions | |
| | Topic / Problem | Suggested Notify Tool | URL | | |
| |------------------|---------------------------|-----| | |
| | Reporting an incident, near miss or injury | **Incident Management Module** | [notifytechnology.com/incident-management](https://www.notifytechnology.com/solutions/incident-management-incident-reporting-software/) | | |
| | Completing workplace audits or inspections | **Audits and Inspections App** | [notifytechnology.com/audit-management](https://www.notifytechnology.com/solutions/audit-management-software/) | | |
| | Managing workplace risks | **Risk Assessment Module** | [notifytechnology.com/risk-assessment](https://www.notifytechnology.com/solutions/risk-assessment-software/) | | |
| | Assigning and tracking corrective actions | **Action Tracking Software** | [notifytechnology.com/action-tracking](https://www.notifytechnology.com/solutions/action-tracking/) | | |
| | Storing and distributing safety documents | **Document Management System** | [notifytechnology.com/document-management](https://www.notifytechnology.com/solutions/document-management-software/) | | |
| | Managing equipment inspections | **Asset Inspection App** | [notifytechnology.com/asset-inspection](https://www.notifytechnology.com/solutions/asset-inspection-app/) | | |
| | Understanding safety trends | **Health & Safety Dashboards / Analytics** | [notifytechnology.com/ehs-dashboard](https://www.notifytechnology.com/solutions/ehs-dashboard/) | | |
| | Creating and sharing Toolbox Talks | **Toolbox Talk Generator** *(Coming Soon)* | Suggest contacting Notify for a demo | | |
| | Supporting health & safety culture | **Behavioural Safety Tools** | [notifytechnology.com/behavioural-safety](https://www.notifytechnology.com/solutions/behavioural-safety-software/) | | |
| | Reporting positive safety behaviours | **Positive Observations Reporting** | [notifytechnology.com/incident-management](https://www.notifytechnology.com/solutions/incident-management-incident-reporting-software/) | | |
| --- | |
| ### If there’s no clear match: | |
| > “Notify Technology offers a full suite of safety management tools that could support you. You can explore more at [https://www.notifytechnology.com](https://www.notifytechnology.com).” | |
| ### At the right time, you should ask the user if they'd like to book a demo of a Notify Technology solution: | |
| > “I can book a demo for you. Would you like me to do that?” | |
| > Ask for their name and email address to book the demo. | |
| ### Important: | |
| - Always use British English spelling and grammar. | |
| - Check your spelling and grammar before responding. | |
| - If you don't know the answer, say so. Don't make up an answer! | |
| """ | |
| self.tools = [record_prospect_details, record_unknown_question] | |
| self.agent = Agent( | |
| name="Safety Copilot by Notify Technology", | |
| model="gpt-4o-mini", | |
| instructions=self.system_prompt, | |
| tools=self.tools | |
| ) | |
| async def chat_async(self, message, history): | |
| # print('***') | |
| # print(history) | |
| # print('***') | |
| # Only keep 'role' and 'content' from history | |
| cleaned_history = [{"role": m["role"], "content": m["content"]} for m in history if "role" in m and "content" in m] | |
| messages = [{"role": "system", "content": self.system_prompt}] + cleaned_history + [{"role": "user", "content": message}] | |
| results = await Runner.run(self.agent, messages) | |
| # print('***') | |
| # print(results.final_output) | |
| # print('***') | |
| return results.final_output | |
| class NotifyTheme(Base): | |
| def __init__( | |
| self, | |
| *, | |
| primary_hue: colors.Color | str = colors.blue, # Use Gradio's blue as base | |
| secondary_hue: colors.Color | str = colors.orange, # Orange for accents | |
| neutral_hue: colors.Color | str = colors.slate, | |
| spacing_size: sizes.Size | str = sizes.spacing_md, | |
| radius_size: sizes.Size | str = sizes.radius_md, | |
| text_size: sizes.Size | str = sizes.text_md, | |
| font: fonts.Font | str | Iterable[fonts.Font | str] = ( | |
| fonts.GoogleFont("Inter"), # Clean, modern font similar to Notify | |
| "ui-sans-serif", | |
| "system-ui", | |
| "sans-serif", | |
| ), | |
| font_mono: fonts.Font | str | Iterable[fonts.Font | str] = ( | |
| fonts.GoogleFont("JetBrains Mono"), | |
| "ui-monospace", | |
| "Consolas", | |
| "monospace", | |
| ), | |
| ): | |
| super().__init__( | |
| primary_hue=primary_hue, | |
| secondary_hue=secondary_hue, | |
| neutral_hue=neutral_hue, | |
| spacing_size=spacing_size, | |
| radius_size=radius_size, | |
| text_size=text_size, | |
| font=font, | |
| font_mono=font_mono, | |
| ) | |
| # Custom color overrides to match Notify's palette | |
| super().set( | |
| # Primary colors (navy blue theme) | |
| button_primary_background_fill="*primary_700", | |
| button_primary_background_fill_hover="*primary_600", | |
| button_primary_background_fill_dark="*primary_800", | |
| button_primary_text_color="white", | |
| # Secondary/accent colors (orange for highlights) | |
| button_secondary_background_fill="*orange_500", | |
| button_secondary_background_fill_hover="*orange_600", | |
| button_secondary_text_color="white", | |
| # Background colors | |
| background_fill_primary="*neutral_50", | |
| background_fill_secondary="*neutral_100", | |
| # Input field styling | |
| input_background_fill="white", | |
| input_background_fill_focus="*neutral_50", | |
| input_border_color="*neutral_300", | |
| input_border_color_focus="*primary_500", | |
| # Text colors | |
| body_text_color="*neutral_700", | |
| body_text_color_subdued="*neutral_500", | |
| # Block styling | |
| block_background_fill="white", | |
| block_border_color="*neutral_200", | |
| block_border_width="1px", | |
| block_radius="8px", | |
| # Layout spacing | |
| layout_gap="16px", | |
| ) | |
| if __name__ == "__main__": | |
| safetyCopilotChat = SafetyCopilotChat() | |
| # gr.ChatInterface(me.chat, type="messages").launch() | |
| theme = NotifyTheme() | |
| def chat_wrapper_async(message, history): | |
| if not history: | |
| history = [{"role": "assistant", "content": "👋 Welcome to Safety Copilot by Notify Technology!\n\nI can help you with workplace health and safety guidance, incident reporting, risk assessments, and more.\n\nHow can I support you today?"}] | |
| return asyncio.run(safetyCopilotChat.chat_async(message, history)) | |
| gr.ChatInterface( | |
| fn=chat_wrapper_async, | |
| type="messages", | |
| title="Safety Copilot", | |
| description="Ask me anything about health and safety. Powered by Notify Technology.", | |
| theme=theme | |
| ).launch() | |