Spaces:
Runtime error
Runtime error
| from unsloth import FastLanguageModel | |
| import torch | |
| import gradio as gr | |
| max_seq_length = 2048 # Choose any! We auto support RoPE Scaling internally! | |
| dtype = None # None for auto detection. Float16 for Tesla T4, V100, Bfloat16 for Ampere+ | |
| load_in_4bit = True # Use 4bit quantization to reduce memory usage. Can be False. | |
| # 4bit pre quantized models we support for 4x faster downloading + no OOMs. | |
| fourbit_models = [ | |
| "unsloth/mistral-7b-v0.3-bnb-4bit", # New Mistral v3 2x faster! | |
| "unsloth/mistral-7b-instruct-v0.3-bnb-4bit", | |
| "unsloth/llama-3-8b-bnb-4bit", # Llama-3 15 trillion tokens model 2x faster! | |
| "unsloth/llama-3-8b-Instruct-bnb-4bit", | |
| "unsloth/llama-3-70b-bnb-4bit", | |
| "unsloth/Phi-3-mini-4k-instruct", # Phi-3 2x faster! | |
| "unsloth/Phi-3-medium-4k-instruct", | |
| "unsloth/mistral-7b-bnb-4bit", | |
| "unsloth/gemma-7b-bnb-4bit", # Gemma 2.2x faster! | |
| ] # More models at https://huggingface.co/unsloth | |
| model, tokenizer = FastLanguageModel.from_pretrained( | |
| model_name = "danishmuhammad/ccat_2025_llama3.1_8B", | |
| max_seq_length = max_seq_length, | |
| dtype = dtype, | |
| load_in_4bit = load_in_4bit, | |
| # token = "hf_...", # use one if using gated models like meta-llama/Llama-2-7b-hf | |
| ) | |
| FastLanguageModel.for_inference(model) | |
| alpaca_prompt = """Below is an input that describes a question, answer the following question as clearly as possible. If additional context is needed, provide it briefly. | |
| ### Input: | |
| {} | |
| ### Response: | |
| {}""" | |
| with gr.Blocks() as demo: | |
| chatbot = gr.Chatbot(layout="bubble") | |
| user_input = gr.Textbox() | |
| clear = gr.ClearButton([user_input, chatbot]) | |
| def answers_chat(user_input,history): | |
| history = history or [] | |
| formatted_input = alpaca_prompt.format(user_input, "") | |
| inputs = tokenizer([formatted_input], return_tensors="pt").to("cuda") | |
| # Generate response with adjusted parameters | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=512, # Increase to allow for longer responses | |
| temperature=0.4, # Add temperature to introduce variation | |
| repetition_penalty=1.2, # Penalize repeating tokens | |
| no_repeat_ngram_size=3, # Avoid repeating sequences of 3 tokens | |
| use_cache=True, | |
| eos_token_id=tokenizer.eos_token_id | |
| ) | |
| response = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0] | |
| formatted_response = response[len(formatted_input):].strip() | |
| history.append((user_input,formatted_response)) | |
| return "",history | |
| user_input.submit(answers_chat, [user_input, chatbot], [user_input, chatbot]) | |
| demo.launch() |