umarfarzan commited on
Commit
2b80913
Β·
verified Β·
1 Parent(s): c58407e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +231 -0
app.py ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from unsloth import FastLanguageModel
3
+ import torch
4
+ import time
5
+
6
+ # ----------------------------
7
+ # πŸš€ Load Model (cached)
8
+ # ----------------------------
9
+ @gr.cache
10
+ def load_model():
11
+ max_seq_length = 1024
12
+ model, tokenizer = FastLanguageModel.from_pretrained(
13
+ model_name="umarfarzan/my-finetuned-model2-lora",
14
+ max_seq_length=max_seq_length,
15
+ dtype=None,
16
+ load_in_4bit=True
17
+ )
18
+ FastLanguageModel.for_inference(model)
19
+ return model, tokenizer
20
+
21
+ print("Loading model...")
22
+ model, tokenizer = load_model()
23
+ print("βœ… Model loaded successfully!")
24
+
25
+ # ----------------------------
26
+ # πŸ’‘ Generate Training Program
27
+ # ----------------------------
28
+ def generate_training_program(
29
+ instruction,
30
+ max_tokens=5500,
31
+ temperature=0.7,
32
+ top_p=0.9
33
+ ):
34
+ """Generate a training program based on user instruction"""
35
+
36
+ # Build prompt in Alpaca format
37
+ prompt_text = f"""Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
38
+
39
+ ### Instruction:
40
+ {instruction}
41
+
42
+ ### Input:
43
+
44
+
45
+ ### Response:
46
+ """
47
+
48
+ # Tokenize
49
+ inputs = tokenizer([prompt_text], return_tensors="pt").to("cuda")
50
+
51
+ # Generate with progress
52
+ start_time = time.time()
53
+
54
+ outputs = model.generate(
55
+ **inputs,
56
+ max_new_tokens=max_tokens,
57
+ temperature=temperature,
58
+ top_p=top_p,
59
+ do_sample=True,
60
+ use_cache=True
61
+ )
62
+
63
+ generation_time = time.time() - start_time
64
+
65
+ # Decode
66
+ generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
67
+
68
+ # Extract only the response part (after "### Response:")
69
+ if "### Response:" in generated_text:
70
+ response = generated_text.split("### Response:")[-1].strip()
71
+ else:
72
+ response = generated_text
73
+
74
+ return response, f"⏱️ Generated in {generation_time:.2f} seconds"
75
+
76
+ # ----------------------------
77
+ # 🎨 Gradio Interface
78
+ # ----------------------------
79
+ # Custom CSS for beautiful styling
80
+ custom_css = """
81
+ .gradio-container {
82
+ font-family: 'Inter', sans-serif;
83
+ }
84
+ .main-header {
85
+ text-align: center;
86
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
87
+ color: white;
88
+ padding: 2rem;
89
+ border-radius: 10px;
90
+ margin-bottom: 2rem;
91
+ }
92
+ .example-box {
93
+ background: #f8f9fa;
94
+ padding: 1rem;
95
+ border-radius: 8px;
96
+ border-left: 4px solid #667eea;
97
+ }
98
+ """
99
+
100
+ # Example prompts
101
+ examples = [
102
+ ["Design a detailed 1-week training program titled 'The Leader's Blueprint for Strategic Problem-Solving' for mid-level to senior-level managers, team leads, and high-potential employees."],
103
+ ["Create a 3-day workshop on effective communication skills for remote teams."],
104
+ ["Develop a 5-day leadership bootcamp for new managers focusing on team management and conflict resolution."],
105
+ ["Design a half-day training session on data-driven decision making for executives."],
106
+ ["Create a 2-week onboarding program for new software engineers including technical and cultural training."],
107
+ ]
108
+
109
+ # Build interface
110
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
111
+
112
+ # Header
113
+ gr.HTML("""
114
+ <div class="main-header">
115
+ <h1>🎯 AI Training Program Generator</h1>
116
+ <p style="font-size: 1.1rem; margin-top: 0.5rem;">
117
+ Generate comprehensive, professional training programs instantly using AI
118
+ </p>
119
+ </div>
120
+ """)
121
+
122
+ # Description
123
+ gr.Markdown("""
124
+ ### ✨ How it works
125
+ Simply describe the training program you need, and our AI will generate a detailed, structured curriculum complete with:
126
+ - Day-by-day breakdown
127
+ - Learning objectives
128
+ - Activities and exercises
129
+ - Materials needed
130
+ - Assessment methods
131
+ """)
132
+
133
+ with gr.Row():
134
+ with gr.Column(scale=1):
135
+ # Input section
136
+ instruction_input = gr.Textbox(
137
+ label="πŸ“ Training Program Description",
138
+ placeholder="Example: Design a 1-week training program on strategic problem-solving for managers...",
139
+ lines=5,
140
+ info="Describe what type of training program you need"
141
+ )
142
+
143
+ # Advanced settings (collapsed by default)
144
+ with gr.Accordion("βš™οΈ Advanced Settings", open=False):
145
+ max_tokens_slider = gr.Slider(
146
+ minimum=500,
147
+ maximum=8000,
148
+ value=5500,
149
+ step=100,
150
+ label="Max Output Length",
151
+ info="Longer programs need more tokens"
152
+ )
153
+ temperature_slider = gr.Slider(
154
+ minimum=0.1,
155
+ maximum=1.5,
156
+ value=0.7,
157
+ step=0.1,
158
+ label="Creativity (Temperature)",
159
+ info="Higher = more creative, Lower = more focused"
160
+ )
161
+ top_p_slider = gr.Slider(
162
+ minimum=0.5,
163
+ maximum=1.0,
164
+ value=0.9,
165
+ step=0.05,
166
+ label="Diversity (Top-p)",
167
+ info="Controls output diversity"
168
+ )
169
+
170
+ generate_btn = gr.Button("πŸš€ Generate Training Program", variant="primary", size="lg")
171
+
172
+ with gr.Column(scale=1):
173
+ # Output section
174
+ output_text = gr.Textbox(
175
+ label="πŸ“‹ Generated Training Program",
176
+ lines=25,
177
+ show_copy_button=True,
178
+ info="Your custom training program will appear here"
179
+ )
180
+ generation_info = gr.Textbox(
181
+ label="ℹ️ Generation Info",
182
+ interactive=False,
183
+ show_label=False
184
+ )
185
+
186
+ # Examples
187
+ gr.Markdown("### πŸ’‘ Example Prompts - Click to try!")
188
+ gr.Examples(
189
+ examples=examples,
190
+ inputs=[instruction_input],
191
+ label="Quick Start Examples"
192
+ )
193
+
194
+ # Footer info
195
+ gr.Markdown("""
196
+ ---
197
+ ### πŸ“Œ Tips for Best Results:
198
+ - **Be specific**: Include duration (1 week, 3 days, etc.), target audience, and focus areas
199
+ - **Add context**: Mention skill level, industry, or specific challenges to address
200
+ - **Iterate**: Generate multiple versions and pick the best elements from each
201
+
202
+ ### πŸ€– Model Information:
203
+ - **Base Model**: Qwen2.5-7B fine-tuned with LoRA
204
+ - **Fine-tuning**: Custom training on professional development curricula
205
+ - **Optimization**: 4-bit quantization for efficient inference
206
+ """)
207
+
208
+ # Connect button to function
209
+ generate_btn.click(
210
+ fn=generate_training_program,
211
+ inputs=[
212
+ instruction_input,
213
+ max_tokens_slider,
214
+ temperature_slider,
215
+ top_p_slider
216
+ ],
217
+ outputs=[output_text, generation_info],
218
+ api_name="generate"
219
+ )
220
+
221
+ # ----------------------------
222
+ # πŸš€ Launch
223
+ # ----------------------------
224
+ if __name__ == "__main__":
225
+ demo.queue(max_size=10) # Enable queue for better UX
226
+ demo.launch(
227
+ share=False,
228
+ show_error=True,
229
+ server_name="0.0.0.0",
230
+ server_port=7860
231
+ )