File size: 2,313 Bytes
d17414f
7e14f73
 
d17414f
7e14f73
 
ea6e496
7e14f73
d17414f
7e14f73
 
 
d17414f
7e14f73
 
 
 
 
 
 
 
d17414f
 
 
 
 
 
 
 
7e14f73
 
 
d17414f
7e14f73
 
 
 
 
 
d17414f
7e14f73
 
d17414f
7e14f73
 
 
 
 
 
 
 
d17414f
7e14f73
 
 
d17414f
7e14f73
d17414f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e14f73
d17414f
 
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
74
75
76
77
import gradio as gr
from llama_cpp import Llama
from huggingface_hub import hf_hub_download

# Define a function to load the model from the Hugging Face Hub
def load_model():
    repo_id = "KolumbusLindh/LoRA-6150"  # Your Hugging Face repo
    model_file = "unsloth.F16.gguf"  # Model file in GGUF format

    # Download the model file
    local_path = hf_hub_download(repo_id=repo_id, filename=model_file)
    print(f"Model loaded from: {local_path}")

    # Load the model using llama_cpp
    model = Llama(model_path=local_path, n_ctx=2048, n_threads=8, use_metal=False)
    return model

# Initialize the model
model = load_model()

# Define the response function for chat interaction
def respond(
    message,
    history: list[tuple[str, str]],
    system_message,
    max_tokens,
    temperature,
    top_p,
):
    try:
        # Prepare the system message and chat history
        messages = [{"role": "system", "content": system_message}]

        # Add the history of the conversation
        for val in history:
            if val[0]:
                messages.append({"role": "user", "content": val[0]})
            if val[1]:
                messages.append({"role": "assistant", "content": val[1]})

        # Add the current message from the user
        messages.append({"role": "user", "content": message})

        # Make the model prediction
        response = model.create_chat_completion(
            messages=messages,
            max_tokens=max_tokens,
            temperature=temperature,
            top_p=top_p,
        )
        return response["choices"][0]["message"]["content"]

    except Exception as e:
        # Return error message if something goes wrong
        return f"Error: {e}"

# Define the Gradio interface
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)",
        ),
    ],
)

# Launch the app
if __name__ == "__main__":
    demo.launch()