Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,128 +1,161 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
import torch
|
| 4 |
import time
|
| 5 |
|
| 6 |
# ----------------------------
|
| 7 |
-
# π Load Model
|
| 8 |
# ----------------------------
|
|
|
|
| 9 |
def load_model():
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
load_in_4bit=True,
|
| 16 |
)
|
| 17 |
-
|
| 18 |
return model, tokenizer
|
| 19 |
|
| 20 |
-
print("
|
| 21 |
model, tokenizer = load_model()
|
| 22 |
print("β
Model loaded successfully!")
|
| 23 |
|
| 24 |
-
|
| 25 |
# ----------------------------
|
| 26 |
# π‘ Generate Training Program
|
| 27 |
# ----------------------------
|
| 28 |
-
def
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
### Instruction:
|
| 34 |
{instruction}
|
| 35 |
|
| 36 |
-
### Input:
|
| 37 |
-
|
| 38 |
-
|
| 39 |
### Response:
|
| 40 |
"""
|
|
|
|
| 41 |
|
| 42 |
-
|
| 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 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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, #
|
| 77 |
color: white;
|
| 78 |
padding: 2rem;
|
| 79 |
-
border-radius:
|
| 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
|
|
|
|
|
|
|
| 92 |
</div>
|
| 93 |
""")
|
| 94 |
|
| 95 |
with gr.Row():
|
| 96 |
with gr.Column(scale=1):
|
| 97 |
-
|
| 98 |
-
label="
|
| 99 |
-
placeholder="Example: Design a
|
| 100 |
-
lines=5
|
| 101 |
)
|
| 102 |
|
| 103 |
with gr.Accordion("βοΈ Advanced Settings", open=False):
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
|
| 110 |
with gr.Column(scale=1):
|
| 111 |
-
|
| 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(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
inputs=[
|
| 123 |
-
outputs=[
|
| 124 |
)
|
| 125 |
|
| 126 |
-
|
| 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)
|