Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from huggingface_hub import InferenceClient | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| from peft import PeftModel | |
| import torch | |
| # Base model name | |
| base_model_name = "meta-llama/Llama-3.2-3B-Instruct" | |
| # Load the base model and tokenizer | |
| tokenizer = AutoTokenizer.from_pretrained(base_model_name) | |
| base_model = AutoModelForCausalLM.from_pretrained( | |
| base_model_name, | |
| device_map="auto", # Automatically map to GPU if available | |
| torch_dtype=torch.float16, # Use float16 for better performance on GPU | |
| ) | |
| # Fine-tuned LoRA adapter | |
| lora_model_name = "shanaka95/autotrain-sios2" | |
| # Load the LoRA adapter and merge it with the base model | |
| model = PeftModel.from_pretrained(base_model, lora_model_name) | |
| # Move the model to GPU if available | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| model = model.to(device) | |
| def respond( | |
| message, | |
| history: list[tuple[str, str]], | |
| system_message, | |
| max_tokens, | |
| temperature, | |
| top_p, | |
| ): | |
| inputs = tokenizer(message, return_tensors="pt").to(device) | |
| # Generate response | |
| outputs = model.generate( | |
| inputs.input_ids, | |
| max_length=300, | |
| temperature=0.2, | |
| top_p=0.8, | |
| do_sample=True, | |
| eos_token_id=tokenizer.eos_token_id, | |
| max_new_tokens=128, | |
| ) | |
| # Decode the response | |
| return tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| """ | |
| For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface | |
| """ | |
| demo = gr.ChatInterface( | |
| respond, | |
| additional_inputs=[ | |
| gr.Textbox(value="You are a friendly Chatbot.", label="System message"), | |
| gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), | |
| gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), | |
| gr.Slider( | |
| minimum=0.1, | |
| maximum=1.0, | |
| value=0.95, | |
| step=0.05, | |
| label="Top-p (nucleus sampling)", | |
| ), | |
| ], | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(show_error=True) |