Spaces:
No application file
No application file
clanflare
Update README and add model files for Mistral-7B LoRA fine-tuning; include usage instructions, model details, and new configuration files.
3c170e1 Download src/test_model.py from clanflare/mistral-7b-finetuning-scripts: direct link, hf CLI and curl.
- Browser
- Download file 2.46 kB
-
https://huggingface.co/spaces/clanflare/mistral-7b-finetuning-scripts/resolve/main/src/test_model.py
- Command line
-
hf download hf://spaces/clanflare/mistral-7b-finetuning-scripts/src/test_model.py
-
curl -L -o test_model.py https://huggingface.co/spaces/clanflare/mistral-7b-finetuning-scripts/resolve/main/src/test_model.py
2.46 kB
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| from peft import PeftModel | |
| import logging | |
| # Set up logging | |
| logging.basicConfig( | |
| level=logging.INFO, | |
| format='%(asctime)s - %(levelname)s - %(message)s' | |
| ) | |
| logger = logging.getLogger(__name__) | |
| def load_model_and_tokenizer(): | |
| """Load the base model and LoRA weights.""" | |
| logger.info("Loading base model and tokenizer...") | |
| base_model = "mistralai/Mistral-7B-v0.1" | |
| # Load tokenizer | |
| tokenizer = AutoTokenizer.from_pretrained(base_model) | |
| tokenizer.pad_token = tokenizer.eos_token | |
| # Load base model | |
| model = AutoModelForCausalLM.from_pretrained( | |
| base_model, | |
| torch_dtype=torch.bfloat16, | |
| device_map="auto", | |
| trust_remote_code=True | |
| ) | |
| # Load LoRA weights | |
| logger.info("Loading LoRA weights...") | |
| model = PeftModel.from_pretrained(model, "./final_model") | |
| return model, tokenizer | |
| def generate_response(model, tokenizer, prompt, max_length=200): | |
| """Generate a response for the given prompt.""" | |
| # Format the prompt | |
| formatted_prompt = f"""### Instruction: {prompt} | |
| ### Response:""" | |
| # Tokenize input | |
| inputs = tokenizer(formatted_prompt, return_tensors="pt").to(model.device) | |
| # Generate response | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| **inputs, | |
| max_length=max_length, | |
| temperature=0.7, | |
| top_p=0.9, | |
| do_sample=True, | |
| pad_token_id=tokenizer.pad_token_id | |
| ) | |
| # Decode and return response | |
| response = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| return response | |
| def main(): | |
| try: | |
| # Load model and tokenizer | |
| model, tokenizer = load_model_and_tokenizer() | |
| # Test prompts | |
| test_prompts = [ | |
| "Write a short poem about artificial intelligence", | |
| "Explain quantum computing in simple terms", | |
| "What are the benefits of meditation?", | |
| "Describe the process of photosynthesis" | |
| ] | |
| # Generate responses | |
| for prompt in test_prompts: | |
| logger.info(f"\nPrompt: {prompt}") | |
| response = generate_response(model, tokenizer, prompt) | |
| logger.info(f"Response: {response}\n") | |
| except Exception as e: | |
| logger.error(f"Error during testing: {e}") | |
| raise | |
| if __name__ == "__main__": | |
| main() |