umarfarzan commited on
Commit
e451b9e
Β·
verified Β·
1 Parent(s): 89dcadb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +93 -60
app.py CHANGED
@@ -1,128 +1,161 @@
1
  import gradio as gr
2
- from unsloth import FastLanguageModel
3
  import torch
4
  import time
5
 
6
  # ----------------------------
7
- # πŸš€ Load Model
8
  # ----------------------------
 
9
  def load_model():
10
- max_seq_length = 1024
11
- model, tokenizer = FastLanguageModel.from_pretrained(
12
- model_name="umarfarzan/my-finetuned-model2-lora",
13
- max_seq_length=max_seq_length,
14
- dtype=None,
15
- load_in_4bit=True,
16
  )
17
- FastLanguageModel.for_inference(model)
18
  return model, tokenizer
19
 
20
- print("πŸ”„ Loading model ...")
21
  model, tokenizer = load_model()
22
  print("βœ… Model loaded successfully!")
23
 
24
-
25
  # ----------------------------
26
  # πŸ’‘ Generate Training Program
27
  # ----------------------------
28
- def generate_program(instruction, max_tokens=4000, temperature=0.7, top_p=0.9):
29
- """Generate a detailed training program from a natural-language prompt"""
30
- prompt_text = f"""Below is an instruction that describes a task, paired with an input that provides further context.
31
- Write a response that appropriately completes the request.
 
 
 
 
 
32
 
33
  ### Instruction:
34
  {instruction}
35
 
36
- ### Input:
37
-
38
-
39
  ### Response:
40
  """
 
41
 
42
- inputs = tokenizer([prompt_text], return_tensors="pt").to("cuda")
43
-
44
- start = time.time()
45
  outputs = model.generate(
46
  **inputs,
47
  max_new_tokens=max_tokens,
48
  temperature=temperature,
49
  top_p=top_p,
50
  do_sample=True,
51
- use_cache=True,
52
  )
53
- end = time.time()
54
-
55
- result = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
56
- if "### Response:" in result:
57
- result = result.split("### Response:")[-1].strip()
58
 
59
- return result, f"⏱️ Generated in {end - start:.2f} sec"
 
 
 
 
 
 
60
 
 
61
 
62
  # ----------------------------
63
  # 🎨 Gradio Interface
64
  # ----------------------------
65
- examples = [
66
- ["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."],
67
- ["Create a 3-day innovation workshop based on TRIZ principles for engineers and R&D specialists."],
68
- ["Develop a 5-day leadership bootcamp focused on emotional intelligence and decision-making."],
69
- ["Design a 2-day workshop on cross-cultural communication for international teams."],
70
- ["Create a 4-week mentorship program for junior data scientists to learn project management and collaboration skills."],
71
- ]
72
-
73
  custom_css = """
 
74
  .main-header {
75
  text-align: center;
76
- background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
77
  color: white;
78
  padding: 2rem;
79
- border-radius: 12px;
80
  margin-bottom: 2rem;
81
  }
82
- .gradio-container {
83
- font-family: 'Inter', sans-serif;
84
- }
85
  """
86
 
 
 
 
 
 
 
 
 
87
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
 
 
88
  gr.HTML("""
89
  <div class="main-header">
90
  <h1>🎯 AI Training Program Generator</h1>
91
- <p>Generate professional, structured training programs instantly with your fine-tuned Qwen2 model</p>
 
 
92
  </div>
93
  """)
94
 
95
  with gr.Row():
96
  with gr.Column(scale=1):
97
- instruction = gr.Textbox(
98
- label="🧠 Describe the training program you want",
99
- placeholder="Example: Design a 3-day workshop on innovation for engineers...",
100
- lines=5,
101
  )
102
 
103
  with gr.Accordion("βš™οΈ Advanced Settings", open=False):
104
- max_tokens = gr.Slider(500, 8000, value=4000, step=100, label="Max Output Length")
105
- temperature = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Creativity (Temperature)")
106
- top_p = gr.Slider(0.5, 1.0, value=0.9, step=0.05, label="Diversity (Top-p)")
107
-
108
- submit = gr.Button("πŸš€ Generate", variant="primary")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
109
 
110
  with gr.Column(scale=1):
111
- output = gr.Textbox(
112
  label="πŸ“‹ Generated Training Program",
113
  lines=25,
114
- show_copy_button=True,
 
 
 
 
 
115
  )
116
- info = gr.Textbox(label="ℹ️ Info", interactive=False, show_label=False)
117
 
118
- gr.Examples(examples, inputs=[instruction], label="πŸ’‘ Example Prompts")
 
 
 
 
119
 
120
- submit.click(
121
- generate_program,
122
- inputs=[instruction, max_tokens, temperature, top_p],
123
- outputs=[output, info],
124
  )
125
 
126
- demo.queue(max_size=10)
127
  if __name__ == "__main__":
 
128
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import torch
4
  import time
5
 
6
  # ----------------------------
7
+ # πŸš€ Load Model (CPU-friendly)
8
  # ----------------------------
9
+ @gr.cache
10
  def load_model():
