File size: 2,371 Bytes
893b350
 
 
 
 
 
 
 
 
 
 
2ac354d
642b87c
893b350
 
63f81dc
 
 
 
 
 
 
 
 
 
 
 
 
586ff4c
893b350
8714fcf
893b350
 
 
 
 
586ff4c
2ac354d
 
893b350
 
 
 
 
 
 
822c6d6
642b87c
893b350
 
 
 
 
 
 
586ff4c
893b350
 
 
 
 
2ac354d
 
893b350
642b87c
 
 
893b350
3506310
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
import os
import json
import gradio as gr
from llama_cpp import Llama

# Get environment variables
model_id = os.getenv('MODEL')
quant = os.getenv('QUANT')
chat_template = os.getenv('CHAT_TEMPLATE')

# Interface variables
model_name = model_id.split('/')[-1]
title = f"🇩🇪 {model_name}"
description = f"Chat with <a href=\"https://huggingface.co/{model_id}\">{model_name}</a> in GGUF format ({quant})!"

print("find gguf file")
import os
from pathlib import Path

# Get the Hugging Face cache directory
hf_cache_dir = os.getenv("HF_HOME", str(Path.home() / ".cache" / "huggingface"))

# List all files in the Hugging Face cache directory
for root, dirs, files in os.walk(hf_cache_dir):
    for file in files:
        print(os.path.join(root, file))


print("loading model")
# Initialize the LLM
llm = Llama(model_path="/home/user/.cache/huggingface/hub/models--LSX-UniWue--LLaMmlein_1B_alternative_formats/snapshots/7d97b69ae6910b5f317be2dbd5b4820d848c66b4/LLaMmlein_1B_chat_selected.gguf",
            n_ctx=32768,
            n_threads=2,
            chat_format=chat_template)

# Function for streaming chat completions
def chat_stream_completion(message, history):
    #messages_prompts = [{"role": "system", "content": system_prompt}]
    messages_prompts = []
    for human, assistant in history:
        messages_prompts.append({"role": "user", "content": human})
        messages_prompts.append({"role": "assistant", "content": assistant})
    messages_prompts.append({"role": "user", "content": message})

    response = llm.create_chat_completion(
        messages=messages_prompts,
        stream=True,
        stop=["<|im_end|>"]
    )
    message_repl = ""
    for chunk in response:
        if len(chunk['choices'][0]["delta"]) != 0 and "content" in chunk['choices'][0]["delta"]:
            message_repl = message_repl + chunk['choices'][0]["delta"]["content"]
        yield message_repl

print("starting gradio")
# Gradio chat interface
gr.ChatInterface(
    fn=chat_stream_completion,
    title=title,
    description=description,
    #additional_inputs=[gr.Textbox("Du bist ein hilfreicher Assistent.")],
    #additional_inputs_accordion="📝 System prompt",
    examples=[
        ["Was ist ein Large Language Model?"],
        ["Was ist 9+2-1?"],
        ["Schreibe Python code um die Fibonacci-Reihenfolge auszugeben."]
    ]
).queue().launch()