Spaces:
Runtime error
Runtime error
Update app.py
Browse files122345678910111213141516171891011121314211516171819202122232425262728293031322728293016 unmodified lines474849555051522 unmodified lines55565764585966676869707172737475767778796081616263848564658889909192939495969798991001011021031041051061071081091101111121131141156667686970717273747576771 unmodified line798081123124125126127128129130131132828384import gradio as grfrom unsloth import FastLanguageModelfrom transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfigimport torchimport time
# ----------------------------# π Load Model (CPU)# ----------------------------def load_model(): max_seq_length = 1024 model, tokenizer = FastLanguageModel.from_pretrained( model_name="umarfarzan/my-finetuned-model2-lora", max_seq_length=max_seq_length, dtype=None, load_in_4bit=True # 4-bit works on CPU too ) FastLanguageModel.for_inference(model) return model, tokenizermodel_name = "umarfarzan/my-finetuned-model2-lora"
# LoRA + 4-bit configbnb_config = BitsAndBytesConfig(load_in_4bit=True)
print("Loading model...")model, tokenizer = load_model()tokenizer = AutoTokenizer.from_pretrained(model_name)model = AutoModelForCausalLM.from_pretrained( model_name, device_map="cpu", quantization_config=bnb_config)model.eval()print("β
Model loaded successfully!")
# ----------------------------# π‘ Generate Training Program# ----------------------------def generate_training_program( instruction, max_tokens=500, temperature=0.7, top_p=0.9):def generate_training_program(instruction, max_tokens=500, temperature=0.7, top_p=0.9): 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.
### Instruction:16 unmodified lines
generated_text = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
# Extract only the response part if "### Response:" in generated_text: response = generated_text.split("### Response:")[-1].strip() else:2 unmodified lines return response, f"β±οΈ Generated in {generation_time:.2f} seconds"
# ----------------------------# π¨ Gradio Interface# π¨ Gradio UI# ----------------------------custom_css = """.gradio-container { font-family: 'Inter', sans-serif;}.main-header { text-align: center; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 2rem; border-radius: 10px; margin-bottom: 2rem;}"""
examples = [ ["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."], ["Design a detailed 1-week training program titled 'The Leader's Blueprint for Strategic Problem-Solving' for mid-level to senior-level managers."], ["Create a 3-day workshop on effective communication skills for remote teams."], ["Develop a 5-day leadership bootcamp for new managers focusing on team management and conflict resolution."], ["Design a half-day training session on data-driven decision making for executives."], ["Create a 2-week onboarding program for new software engineers including technical and cultural training."]]
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo: gr.HTML(""" <div class="main-header"> <h1>π― AI Training Program Generator</h1> <p>Generate professional training programs instantly using AI</p> </div> """)
with gr.Row(): with gr.Column(scale=1): instruction_input = gr.Textbox( label="π Training Program Description", placeholder="Describe your training program...", lines=5 )
with gr.Accordion("βοΈ Advanced Settings", open=False): max_tokens_slider = gr.Slider(500, 2000, value=500, step=100, label="Max Output Length") temperature_slider = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Creativity (Temperature)") top_p_slider = gr.Slider(0.5, 1.0, value=0.9, step=0.05, label="Diversity (Top-p)")
generate_btn = gr.Button("π Generate Training Program", variant="primary")
with gr.Column(scale=1): output_text = gr.Textbox(label="π Generated Training Program", lines=25, show_copy_button=True) generation_info = gr.Textbox(label="βΉοΈ Generation Info", interactive=False, show_label=False)
gr.Examples(examples=examples, inputs=[instruction_input], label="π‘ Example Prompts")with gr.Blocks() as demo: gr.Markdown("## π― AI Training Program Generator") instruction_input = gr.Textbox(label="Training Program Description", lines=5) max_tokens_slider = gr.Slider(500, 2000, value=500, step=100, label="Max Output Length") temperature_slider = gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Creativity (Temperature)") top_p_slider = gr.Slider(0.5, 1.0, value=0.9, step=0.05, label="Diversity (Top-p)") generate_btn = gr.Button("π Generate Training Program") output_text = gr.Textbox(label="Generated Training Program", lines=25, show_copy_button=True) generation_info = gr.Textbox(label="Generation Info", interactive=False, show_label=False)
generate_btn.click( fn=generate_training_program,1 unmodified line outputs=[output_text, generation_info] )
# ----------------------------# π Launch App# ----------------------------if __name__ == "__main__": demo.queue(max_size=10) demo.launch( share=True, server_name="0.0.0.0", server_port=7860 ) gr.Examples(examples=examples, inputs=[instruction_input])
demo.launch(share=True)app.py+27-75