test / app.py
Luca Latini
replace with create enrollment rule
81f7e58
raw
history blame
5.98 kB
import gradio as gr
import pydantic
import random
from pydantic import Field, field_validator, BaseModel
from enum import Enum
from typing import List
class Group(str, Enum):
onboarding = "onboarding"
sales_enablement = "sales enablement"
marketing = "marketing"
leadership = "leadership"
GROUPS = ["onboarding", "sales enablement", "marketing", "leadership"]
class Course(str, Enum):
company_culture_and_values = "company culture and values"
product_knowledge_and_features = "product knowledge and features"
sales_process_and_methodology = "sales process and methodology"
customer_relationship_management_system_training = "customer relationship management system training"
sales_enablement_strategy_and_planning = "sales enablement strategy and planning"
content_creation_and_curation = "content creation and curation"
sales_coaching_and_mentoring = "sales coaching and mentoring"
sales_metrics_and_analytics = "sales metrics and analytics"
digital_marketing_fundamentals = "digital marketing fundamentals"
content_marketing_strategy = "content marketing strategy"
social_media_marketing = "social media marketing"
search_engine_optimization = "search engine optimization"
leadership_development_and_coaching = "leadership development and coaching"
strategic_planning_and_decision_making = "strategic planning and decision making"
change_management_and_organizational_development = "change management and organizational development"
emotional_intelligence_and_communication_skills = "emotional intelligence and communication skills"
COURSES = ["company culture and values",
"product knowledge and features",
"sales process and methodology",
"customer relationship management system training",
"sales enablement strategy and planning",
"content creation and curation",
"sales coaching and mentoring",
"sales metrics and analytics",
"digital marketing fundamentals",
"content marketing strategy",
"social media marketing",
"search engine optimization",
"leadership development and coaching",
"strategic planning and decision making",
"change management and organizational development",
"emotional intelligence and communication skills"
]
class EnrollmentRule(BaseModel):
rule_code: str = Field(description="unique identifier code for the rule", default="")
rule_name: str = Field(description="name of the rule", default="")
group: Group = Field(description="group to apply the rule to", default=Group.onboarding)
courses: List[Course] = Field(description="list of courses that the members of the group will follow", default=[course.value for course in Course])
@field_validator("rule_code")
def rule_code_max_length(cls, rule_code):
# Check if empty
if not rule_code.strip():
raise ValueError("The rule code cannot be empty.")
# Check length
if len(rule_code) > 50:
raise ValueError("The rule code must contain less than 50 characters.")
return rule_code
@field_validator("rule_name")
def rule_name_max_length(cls, rule_name):
# Check if empty
if not rule_name.strip():
raise ValueError("The rule name cannot be empty.")
# Check length
if len(rule_name) > 255:
raise ValueError("The rule name must contain less than 255 characters.")
return rule_name
@field_validator("group")
def group_is_valid(cls, group):
# Check if empty
if not group or not group.strip():
raise ValueError("Group cannot be empty.")
# Validate group value
if group.lower().strip() not in GROUPS:
raise ValueError(f"group '{group}' is not a valid value for field 'group'. 'group' must be one of the following: '{', '.join(GROUPS)}'")
return group
@field_validator("courses")
def course_list_is_valid(cls, courses):
# Ensure at least one course is selected
if not courses:
raise ValueError("At least one course must be provided.")
# Validate all listed courses
invalid_courses = []
for course in courses:
if course.lower().strip() not in COURSES:
invalid_courses.append(course)
if invalid_courses:
raise ValueError(f"course(s) '{', '.join(invalid_courses)}' is not a valid value for field 'courses'. 'course' must be one or more of the following: '{', '.join(COURSES)}'")
return courses
def create_enrollment_rule(enrollment_rule: EnrollmentRule) -> dict:
"""Create an enrollment rule, based on the rule parameters provided by the user"""
payload = enrollment_rule.model_dump(mode="json")
return payload
# A small Gradio-friendly wrapper that converts user inputs into an EnrollmentRule
def gr_create_enrollment_rule(rule_code, rule_name, group, courses):
# Build the Pydantic model from the raw inputs
enrollment_rule = EnrollmentRule(
rule_code=rule_code,
rule_name=rule_name,
group=group,
courses=courses
)
return create_enrollment_rule(enrollment_rule)
# Define the Gradio interface
demo = gr.Interface(
fn=gr_create_enrollment_rule,
inputs=[
gr.Textbox(label="Rule Code", placeholder="Enter a unique identifier (max 50 characters)"),
gr.Textbox(label="Rule Name", placeholder="Enter a descriptive name (max 255 characters)"),
gr.Dropdown(label="Group", choices=GROUPS, value=GROUPS[0]),
gr.CheckboxGroup(label="Courses", choices=COURSES, value=[], info="Select one or more courses")
],
outputs="json",
title="Enrollment Rule Creator",
description="Create an enrollment rule, based on the rule parameters provided by the user",
allow_flagging="never"
)
demo.launch()