test / app.py
Luca Latini
Refactor EnrollmentRule handling of GROUPS and COURSES
d2ece60
raw
history blame
4.39 kB
import gradio as gr
from pydantic import BaseModel, Field, field_validator
from enum import Enum
from typing import List
class Group(str, Enum):
onboarding = "onboarding"
sales_enablement = "sales enablement"
marketing = "marketing"
leadership = "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"
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_factory=lambda: list(Course)
)
@field_validator("rule_code")
def rule_code_max_length(cls, rule_code):
if not rule_code.strip():
raise ValueError("The rule code cannot be empty.")
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):
if not rule_name.strip():
raise ValueError("The rule name cannot be empty.")
if len(rule_name) > 255:
raise ValueError("The rule name must contain less than 255 characters.")
return rule_name
@field_validator("courses")
def course_list_not_empty(cls, courses):
if not courses:
raise ValueError("At least one course must be provided.")
return courses
def create_enrollment_rule(enrollment_rule: EnrollmentRule) -> dict:
return enrollment_rule.model_dump(mode="json")
def gr_create_enrollment_rule(rule_code, rule_name, group, courses):
# Convert group and courses from strings to enum values
# Pydantic will handle this automatically, but we need to ensure the raw inputs are enums.
# If the input is directly from the Gradio UI, they will be strings. Pydantic tries to coerce
# them into enum values. If it fails, it will raise a validation error.
enrollment_rule = EnrollmentRule(
rule_code=rule_code,
rule_name=rule_name,
group=Group(group), # Will raise ValueError if invalid
courses=[Course(c) for c in courses] # Will raise ValueError if invalid
)
return create_enrollment_rule(enrollment_rule)
# Derive the choices from the Enums directly:
GROUPS = [g.value for g in Group]
COURSES = [c.value for c in Course]
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()