Luca Latini commited on
Commit
7a6eb82
·
1 Parent(s): d2ece60

refactor enrollment rule creator to accept any non-empty text input fields without validation

Browse files
Files changed (1) hide show
  1. app.py +17 -83
app.py CHANGED
@@ -1,94 +1,28 @@
1
  import gradio as gr
2
- from pydantic import BaseModel, Field, field_validator
3
- from enum import Enum
4
- from typing import List
5
 
6
- class Group(str, Enum):
7
- onboarding = "onboarding"
8
- sales_enablement = "sales enablement"
9
- marketing = "marketing"
10
- leadership = "leadership"
11
-
12
- class Course(str, Enum):
13
- company_culture_and_values = "company culture and values"
14
- product_knowledge_and_features = "product knowledge and features"
15
- sales_process_and_methodology = "sales process and methodology"
16
- customer_relationship_management_system_training = "customer relationship management system training"
17
- sales_enablement_strategy_and_planning = "sales enablement strategy and planning"
18
- content_creation_and_curation = "content creation and curation"
19
- sales_coaching_and_mentoring = "sales coaching and mentoring"
20
- sales_metrics_and_analytics = "sales metrics and analytics"
21
- digital_marketing_fundamentals = "digital marketing fundamentals"
22
- content_marketing_strategy = "content marketing strategy"
23
- social_media_marketing = "social media marketing"
24
- search_engine_optimization = "search engine optimization"
25
- leadership_development_and_coaching = "leadership development and coaching"
26
- strategic_planning_and_decision_making = "strategic planning and decision making"
27
- change_management_and_organizational_development = "change management and organizational development"
28
- emotional_intelligence_and_communication_skills = "emotional intelligence and communication skills"
29
-
30
- class EnrollmentRule(BaseModel):
31
- rule_code: str = Field(description="unique identifier code for the rule", default="")
32
- rule_name: str = Field(description="name of the rule", default="")
33
- group: Group = Field(description="group to apply the rule to", default=Group.onboarding)
34
- courses: List[Course] = Field(
35
- description="list of courses that the members of the group will follow",
36
- default_factory=lambda: list(Course)
37
- )
38
-
39
- @field_validator("rule_code")
40
- def rule_code_max_length(cls, rule_code):
41
- if not rule_code.strip():
42
- raise ValueError("The rule code cannot be empty.")
43
- if len(rule_code) > 50:
44
- raise ValueError("The rule code must contain less than 50 characters.")
45
- return rule_code
46
-
47
- @field_validator("rule_name")
48
- def rule_name_max_length(cls, rule_name):
49
- if not rule_name.strip():
50
- raise ValueError("The rule name cannot be empty.")
51
- if len(rule_name) > 255:
52
- raise ValueError("The rule name must contain less than 255 characters.")
53
- return rule_name
54
-
55
- @field_validator("courses")
56
- def course_list_not_empty(cls, courses):
57
- if not courses:
58
- raise ValueError("At least one course must be provided.")
59
- return courses
60
-
61
- def create_enrollment_rule(enrollment_rule: EnrollmentRule) -> dict:
62
- return enrollment_rule.model_dump(mode="json")
63
-
64
- def gr_create_enrollment_rule(rule_code, rule_name, group, courses):
65
- # Convert group and courses from strings to enum values
66
- # Pydantic will handle this automatically, but we need to ensure the raw inputs are enums.
67
- # If the input is directly from the Gradio UI, they will be strings. Pydantic tries to coerce
68
- # them into enum values. If it fails, it will raise a validation error.
69
- enrollment_rule = EnrollmentRule(
70
- rule_code=rule_code,
71
- rule_name=rule_name,
72
- group=Group(group), # Will raise ValueError if invalid
73
- courses=[Course(c) for c in courses] # Will raise ValueError if invalid
74
- )
75
- return create_enrollment_rule(enrollment_rule)
76
-
77
- # Derive the choices from the Enums directly:
78
- GROUPS = [g.value for g in Group]
79
- COURSES = [c.value for c in Course]
80
 
81
  demo = gr.Interface(
82
- fn=gr_create_enrollment_rule,
83
  inputs=[
84
- gr.Textbox(label="Rule Code", placeholder="Enter a unique identifier (max 50 characters)"),
85
- gr.Textbox(label="Rule Name", placeholder="Enter a descriptive name (max 255 characters)"),
86
- gr.Dropdown(label="Group", choices=GROUPS, value=GROUPS[0]),
87
- gr.CheckboxGroup(label="Courses", choices=COURSES, value=[], info="Select one or more courses")
88
  ],
89
  outputs="json",
90
  title="Enrollment Rule Creator",
91
- description="Create an enrollment rule, based on the rule parameters provided by the user",
92
  allow_flagging="never"
93
  )
94
 
 
1
  import gradio as gr
 
 
 
2
 
3
+ def create_enrollment_rule(rule_code: str, rule_name: str, group: str, courses: str) -> dict:
4
+ # Just return the inputs as a dictionary without any validation.
5
+ # Accept any non-empty text as input. Assume courses is a comma-separated string.
6
+ # Convert courses string to a list by splitting on commas (optional).
7
+ courses_list = [c.strip() for c in courses.split(",")] if courses.strip() else []
8
+ return {
9
+ "rule_code": rule_code.strip(),
10
+ "rule_name": rule_name.strip(),
11
+ "group": group.strip(),
12
+ "courses": courses_list
13
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  demo = gr.Interface(
16
+ fn=create_enrollment_rule,
17
  inputs=[
18
+ gr.Textbox(label="Rule Code", placeholder="Unique identifier for the rule, e.g. 'rule_001'"),
19
+ gr.Textbox(label="Rule Name", placeholder="Descriptive name for the rule, e.g. 'New Hire Onboarding'"),
20
+ gr.Textbox(label="Group", placeholder="Group to apply the rule to, e.g. 'Onboarding'"),
21
+ gr.Textbox(label="Courses", placeholder="Courses separated by commas, e.g. 'Company Culture and Values, Product Knowledge and Features'")
22
  ],
23
  outputs="json",
24
  title="Enrollment Rule Creator",
25
+ description="Create an enrollment rule based on the parameters you provide.",
26
  allow_flagging="never"
27
  )
28