11
+ model_name = "umarfarzan/my-finetuned-model2-lora"
12
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
13
+ model = AutoModelForCausalLM.from_pretrained(
14
+ model_name,
15
+ torch_dtype=torch.float32 # CPU-friendly
 
16
  )
17
+ model.to("cpu")
18
  return model, tokenizer
19
 
20
+ print("Loading model...")
21
  model, tokenizer = load_model()
22
  print("βœ… Model loaded successfully!")
23
 
 
24
  # ----------------------------
25
  # πŸ’‘ Generate Training Program
26
  # ----------------------------
27
+ def generate_training_program(
28
+ instruction,
29
+ max_tokens=500,
30
+ temperature=0.7,
31
+ top_p=0.9
32
+ ):
33
+ """Generate a training program based on user instruction"""
34
+
35
+ prompt_text = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
36
 
37
  ### Instruction:
38
  {instruction}
39
 
 
 
 
40
  ### Response:
41
  """
42
+ inputs = tokenizer(prompt_text, return_tensors="pt")
43
 
44
+ start_time = time.time()
 
 
45
  outputs = model.generate(
46
  **inputs,
47
  max_new_tokens=max_tokens,
48
  temperature=temperature,
49
  top_p=top_p,
50
  do_sample=True,
51
+ use_cache=True
52
  )
53
+ generation_time = time.time() - start_time
 
 
 
 
54
 
55
+ generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
56
+
57
+ # Extract response after "### Response:"
58
+ if "### Response:" in generated_text:
59
+ response = generated_text.split("### Response:")[-1].strip()
60
+ else:
61
+ response = generated_text
62
 
63
+ return response, f"⏱️ Generated in {generation_time:.2f} seconds"
64
 
65
  # ----------------------------
66
  # 🎨 Gradio Interface
67
  # ----------------------------
 
 
 
 
 
 
 
 
68
  custom_css = """
69
+ .gradio-container { font-family: 'Inter', sans-serif; }
70
  .main-header {
71
  text-align: center;
72
+ background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
73
  color: white;
74
  padding: 2rem;
75
+ border-radius: 10px;
76
  margin-bottom: 2rem;
77
  }
 
 
 
78
  """
79
 
80
+ examples = [
81
+ ["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."],
82
+ ["Create a 3-day workshop on effective communication skills for remote teams."],
83
+ ["Develop a 5-day leadership bootcamp for new managers focusing on team management and conflict resolution."],
84
+ ["Design a half-day training session on data-driven decision making for executives."],
85
+ ["Create a 2-week onboarding program for new software engineers including technical and cultural training."]
86
+ ]
87
+
88
  with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
89
+
90
+ # Header
91
  gr.HTML("""
92
  <div class="main-header">
93
  <h1>🎯 AI Training Program Generator</h1>
94
+ <p style="font-size: 1.1rem; margin-top: 0.5rem;">
95
+ Generate comprehensive, professional training programs instantly using AI
96
+ </p>
97
  </div>
98
  """)
99
 
100
  with gr.Row():
101
  with gr.Column(scale=1):
102
+ instruction_input = gr.Textbox(
103
+ label="πŸ“ Training Program Description",
104
+ placeholder="Example: Design a 1-week training program on strategic problem-solving for managers...",
105
+ lines=5
106
  )
107
 
108
  with gr.Accordion("βš™οΈ Advanced Settings", open=False):
109
+ max_tokens_slider = gr.Slider(
110
+ minimum=100,
111
+ maximum=5500,
112
+ value=500,
113
+ step=50,
114
+ label="Max Output Length",
115
+ info="Longer programs take more time on CPU"
116
+ )
117
+ temperature_slider = gr.Slider(
118
+ minimum=0.1,
119
+ maximum=1.5,
120
+ value=0.7,
121
+ step=0.1,
122
+ label="Creativity (Temperature)"
123
+ )
124
+ top_p_slider = gr.Slider(
125
+ minimum=0.5,
126
+ maximum=1.0,
127
+ value=0.9,
128
+ step=0.05,
129
+ label="Diversity (Top-p)"
130
+ )
131
+
132
+ generate_btn = gr.Button("πŸš€ Generate Training Program", variant="primary", size="lg")
133
 
134
  with gr.Column(scale=1):
135
+ output_text = gr.Textbox(
136
  label="πŸ“‹ Generated Training Program",
137
  lines=25,
138
+ show_copy_button=True
139
+ )
140
+ generation_info = gr.Textbox(
141
+ label="ℹ️ Generation Info",
142
+ interactive=False,
143
+ show_label=False
144
  )
 
145
 
146
+ gr.Examples(
147
+ examples=examples,
148
+ inputs=[instruction_input],
149
+ label="πŸ’‘ Quick Start Examples"
150
+ )
151
 
152
+ generate_btn.click(
153
+ fn=generate_training_program,
154
+ inputs=[instruction_input, max_tokens_slider, temperature_slider, top_p_slider],
155
+ outputs=[output_text, generation_info]
156
  )
157
 
158
+ # Launch
159
  if __name__ == "__main__":
160
+ demo.queue(max_size=5)
161
  demo.launch(server_name="0.0.0.0", server_port=7860)