Download chat_model.py from paswans05/Ini-i0.5: direct link, hf CLI and curl.
- Browser
- Download file 2.04 kB
-
https://huggingface.co/paswans05/Ini-i0.5/resolve/main/chat_model.py
- Command line
-
hf download hf://paswans05/Ini-i0.5/chat_model.py
-
curl -L -o chat_model.py https://huggingface.co/paswans05/Ini-i0.5/resolve/main/chat_model.py
2.04 kB
| import torch | |
| from transformers import AutoTokenizer, GenerationConfig | |
| from modeling_ini import IniForCausalLM | |
| def chat(): | |
| print("Loading model...") | |
| model_path = "./model_save" | |
| try: | |
| model = IniForCausalLM.from_pretrained(model_path) | |
| except Exception as e: | |
| import traceback | |
| traceback.print_exc() | |
| print(f"Error loading model: {e}") | |
| return | |
| # Using GPT-2 tokenizer as a placeholder since we don't have a custom one yet | |
| # The model vocab is 102400, GPT-2 is ~50257, so all IDs are valid. | |
| print("Loading tokenizer (gpt2 placeholder)...") | |
| tokenizer = AutoTokenizer.from_pretrained("gpt2") | |
| # Configure padding | |
| tokenizer.pad_token = tokenizer.eos_token | |
| model.config.pad_token_id = tokenizer.eos_token_id | |
| device = "cuda" if torch.cuda.is_available() else "cpu" | |
| print(f"Using device: {device}") | |
| model = model.to(device) | |
| model.eval() | |
| print("\n" + "="*40) | |
| print("Ini-i0.5 Chat (Untrained / Random Weights)") | |
| print("Type 'exit' or 'quit' to stop.") | |
| print("="*40 + "\n") | |
| history = [] | |
| while True: | |
| user_input = input("You: ") | |
| if user_input.lower() in ["exit", "quit"]: | |
| break | |
| # Simple prompt construction | |
| input_text = f"User: {user_input}\nAssistant:" | |
| inputs = tokenizer(input_text, return_tensors="pt").to(device) | |
| print("Generating...", end="\r") | |
| with torch.no_grad(): | |
| output_ids = model.generate( | |
| **inputs, | |
| max_new_tokens=50, | |
| do_sample=True, | |
| temperature=0.7, | |
| top_k=50, | |
| pad_token_id=tokenizer.eos_token_id, | |
| repetition_penalty=1.2 | |
| ) | |
| response = tokenizer.decode(output_ids[0], skip_special_tokens=True) | |
| # Extract just the new part | |
| new_response = response[len(input_text):].strip() | |
| print(f"Assistant: {new_response}\n") | |
| if __name__ == "__main__": | |
| chat() | |