File size: 2,723 Bytes
bc1ec3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f9a5f0
bc1ec3f
e4fa4da
bc1ec3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b018467
bc1ec3f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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 = FastLanguageModel(device="cpu")
model, tokenizer = FastLanguageModel.from_pretrained(
    model_name = "danishmuhammad/ccat2025_llama_gguf",
    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(model.device)["input_ids"]

        # 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()