umarfarzan commited on
Commit
d5ac8a3
Β·
verified Β·
1 Parent(s): 8e55803

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -75
app.py CHANGED
@@ -1,35 +1,30 @@
1
  import gradio as gr
2
- from unsloth import FastLanguageModel
3
  import torch
4
  import time
5
 
6
  # ----------------------------
7
  # πŸš€ Load Model (CPU)
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 # 4-bit works on CPU too
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
  # πŸ’‘ 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
  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.
34
 
35
  ### Instruction:
@@ -52,7 +47,6 @@ def generate_training_program(
52
 
53
  generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
54
 
55
- # Extract only the response part
56
  if "### Response:" in generated_text:
57
  response = generated_text.split("### Response:")[-1].strip()
58
  else:
@@ -61,58 +55,23 @@ def generate_training_program(
61
  return response, f"⏱️ Generated in {generation_time:.2f} seconds"
62
 
63
  # ----------------------------
64
- # 🎨 Gradio Interface
65
  # ----------------------------
66
- custom_css = """
67
- .gradio-container {
68
- font-family: 'Inter', sans-serif;
69
- }
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
- gr.HTML("""
90
- <div class="main-header">
91
- <h1>🎯 AI Training Program Generator</h1>
92
- <p>Generate professional training programs instantly using AI</p>
93
- </div>
94
- """)
95
-
96
- with gr.Row():
97
- with gr.Column(scale=1):
98
- instruction_input = gr.Textbox(
99
- label="πŸ“ Training Program Description",
100
- placeholder="Describe your training program...",
101
- lines=5
102
- )
103
-
104
- with gr.Accordion("βš™οΈ Advanced Settings", open=False):
105
- max_tokens_slider = gr.Slider(500, 2000, value=500, step=100, label="Max Output Length")
106
- temperature_slider = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Creativity (Temperature)")
107
- top_p_slider = gr.Slider(0.5, 1.0, value=0.9, step=0.05, label="Diversity (Top-p)")
108
-
109
- generate_btn = gr.Button("πŸš€ Generate Training Program", variant="primary")
110
-
111
- with gr.Column(scale=1):
112
- output_text = gr.Textbox(label="πŸ“‹ Generated Training Program", lines=25, show_copy_button=True)
113
- generation_info = gr.Textbox(label="ℹ️ Generation Info", interactive=False, show_label=False)
114
-
115
- gr.Examples(examples=examples, inputs=[instruction_input], label="πŸ’‘ Example Prompts")
116
 
117
  generate_btn.click(
118
  fn=generate_training_program,
@@ -120,13 +79,6 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
120
  outputs=[output_text, generation_info]
121
  )
122
 
123
- # ----------------------------
124
- # πŸš€ Launch App
125
- # ----------------------------
126
- if __name__ == "__main__":
127
- demo.queue(max_size=10)
128
- demo.launch(
129
- share=True,
130
- server_name="0.0.0.0",
131
- server_port=7860
132
- )
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
3
  import torch
4
  import time
5
 
6
  # ----------------------------
7
  # πŸš€ Load Model (CPU)
8
  # ----------------------------
9
+ model_name = "umarfarzan/my-finetuned-model2-lora"
10
+
11
+ # LoRA + 4-bit config
12
+ bnb_config = BitsAndBytesConfig(load_in_4bit=True)
 
 
 
 
 
 
13
 
14
  print("Loading model...")
15
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
16
+ model = AutoModelForCausalLM.from_pretrained(
17
+ model_name,
18
+ device_map="cpu",
19
+ quantization_config=bnb_config
20
+ )
21
+ model.eval()
22
  print("βœ… Model loaded successfully!")
23
 
24
  # ----------------------------
25
  # πŸ’‘ Generate Training Program
26
  # ----------------------------
27
+ def generate_training_program(instruction, max_tokens=500, temperature=0.7, top_p=0.9):
 
 
 
 
 
28
  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.
29
 
30
  ### Instruction:
 
47
 
48
  generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
49
 
 
50
  if "### Response:" in generated_text:
51
  response = generated_text.split("### Response:")[-1].strip()
52
  else:
 
55
  return response, f"⏱️ Generated in {generation_time:.2f} seconds"
56
 
57
  # ----------------------------
58
+ # 🎨 Gradio UI
59
  # ----------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  examples = [
61
+ ["Design a detailed 1-week training program titled 'The Leader's Blueprint for Strategic Problem-Solving' for mid-level to senior-level managers."],
62
  ["Create a 3-day workshop on effective communication skills for remote teams."],
63
  ["Develop a 5-day leadership bootcamp for new managers focusing on team management and conflict resolution."],
 
 
64
  ]
65
 
66
+ with gr.Blocks() as demo:
67
+ gr.Markdown("## 🎯 AI Training Program Generator")
68
+ instruction_input = gr.Textbox(label="Training Program Description", lines=5)
69
+ max_tokens_slider = gr.Slider(500, 2000, value=500, step=100, label="Max Output Length")
70
+ temperature_slider = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Creativity (Temperature)")
71
+ top_p_slider = gr.Slider(0.5, 1.0, value=0.9, step=0.05, label="Diversity (Top-p)")
72
+ generate_btn = gr.Button("πŸš€ Generate Training Program")
73
+ output_text = gr.Textbox(label="Generated Training Program", lines=25, show_copy_button=True)
74
+ generation_info = gr.Textbox(label="Generation Info", interactive=False, show_label=False)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  generate_btn.click(
77
  fn=generate_training_program,
 
79
  outputs=[output_text, generation_info]
80
  )
81
 
82
+ gr.Examples(examples=examples, inputs=[instruction_input])
83
+
84
+ demo.launch(share=True)