Luca Latini commited on
Commit
81f7e58
·
1 Parent(s): 0b5661c

replace with create enrollment rule

Browse files
Files changed (2) hide show
  1. app.py +128 -150
  2. requirements.txt +1 -6
app.py CHANGED
@@ -1,154 +1,132 @@
1
  import gradio as gr
2
- import numpy as np
3
  import random
4
 
5
- # import spaces #[uncomment to use ZeroGPU]
6
- from diffusers import DiffusionPipeline
7
- import torch
8
-
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
11
-
12
- if torch.cuda.is_available():
13
- torch_dtype = torch.float16
14
- else:
15
- torch_dtype = torch.float32
16
-
17
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
- pipe = pipe.to(device)
19
-
20
- MAX_SEED = np.iinfo(np.int32).max
21
- MAX_IMAGE_SIZE = 1024
22
-
23
-
24
- # @spaces.GPU #[uncomment to use ZeroGPU]
25
- def infer(
26
- prompt,
27
- negative_prompt,
28
- seed,
29
- randomize_seed,
30
- width,
31
- height,
32
- guidance_scale,
33
- num_inference_steps,
34
- progress=gr.Progress(track_tqdm=True),
35
- ):
36
- if randomize_seed:
37
- seed = random.randint(0, MAX_SEED)
38
-
39
- generator = torch.Generator().manual_seed(seed)
40
-
41
- image = pipe(
42
- prompt=prompt,
43
- negative_prompt=negative_prompt,
44
- guidance_scale=guidance_scale,
45
- num_inference_steps=num_inference_steps,
46
- width=width,
47
- height=height,
48
- generator=generator,
49
- ).images[0]
50
-
51
- return image, seed
52
-
53
-
54
- examples = [
55
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
56
- "An astronaut riding a green horse",
57
- "A delicious ceviche cheesecake slice",
58
- ]
59
-
60
- css = """
61
- #col-container {
62
- margin: 0 auto;
63
- max-width: 640px;
64
- }
65
- """
66
-
67
- with gr.Blocks(css=css) as demo:
68
- with gr.Column(elem_id="col-container"):
69
- gr.Markdown(" # Text-to-Image Gradio Template")
70
-
71
- with gr.Row():
72
- prompt = gr.Text(
73
- label="Prompt",
74
- show_label=False,
75
- max_lines=1,
76
- placeholder="Enter your prompt",
77
- container=False,
78
- )
79
-
80
- run_button = gr.Button("Run", scale=0, variant="primary")
81
-
82
- result = gr.Image(label="Result", show_label=False)
83
-
84
- with gr.Accordion("Advanced Settings", open=False):
85
- negative_prompt = gr.Text(
86
- label="Negative prompt",
87
- max_lines=1,
88
- placeholder="Enter a negative prompt",
89
- visible=False,
90
- )
91
-
92
- seed = gr.Slider(
93
- label="Seed",
94
- minimum=0,
95
- maximum=MAX_SEED,
96
- step=1,
97
- value=0,
98
- )
99
-
100
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
101
-
102
- with gr.Row():
103
- width = gr.Slider(
104
- label="Width",
105
- minimum=256,
106
- maximum=MAX_IMAGE_SIZE,
107
- step=32,
108
- value=1024, # Replace with defaults that work for your model
109
- )
110
-
111
- height = gr.Slider(
112
- label="Height",
113
- minimum=256,
114
- maximum=MAX_IMAGE_SIZE,
115
- step=32,
116
- value=1024, # Replace with defaults that work for your model
117
- )
118
-
119
- with gr.Row():
120
- guidance_scale = gr.Slider(
121
- label="Guidance scale",
122
- minimum=0.0,
123
- maximum=10.0,
124
- step=0.1,
125
- value=0.0, # Replace with defaults that work for your model
126
- )
127
-
128
- num_inference_steps = gr.Slider(
129
- label="Number of inference steps",
130
- minimum=1,
131
- maximum=50,
132
- step=1,
133
- value=2, # Replace with defaults that work for your model
134
- )
135
-
136
- gr.Examples(examples=examples, inputs=[prompt])
137
- gr.on(
138
- triggers=[run_button.click, prompt.submit],
139
- fn=infer,
140
- inputs=[
141
- prompt,
142
- negative_prompt,
143
- seed,
144
- randomize_seed,
145
- width,
146
- height,
147
- guidance_scale,
148
- num_inference_steps,
149
- ],
150
- outputs=[result, seed],
151
  )
152
-
153
- if __name__ == "__main__":
154
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ import pydantic
3
  import random
4
 
