Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,38 +1,44 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
import time
|
| 5 |
|
| 6 |
# ----------------------------
|
| 7 |
-
#
|
| 8 |
# ----------------------------
|
| 9 |
-
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
bnb_config = BitsAndBytesConfig(load_in_4bit=True)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# ----------------------------
|
| 25 |
-
#
|
| 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
|
| 29 |
-
|
| 30 |
### Instruction:
|
| 31 |
{instruction}
|
| 32 |
-
### Input:
|
| 33 |
### Response:
|
| 34 |
"""
|
| 35 |
-
inputs = tokenizer([prompt_text], return_tensors="pt").to(
|
| 36 |
|
| 37 |
start_time = time.time()
|
| 38 |
outputs = model.generate(
|
|
@@ -43,8 +49,7 @@ def generate_training_program(instruction, max_tokens=500, temperature=0.7, top_
|
|
| 43 |
do_sample=True,
|
| 44 |
use_cache=True
|
| 45 |
)
|
| 46 |
-
|
| 47 |
-
|
| 48 |
generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 49 |
|
| 50 |
if "### Response:" in generated_text:
|
|
@@ -52,33 +57,35 @@ def generate_training_program(instruction, max_tokens=500, temperature=0.7, top_
|
|
| 52 |
else:
|
| 53 |
response = generated_text
|
| 54 |
|
| 55 |
-
return response, f"β±οΈ Generated in {
|
| 56 |
|
| 57 |
# ----------------------------
|
| 58 |
-
#
|
| 59 |
# ----------------------------
|
| 60 |
examples = [
|
| 61 |
-
["Design a
|
| 62 |
-
["Create a 3-day workshop on effective communication
|
| 63 |
-
["Develop a 5-day leadership bootcamp for new managers
|
|
|
|
|
|
|
| 64 |
]
|
| 65 |
|
| 66 |
with gr.Blocks() as demo:
|
| 67 |
-
gr.
|
| 68 |
-
instruction_input = gr.Textbox(label="Training Program Description", lines=5)
|
| 69 |
-
max_tokens_slider = gr.Slider(
|
| 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 |
-
|
| 78 |
inputs=[instruction_input, max_tokens_slider, temperature_slider, top_p_slider],
|
| 79 |
outputs=[output_text, generation_info]
|
| 80 |
)
|
| 81 |
|
| 82 |
-
gr.Examples(examples=examples, inputs=
|
| 83 |
|
| 84 |
-
demo.launch(
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
from peft import PeftModel
|
| 5 |
import time
|
| 6 |
|
| 7 |
# ----------------------------
|
| 8 |
+
# πΉ Load base model + LoRA weights
|
| 9 |
# ----------------------------
|
| 10 |
+
BASE_MODEL = "unsloth/qwen2.5-7b" # Original base model
|
| 11 |
+
LORA_WEIGHTS = "umarfarzan/my-finetuned-model2-lora"
|
| 12 |
|
| 13 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 14 |
|
| 15 |
+
@torch.inference_mode()
|
| 16 |
+
def load_model():
|
| 17 |
+
print("Loading base model...")
|
| 18 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 19 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 20 |
+
BASE_MODEL,
|
| 21 |
+
device_map={"": device},
|
| 22 |
+
torch_dtype=torch.float32
|
| 23 |
+
)
|
| 24 |
+
print("Applying LoRA weights...")
|
| 25 |
+
model = PeftModel.from_pretrained(model, LORA_WEIGHTS, device_map={"": device})
|
| 26 |
+
model.eval()
|
| 27 |
+
print("β
Model loaded successfully!")
|
| 28 |
+
return model, tokenizer
|
| 29 |
+
|
| 30 |
+
model, tokenizer = load_model()
|
| 31 |
|
| 32 |
# ----------------------------
|
| 33 |
+
# πΉ Generation function
|
| 34 |
# ----------------------------
|
| 35 |
def generate_training_program(instruction, max_tokens=500, temperature=0.7, top_p=0.9):
|
| 36 |
+
prompt_text = f"""Below is an instruction that describes a task. Write a response that appropriately completes the request.
|
|
|
|
| 37 |
### Instruction:
|
| 38 |
{instruction}
|
|
|
|
| 39 |
### Response:
|
| 40 |
"""
|
| 41 |
+
inputs = tokenizer([prompt_text], return_tensors="pt").to(device)
|
| 42 |
|
| 43 |
start_time = time.time()
|
| 44 |
outputs = model.generate(
|
|
|
|
| 49 |
do_sample=True,
|
| 50 |
use_cache=True
|
| 51 |
)
|
| 52 |
+
gen_time = time.time() - start_time
|
|
|
|
| 53 |
generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
|
| 54 |
|
| 55 |
if "### Response:" in generated_text:
|
|
|
|
| 57 |
else:
|
| 58 |
response = generated_text
|
| 59 |
|
| 60 |
+
return response, f"β±οΈ Generated in {gen_time:.2f} seconds"
|
| 61 |
|
| 62 |
# ----------------------------
|
| 63 |
+
# πΉ Gradio UI
|
| 64 |
# ----------------------------
|
| 65 |
examples = [
|
| 66 |
+
["Design a 1-week training program 'The Leader's Blueprint' for mid-level managers and team leads."],
|
| 67 |
+
["Create a 3-day workshop on effective communication for remote teams."],
|
| 68 |
+
["Develop a 5-day leadership bootcamp for new managers."],
|
| 69 |
+
["Design a half-day data-driven decision-making session for executives."],
|
| 70 |
+
["Create a 2-week onboarding program for new software engineers."]
|
| 71 |
]
|
| 72 |
|
| 73 |
with gr.Blocks() as demo:
|
| 74 |
+
gr.HTML("<h1 style='text-align:center'>π― AI Training Program Generator</h1>")
|
| 75 |
+
instruction_input = gr.Textbox(label="π Training Program Description", lines=5)
|
| 76 |
+
max_tokens_slider = gr.Slider(100, 8000, value=500, step=100, label="Max Output Length")
|
| 77 |
temperature_slider = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Creativity (Temperature)")
|
| 78 |
top_p_slider = gr.Slider(0.5, 1.0, value=0.9, step=0.05, label="Diversity (Top-p)")
|
| 79 |
generate_btn = gr.Button("π Generate Training Program")
|
| 80 |
+
output_text = gr.Textbox(label="π Generated Training Program", lines=25, show_copy_button=True)
|
| 81 |
+
generation_info = gr.Textbox(label="βΉοΈ Generation Info", interactive=False, show_label=False)
|
| 82 |
|
| 83 |
generate_btn.click(
|
| 84 |
+
generate_training_program,
|
| 85 |
inputs=[instruction_input, max_tokens_slider, temperature_slider, top_p_slider],
|
| 86 |
outputs=[output_text, generation_info]
|
| 87 |
)
|
| 88 |
|
| 89 |
+
gr.Examples(examples=examples, inputs=instruction_input)
|
| 90 |
|
| 91 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|