| import gradio as gr |
| import torch |
| from transformers import AutoModelForCausalLM, AutoTokenizer |
| from peft import PeftModel |
|
|
| |
| |
| base_model_id = "unsloth/llama-3-8b" |
| adapter_model_id = "ZenJony/bengali-sms-ai-model" |
|
|
| print("Loading model on CPU... This may take several minutes.") |
|
|
| |
| tokenizer = AutoTokenizer.from_pretrained(base_model_id) |
| model = AutoModelForCausalLM.from_pretrained( |
| base_model_id, |
| torch_dtype=torch.float32, |
| device_map="cpu", |
| low_cpu_mem_usage=True |
| ) |
|
|
| |
| model = PeftModel.from_pretrained(model, adapter_model_id) |
| print("Model loaded successfully on CPU! ✅") |
|
|
| |
| alpaca_prompt = """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: |
| {} |
| |
| ### Input: |
| {} |
| |
| ### Response: |
| {}""" |
|
|
| def respond(message, history, system_message, max_tokens, temperature, top_p): |
| full_instruction = f"{system_message}\n\n{message}" |
| prompt = alpaca_prompt.format(full_instruction, "", "") |
| |
| inputs = tokenizer([prompt], return_tensors="pt") |
| |
| with torch.no_grad(): |
| generated_ids = model.generate( |
| **inputs, |
| max_new_tokens=max_tokens, |
| temperature=temperature, |
| top_p=top_p, |
| do_sample=True, |
| eos_token_id=tokenizer.eos_token_id, |
| ) |
| |
| full_response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0] |
| |
| if "### Response:" in full_response: |
| response = full_response.split("### Response:")[1].strip() |
| else: |
| response = full_response |
| |
| return response |
|
|
| |
| chatbot = gr.ChatInterface( |
| respond, |
| additional_inputs=[ |
| gr.Textbox(value="তুমি একজন আধুনিক ও স্মার্ট এআই অ্যাসিস্ট্যান্ট। তোমাকে তৈরি করেছেন ZenJony।", label="System message"), |
| gr.Slider(minimum=1, maximum=512, value=128, step=1, label="Max new tokens"), |
| gr.Slider(minimum=0.1, maximum=1.5, value=0.7, step=0.1, label="Temperature"), |
| gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p"), |
| ], |
| title="ZenJony AI Assistant 🤖" |
| ) |
|
|
| if __name__ == "__main__": |
| chatbot.launch() |