5
+ from pydantic import Field, field_validator, BaseModel
6
+ from enum import Enum
7
+ from typing import List
8
+
9
+ class Group(str, Enum):
10
+ onboarding = "onboarding"
11
+ sales_enablement = "sales enablement"
12
+ marketing = "marketing"
13
+ leadership = "leadership"
14
+
15
+ GROUPS = ["onboarding", "sales enablement", "marketing", "leadership"]
16
+
17
+ class Course(str, Enum):
18
+ company_culture_and_values = "company culture and values"
19
+ product_knowledge_and_features = "product knowledge and features"
20
+ sales_process_and_methodology = "sales process and methodology"
21
+ customer_relationship_management_system_training = "customer relationship management system training"
22
+ sales_enablement_strategy_and_planning = "sales enablement strategy and planning"
23
+ content_creation_and_curation = "content creation and curation"
24
+ sales_coaching_and_mentoring = "sales coaching and mentoring"
25
+ sales_metrics_and_analytics = "sales metrics and analytics"
26
+ digital_marketing_fundamentals = "digital marketing fundamentals"
27
+ content_marketing_strategy = "content marketing strategy"
28
+ social_media_marketing = "social media marketing"
29
+ search_engine_optimization = "search engine optimization"
30
+ leadership_development_and_coaching = "leadership development and coaching"
31
+ strategic_planning_and_decision_making = "strategic planning and decision making"
32
+ change_management_and_organizational_development = "change management and organizational development"
33
+ emotional_intelligence_and_communication_skills = "emotional intelligence and communication skills"
34
+
35
+ COURSES = ["company culture and values",
36
+ "product knowledge and features",
37
+ "sales process and methodology",
38
+ "customer relationship management system training",
39
+ "sales enablement strategy and planning",
40
+ "content creation and curation",
41
+ "sales coaching and mentoring",
42
+ "sales metrics and analytics",
43
+ "digital marketing fundamentals",
44
+ "content marketing strategy",
45
+ "social media marketing",
46
+ "search engine optimization",
47
+ "leadership development and coaching",
48
+ "strategic planning and decision making",
49
+ "change management and organizational development",
50
+ "emotional intelligence and communication skills"
51
+ ]
52
+
53
+ class EnrollmentRule(BaseModel):
54
+ rule_code: str = Field(description="unique identifier code for the rule", default="")
55
+ rule_name: str = Field(description="name of the rule", default="")
56
+ group: Group = Field(description="group to apply the rule to", default=Group.onboarding)
57
+ courses: List[Course] = Field(description="list of courses that the members of the group will follow", default=[course.value for course in Course])
58
+
59
+ @field_validator("rule_code")
60
+ def rule_code_max_length(cls, rule_code):
61
+ # Check if empty
62
+ if not rule_code.strip():
63
+ raise ValueError("The rule code cannot be empty.")
64
+ # Check length
65
+ if len(rule_code) > 50:
66
+ raise ValueError("The rule code must contain less than 50 characters.")
67
+ return rule_code
68
+ @field_validator("rule_name")
69
+ def rule_name_max_length(cls, rule_name):
70
+ # Check if empty
71
+ if not rule_name.strip():
72
+ raise ValueError("The rule name cannot be empty.")
73
+ # Check length
74
+ if len(rule_name) > 255:
75
+ raise ValueError("The rule name must contain less than 255 characters.")
76
+ return rule_name
77
+ @field_validator("group")
78
+ def group_is_valid(cls, group):
79
+ # Check if empty
80
+ if not group or not group.strip():
81
+ raise ValueError("Group cannot be empty.")
82
+ # Validate group value
83
+ if group.lower().strip() not in GROUPS:
84
+ raise ValueError(f"group '{group}' is not a valid value for field 'group'. 'group' must be one of the following: '{', '.join(GROUPS)}'")
85
+ return group
86
+ @field_validator("courses")
87
+ def course_list_is_valid(cls, courses):
88
+ # Ensure at least one course is selected
89
+ if not courses:
90
+ raise ValueError("At least one course must be provided.")
91
+
92
+ # Validate all listed courses
93
+ invalid_courses = []
94
+ for course in courses:
95
+ if course.lower().strip() not in COURSES:
96
+ invalid_courses.append(course)
97
+ if invalid_courses:
98
+ 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)}'")
99
+ return courses
100
+
101
+ def create_enrollment_rule(enrollment_rule: EnrollmentRule) -> dict:
102
+ """Create an enrollment rule, based on the rule parameters provided by the user"""
103
+ payload = enrollment_rule.model_dump(mode="json")
104
+ return payload
105
+
106
+ # A small Gradio-friendly wrapper that converts user inputs into an EnrollmentRule
107
+ def gr_create_enrollment_rule(rule_code, rule_name, group, courses):
108
+ # Build the Pydantic model from the raw inputs
109
+ enrollment_rule = EnrollmentRule(
110
+ rule_code=rule_code,
111
+ rule_name=rule_name,
112
+ group=group,
113
+ courses=courses
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  )
115
+ return create_enrollment_rule(enrollment_rule)
116
+
117
+ # Define the Gradio interface
118
+ demo = gr.Interface(
119
+ fn=gr_create_enrollment_rule,
120
+ inputs=[
121
+ gr.Textbox(label="Rule Code", placeholder="Enter a unique identifier (max 50 characters)"),
122
+ gr.Textbox(label="Rule Name", placeholder="Enter a descriptive name (max 255 characters)"),
123
+ gr.Dropdown(label="Group", choices=GROUPS, value=GROUPS[0]),
124
+ gr.CheckboxGroup(label="Courses", choices=COURSES, value=[], info="Select one or more courses")
125
+ ],
126
+ outputs="json",
127
+ title="Enrollment Rule Creator",
128
+ description="Create an enrollment rule, based on the rule parameters provided by the user",
129
+ allow_flagging="never"
130
+ )
131
+
132
+ demo.launch()
requirements.txt CHANGED
@@ -1,6 +1 @@
1
- accelerate
2
- diffusers
3
- invisible_watermark
4
- torch
5
- transformers
6
- xformers
 
1
+ gradio